Explorar el Código

feature: 处理乐鑫添加wifi设备

Damon hace 7 meses
padre
commit
4e39b07fdc
Se han modificado 4 ficheros con 86 adiciones y 58 borrados
  1. 44 23
      app.js
  2. 1 1
      pages/deviceConnect3/deviceConnect3.js
  3. 18 32
      pages/index/index.js
  4. 23 2
      utils/lexin/service.js

+ 44 - 23
app.js

@@ -91,40 +91,46 @@ App({
 
     // 设备连接
     that.globalData.client.on('connect', () => {
-      var page = getCurrentPages()[getCurrentPages().length - 1];
-      if (!strings.isEmpty(page.mqttCallback)) {
-        page.mqttCallback("connect")
+      var pages = getCurrentPages();
+      var length = pages.length;
+      var currentPage = pages[length - 1];
+      if (!strings.isEmpty(currentPage.mqttCallback)) {
+        currentPage.mqttCallback("connect")
       };
+      ///多个界面回调首页
+      if (length > 1) {
+        pages[0].mqttCallback("connect");
+      }
     });
 
     // 消息回调 wx.hideLoading();
     that.globalData.client.on("message", function (topic, payload) {
-      var page = getCurrentPages()[getCurrentPages().length - 1];
+      var pages = getCurrentPages();
+      var length = pages.length;
+      var currentPage = pages[length - 1];
       if (topic.indexOf("status/onoffline") !== -1) {
         ///当前界面回调
-        if (!strings.isEmpty(page.mqttCallback)) {
-          page.mqttCallback("message_onoffline", {
+        if (!strings.isEmpty(currentPage.mqttCallback)) {
+          currentPage.mqttCallback("message_onoffline", {
             topic,
             payload
           });
         }
-        ///多个界面回调首页
-        if (getCurrentPages().length != 1) {
-          getCurrentPages()[0].mqttCallback("message_onoffline", {
+        if (length > 1) {
+          pages[0].mqttCallback("message_onoffline", {
             topic,
             payload
           });
         }
       } else if (topic.indexOf("user/pub_response") !== -1) {
-        if (!strings.isEmpty(page.mqttCallback)) {
-          page.mqttCallback("message", {
+        if (!strings.isEmpty(currentPage.mqttCallback)) {
+          currentPage.mqttCallback("message", {
             topic,
             payload
           });
         }
-        ///多个界面回调首页
-        if (getCurrentPages().length != 1) {
-          getCurrentPages()[0].mqttCallback("message", {
+        if (length > 1) {
+          pages[0].mqttCallback("message", {
             topic,
             payload
           });
@@ -135,28 +141,43 @@ App({
     // 重连
     that.globalData.client.on("reconnect", function (errr) {
       // console.log("reconnect的回调==" + JSON.stringify(errr))
-      var page = getCurrentPages()[getCurrentPages().length - 1];
-      if (!strings.isEmpty(page.mqttCallback)) {
-        page.mqttCallback("reconnect")
+      var pages = getCurrentPages();
+      var length = pages.length;
+      var currentPage = pages[length - 1];
+      if (!strings.isEmpty(currentPage.mqttCallback)) {
+        currentPage.mqttCallback("reconnect")
       };
+      if (length > 1) {
+        pages[0].mqttCallback("reconnect");
+      }
     });
 
     // 离线回调
     that.globalData.client.on("offline", function (errr) {
       // console.log("offline的回调==" + JSON.stringify(errr))
-      var page = getCurrentPages()[getCurrentPages().length - 1];
-      if (!strings.isEmpty(page.mqttCallback)) {
-        page.mqttCallback("offline")
+      var pages = getCurrentPages();
+      var length = pages.length;
+      var currentPage = pages[length - 1];
+      if (!strings.isEmpty(currentPage.mqttCallback)) {
+        currentPage.mqttCallback("offline")
       };
+      if (length > 1) {
+        pages[0].mqttCallback("offline");
+      }
     });
 
     // 错误回调
     that.globalData.client.on("error", function (error) {
       // console.log("错误码的回调==" + JSON.stringify(errr))
-      var page = getCurrentPages()[getCurrentPages().length - 1];
-      if (!strings.isEmpty(page.mqttCallback)) {
-        page.mqttCallback("error", {})
+      var pages = getCurrentPages();
+      var length = pages.length;
+      var currentPage = pages[length - 1];
+      if (!strings.isEmpty(currentPage.mqttCallback)) {
+        currentPage.mqttCallback("error", {})
       };
+      if (length > 1) {
+        pages[0].mqttCallback("error", {});
+      }
     });
   },
 

+ 1 - 1
pages/deviceConnect3/deviceConnect3.js

@@ -97,7 +97,7 @@ Page({
             clearTimeout(errTi);
             clearInterval(percentIn);
             // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
-            getCurrentPages()[0].addConnectWifiDevice({
+            getCurrentPages()[0].addWifiDevice({
               deviceId: that.data.name,
             });
             that.setData({

+ 18 - 32
pages/index/index.js

@@ -9,8 +9,11 @@ import strings from '../../utils/strings.js';
 import store from '../../utils/store.js';
 import route_constant from '../../utils/route_constant.js';
 import route_util from '../../utils/route_util.js';
+
+/// 乐鑫相关
 import lexin_util from '../../utils/lexin/util.js';
 import lexin_jump from '../../utils/lexin/jump.js';
+import lexin_service from '../../utils/lexin/service.js';
 import {
   BtHelper
 } from '../../devices/bt_helper.js';
@@ -447,7 +450,7 @@ Page({
     };
 
     ///  /AIrSMArT_7cdfa1fd3af0/user/pub_response
-    var pubResponse = thalexinUtilt.getResponseByDeviceMacId(deviceMacId);
+    var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
     app.subscribe(pubResponse);
 
     app.PubMsg({
@@ -537,7 +540,7 @@ Page({
       });
 
       // 接收设备当前播放状态
-      var deviceMacId = that.getDeviceMacId(deviceId);
+      var deviceMacId = lexin_util.getDeviceMacId(deviceId);
       const obj = {
         DstDeviceName: deviceMacId
       }
@@ -847,42 +850,23 @@ Page({
     }
   },
 
-  ///新添加wifi设备
-  async addConnectWifiDevice(newDevice) {
+  // 新添加Wifi设备
+  addWifiDevice(device) {
     var that = this;
-    // 同一个设备
     var deviceList = that.data.deviceList;
-    var tempList = deviceList.filter((v) => v.deviceId === newDevice.deviceId);
-    if (tempList && tempList.length > 0) {
-      deviceList = deviceList.filter((v) => v.deviceId !== newDevice.deviceId);
-    };
-
-    // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
-    deviceList.unshift({
-      /// Wifi连接
-      connectType: 3,
-      deviceId: newDevice.deviceId,
-      name: "猫王小王子OTR-X",
-      state: "online",
-      devName: "",
-      // devName: "猫王小王子OTR-X",
-      mac: newDevice.deviceId,
-      image: "./../../img/min.png",
-    });
-
-    ///在线排序前面,wifi设备排序前面
-    that.updateDeviceList(deviceList, false, false);
+    var addDeviceList = lexin_service.addWifiDevice(device, deviceList);
+    that.updateDeviceList(addDeviceList, false, false);
     var indexPage = that.data.indexPage;
     if (indexPage != 0) {
       that.setData({
         indexPage: 0,
+        deviceListSelect: null,
+      });
+    } else {
+      that.setData({
+        deviceListSelect: null,
       });
     }
-    ///断开蓝牙连接
-    // await BtHelper.getInstance().disconnect();
-    that.setData({
-      deviceListSelect: null,
-    });
     that.subscribeDevicesStatus();
   },
 
@@ -890,13 +874,15 @@ Page({
   closeBlueResetOffline(isInit, closeAllBlue) {
     var that = this;
     var deviceList = that.data.deviceList;
-    updateDeviceList(deviceList, isInit, closeAllBlue);
+    that.updateDeviceList(deviceList, isInit, closeAllBlue);
   },
 
   /// 更新列表排序
   updateDeviceList(deviceList, isInit, closeAllBlue) {
-    var finalList = store.updateDeviceList(deviceList, isInit, closeAllBlue);
+    var that = this;
+    var finalList = lexin_service.updateDeviceList(deviceList, isInit, closeAllBlue);
     if (!strings.isEmpty(deviceList) || !strings.isEmpty(finalList)) {
+      store.setStore("deviceList", finalList);
       that.setData({
         deviceList: finalList,
       });

+ 23 - 2
utils/lexin/service.js

@@ -1,7 +1,30 @@
 module.exports = {
+  addWifiDevice: addWifiDevice,
   updateDeviceList: updateDeviceList,
 }
 
+// 新添加Wifi设备  猫王小王子OTR-X
+// [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
+function addWifiDevice(device, deviceList) {
+  // 同一个设备处理
+  var tempList = deviceList.filter((v) => v.deviceId === device.deviceId);
+  const strings = require('../strings');
+  if (!strings.isEmpty(tempList)) {
+    deviceList = deviceList.filter((v) => v.deviceId !== device.deviceId);
+  }
+  /// 添加到第一个
+  deviceList.unshift({
+    connectType: 3,
+    devName: "",
+    state: "online",
+    name: device.deviceId,
+    deviceId: device.deviceId,
+    mac: device.deviceId,
+    image: "./../../img/min.png",
+  });
+  return deviceList;
+};
+
 ///更新列表排序
 function updateDeviceList(deviceList, isInit, closeAllBlue) {
   const strings = require('../strings');
@@ -10,7 +33,6 @@ function updateDeviceList(deviceList, isInit, closeAllBlue) {
   }
 
   const app = getApp();
-  const store = require('../store');
 
   var finalList = [];
   if (isInit) {
@@ -107,6 +129,5 @@ function updateDeviceList(deviceList, isInit, closeAllBlue) {
   finalList = finalList.concat(onNoLineWifiList);
   finalList = finalList.concat(onNoLineBlueList);
 
-  store.setStore("deviceList", finalList);
   return finalList;
 };