app.js 8.9 KB

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