app.js 8.7 KB

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