Pārlūkot izejas kodu

feature: 处理电池信息和播放状态以及播放暂停

Damon 7 mēneši atpakaļ
vecāks
revīzija
28c00c8edb
4 mainītis faili ar 178 papildinājumiem un 38 dzēšanām
  1. 3 38
      pages/index/index.js
  2. 99 0
      utils/lexin/connect.js
  3. 43 0
      utils/lexin/message.js
  4. 33 0
      utils/lexin/subscribe.js

+ 3 - 38
pages/index/index.js

@@ -168,29 +168,15 @@ Page({
 
           ///获取电量
           else if (payloadType === "battery" && other) {
+            var battery = lexin_message.getDeviceInfo(other.battery);
             that.setData({
-              battery: that._battery(other.battery),
+              battery: battery,
             })
           }
 
           ///获取播放状态
           else if (payloadType === "play" || payloadType === "play_state") {
-            var deviceList = that.getDeviceList();
-            var deviceListSelect = that.getDeviceListSelect();
-            if (deviceListSelect === null || deviceList.length <= deviceListSelect) {
-              return;
-            };
-
-            var deviceId = deviceList[deviceListSelect].deviceId;
-            // 接收设备当前播放状态
-            var deviceMacId = lexin_util.getDeviceMacId(deviceId);
-            const obj = {
-              DstDeviceName: deviceMacId
-            }
-            app.PubMsg({
-              type: "get_position",
-              ...obj
-            });
+            that.getPlay();
           }
         }
         break;
@@ -238,33 +224,12 @@ Page({
     ///  /AIrSMArT_7cdfa1fd3af0/user/pub_response
     var pubResponse = lexin_util.getResponseByDeviceMacId(deviceMacId);
     app.subscribe(pubResponse);
-
-    console.log("gadsfqwerqwerqr====333==");
     app.PubMsg({
       type: "get_dev_info",
       ...obj
     });
   },
 
-  // 格式化电量
-  _battery(battery) {
-    let _battery = 0;
-    if (battery < 20) {
-      _battery = 0
-    } else if (20 <= battery && battery < 40) {
-      _battery = 1
-    } else if (40 <= battery && battery < 60) {
-      _battery = 2
-    } else if (60 <= battery && battery < 80) {
-      _battery = 3
-    } else if (80 <= battery && battery <= 100) {
-      _battery = 4
-    } else if (battery > 100) {
-      _battery = 5
-    };
-    return _battery
-  },
-
   actionMusic(e) {
     var that = this;
     var actionIndex = that.data.actionIndex;

+ 99 - 0
utils/lexin/connect.js

@@ -0,0 +1,99 @@
+module.exports = {
+  searchOnlineDevice: searchOnlineDevice,
+}
+
+// [{"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 searchOnlineDevice(payloads, changeCallback, resetCallback) {
+  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);
+  }
+  _connectToDevice(resetCallback);
+};
+
+
+/// 连接设备
+function _connectToDevice(resetCallback) {
+  ///是否已登录
+  var isLogin = getCurrentPages()[0].getIsLogin();
+  if (!isLogin) {
+    return;
+  }
+
+  ///当前没有连接设备,则去连接第一个wifi设备
+  var deviceList = getCurrentPages()[0].getDeviceList();
+  var deviceListSelect = getCurrentPages()[0].getDeviceListSelect();
+  ///去连接第一个
+  if (deviceListSelect === null) {
+    const strings = require('../strings');
+    if (!strings.isEmpty(deviceList)) {
+      var autoConnected = getCurrentPages()[0].getAutoConnected();
+      var connectDeviceIding = getCurrentPages()[0].getConnectDeviceIding();
+
+      console.log("呃呃呃呃呃呃呃==000===" + connectDeviceIding);
+      /// 还没有自动连接采用第一个
+      if (!autoConnected) {
+        for (var i = 0; i < deviceList.length; i++) {
+          var device = deviceList[i];
+          var deviceId = device.deviceId;
+          if (device.connectType == 3 && device.state === "online") {
+            connectDeviceIding = deviceId;
+            break;
+          }
+        }
+      }
+
+      for (var i = 0; i < deviceList.length; i++) {
+        var device = deviceList[i];
+        var deviceId = device.deviceId;
+        console.log("呃呃呃呃呃呃呃==111===" + deviceId + "===" + connectDeviceIding);
+        if (deviceId == connectDeviceIding) {
+          console.log("呃呃呃呃呃呃呃==22222===" + deviceId + "===" + connectDeviceIding);
+          if (device.connectType == 3 && device.state === "online") {
+            getCurrentPages()[0].actionDevice(device);
+          }
+          break;
+        }
+      }
+    }
+  } else {
+    // 当前播放设备离线
+    if (deviceList.length > deviceListSelect && deviceList[deviceListSelect].state !== "online") {
+      resetCallback();
+    };
+  }
+}

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 43 - 0
utils/lexin/message.js


+ 33 - 0
utils/lexin/subscribe.js

@@ -0,0 +1,33 @@
+module.exports = {
+  subscribeSingleDevice: subscribeSingleDevice,
+  subscribeAllDevice: subscribeAllDevice,
+}
+
+// 订阅在线单个设备
+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;
+    }
+  }
+};