app.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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", { topic, payload });
  57. if (!thisPageIsIndex) {
  58. getCurrentPages()[0].mqttCallback("message_onoffline", { topic, payload })
  59. }
  60. } else if (topic.indexOf("user/pub_response") !== -1) {
  61. pageinfo.mqttCallback("message", { topic, payload });
  62. }
  63. };
  64. });
  65. //服务器连接异常的回调
  66. this.globalData.client.on("error", function (error) {
  67. console.log(" 服务器 error 的回调" + error)
  68. const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
  69. if (pageinfo.mqttCallback) {
  70. pageinfo.mqttCallback("error")
  71. };
  72. })
  73. // 服务器重连
  74. this.globalData.client.on("reconnect", function (errr) {
  75. console.log(" 服务器 reconnect的回调", errr);
  76. const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
  77. if (pageinfo.mqttCallback) {
  78. pageinfo.mqttCallback("reconnect")
  79. };
  80. })
  81. //服务器断开连接
  82. this.globalData.client.on("offline", function (errr) {
  83. console.log(" 服务器 offline的回调", errr)
  84. const pageinfo = getCurrentPages()[getCurrentPages().length - 1];
  85. if (pageinfo.mqttCallback) {
  86. pageinfo.mqttCallback("offline")
  87. };
  88. })
  89. },
  90. // 订阅主题
  91. subscribe: function (topic, callback) {
  92. if (this.globalData.client && this.globalData.client.connected) {
  93. //订阅主题
  94. this.globalData.client.subscribe(topic, function (err, granted) {
  95. if (!err) {
  96. if (callback) {
  97. callback();
  98. }
  99. console.log("订阅成功");
  100. } else {
  101. console.log('订阅主题失败');
  102. }
  103. })
  104. } else {
  105. console.log("服务器已断开");
  106. // wx.showToast({
  107. // title: '服务器已断开',
  108. // icon: 'error',
  109. // duration: 2000
  110. // })
  111. }
  112. },
  113. // 取消订阅
  114. unsubscribe: function (Topic) {
  115. if (this.globalData.client && this.globalData.client.connected) {
  116. this.globalData.client.unsubscribe(Topic);
  117. } else {
  118. console.log('请先连接服务器');
  119. // wx.showToast({
  120. // title: '请先连接服务器',
  121. // icon: 'none',
  122. // duration: 2000
  123. // })
  124. }
  125. },
  126. /**
  127. * 发布信息
  128. * @param {*} data
  129. * DstDeviceName:目标设备
  130. * type: 指令
  131. * other:other
  132. * .......................
  133. * AIrSMArT_: 设备前缀
  134. */
  135. PubMsg(option, callback) {
  136. if (this.globalData.client && this.globalData.client.connected) {
  137. wx.showLoading({
  138. title: '请稍后',
  139. });
  140. const data = { "DstDeviceName": option.DstDeviceName, "SrcDeviceName": `ALY_${this.globalData.userInfo.userId}`, "type": option.type };
  141. if (option.other) {
  142. data.other = option.other;
  143. };
  144. this.globalData.client.publish(`/${option.DstDeviceName}/user/sub_control`, `${JSON.stringify(data)}`, (err) => {
  145. if (err) {
  146. console.log("发布消息失败");
  147. }
  148. });
  149. } else {
  150. console.log("服务器已断开");
  151. // wx.showToast({
  152. // title: '服务器已断开',
  153. // icon: 'error',
  154. // duration: 2000
  155. // })
  156. }
  157. },
  158. onLaunch() {
  159. var _this = this;
  160. wx.onAppHide(() => {
  161. if (_this.globalData.client && _this.globalData.client.connected) {
  162. _this.globalData.client.end(true);
  163. _this.globalData.client.end(true);
  164. console.log("断开");
  165. };
  166. });
  167. wx.onAppShow(() => {
  168. console.log("加载")
  169. if (_this.globalData.userInfo !== null) {
  170. if (_this.globalData.client) {
  171. _this.globalData.client.end(true);
  172. _this.globalData.client.end(true);
  173. }
  174. console.log("重连");
  175. _this.connect();
  176. };
  177. });
  178. this.getBluetoothStatus();
  179. //自定义导航栏 获取设备顶部窗口的高度(不同设备窗口高度不一样,根据这个来设置自定义导航栏的高度)
  180. wx.getSystemInfo({
  181. success: (res) => {
  182. let custom = wx.getMenuButtonBoundingClientRect();
  183. _this.globalData.navBarHeight = res.statusBarHeight + custom.height + (custom.top - res.statusBarHeight) * 2;
  184. _this.globalData.MenuButtonheight = custom.height;
  185. _this.globalData.MenuButtonTop = custom.top;
  186. const platform = res.platform;
  187. _this.globalData.isIOS = platform == "ios";
  188. _this.globalData.isAndroid = platform == "android";
  189. }
  190. })
  191. },
  192. // 获取蓝牙权限
  193. getBluetoothStatus() {
  194. const _this = this;
  195. },
  196. })