app.js 8.7 KB

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