123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- 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,
- });
- };
- }
- };
|