const {postData} = require('../api/intell') const {putDevice,loopSwitch,loopSwitchstate} = require('../api/device') function getlooplist(){ var group = wx.getStorageSync('group') //获取分组下拉 var groups = []//分组列表 var device = []//设备列表 for(let i = 0;i < group.length;i++){ groups.push(group[i].groupName) } //改变分组下标 if(wx.getStorageSync('groupName') != ''){ for(let n = 0;n < groups.length;n++){ if(groups[n] == wx.getStorageSync('groupName')){ var groupsIndex = n//分组下标 } } } for(let j = 0;j < group.length;j++){ if(groups[groupsIndex] == group[j].groupName){ if(group[j].devices != null && group[j].devices.length != 0){ for(let p = 0;p < group[j].devices.length;p++){ device.push(group[j].devices[p].deviceName) } if(wx.getStorageSync('deviceName') != ''){ for(let m = 0;m < device.length;m++){ if(device[m] == wx.getStorageSync('deviceName')){ var deviceIndex = m//设备下标 } } } //根据分组名和设备名获取key和id以及回路名称 for(let d = 0;d < group.length;d++){ if(groups[groupsIndex] == group[d].groupName){ for(let g = 0;g < group[d].devices.length;g++){ if(device[deviceIndex] == group[d].devices[g].deviceName){ var loopnum = group[d].devices[g].circuits var deviceId = group[d].devices[g].deviceId var deviceKey = group[d].devices[g].deviceKey } } } } } } } return [groups,groupsIndex,device,deviceIndex,loopnum,deviceId,deviceKey] } //回路开/关(旧设备) function Switch(deviceId,deviceKey,Rel){ postData(deviceId,deviceKey,Rel).then(res => { if(res.data.errno == 10){ wx.showModal({ title:'错误', content:'设备处于离线状态', showCancel:false }) } }) } //回路开/关读取(旧设备) function switchReal(police,lamp,index){ switch(police.length){ case 1: police = '000' + police break; case 2: police = '00' + police break; case 3: police = '0' + police break; default: police = police break; } if(police.substring(2,3) == '0' && police.substring(3,4) == '0'){ lamp[index] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png' } else if(police.substring(2,3) == '0' && police.substring(3,4) == '1'){ lamp[index] = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png' } return lamp } //回路开/关(新设备) function newSwitch(swit){ loopSwitch(swit).then(res =>{ if(res.data.code != 0){ wx.showModal({ title:'错误', content:res.data.msg, showCancel:false }) } }) } //回路开/关读取(新设备) function newswitchReal(Device){ loopSwitchstate(Device).then(res=>{ if(res.data.code == 0){ var relay = res.data.data if(relay[0].status == 1){ let ever = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp1.png' console.log(ever) return ever } else{ let ever = 'https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png' console.log(ever) return ever } } else{ wx.showModal({ title:'错误', content:res.data.msg, showCancel:false }) } }) } //分组下拉和设备下拉的获取(主页界面) function droplist(){ var group = wx.getStorageSync('group') //获取分组下拉 var groups = [] for(let i = 0;i < group.length;i++){ groups.push(group[i].groupName) } //改变分组下标 if(wx.getStorageSync('groupName') != ''){ let GroupName = that.data.groupName for(let n = 0;n < GroupName.length;n++){ if(GroupName[n] == wx.getStorageSync('groupName')){ var groupIndex = n } } } else{wx.setStorageSync('groupName',that.data.groupName[that.data.groupIndex])} //获取设备下拉 var device = [] var groupName = that.data.groupName var groupIndex = that.data.groupIndex for(let j = 0;j < group.length;j++){ if(groupName[groupIndex] == group[j].groupName){ if(group[j].devices != null && group[j].devices.length != 0){ for(let p = 0;p < group[j].devices.length;p++){ device.push(group[j].devices[p].deviceName) } that.setData({deviceName:device}) //改变设备下标 if(wx.getStorageSync('deviceName') != ''){ var DeviceName = that.data.deviceName for(let m = 0;m < DeviceName.length;m++){ if(DeviceName[m] == wx.getStorageSync('deviceName')){ that.setData({ deviceIndex:m }) } } } else{ wx.setStorageSync('deviceName', that.data.deviceName[that.data.deviceIndex]) } } } } } //刷新主页界面的显示 function Refresh(){ //重置状态 let loopName = []//回路名称 let loopIcon = []//回路图标 let switchState = []//开关状态 let size = []//字体大小 let fontColor = []//字体颜色 for(let p = 0;p < 12;p++){ loopName.push('回路' + (p+1)) loopIcon.push('') switchState.push('https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/lamp.png') size.push('calc(100vw * 16/375)') fontColor.push('#d5dbdf') } let combination = [loopName,loopIcon,switchState,size,fontColor] return combination } module.exports = { getlooplist:getlooplist, Switch:Switch, switchReal:switchReal, Refresh:Refresh, newSwitch:newSwitch, newswitchReal:newswitchReal, }