app.js 8.5 KB

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