app.js 6.9 KB

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