Browse Source

feature: 适配各类数据类型处理

Damon 7 months atrás
parent
commit
842da112a4
7 changed files with 96 additions and 60 deletions
  1. 2 0
      app.js
  2. 12 8
      pages/index/index.js
  3. 19 11
      pages/index/index.wxml
  4. 13 11
      utils/lexin/connect.js
  5. 14 23
      utils/lexin/message.js
  6. 9 3
      utils/lexin/subscribe.js
  7. 27 4
      utils/lexin/util.js

+ 2 - 0
app.js

@@ -199,6 +199,8 @@ App({
     var that = this;
     if (that.globalData.client && that.globalData.client.connected) {
       //订阅主题
+      // err:null
+      // granted:[{"topic":"/AIrSMArT_861210052355545/status/onoffline","qos":0}]
       that.globalData.client.subscribe(topic, function (err, granted) {
         if (!err) {
           if (callback) {

+ 12 - 8
pages/index/index.js

@@ -120,7 +120,7 @@ Page({
   // 回调
   mqttCallback(type, option) {
     // console.log("gadsfadsfadsfa==888===" + type);
-    // console.log("weqrweqrwqwerqer====22==" + type);
+    console.log("呃呃呃呃呃呃呃==aaa===" + type);
     var that = this;
     let payloads = null;
     if (option && option.payload) {
@@ -132,7 +132,6 @@ Page({
       case "connect":
         lexin_subscribe.subscribeAllDevice();
         break;
-
         ///再走有在线
       case "message_onoffline":
         lexin_connect.searchOnlineDevice(payloads, function (list) {
@@ -147,8 +146,10 @@ Page({
 
         // PlayState:  ///播放状态:0默认状态,1播放状态,2暂停状态, 3停止状态,4缓冲状态。
       case "message":
+        console.log("呃呃呃呃呃呃呃==bbb===" + type);
         if (payloads) {
           var payloadType = payloads.type;
+          console.log("呃呃呃呃呃呃呃==ccc===" + payloadType);
           var other = payloads.other;
 
           ///获取频道位置
@@ -168,15 +169,16 @@ Page({
 
           ///获取电量
           else if (payloadType === "battery" && other) {
+            console.log("呃呃呃呃呃呃呃==xxxx===" + JSON.stringify(other));
             var battery = lexin_message.getDeviceInfo(other.battery);
             that.setData({
               battery: battery,
-            })
+            });
           }
 
           ///获取播放状态
           else if (payloadType === "play" || payloadType === "play_state") {
-            that.getPlay();
+            lexin_message.getPlay();
           }
         }
         break;
@@ -187,6 +189,7 @@ Page({
   ///去连接设备数据
   actionDevice(device) {
     var that = this;
+    console.log("呃呃呃呃呃呃呃==444===");
     // 取消订阅
     var deviceMacId = that.data.deviceMacId;
     if (!strings.isEmpty(deviceMacId)) {
@@ -196,6 +199,7 @@ Page({
 
     var deviceId = device.deviceId;
     deviceMacId = lexin_util.getDeviceMacId(deviceId);
+    console.log("呃呃呃呃呃呃呃==555===");
     that.setData({
       deviceMacId: deviceMacId,
     });
@@ -206,6 +210,7 @@ Page({
   subscribeCurrDevice(device) {
     var that = this;
     var isOK = app.globalData.client && app.globalData.client.connected;
+    console.log("呃呃呃呃呃呃呃==666===");
     if (!isOK) {
       app.connect();
       setTimeout(() => {
@@ -214,7 +219,7 @@ Page({
       return;
     };
 
-    console.log("gadsfqwerqwerqr====22==");
+    console.log("呃呃呃呃呃呃呃==777===");
     var deviceId = device.deviceId;
     var deviceMacId = lexin_util.getDeviceMacId(deviceId);
     const obj = {
@@ -230,10 +235,10 @@ Page({
     });
   },
 
-  actionMusic(e) {
+  onTapActionMusic(e) {
     var that = this;
     var actionIndex = that.data.actionIndex;
-    if (!strings.isEmpty(actionIndex)) {
+    if (strings.isEmpty(actionIndex)) {
       return;
     }
 
@@ -431,7 +436,6 @@ Page({
     };
 
     var device = deviceList[deviceListSelect];
-    console.log("gadsfadsfadsfa==999===" + JSON.stringify(device));
     wx.navigateTo({
       url: './../deviceWake/deviceWake?deviceId=' + device.deviceId + "&clientType=" + device.ProdModel,
     });

File diff suppressed because it is too large
+ 19 - 11
pages/index/index.wxml


+ 13 - 11
utils/lexin/connect.js

@@ -13,6 +13,7 @@ module.exports = {
 function searchOnlineDevice(payloads, changeCallback, resetCallback) {
   var isChanged = false;
   const strings = require('../strings');
+  const lexin_util = require('../lexin/util');
   var deviceList = getCurrentPages()[0].getDeviceList();
 
   /// 处理在线设备
@@ -23,16 +24,15 @@ function searchOnlineDevice(payloads, changeCallback, resetCallback) {
         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;
+          var mac = lexin_util.getMacByDeviceId(deviceId);
+          var index = payloads.uuid.indexOf(mac);
+          if (index !== -1) {
+            console.log("呃呃呃呃呃呃呃==333===" + payloads.state + "===" + JSON.stringify(device));
+            if (device.state != payloads.state) {
+              isChanged = true;
+              device.state = payloads.state;
             }
+            break;
           }
         }
       }
@@ -68,6 +68,7 @@ function _connectToDevice(resetCallback) {
         for (var i = 0; i < deviceList.length; i++) {
           var device = deviceList[i];
           var deviceId = device.deviceId;
+          console.log("呃呃呃呃呃呃呃==888===" + JSON.stringify(device));
           if (device.connectType == 3 && device.state === "online") {
             connectDeviceIding = deviceId;
             break;
@@ -75,13 +76,14 @@ function _connectToDevice(resetCallback) {
         }
       }
 
+      console.log("呃呃呃呃呃呃呃==aaa===");
       for (var i = 0; i < deviceList.length; i++) {
         var device = deviceList[i];
         var deviceId = device.deviceId;
+        console.log("呃呃呃呃呃呃呃==bbb===" + deviceId);
+        console.log("呃呃呃呃呃呃呃==ccc===" + connectDeviceIding);
         if (deviceId == connectDeviceIding) {
-          console.log("呃呃呃呃呃呃呃==22222===" + deviceId + "===" + connectDeviceIding);
           if (device.connectType == 3 && device.state === "online") {
-            console.log("呃呃呃呃呃呃呃==333===" + deviceId + "===" + connectDeviceIding);
             getCurrentPages()[0].actionDevice(device);
           }
           break;

+ 14 - 23
utils/lexin/message.js

@@ -29,50 +29,41 @@ function getDeviceInfo(payloads) {
   var other = payloads.other;
   var SrcDeviceName = payloads.SrcDeviceName;
 
-  console.log("呃呃呃呃呃呃呃==000===");
+  console.log("呃呃呃呃呃呃呃==ddddd===");
   ///连上就调用2次 payloads.SrcDeviceName:AIrSMArT_7cdfa1fcbb24
   const strings = require('../strings');
+  const lexin_util = require('../lexin/util');
   var deviceList = getCurrentPages()[0].getDeviceList();
   if (!strings.isEmpty(deviceList)) {
     var currentDeviceId = "";
     /// 获取需要连接的那个设备,获取设备名称
     for (var i = 0; i < deviceList.length; i++) {
       if (SrcDeviceName) {
+        console.log("呃呃呃呃呃呃呃==eeee===");
         var device = deviceList[i];
         var deviceId = device.deviceId;
-        var splitDeviceId = deviceId.split("BLUFI_");
-        console.log("呃呃呃呃呃呃呃==111===");
-        if (splitDeviceId.length == 2) {
-          var index = SrcDeviceName.indexOf(splitDeviceId[1]);
-          console.log("呃呃呃呃呃呃呃==2222===");
-          ///彻底连接成功
-          if (index !== -1) {
-            console.log("呃呃呃呃呃呃呃==333===" + deviceId);
-            currentDeviceId = deviceId;
-            console.log("呃呃呃呃呃呃呃==xxx===" + currentDeviceId);
-            var ProdModel = other.ProdModel;
-            var devName = other.devName;
-            if (device.ProdModel != ProdModel || device.devName != devName) {
-              console.log("呃呃呃呃呃呃呃==444===");
-              isUpdate = true;
-              device.ProdModel = ProdModel;
-              device.devName = devName;
-            }
-            break;
+        var mac = lexin_util.getMacByDeviceId(deviceId);
+        var index = SrcDeviceName.indexOf(mac);
+        ///彻底连接成功
+        if (index !== -1) {
+          currentDeviceId = deviceId;
+          var ProdModel = other.ProdModel;
+          var devName = other.devName;
+          if (device.ProdModel != ProdModel || device.devName != devName) {
+            device.devName = devName;
+            device.ProdModel = ProdModel;
           }
+          break;
         }
       }
     }
 
-    console.log("呃呃呃呃呃呃呃==555===" + currentDeviceId);
     /// 去更新选中目标
     if (!strings.isEmpty(currentDeviceId)) {
       for (var i = 0; i < deviceList.length; i++) {
         var device = deviceList[i];
         var deviceId = device.deviceId;
-        console.log("呃呃呃呃呃呃呃==aaa===" + currentDeviceId);
         if (deviceId == currentDeviceId) {
-          console.log("呃呃呃呃呃呃呃==bbb===" + currentDeviceId);
           getCurrentPages()[0].refreshCurrentDevice(device);
           break;
         }

+ 9 - 3
utils/lexin/subscribe.js

@@ -5,7 +5,11 @@ module.exports = {
 
 // 订阅在线单个设备
 function subscribeSingleDevice(deviceId) {
-  var topic = `/AIrSMArT_${deviceId.split("BLUFI_")[1]}/status/onoffline`;
+  //  /AIrSMArT_210052355545/status/onoffline 魅族款 APP
+  // /AIrSMArT_861210052355545/status/onoffline 魅族款 小程序
+  const lexin_util = require('../lexin/util');
+  var mac = lexin_util.getMacByDeviceId(deviceId);
+  var topic = `/AIrSMArT_${mac}/status/onoffline`;
   const app = getApp();
   app.unsubscribe(topic);
   app.subscribe(topic);
@@ -20,14 +24,16 @@ function subscribeAllDevice() {
   }
 
   const app = getApp();
+  const lexin_util = require('../lexin/util');
   ///扫描所有在线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`;
+      var mac = lexin_util.getMacByDeviceId(deviceId);
+      console.log("呃呃呃呃呃呃呃==wwwww===" + mac);
+      var topic = `/AIrSMArT_${mac}/status/onoffline`;
       app.subscribe(topic);
-      // break;
     }
   }
 };

+ 27 - 4
utils/lexin/util.js

@@ -1,18 +1,41 @@
 module.exports = {
+  getMacByDeviceId: getMacByDeviceId,
+  getMacBySplitDeviceId: getMacBySplitDeviceId,
   getDeviceMacId: getDeviceMacId,
   getResponseByDeviceId: getResponseByDeviceId,
   getResponseByDeviceMacId: getResponseByDeviceMacId,
 }
 
-///获取设备mac地址
+///获取设备mac相关 macId: deviceId.split("BLUFI_")[1]
+function getMacBySplitDeviceId(splitDeviceId) {
+  if (splitDeviceId.length < 12) {
+    return splitDeviceId;
+  }
+  return splitDeviceId.slice(-12);
+};
+
+function getMacByDeviceId(deviceId) {
+  var splitDeviceId = deviceId.split("BLUFI_");
+  if (splitDeviceId.length != 2) {
+    return "";
+  }
+  return getMacBySplitDeviceId(splitDeviceId[1]);
+};
+
+///获取设备mac相关
 function getDeviceMacId(deviceId) {
-  return `AIrSMArT_${deviceId.split("BLUFI_")[1]}`
+  var splitDeviceId = deviceId.split("BLUFI_");
+  if (splitDeviceId.length != 2) {
+    return "";
+  }
+  var mac = getMacBySplitDeviceId(splitDeviceId[1]);
+  return `AIrSMArT_${mac}`
 };
 
 ///通过设备id获取mac地址
 function getResponseByDeviceId(deviceId) {
-  var deviceMacId = `AIrSMArT_${deviceId.split("BLUFI_")[1]}`
-  return `/${deviceMacId}/user/pub_response`;
+  var deviceMacId = getDeviceMacId(deviceId);
+  return getResponseByDeviceMacId(deviceMacId);
 };
 
 ///通过mac地址过去请求