app.js 7.3 KB

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