app.js 8.4 KB

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