app.js 8.4 KB

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