app.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // app.ts
  2. import btHelper from './devices/bt_helper';
  3. import mqtt from './utils/mqtt';
  4. //连接的服务器域名
  5. // const host = 'wxs://mqtt.test.radio1964.com';
  6. const host = 'wxs://mqtt.ssl.keepradioon.net:8884'
  7. // const host = 'wxs://mqtt.test.radio1964.com:8884';
  8. // const host = 'wxs://mqtt.ssl.keepradioon.net:8884'
  9. App({
  10. globalData: {
  11. statusBarHeight: 0,
  12. navBarHeight: 0,
  13. MenuButtonheight: 0,
  14. MenuButtonTop: 0,
  15. scopeBluetooth: true,
  16. ssid: "",
  17. pwdData: "",
  18. userData: null,
  19. userInfo: null,
  20. newDeviceId: null,
  21. is_debug: 2, // 1 测试环境 // 2正式环境
  22. client: null,
  23. oneInitBluetooth: true,
  24. isIOS: false,
  25. isAndroid: false,
  26. isHuaWei: false,
  27. // 设备列表,跟app同步字段
  28. classifyProducts: [],
  29. //MQTT连接的配置
  30. options: {
  31. clientId: "wx_" + parseInt(Math.random() * 100 + 800, 10),
  32. reconnectPeriod: 1000, //1000毫秒,两次重新连接之间的间隔
  33. connectTimeout: 30 * 1000, //1000毫秒,两次重新连接之间的间隔
  34. resubscribe: true, //如果连接断开并重新连接,则会再次自动订阅已订阅的主题(默认true
  35. keepalive: 3, //每3秒发送一次心跳
  36. },
  37. },
  38. /**
  39. * 连接设备
  40. * mqttCallback:mqtt回调统一方法
  41. */
  42. connect() {
  43. this.globalData.client = mqtt.connect(host, this.globalData.options);
  44. // 连接成功
  45. this.globalData.client.on('connect', () => {
  46. const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
  47. if (pageinfo.mqttCallback) {
  48. pageinfo.mqttCallback("connect")
  49. };
  50. });
  51. // 接收消息
  52. this.globalData.client.on("message", function (topic, payload) {
  53. wx.hideLoading();
  54. const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
  55. const thisPageIsIndex = getCurrentPages().length === 1;
  56. if (pageinfo.mqttCallback) {
  57. if (topic.indexOf("status/onoffline") !== -1) {
  58. pageinfo.mqttCallback("message_onoffline", {
  59. topic,
  60. payload
  61. });
  62. if (!thisPageIsIndex) {
  63. getCurrentPages()[0].mqttCallback("message_onoffline", {
  64. topic,
  65. payload
  66. })
  67. }
  68. } else if (topic.indexOf("user/pub_response") !== -1) {
  69. pageinfo.mqttCallback("message", {
  70. topic,
  71. payload
  72. });
  73. }
  74. };
  75. });
  76. //服务器连接异常的回调
  77. this.globalData.client.on("error", function (error) {
  78. console.log(" 服务器 error 的回调" + error)
  79. const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
  80. if (pageinfo.mqttCallback) {
  81. pageinfo.mqttCallback("error")
  82. };
  83. })
  84. // 服务器重连
  85. this.globalData.client.on("reconnect", function (errr) {
  86. console.log(" 服务器 reconnect的回调", errr);
  87. const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
  88. if (pageinfo.mqttCallback) {
  89. pageinfo.mqttCallback("reconnect")
  90. };
  91. })
  92. //服务器断开连接
  93. this.globalData.client.on("offline", function (errr) {
  94. console.log(" 服务器 offline的回调", errr)
  95. const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
  96. if (pageinfo.mqttCallback) {
  97. pageinfo.mqttCallback("offline")
  98. };
  99. })
  100. },
  101. // 订阅主题
  102. subscribe: function (topic, callback) {
  103. if (this.globalData.client && this.globalData.client.connected) {
  104. //订阅主题
  105. this.globalData.client.subscribe(topic, function (err, granted) {
  106. if (!err) {
  107. if (callback) {
  108. callback();
  109. }
  110. console.log("订阅成功");
  111. } else {
  112. console.log('订阅主题失败');
  113. }
  114. })
  115. } else {
  116. console.log("服务器已断开");
  117. // wx.showToast({
  118. // title: '服务器已断开',
  119. // icon: 'error',
  120. // duration: 2000
  121. // })
  122. }
  123. },
  124. // 取消订阅
  125. unsubscribe: function (Topic) {
  126. if (this.globalData.client && this.globalData.client.connected) {
  127. this.globalData.client.unsubscribe(Topic);
  128. } else {
  129. console.log('请先连接服务器');
  130. // wx.showToast({
  131. // title: '请先连接服务器',
  132. // icon: 'none',
  133. // duration: 2000
  134. // })
  135. }
  136. },
  137. /**
  138. * 发布信息
  139. * @param {*} data
  140. * DstDeviceName:目标设备
  141. * type: 指令
  142. * other:other
  143. * .......................
  144. * AIrSMArT_: 设备前缀
  145. */
  146. PubMsg(option, callback) {
  147. if (this.globalData.client && this.globalData.client.connected) {
  148. wx.showLoading({
  149. title: '请稍后',
  150. });
  151. const data = {
  152. "DstDeviceName": option.DstDeviceName,
  153. "SrcDeviceName": `ALY_${this.globalData.userInfo.userId}`,
  154. "type": option.type
  155. };
  156. if (option.other) {
  157. data.other = option.other;
  158. };
  159. this.globalData.client.publish(`/${option.DstDeviceName}/user/sub_control`, `${JSON.stringify(data)}`, (err) => {
  160. if (err) {
  161. console.log("发布消息失败");
  162. }
  163. });
  164. } else {
  165. console.log("服务器已断开");
  166. // wx.showToast({
  167. // title: '服务器已断开',
  168. // icon: 'error',
  169. // duration: 2000
  170. // })
  171. }
  172. },
  173. onLaunch() {
  174. var _this = this;
  175. wx.onAppHide(() => {
  176. if (_this.globalData.client && _this.globalData.client.connected) {
  177. _this.globalData.client.end(true);
  178. _this.globalData.client.end(true);
  179. console.log("断开");
  180. };
  181. });
  182. wx.onAppShow(() => {
  183. console.log("加载")
  184. if (_this.globalData.userInfo !== null) {
  185. if (_this.globalData.client) {
  186. _this.globalData.client.end(true);
  187. _this.globalData.client.end(true);
  188. }
  189. console.log("重连");
  190. _this.connect();
  191. };
  192. });
  193. this.getBluetoothStatus();
  194. //自定义导航栏 获取设备顶部窗口的高度(不同设备窗口高度不一样,根据这个来设置自定义导航栏的高度)
  195. wx.getSystemInfo({
  196. success: (res) => {
  197. let custom = wx.getMenuButtonBoundingClientRect();
  198. _this.globalData.navBarHeight = res.statusBarHeight + custom.height + (custom.top - res.statusBarHeight) * 2;
  199. _this.globalData.MenuButtonheight = custom.height;
  200. _this.globalData.MenuButtonTop = custom.top;
  201. const platform = res.platform;
  202. _this.globalData.isIOS = platform == "ios";
  203. _this.globalData.isAndroid = platform == "android";
  204. }
  205. })
  206. },
  207. // 获取蓝牙权限
  208. getBluetoothStatus() {
  209. const _this = this;
  210. wx.getSetting({
  211. success(res) {
  212. if (res.authSetting["scope.bluetooth"]) {
  213. _this.globalData.scopeBluetooth = true;
  214. } else if (res.authSetting["scope.bluetooth"] === undefined) {
  215. _this.globalData.scopeBluetooth = false;
  216. wx.authorize({
  217. scope: "scope.bluetooth",
  218. complete() {
  219. _this.getBluetoothStatus();
  220. }
  221. });
  222. } else {
  223. _this.globalData.scopeBluetooth = false;
  224. wx.showModal({
  225. title: '请打开系统蓝牙进行配网',
  226. content: '如已打开蓝牙仍然弹框,请尝试重启小程序',
  227. success(res) {
  228. if (res.confirm) {
  229. console.log('用户点击确定')
  230. wx.openSetting({
  231. complete() {
  232. // _this.getBluetoothStatus();
  233. }
  234. })
  235. } else if (res.cancel) {
  236. console.log('用户点击取消')
  237. }
  238. }
  239. })
  240. };
  241. if (getCurrentPages()[getCurrentPages().length - 1].getBluetoothStatusCallck) {
  242. getCurrentPages()[getCurrentPages().length - 1].getBluetoothStatusCallck(_this.globalData.scopeBluetooth);
  243. }
  244. // res.authSetting = {
  245. // "scope.userInfo": true,
  246. // "scope.userLocation": true
  247. // }
  248. }
  249. })
  250. },
  251. })
  252. // "tabBar": {
  253. // "borderStyle": "black",
  254. // "list": [{
  255. // "pagePath": "pages/index/index",
  256. // "text": "首页",
  257. // "iconPath": "./img/index_unaction.png",
  258. // "selectedIconPath": "./img/index_action.png"
  259. // },
  260. // {
  261. // "pagePath": "pages/me/me",
  262. // "text": "我的",
  263. // "iconPath": "./img/me_unaction.png",
  264. // "selectedIconPath": "./img/me_action.png"
  265. // }
  266. // ]
  267. // },