app.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. console.log("gadsfasdfaf==22==");
  55. if (!(that.globalData.client && that.globalData.client.connected)) {
  56. console.log("gadsfasdfaf==333==");
  57. that.connect();
  58. }
  59. });
  60. ///界面后台化
  61. wx.onAppHide(() => {
  62. // if (that.globalData.client && that.globalData.client.connected) {
  63. // that.globalData.client.end(true);
  64. // };
  65. });
  66. // that.getBluetoothStatus();
  67. //自定义导航栏 获取设备顶部窗口的高度(不同设备窗口高度不一样,根据这个来设置自定义导航栏的高度)
  68. wx.getSystemInfo({
  69. success: (res) => {
  70. let custom = wx.getMenuButtonBoundingClientRect();
  71. that.globalData.navBarHeight = res.statusBarHeight + custom.height + (custom.top - res.statusBarHeight) * 2;
  72. that.globalData.MenuButtonheight = custom.height;
  73. that.globalData.MenuButtonTop = custom.top;
  74. // var platform = res.platform;
  75. // that.globalData.isIOS = platform == "ios";
  76. // that.globalData.isAndroid = platform == "android";
  77. }
  78. });
  79. },
  80. onShow: function () {
  81. update.update();
  82. },
  83. onHide: function () {},
  84. ///连接设备
  85. connect() {
  86. var that = this;
  87. var options = that.globalData.options;
  88. console.log("gadsfasdfaf==444==");
  89. that.globalData.client = mqtt.connect(host, options);
  90. console.log("gadsfasdfaf==555==" + that.globalData.client);
  91. console.log("gadsfasdfaf==aaaa==" + JSON.stringify(that.globalData.client));
  92. console.log("gadsfasdfaf==666==" + that.globalData.client.connected);
  93. // 设备连接
  94. that.globalData.client.on('connect', () => {
  95. console.log("gadsfasdfaf==777==");
  96. var pages = getCurrentPages();
  97. var length = pages.length;
  98. var currentPage = pages[length - 1];
  99. if (!strings.isEmpty(currentPage.mqttCallback)) {
  100. currentPage.mqttCallback("connect")
  101. };
  102. ///多个界面回调首页
  103. if (length > 1) {
  104. pages[0].mqttCallback("connect");
  105. }
  106. });
  107. // 消息回调 wx.hideLoading();
  108. that.globalData.client.on("message", function (topic, payload) {
  109. var pages = getCurrentPages();
  110. var length = pages.length;
  111. var currentPage = pages[length - 1];
  112. if (topic.indexOf("status/onoffline") !== -1) {
  113. ///当前界面回调
  114. if (!strings.isEmpty(currentPage.mqttCallback)) {
  115. currentPage.mqttCallback("message_onoffline", {
  116. topic,
  117. payload
  118. });
  119. }
  120. if (length > 1) {
  121. pages[0].mqttCallback("message_onoffline", {
  122. topic,
  123. payload
  124. });
  125. }
  126. } else if (topic.indexOf("user/pub_response") !== -1) {
  127. if (!strings.isEmpty(currentPage.mqttCallback)) {
  128. currentPage.mqttCallback("message", {
  129. topic,
  130. payload
  131. });
  132. }
  133. if (length > 1) {
  134. pages[0].mqttCallback("message", {
  135. topic,
  136. payload
  137. });
  138. }
  139. }
  140. });
  141. // 重连
  142. that.globalData.client.on("reconnect", function (errr) {
  143. console.log("reconnect的回调==" + JSON.stringify(errr))
  144. var pages = getCurrentPages();
  145. var length = pages.length;
  146. var currentPage = pages[length - 1];
  147. if (!strings.isEmpty(currentPage.mqttCallback)) {
  148. currentPage.mqttCallback("reconnect")
  149. };
  150. if (length > 1) {
  151. pages[0].mqttCallback("reconnect");
  152. }
  153. });
  154. // 离线回调
  155. that.globalData.client.on("offline", function (errr) {
  156. console.log("offline的回调==" + JSON.stringify(errr))
  157. var pages = getCurrentPages();
  158. var length = pages.length;
  159. var currentPage = pages[length - 1];
  160. if (!strings.isEmpty(currentPage.mqttCallback)) {
  161. currentPage.mqttCallback("offline")
  162. };
  163. if (length > 1) {
  164. pages[0].mqttCallback("offline");
  165. }
  166. });
  167. // 错误回调
  168. that.globalData.client.on("error", function (error) {
  169. console.log("错误码的回调==" + JSON.stringify(errr))
  170. var pages = getCurrentPages();
  171. var length = pages.length;
  172. var currentPage = pages[length - 1];
  173. if (!strings.isEmpty(currentPage.mqttCallback)) {
  174. currentPage.mqttCallback("error", {})
  175. };
  176. if (length > 1) {
  177. pages[0].mqttCallback("error", {});
  178. }
  179. });
  180. },
  181. // topic
  182. // /AIrSMArT_7cdfa1fd3af0/status/onoffline
  183. // /AIrSMArT_7cdfa1fd3af0/user/pub_response
  184. // 订阅主题
  185. subscribe: function (topic, callback) {
  186. var that = this;
  187. if (that.globalData.client && that.globalData.client.connected) {
  188. //订阅主题
  189. // err:null
  190. // granted:[{"topic":"/AIrSMArT_861210052355545/status/onoffline","qos":0}]
  191. that.globalData.client.subscribe(topic, function (err, granted) {
  192. if (!err) {
  193. if (callback) {
  194. callback();
  195. }
  196. console.log("订阅成功===" + topic);
  197. } else {
  198. console.log('订阅主题失败');
  199. }
  200. })
  201. } else {
  202. console.log("服务器已断开");
  203. }
  204. },
  205. // 取消订阅
  206. unsubscribe: function (topic) {
  207. var that = this;
  208. if (that.globalData.client && that.globalData.client.connected) {
  209. that.globalData.client.unsubscribe(topic);
  210. } else {
  211. console.log('请先连接服务器');
  212. }
  213. },
  214. /**
  215. * 发布信息
  216. * @param {*} data
  217. * DstDeviceName:目标设备
  218. * type: 指令
  219. * other:other
  220. * .......................
  221. * AIrSMArT_: 设备前缀
  222. */
  223. PubMsg(option, callback) {
  224. var that = this;
  225. if (strings.isEmpty(that.globalData.userInfo)) {
  226. return;
  227. }
  228. // {"type":"get_dev_info","DstDeviceName":"AIrSMArT_7cdfa1fd3af0"}
  229. // get_dev_info
  230. var type = option.type;
  231. // AIrSMArT_7cdfa1fd3af0
  232. var DstDeviceName = option.DstDeviceName;
  233. // ALY_933625
  234. var SrcDeviceName = `ALY_${that.globalData.userInfo.userId}`;
  235. if (that.globalData.client && that.globalData.client.connected) {
  236. const data = {
  237. "type": type,
  238. "DstDeviceName": DstDeviceName,
  239. "SrcDeviceName": SrcDeviceName,
  240. };
  241. if (option.other) {
  242. data.other = option.other;
  243. };
  244. // /AIrSMArT_7cdfa1fd3af0/user/sub_control
  245. var publish = `/${DstDeviceName}/user/sub_control`;
  246. // {"type":"get_dev_info","DstDeviceName":"AIrSMArT_7cdfa1fd3af0","SrcDeviceName":"ALY_933625"}
  247. var dataString = `${JSON.stringify(data)}`;
  248. that.globalData.client.publish(publish, dataString, (err) => {
  249. if (err) {
  250. console.log("发布消息失败");
  251. }
  252. });
  253. } else {
  254. console.log("服务器已断开");
  255. }
  256. },
  257. // 获取蓝牙权限
  258. getBluetoothStatus() {
  259. var that = this;
  260. wx.getSetting({
  261. success(res) {
  262. if (res.authSetting["scope.bluetooth"]) {
  263. that.globalData.scopeBluetooth = true;
  264. } else if (res.authSetting["scope.bluetooth"] === undefined) {
  265. that.globalData.scopeBluetooth = false;
  266. wx.authorize({
  267. scope: "scope.bluetooth",
  268. complete() {
  269. that.getBluetoothStatus();
  270. }
  271. });
  272. } else {
  273. that.globalData.scopeBluetooth = false;
  274. wx.showModal({
  275. title: '请打开系统蓝牙进行配网',
  276. content: '如已打开蓝牙仍然弹框,请尝试重启小程序',
  277. success(res) {
  278. if (res.confirm) {
  279. console.log('用户点击确定')
  280. wx.openSetting({
  281. complete() {
  282. // that.getBluetoothStatus();
  283. }
  284. })
  285. } else if (res.cancel) {
  286. console.log('用户点击取消')
  287. }
  288. }
  289. })
  290. };
  291. if (getCurrentPages()[getCurrentPages().length - 1].getBluetoothStatusCallck) {
  292. getCurrentPages()[getCurrentPages().length - 1].getBluetoothStatusCallck(that.globalData.scopeBluetooth);
  293. }
  294. }
  295. });
  296. },
  297. })