app.js 8.8 KB

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