app.js 9.2 KB

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