app.js 7.5 KB

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