app.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 (!strings.isEmpty(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 (!strings.isEmpty(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. if (!strings.isEmpty(page.mqttCallback)) {
  109. page.mqttCallback("message", {
  110. topic,
  111. payload
  112. });
  113. }
  114. }
  115. });
  116. // 重连
  117. that.globalData.client.on("reconnect", function (errr) {
  118. console.log("reconnect的回调==" + JSON.stringify(errr))
  119. var page = getCurrentPages()[getCurrentPages().length - 1];
  120. if (!strings.isEmpty(page.mqttCallback)) {
  121. page.mqttCallback("reconnect")
  122. };
  123. });
  124. // 离线回调
  125. that.globalData.client.on("offline", function (errr) {
  126. console.log("offline的回调==" + JSON.stringify(errr))
  127. var page = getCurrentPages()[getCurrentPages().length - 1];
  128. if (!strings.isEmpty(page.mqttCallback)) {
  129. page.mqttCallback("offline")
  130. };
  131. });
  132. // 错误回调
  133. that.globalData.client.on("error", function (error) {
  134. console.log("错误码的回调==" + JSON.stringify(errr))
  135. var page = getCurrentPages()[getCurrentPages().length - 1];
  136. if (!strings.isEmpty(page.mqttCallback)) {
  137. page.mqttCallback("error", {})
  138. };
  139. });
  140. },
  141. // topic
  142. // /AIrSMArT_7cdfa1fd3af0/status/onoffline
  143. // /AIrSMArT_7cdfa1fd3af0/user/pub_response
  144. // 订阅主题
  145. subscribe: function (topic, callback) {
  146. var that = this;
  147. if (that.globalData.client && that.globalData.client.connected) {
  148. //订阅主题
  149. that.globalData.client.subscribe(topic, function (err, granted) {
  150. if (!err) {
  151. if (callback) {
  152. callback();
  153. }
  154. console.log("订阅成功");
  155. } else {
  156. console.log('订阅主题失败');
  157. }
  158. })
  159. } else {
  160. console.log("服务器已断开");
  161. }
  162. },
  163. // 取消订阅
  164. unsubscribe: function (topic) {
  165. var that = this;
  166. if (that.globalData.client && that.globalData.client.connected) {
  167. that.globalData.client.unsubscribe(topic);
  168. } else {
  169. console.log('请先连接服务器');
  170. }
  171. },
  172. /**
  173. * 发布信息
  174. * @param {*} data
  175. * DstDeviceName:目标设备
  176. * type: 指令
  177. * other:other
  178. * .......................
  179. * AIrSMArT_: 设备前缀
  180. */
  181. PubMsg(option, callback) {
  182. var that = this;
  183. if (strings.isEmpty(that.globalData.userInfo)) {
  184. return;
  185. }
  186. // {"type":"get_dev_info","DstDeviceName":"AIrSMArT_7cdfa1fd3af0"}
  187. // get_dev_info
  188. var type = option.type;
  189. // AIrSMArT_7cdfa1fd3af0
  190. var DstDeviceName = option.DstDeviceName;
  191. // ALY_933625
  192. var SrcDeviceName = `ALY_${that.globalData.userInfo.userId}`;
  193. if (that.globalData.client && that.globalData.client.connected) {
  194. const data = {
  195. "type": type,
  196. "DstDeviceName": DstDeviceName,
  197. "SrcDeviceName": SrcDeviceName,
  198. };
  199. if (option.other) {
  200. data.other = option.other;
  201. };
  202. // /AIrSMArT_7cdfa1fd3af0/user/sub_control
  203. var publish = `/${DstDeviceName}/user/sub_control`;
  204. // {"type":"get_dev_info","DstDeviceName":"AIrSMArT_7cdfa1fd3af0","SrcDeviceName":"ALY_933625"}
  205. var dataString = `${JSON.stringify(data)}`;
  206. that.globalData.client.publish(publish, dataString, (err) => {
  207. if (err) {
  208. console.log("发布消息失败");
  209. }
  210. });
  211. } else {
  212. console.log("服务器已断开");
  213. }
  214. },
  215. // 获取蓝牙权限
  216. getBluetoothStatus() {
  217. var that = this;
  218. wx.getSetting({
  219. success(res) {
  220. if (res.authSetting["scope.bluetooth"]) {
  221. that.globalData.scopeBluetooth = true;
  222. } else if (res.authSetting["scope.bluetooth"] === undefined) {
  223. that.globalData.scopeBluetooth = false;
  224. wx.authorize({
  225. scope: "scope.bluetooth",
  226. complete() {
  227. that.getBluetoothStatus();
  228. }
  229. });
  230. } else {
  231. that.globalData.scopeBluetooth = false;
  232. wx.showModal({
  233. title: '请打开系统蓝牙进行配网',
  234. content: '如已打开蓝牙仍然弹框,请尝试重启小程序',
  235. success(res) {
  236. if (res.confirm) {
  237. console.log('用户点击确定')
  238. wx.openSetting({
  239. complete() {
  240. // that.getBluetoothStatus();
  241. }
  242. })
  243. } else if (res.cancel) {
  244. console.log('用户点击取消')
  245. }
  246. }
  247. })
  248. };
  249. if (getCurrentPages()[getCurrentPages().length - 1].getBluetoothStatusCallck) {
  250. getCurrentPages()[getCurrentPages().length - 1].getBluetoothStatusCallck(that.globalData.scopeBluetooth);
  251. }
  252. }
  253. });
  254. },
  255. })