app.js 7.4 KB

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