module.exports = {
  subscribeSingleDevice: subscribeSingleDevice,
  subscribeAllDevice: subscribeAllDevice,
}

// 订阅在线单个设备   pages[0].actionDevice(device);
function subscribeSingleDevice(device) {
  //  /AIrSMArT_210052355545/status/onoffline 魅族款 APP
  // /AIrSMArT_861210052355545/status/onoffline 魅族款 小程序
  var deviceId = device.deviceId;
  var pages = getCurrentPages();
  const strings = require('../strings');
  var deviceList = pages[0].getDeviceList();
  // 猫王XOG-CyberRadio: BLUFI_864423060242830
  if (!strings.isEmpty(deviceList)) {
    var deviceListSelect = pages[0].getDeviceListSelect();
    ///当前已连接
    if (deviceListSelect != null && deviceList.length > deviceListSelect) {
      var d = deviceList[deviceListSelect].deviceId;
      if (d == deviceId) {
        return;
      }
    }
  }

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

// 订阅在线设备
function subscribeAllDevice() {
  const strings = require('../strings');
  var pages = getCurrentPages();
  var deviceList = pages[0].getDeviceList();
  if (strings.isEmpty(deviceList)) {
    return;
  }

  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 mac = lexin_util.getMacByDeviceId(deviceId);
      var topic = `/AIrSMArT_${mac}/status/onoffline`;
      app.subscribe(topic);
    }
  }
};