app.js 8.0 KB

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