Explorar o código

feature: 处理多款设备流程配网

Damon hai 7 meses
pai
achega
76d4d5df53

+ 16 - 5
pages/deviceConnect3/deviceConnect3.js

@@ -97,21 +97,32 @@ Page({
             clearTimeout(errTi);
             clearInterval(percentIn);
             // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
-            getCurrentPages()[0].addWifiDevice({
-              deviceId: that.data.name,
-            });
+
             that.setData({
               percent: 100,
-              ruterStatus: 1
+              ruterStatus: 1,
             });
+
             // 记住密码
             wx.setStorage({
               key: "wifiInfo",
               data: JSON.stringify({
                 "password": app.globalData.pwdData,
                 "ssid": app.globalData.ssid
-              })
+              }),
             });
+
+            var deviceId = that.data.name;
+            var device = {
+              connectType: 3,
+              devName: "",
+              state: "online",
+              name: deviceId,
+              deviceId: deviceId,
+              mac: deviceId,
+              image: "./../../img/min.png",
+            }
+            getCurrentPages()[0].addWifiDevice(device);
           }
         }
         break;

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 157 - 261
pages/index/index.js


+ 5 - 1
pages/index/index.wxml

@@ -13,7 +13,7 @@
         </block>
       </swiper>
     </view>
-    <view class="add_device_public" bind:tap="addDevice">
+    <view class="add_device_public" bind:tap="jumpToAddDevice">
       <image mode="heightFix" src="./../../img/add_device.png" style="width: 48rxp;height: 48rpx;"></image>
       <view style="color: white; margin-left: 8rpx; font-size: 32rpx;">添加设备</view>
     </view>
@@ -22,6 +22,10 @@
       <view wx:if="{{deviceList}}">
         <block wx:key="key" wx:for-item="item" wx:for-index="index" wx:for="{{deviceList}}">
 
+          <!-- {{deviceListSelect}}
+          {{index}}
+          {{item.state}}
+          {{item.state && item.state === 'online'&&deviceListSelect === index  }} -->
           <view class="item_public" style="width: calc(100vw - {{item.state && item.state === 'online'&&deviceListSelect === index ?76:70}}rpx);border: {{item.state && item.state === 'online'&&deviceListSelect === index  ? '8rpx solid #6546A3' : item.state && item.state === 'online'?'2rpx solid #6546A3':'2rpx solid #F2F5F7'}}; margin-left: {{item.state && item.state === 'online'&&deviceListSelect === index  ?0:4}}rpx;" data-item="{{item}}" data-index="{{index}}" bind:tap="onTapItem">
 
             <image wx:if="{{item.connectType==3}}" class="item_public_image" src="./../../img/min.png" style="opacity: {{item.state && item.state === 'online' ? '1' : '0.5'}};"></image>

+ 17 - 25
utils/lexin/add.js

@@ -6,23 +6,16 @@ module.exports = {
 
 // 新添加Wifi设备  猫王小王子OTR-X
 // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
-function addWifiDevice(device, deviceList) {
+function addWifiDevice(device) {
   // 同一个设备处理
+  var deviceList = getCurrentPages()[0].getDeviceList();
   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",
-  });
+  deviceList.unshift(device);
   return deviceList;
 };
 
@@ -31,14 +24,14 @@ function addWifiDevice(device, deviceList) {
 /// clientType 连接方式:bt-0,ble-1,upnp-2,mqtt-3
 // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
 ///新添加蓝牙设备
-function addBlueDevice(device, deviceList) {
+function addBlueDevice(device) {
   // 同一个设备处理
+  var deviceList = getCurrentPages()[0].getDeviceList();
   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: 1,
@@ -55,22 +48,21 @@ function addBlueDevice(device, deviceList) {
 };
 
 ///更新列表排序
-function updateDeviceList(deviceList, isInit, closeAllBlue) {
+function updateDeviceList(list, isInit, closeAllBlue) {
   const strings = require('../strings');
-  if (strings.isEmpty(deviceList)) {
+  if (strings.isEmpty(list)) {
     return [];
   }
 
   const app = getApp();
-
   var finalList = [];
   if (isInit) {
-    deviceList[0].state = "offline";
+    list[0].state = "offline";
   }
 
   /// 让所有蓝牙设备离线
   if (closeAllBlue) {
-    deviceList.forEach(element => {
+    list.forEach(element => {
       if (element.connectType != 3) {
         element.state = "offline";
       }
@@ -79,15 +71,15 @@ function updateDeviceList(deviceList, isInit, closeAllBlue) {
 
   ///挑选出第一个在线设备
   var isFirstOnline = false;
-  if (deviceList[0].state == "online") {
+  if (list[0].state == "online") {
     isFirstOnline = true;
-    finalList.push(deviceList[0]);
+    finalList.push(list[0]);
   }
 
   ///区分在线和离线
-  for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
+  for (var i = isFirstOnline ? 1 : 0; i < list.length; i++) {
     if (isInit) {
-      deviceList[i].state = "offline";
+      list[i].state = "offline";
     }
   }
 
@@ -95,16 +87,16 @@ function updateDeviceList(deviceList, isInit, closeAllBlue) {
   var onNoLineList = [];
 
   ///添加在线的
-  for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
-    var device = deviceList[i];
+  for (var i = isFirstOnline ? 1 : 0; i < list.length; i++) {
+    var device = list[i];
     if (device.state == "online") {
       onLineList.push(device)
     }
   }
 
   ///添加离线的
-  for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
-    var device = deviceList[i];
+  for (var i = isFirstOnline ? 1 : 0; i < list.length; i++) {
+    var device = list[i];
     if (device.state != "online") {
       onNoLineList.push(device)
     }

+ 0 - 9
utils/lexin/connect.js

@@ -1,9 +0,0 @@
-module.exports = {
-  toAboutUs: toAboutUs,
-}
-
-function toAboutUs() {
-  const route_util = require('../route_util');
-  const route_constant = require('../route_constant');
-  route_util.jump(route_constant.about);
-};

+ 126 - 0
utils/lexin/message.js

@@ -0,0 +1,126 @@
+module.exports = {
+  subscribeSingleDevice: subscribeSingleDevice,
+  subscribeAllDevice: subscribeAllDevice,
+  onlineDevice: onlineDevice,
+}
+
+// 订阅在线单个设备
+function subscribeSingleDevice(deviceId) {
+  var topic = `/AIrSMArT_${deviceId.split("BLUFI_")[1]}/status/onoffline`;
+  const app = getApp();
+  app.unsubscribe(topic);
+  app.subscribe(topic);
+};
+
+// 订阅在线设备
+function subscribeAllDevice() {
+  const strings = require('../strings');
+  var deviceList = getCurrentPages()[0].getDeviceList();
+  if (strings.isEmpty(deviceList)) {
+    return;
+  }
+
+  const app = getApp();
+  ///扫描所有在线Wifi设备
+  for (var i = 0; i < deviceList.length; i++) {
+    var device = deviceList[i];
+    if (device.connectType == 3) {
+      var deviceId = device.deviceId;
+      var topic = `/AIrSMArT_${deviceId.split("BLUFI_")[1]}/status/onoffline`;
+      app.subscribe(topic);
+      // break;
+    }
+  }
+};
+
+// [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
+///连上就调用2次 处理离线在线问题 wifi设备 BLUFI_
+/// payloads:{"uuid":"AIrSMArT_7cdfa1fcbb24","state":"online","userid":"1"}
+// [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
+
+///更新在线状态,连接第一个在线设备
+// deviceId: BLUFI_7cdfa1fd3af0
+// uuid: AIrSMArT_7cdfa1fd3af0
+function onlineDevice(payloads, changeCallback) {
+  var isChanged = false;
+  const strings = require('../strings');
+  var deviceList = getCurrentPages()[0].getDeviceList();
+
+  /// 处理在线设备
+  if (!strings.isEmpty(deviceList)) {
+    for (var i = 0; i < deviceList.length; i++) {
+      if (payloads && payloads.uuid) {
+        var device = deviceList[i];
+        var connectType = device.connectType;
+        if (connectType == 3) {
+          var deviceId = device.deviceId;
+          var splitDeviceId = deviceId.split("BLUFI_");
+          if (splitDeviceId.length == 2) {
+            var index = payloads.uuid.indexOf(splitDeviceId[1]);
+            if (index !== -1) {
+              if (device.state != payloads.state) {
+                isChanged = true;
+                device.state = payloads.state;
+              }
+              break;
+            }
+          }
+        }
+      }
+    }
+  }
+
+  ///更新数据
+  if (isChanged) {
+    changeCallback(deviceList);
+  }
+
+  ///是否已登录
+  var isLogin = getCurrentPages()[0].getIsLogin();
+  if (!isLogin) {
+    return;
+  }
+
+  ///当前没有连接设备,则去连接第一个wifi设备
+  var list = getCurrentPages()[0].getDeviceList();
+  var deviceListSelect = getCurrentPages()[0].getDeviceListSelect();
+  ///去连接第一个
+  if (deviceListSelect === null) {
+    if (!strings.isEmpty(list)) {
+      var deviceList = getCurrentPages()[0].getDeviceList();
+      var autoConnected = getCurrentPages()[0].getAutoConnected();
+      var connectDeviceIding = getCurrentPages()[0].getConnectDeviceIding();
+
+      /// 还没有自动连接采用第一个
+      if (!autoConnected) {
+        for (var i = 0; i < list.length; i++) {
+          var device = list[i];
+          var deviceId = device.deviceId;
+          if (device.connectType == 3 && device.state === "online") {
+            connectDeviceIding = deviceId;
+            break;
+          }
+        }
+      }
+
+      for (var i = 0; i < list.length; i++) {
+        var device = list[i];
+        var deviceId = device.deviceId;
+        if (deviceId == connectDeviceIding) {
+          if (device.connectType == 3 && device.state === "online") {
+            getCurrentPages()[0].actionDevice(device);
+          }
+          break;
+        }
+      }
+    }
+  } else {
+    // 当前播放设备离线
+    if (list.length > deviceListSelect && list[deviceListSelect].state !== "online") {
+      that.setData({
+        actionIndex: null,
+        deviceListSelect: null,
+      });
+    };
+  }
+};

+ 4 - 1
utils/lexin/scan.js

@@ -2,7 +2,8 @@ module.exports = {
   compareList: compareList,
 }
 
-function compareList(deviceList, changeCallback, addBlueCallback, autoConnected, deviceListSelect) {
+function compareList(changeCallback, addBlueCallback) {
+  var deviceList = getCurrentPages()[0].getDeviceList()
   if (deviceList.length > 0) {
     const {
       BtHelper
@@ -55,6 +56,8 @@ function compareList(deviceList, changeCallback, addBlueCallback, autoConnected,
         }
 
         ///没有连接则连接第一个在线的蓝牙
+        var autoConnected = getCurrentPages()[0].getAutoConnected();
+        var deviceListSelect = getCurrentPages()[0].getDeviceListSelect();
         if (!autoConnected && deviceListSelect == null) {
           var list = getCurrentPages()[0].getDeviceList();
           for (var i = 0; i < list.length; i++) {