app.js 8.5 KB

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