瀏覽代碼

feature: 处理otr-x在线离线回调处理

Damon 8 月之前
父節點
當前提交
57da0c4ab7
共有 2 個文件被更改,包括 113 次插入128 次删除
  1. 111 126
      pages/index/index.js
  2. 2 2
      utils/update.js

+ 111 - 126
pages/index/index.js

@@ -14,6 +14,7 @@ Page({
       showCapsule: 0, //是否显示左上角图标   1表示显示    0表示不显示
       title: 'OhPlay', //导航栏 中间的标题
     },
+
     ///下午好
     isLogin: false,
     greeting: "",
@@ -194,15 +195,11 @@ Page({
               that.data.deviceList[index].devName = payloads.other.devName;
             }
           });
+
           // 更新缓存
-          wx.setStorage({
-            key: "devicelist",
-            data: JSON.stringify(that.data.deviceList),
-            success() {
-              that.setData({
-                deviceList: that.data.deviceList
-              })
-            }
+          wx.setStorageSync("devicelist", JSON.stringify(that.data.deviceList));
+          that.setData({
+            deviceList: that.data.deviceList
           });
         } else if (payloads.type === "battery" && payloads.other) {
           that.setData({
@@ -241,42 +238,53 @@ Page({
 
   // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online","ProdModel":"MW-2AX(WIFI-N)","devName":"猫王小王子OTR-X"}]
 
-  ///连上就调用2次
+  ///连上就调用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"}]
   online(payloads) {
     // 设置在线状态
     var that = this;
     console.log("gadsfadsfadsfa==777===" + JSON.stringify(payloads));
-    that.data.deviceList.map((v, index) => {
-      if (payloads.uuid && payloads.uuid.indexOf(v.name.split("BLUFI_")[1]) !== -1) {
-        that.data.deviceList[index].state = payloads.state;
-      }
-    });
 
-    that.setData({
-      deviceList: that.data.deviceList,
-    });
-    // 更新缓存
-    wx.setStorage({
-      key: "devicelist",
-      data: JSON.stringify(that.data.deviceList)
-    });
-    // 如没有选中,选中最新的
-    (() => {
-      if (that.data.deviceListIndex === null) {
-        let itue = false;
-        that.data.deviceList.map((v, index) => {
-          if (v.state === "online" && !itue) {
-            itue = true;
-            that.actionDevice(index);
+    var isUpdate = false;
+    var deviceList = that.data.deviceList;
+    if (!strings.isEmpty(deviceList)) {
+      for (var i = 0; i < deviceList.length; i++) {
+        if (payloads.uuid) {
+          var deviceId = deviceList[i].deviceId;
+          var splitDeviceId = deviceId.split("BLUFI_");
+          if (splitDeviceId.length == 2) {
+            var index = payloads.uuid.indexOf(splitDeviceId[1]);
+            if (index !== -1) {
+              if (deviceList[i].state != payloads.state) {
+                isUpdate = true;
+                deviceList[i].state = payloads.state;
+                break;
+              }
+            }
           }
-        });
+        }
       }
-      //  else {
-      //   that.actionDevice(that.data.deviceListIndex);
-      // };
+    }
 
-    })();
+    ///数据有更新
+    if (isUpdate) {
+      var finalList = that.updateDeviceList(deviceList);
+      that.setData({
+        deviceList: finalList,
+      });
+    }
+
+    ///当前没有连接设备,则去连接第一个wifi设备
+    if (that.data.deviceListIndex === null) {
+      var list = that.data.deviceList;
+      for (var i = 0; i < list.length; i++) {
+        if (list[i].state === "online" && list[i].ProdModel == "MW-2AX(WIFI-N)") {
+          that.actionDevice(i);
+          break;
+        }
+      }
+    }
 
     // 当前播放设备离线
     if (that.data.deviceListIndex !== null && that.data.deviceList[that.data.deviceListIndex].state !== "online") {
@@ -306,9 +314,11 @@ Page({
       }, 500);
       return;
     };
+
     if (that.data.deviceList.length === 0 || that.data.deviceListIndex === null) {
       return
     };
+
     let topic = `/AIrSMArT_${that.data.deviceList[that.data.deviceListIndex].name.split("BLUFI_")[1]}/user/pub_response`;
     app.subscribe(topic);
     const obj = {
@@ -379,10 +389,8 @@ Page({
         type: "get_position",
         ...obj
       });
-      wx.setStorage({
-        key: "channelData",
-        data: res
-      });
+
+      wx.setStorageSync("channelData", res);
 
       // 有新设备
       if (app.globalData.newDeviceId) {
@@ -412,20 +420,10 @@ Page({
 
   goChannel() {
     var that = this;
-    wx.setStorage({
-      key: "channelDeta",
-      data: that.data.channelData[that.data.actionIndex],
-      success() {
-        wx.navigateTo({
-          url: './../channelDetails/channelDetails'
-        })
-      }
-    })
-  },
-
-  updata() {
-    // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
-    wx.getUpdateManager().applyUpdate()
+    wx.setStorageSync("channelDeta", that.data.channelData[that.data.actionIndex]);
+    wx.navigateTo({
+      url: './../channelDetails/channelDetails'
+    });
   },
 
   onUnload() {
@@ -507,18 +505,22 @@ Page({
     var item = e.currentTarget.dataset.item;
     var ProdModel = item.ProdModel;
     if (ProdModel == "MW-2AX(WIFI-N)") {
-      console.log("gadfadsfadsfaf===aa");
       if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
-        console.log("gadfadsfadsfaf===bbb");
         that.goWake();
       } else {
-        console.log("gadfadsfadsfaf===ccc");
         that.actionDevice(e.currentTarget.dataset.index)
       }
       return;
     }
     console.log(e)
+
     ///去蓝牙连接处理
+    // if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
+    //   that.goWake();
+    // } else {
+    //   that.actionDevice(e.currentTarget.dataset.index)
+    // }
+
     if (item.state !== "online") {
       that.addConnectBlueDevice({
         deviceId: item.deviceId,
@@ -547,7 +549,7 @@ Page({
 
     that.setData({
       deviceListIndex: index,
-      thisDeviceMac: `AIrSMArT_${that.data.deviceList[index].name.split("BLUFI_")[1]}`
+      thisDeviceMac: `AIrSMArT_${that.data.deviceList[index].deviceId.split("BLUFI_")[1]}`
     });
 
     // app.PubMsg({
@@ -585,22 +587,17 @@ Page({
               return id !== item.deviceId
             });
 
-            wx.setStorage({
-              key: "devicelist",
-              data: JSON.stringify(deviceList),
-              success() {
-                that.setData({
-                  deviceList,
-                });
-
-                // 当前没有设备
-                if (deviceList.length === 0) {
-                  that.setData({
-                    deviceListIndex: null,
-                  });
-                }
-              }
+            wx.setStorageSync("devicelist", JSON.stringify(deviceList));
+            that.setData({
+              deviceList,
             });
+
+            // 当前没有设备
+            if (deviceList.length === 0) {
+              that.setData({
+                deviceListIndex: null,
+              });
+            }
             return;
           }
 
@@ -614,53 +611,48 @@ Page({
           // 取消订阅
           app.unsubscribe(`/AIrSMArT_${that.data.deviceList[e.currentTarget.dataset.index].name.split("BLUFI_")[1]}/user/pub_response`);
 
-          wx.setStorage({
-            key: "devicelist",
-            data: JSON.stringify(deviceList),
-            success() {
+          wx.setStorageSync("devicelist", JSON.stringify(deviceList));
+          that.setData({
+            deviceList,
+            thisDeviceMac: null
+          });
+
+          // 当前没有设备
+          if (deviceList.length === 0) {
+            that.setData({
+              actionIndex: null,
+              deviceListIndex: null,
+            });
+          }
+
+          if (that.data.deviceListIndex === null) {
+            return
+          };
+
+          if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
+            let index_ = null;
+            deviceList.map((v, index) => {
+              if (v.state === "online" && index_ === null) {
+                index_ = index;
+              }
+            });
+            if (index_ !== null) {
+              that.actionDevice(index_);
+            } else {
               that.setData({
-                deviceList,
-                thisDeviceMac: null
+                actionIndex: null,
+                deviceListIndex: null,
               });
-
-              // 当前没有设备
-              if (deviceList.length === 0) {
+            };
+          } else {
+            deviceList.map((v, index) => {
+              if (v.name === name) {
                 that.setData({
-                  actionIndex: null,
-                  deviceListIndex: null,
+                  deviceListIndex: index,
                 });
               }
-
-              if (that.data.deviceListIndex === null) {
-                return
-              };
-
-              if (e.currentTarget.dataset.index === that.data.deviceListIndex) {
-                let index_ = null;
-                deviceList.map((v, index) => {
-                  if (v.state === "online" && index_ === null) {
-                    index_ = index;
-                  }
-                });
-                if (index_ !== null) {
-                  that.actionDevice(index_);
-                } else {
-                  that.setData({
-                    actionIndex: null,
-                    deviceListIndex: null,
-                  });
-                };
-              } else {
-                deviceList.map((v, index) => {
-                  if (v.name === name) {
-                    that.setData({
-                      deviceListIndex: index,
-                    });
-                  }
-                });
-              }
-            }
-          });
+            });
+          }
         }
       }
     });
@@ -692,6 +684,7 @@ Page({
       mac: newDevice.mac,
     });
 
+    ///在线排序前面,wifi设备排序前面
     var finalList = that.updateDeviceList(deviceList);
     var indexPage = that.data.indexPage;
     if (indexPage != 0) {
@@ -706,16 +699,11 @@ Page({
         deviceList: finalList,
       });
     }
-
-    // 更新缓存
-    wx.setStorage({
-      key: "devicelist",
-      data: JSON.stringify(finalList)
-    });
   },
 
   addConnectWifiDevice(deviceList) {
     var that = this;
+    ///在线排序前面,wifi设备排序前面
     var finalList = that.updateDeviceList(deviceList);
     var indexPage = that.data.indexPage;
     if (indexPage != 0) {
@@ -730,14 +718,9 @@ Page({
         deviceList: finalList,
       });
     }
-
-    // 更新缓存
-    wx.setStorage({
-      key: "devicelist",
-      data: JSON.stringify(finalList)
-    });
   },
 
+  ///更新列表排序
   updateDeviceList(deviceList) {
     if (deviceList.length == 0) return [];
 
@@ -787,6 +770,8 @@ Page({
     });
     finalList = finalList.concat(onNoLineWifiList);
     finalList = finalList.concat(onNoLineWifiList);
+    // 更新缓存
+    wx.setStorageSync("devicelist", JSON.stringify(finalList));
     return finalList;
   },
 

+ 2 - 2
utils/update.js

@@ -22,8 +22,8 @@ function update() {
   manager.onUpdateReady(function (e) {
     isShowed = true;
     wx.showModal({
-      title: '已有新版本咯',
-      content: '请您删除当前小程序,重新打开呦~',
+      title: '更新提示',
+      content: '新版本已准备好,是否重启应用?',
       success: function (res) {
         if (res.confirm) {
           manager.applyUpdate();