app.js 8.4 KB

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