import { alone, presentStationData } from "@/utils/deviceUtil/js/baseStation.js" import { flapValveControl, flapValvePreload, threeWayValveControl, threeWayValvePreload, fiveWayValveControl, getValveAngleConfig, calcEndTime } from "@/utils/deviceUtil/js/valveNew.js" const msgType = { default: 0, ok: 1, err: -1, } /** * 设备控制 */ class deviceControl { // 双击操作 doubleClick = true; /** * @param {landData} 地块信息 */ land = { data: [], item: { id: null, name: "" }, }; /** * @param {stations} 基站列表 */ stations = []; /** * @param {flapValves} 基站列表 */ flapValves = []; /** * @param {valves} 阀门列表 */ valves = []; /** * @param {adjustNumber} 调压值 */ adjustNumber = 1; /** * @param {others} 其他列表 */ others = { header: [], agricultural: [], deviceMp: [], } /** * @param {topics} 订阅topic */ topics = { oldV: [], newV: [], }; /** * 数据筛选 */ forward = false; screenKey = ""; screen = { all: 0, online: 0, offline: 0, watering: 0, unwater: 0, error: 0, }; /** * @param {Object} 初始化方法 */ constructor(eventHandler, msgHandler) { this.eventHandler = eventHandler; this.msgHandler = msgHandler; }; //获取消息类型 get msgType() { return msgType; } get topic() { if (this.topics.newV.length && this.topics.newV.length) { const newV = new Set(this.topics.newV); const oldV = this.topics.oldV.filter(x => !newV.has(x)); return { oldV: oldV, newV: this.topics.newV, } } else { return this.topics; } } /** * 设置地块数据 * @param companys 公司 * @param lands 地块 * @param id 当前id */ setLandData(companys, lands, id) { let selectItem = null; if (companys.length && lands.length) { const companyObj = companys.reduce((accumulator, current) => { accumulator[current.id] = current; return accumulator; }, {}); let groups = {}; lands.map(item => { let param = { companyId: item.companyId, companyName: companyObj[item.companyId] ? companyObj[item.companyId].label : "", id: item.id, name: item.landName, landIcon: item.landIcon || '', valueNum: item.valveNum, controlFlag: item.controlFlag, //设备是否可控(0是 1否) enableFlapValve: item.enableFlapValve == 0, //启用蝶阀 landManualOperation: item.landManualOperation == 0, //启用批量控制 mapContent: null } // 获取地图信息 if (item.landMap != null) { param.mapContent = JSON.parse(decodeURIComponent(item.landMap.mapContent)) //地块地图 地图标点 } // 判断公司是否已经分组,如果未分组,则添加分组 if (!groups[item.companyId]) { groups[item.companyId] = []; } groups[item.companyId].push(param); if (param.id == id) { selectItem = param; } }) companys.forEach(item => { item.children = groups[item.id] || []; }); if (!selectItem) { selectItem = landArr[0]; } } this.land.data = companys; return selectItem; } /** * 设置选中地块数据 * @param item 选中列表 * @param callback */ setLandItem(item, callback) { if (item != this.land.item) { this.land.item = item; this.stations = []; this.flapValves = []; this.valves = []; this.adjustNumber = 1; this.others = { header: [], agricultural: [], deviceMp: [], } this.topics = { oldV: this.topics.newV, newV: [], }; this.forward = false; this.screenKey = ""; this.screen = { all: 0, online: 0, offline: 0, watering: 0, unwater: 0, error: 0, }; this.addTopics([`jsy/iot/control/${item.id}`]); this.getTaskStatus(); if (typeof callback === 'function') { callback(); } } } /** * 设置设备数据(除阀门外的设备) * @param {Object} data */ setOtherDevices(data) { if (data && data.length) { let stations = [], header = [], agricultural = [], flapValves = []; data.forEach(item => { if (item.deviceTypeId === 1) { stations.push(item); } else if (item.deviceTypeId === 4 || item.deviceTypeId === 5) { header.push(item); } else if (item.deviceTypeId === 6 || item.deviceTypeId === 7 || item.deviceTypeId === 8) { agricultural.push(item); } else if (item.deviceTypeId === 12) { flapValves.push(item); } }); this.setStations(stations); this.setFlapValves(flapValves); this.others.header = header; this.others.agricultural = agricultural; } } /** * 设置墒情 * @param {Object} data */ setDeviceMp(data) { if (data && data.length) { this.others.deviceMp = data; } else { this.others.deviceMp = [] } } /** * 设置基站数据 * @param {Object} data */ setStations(data) { let topic = []; if (data && data.length) { let stations = presentStationData(data); stations.forEach(item => { topic.push("jsy/iot/push/" + item.iotId); item["hidden"] = false; }); this.stations = stations; } else { this.stations = [] } this.addTopics(topic); } /** * 设置蝶阀数据 * @param {Object} data */ setFlapValves(data) { let topic = []; if (data && data.length) { for (let i = 0; i < data.length; i++) { data[i] = flapValveControl(data[i]); topic.push("jsy/iot/push/" + data[i].iotId); } this.flapValves = data; } else { this.flapValves = []; } this.addTopics(topic); } /** * 设置阀门数据 * @param landId * @param {Object} data */ setValves(landId, data) { const that = this; let topic = [], loadData = []; if (data && data.length) { // 数据排序 data.sort(commonUtil.compareDataNew("landGroup", "branchCanalCode")); data.forEach((item, i) => { item.isOpened = false; // 展开折叠 item.isHide = false; // 隐藏 item.presentGroup = true; // 组状态 item.rowKey = item.pipeNetworkName; // 出水口名称 item.stationCode = "无"; // 蝶阀数据设置 item.device = item.controlDevice; if (item.deviceId && that.flapValves.length) { const device = that.flapValves.find(x => x.id == item.deviceId); item.device = device || null; if (item.deviceFlapValveSub) { item.subIndex = item.deviceFlapValveSub.subIndex; } } let Controllable = false; // 蝶阀权限字段 //便利出水口设备 if (item.children && item.children.length) { item.children.sort(commonUtil.compareDataNew("outletCode", "multiPortCode")); item.children.forEach((x, j) => { x.isHide = false; // 隐藏 x.rowKey = x.waterOutletName; // 出水口名称 x.stationCode = "无"; // 阀门数据设置 x.device = x.controlDevice; if (x.device) { topic.push("jsy/iot/push/" + x.device.iotId); if (x.device.deviceTypeId == 2 || x.device.deviceTypeId == 3) { x.device.deviceValve = x.deviceValve x.device = threeWayValveControl(x.device); that.setValveShowRate(x, null); // 这里的状态都是本地或者根据状态判断了,所以这个预加载没有意义了 // if (x.device.loading && x.device.loading.vss) { // x.device = threeWayValvePreload(x.device, x.device.loading.vss, x // .device.loading.mc); // } if (x.device.data && x.device.data.items && x.device.data.items .mc) { let mc = Math.abs(Number(x.device.data.items.mc.value)); if (mc == 0 || mc > 5) { x.device.stall = that.adjustNumber; } else { x.device.stall = mc; } } else { x.device.stall = that.adjustNumber; } // 判断设备权限 const btnControllable = x.device.custom.btnControllable if (btnControllable.closeAll || btnControllable.open1 || btnControllable.open2 || btnControllable.openAll) { Controllable = true; } } else if (x.device.deviceTypeId == 13) { x.device.deviceValve = x.deviceValve; x.device = fiveWayValveControl(x.device); that.setValveShowRate(x, 1); x.device.stall = that.adjustNumber; const btnControllable = x.device.custom.btnControllable if (btnControllable.open1 || btnControllable.open2 || btnControllable.open3 || btnControllable.open4) { Controllable = true; } } if (that.isLoading(x.device)) { loadData.push({ device: x.device, arr: [i, j], }); } x.stationCode = x.device.deviceValve.stationName || "无"; } }); } item.btnControllable = Controllable; }); if (that.valves && that.valves.length && landId == that.land.item.id) { // 更新数据 if (data.length == that.valves.length) { for (var i = 0; i < that.valves.length; i++) { let item = that.valves[i]; if (item.id == data[i].id) { item.controlDevice = data[i].controlDevice; item.device = data[i].device; item.deviceCode = data[i].deviceCode; item.deviceFlapValve = data[i].deviceFlapValve; item.deviceFlapValveSub = data[i].deviceFlapValveSub; item.deviceId = data[i].deviceId; item.children = data[i].children; } that.valves[i] = item; } } else { // 替换数据 that.valves = data; that.onMsgHandler({ success: msgType.default, msg: "数据变动,重新加载" }) } } else { // 替换数据 that.valves = data; } } else { that.valves = []; } if (loadData.length) { loadData.forEach(item => { that.setLoadTimersData(item.device, item.arr); }); } that.addTopics(topic); that.screenData(); that.startRunningTimer(); // // 如果数据有效,查询相关数据 // if(that.valves.length){ // //给设备匹配时间 // this.matchTime("all"); // } } /** * 设置阀门显示的角度百分比 * @param {Object} item * @param {Object} index */ setValveShowRate(item, index) { if (item.device.deviceTypeId == 2 || item.device.deviceTypeId == 3) { for (var key in item.device.custom.btnState) { if (item.device.custom.btnState[key].color != 'info') { if (key == 'closeAll') { item.showRate = 0; } else if (key == 'openAll') { item.showRate = 100; } else { item.showRate = item.device.custom.aRate; } } } } if (item.device.deviceTypeId == 13) { item.showRate = `open${index}`; } } /** * 添加tioic * @param {Object} topic */ addTopics(topic) { this.topics.newV = this.topics.newV.concat(topic); } /** * 设置调压等级 * @param {Object} number */ setAdjustNumber(number) { this.adjustNumber = Number(number); } /****************************** 数据、操作验证 ******************************/ /** * 获取地块轮灌状态 * @param {Object} id */ getTaskStatus(id) { console.error("轮灌状态查询") } // 双击参数 dblClickThreshold = 500; dblClicObj = { time: 0, type: undefined }; /** * 验证双击 * @param type 按钮类型 */ dbClickHandle(type) { const that = this; if (!that.doubleClick) { return true; } const timestamp = new Date().getTime(); if (that.dblClicObj.type === type) { if ((timestamp - that.dblClicObj.time) < that.dblClickThreshold) { that.dblClicObj.type = undefined; that.dblClicObj.time = 0; return true; } else { that.dblClicObj.type = type; that.dblClicObj.time = timestamp; return false; } } else { that.dblClicObj.type = type; that.dblClicObj.time = timestamp; return false; } } //点击的时候判断有没有操作权限 hasPermission(item, index) { let msg = ""; if (item.device.deviceTypeId == 2 || item.device.deviceTypeId == 3) { const btnOpen = item.device.custom.btnOpen; let state = true; for (const key in btnOpen) { if (btnOpen[key]) { state = item.device.custom.btnControllable[key]; } } if (!state) { msg = "对方正在使用,请联系对方出水口账号人员关闭阀门后操作。" } } else if (item.device.deviceTypeId == 13) { if (!item.device.custom.btnControllable[`open${index}`]) { msg = "无操作权限!" } } if (msg) { that.onMsgHandler({ success: msgType.default, msg: msg }) return false; } else { return true; } } /****************************** 设备操作 ******************************/ /** * 刷新设备 * @param {Object} item */ refreshDeviceItem(item) { if (!this.dbClickHandle("refreshDeviceItem")) return; const that = this; that.onMsgHandler({ success: msgType.ok, msg: `${item.rowKey}刷新` }) item.device.custom.ws = 1; that.onEventHandler(that.eventType.refresh, item); } /** * 设置刷新状态数据 * @param {Object} item * @param {Object} res */ setRefreshDeviceState(item, res) { item.device.status = res.data.status; item.device.data = res.data.data; //数据解析 if (item.device.deviceTypeId == 11 || item.device.deviceTypeId == 12) { item.device = flapValveControl(item.device); } else if (item.device.deviceTypeId == 2 || item.device.deviceTypeId == 3) { item.device = threeWayValveControl(item.device); } else if (item.device.deviceTypeId == 13) { item.device = fiveWayValveControl(item.device); } item.device.custom.ws = 1; // loadTimer(row.device, arr); } /** * 控制按钮事件 * @param {Object} item * @param {Object} index */ deviceControlHandle(item, index, value) { this.setValveShowRate(item, index); if (!this.dbClickHandle(`${item.deviceId}-${index}`)) return; // if (!this.hasPermission(item, index)) return; this.buildCommand(item, index, value); } /** * 组织命令 * @param {Object} item * @param {Object} index * @param {Object} value */ buildCommand(item, index, value) { const that = this; if (item.device.deviceTypeId == 11 || item.device.deviceTypeId == 12) { // 配置的角度参数 const config = getValveAngleConfig(item.device.deviceTypeId); let params = { deviceTypeId: item.device.deviceTypeId, privateSeal: item.device.privateSeal, deviceVersionId: item.device.deviceVerId, angleKey: config[Number(value)], angleSize: 0, //大角度控制,正负数未调压的大小 valveNo: index, operationType: Number(value) > 0 ? 10 : 11, } item.device.custom.subData[index - 1].btnPreload = true; item.device.custom.subData[index - 1].btnLoading.closeAll = true; item.device.custom.subData[index - 1].btnLoading.openAll = false; item.device.custom.subData[index - 1].btnDisable.closeAll = true; item.device.custom.subData[index - 1].btnDisable.openAll = true; this.onEventHandler(this.eventType.send, params, item); } else { if (value >= 0) { const config = getValveAngleConfig(item.device.deviceTypeId); const deviceValveAngles = item.device.deviceValveAngles.reduce((obj, item) => { obj[item.angleKey] = item; return obj; }, {}); let angleObj = null, angleADC = null, operationType = undefined; let params = null; if (item.device.deviceTypeId == 2 || item.device.deviceTypeId == 3) { let btnKey = ""; if (index == 0) { // 全关 angleObj = deviceValveAngles[config[0]]; angleADC = angleObj.angleAdc; operationType = 6; btnKey = "closeAll"; } else if (index == 1 || index == 2) { // 12开 if (value == 0) { angleObj = deviceValveAngles[config[0]]; angleADC = angleObj.angleAdc; operationType = 6; btnKey = "closeAll"; } else if (value == 100) { if (index == 1) { angleObj = deviceValveAngles[config[2]]; angleADC = angleObj.angleAdc; operationType = 3; btnKey = "open1"; } if (index == 2) { angleObj = deviceValveAngles[config[4]]; angleADC = angleObj.angleAdc; operationType = 4; btnKey = "open2"; } } else { if (index == 1) { angleObj = deviceValveAngles[config[3]]; let angleInterval = angleObj.angleAdcUpperLimit - angleObj.angleAdcLowerLimit; angleADC = angleObj.angleAdcUpperLimit - Math.ceil(angleInterval * value / 100); operationType = 7; btnKey = "open1"; } if (index == 2) { angleObj = deviceValveAngles[config[5]]; let angleInterval = angleObj.angleAdcUpperLimit - angleObj.angleAdcLowerLimit; angleADC = Math.ceil(angleInterval * value / 100) + angleObj.angleAdcLowerLimit; operationType = 7; btnKey = "open2"; } } } else if (index == 3) { // 全开 angleObj = deviceValveAngles[config[1]]; angleADC = angleObj.angleAdc; operationType = 5; btnKey = "openAll"; } else { return null; } // 控制参数 params = { deviceTypeId: item.device.deviceTypeId, privateSeal: item.device.privateSeal, deviceVersionId: item.device.deviceVerId, angleKey: angleObj.angleKey, // angleSize: angleSize, //大角度控制,正负数未调压的大小 valveNo: index, angleADC: angleADC, operationType: operationType, }; // 更新预加载状态 item.device.custom.ct.time = 0; item.device.custom.ct.value = 0; item.device.custom.ct.endS = -1; for (let key in item.device.custom.btnState) { if (key == btnKey) { item.device.custom.btnState[key].loading = true; item.device.custom.btnState[key].color = item.device.custom.btnColorType.success; } else { item.device.custom.btnState[key].loading = false; item.device.custom.btnState[key].color = item.device.custom.btnColorType.info; } for (let key in item.device.custom.btnState) { if (key == btnKey) { item.device.custom.btnState[key].loading = true; item.device.custom.btnState[key].color = item.device.custom.btnColorType .success; } else { item.device.custom.btnState[key].loading = false; item.device.custom.btnState[key].color = item.device.custom.btnColorType.info; } } } } else if (item.device.deviceTypeId == 13) { if (value == 0 || value == 100) { angleObj = deviceValveAngles[config[value == 100 ? 1 : 0]]; angleADC = angleObj.angleAdc; operationType = (value == 100 ? 51 : 55) + Number(index) - 1; } else { angleObj = deviceValveAngles[config[2]]; let angleInterval = angleObj.angleAdcUpperLimit - angleObj.angleAdcLowerLimit; angleADC = Math.ceil(angleInterval * value / 100) + angleObj.angleAdcLowerLimit; operationType = 59 + Number(index) - 1; } // 控制参数 params = { deviceTypeId: item.device.deviceTypeId, privateSeal: item.device.privateSeal, deviceVersionId: item.device.deviceVerId, angleKey: angleObj.angleKey, // angleSize: angleSize, //大角度控制,正负数未调压的大小 valveNo: index, angleADC: angleADC, operationType: operationType, }; // 更新预加载状态 let key = "open" + index; item.device.custom.btnPreload = true; item.device.custom.btnState[key].loading = true; if (value == 0) { item.device.custom.btnState[key].color = item.device.custom.btnColorType.danger; } else { item.device.custom.btnState[key].color = item.device.custom.btnColorType.success; } } this.onEventHandler(this.eventType.send, params, item); } else { if ((item.device.deviceTypeId == 2 || item.device.deviceTypeId == 3) && (index == 0 || index == 3)) { const config = getValveAngleConfig(item.device.deviceTypeId); let params = { deviceTypeId: item.device.deviceTypeId, privateSeal: item.device.privateSeal, deviceVersionId: item.device.deviceVerId, angleKey: index == 0 ? config[0] : config[1], angleSize: 0, //大角度控制,正负数未调压的大小 // valveNo: swiperData.value.index, // angleADC: angleADC, operationType: index == 0 ? 6 : 5, }; // 更新预加载状态 let btnKey = index == 0 ? "closeAll" : "openAll"; item.device.custom.ct.time = 0; item.device.custom.ct.value = 0; item.device.custom.ct.endS = -1; for (let key in item.device.custom.btnState) { if (key == btnKey) { item.device.custom.btnState[key].loading = true; item.device.custom.btnState[key].color = item.device.custom.btnColorType.success; } else { item.device.custom.btnState[key].loading = false; item.device.custom.btnState[key].color = item.device.custom.btnColorType.info; } } this.onEventHandler(this.eventType.send, params, item); } else { if (item.device.deviceTypeId == 2 || item.device.deviceTypeId == 3) { this.onEventHandler(this.eventType.interface, { title: item.rowKey, extra: `${index}`, value: item.device.custom.aRate, other: { item: item, index: index, } }, item); } else if (item.device.deviceTypeId == 13) { this.onEventHandler(this.eventType.interface, { title: item.rowKey, extra: index, value: item.device.custom.angle["open" + index].aRate, other: { item: item, index: index, } }, item); } else { return null; } } } } } /****************************** 数据更新 ******************************/ /** * 解析数据 * @param {Object} message */ handleMessage(message) { const that = this; let obj = message; let { iotId, items, status } = obj let deviceIndex = null; deviceIndex = that.stations.findIndex(x => x.iotId == iotId); if (deviceIndex != -1) { let newData = that.stations[deviceIndex]; if (items && obj.iotId) { obj.privateSeal = newData.privateSeal; obj.deviceTypeId = newData.deviceTypeId; obj.rolaFrequency = newData.rolaFrequency; newData = alone(obj, newData.online) newData.loading = null; } else { newData.online = status === "online"; newData.loading = null; } that.stations[deviceIndex] = newData; } else { let indexArr = commonUtil.findIndexByIotId(that.valves, iotId); if (indexArr.length) { let device = null; if (indexArr.length == 1) { device = that.valves[indexArr[0]].device; } else if (indexArr.length == 2) { device = that.valves[indexArr[0]].children[indexArr[1]].device; } //更新状态 if (items) { device.data = obj } else { device.status = status; } //数据解析 if (device.deviceTypeId == 11 || device.deviceTypeId == 12) { device = flapValveControl(device); } else if (device.deviceTypeId == 2 || device.deviceTypeId == 3) { device = threeWayValveControl(device); } else if (device.deviceTypeId == 13) { device = fiveWayValveControl(device); } //刷新数据 if (indexArr.length == 1) { this.valves[indexArr[0]].device = device; } else if (indexArr.length == 2) { this.valves[indexArr[0]].children[indexArr[1]].device = device; } // 设置阀门倒计时 this.setLoadTimersData(device, indexArr); this.screenData(); // if (items && device) { // // 如果上报了数据,就更新一下阀门的一些时间 // this.matchTime("alone", device.id) // } } } } /****************************** 计时器 ******************************/ loadTimerObj = {}; loadTimerState = false; // 判断是否是控制中的设备 isLoading(device) { let isLoading = false; if ((device.deviceTypeId == 2 || device.deviceTypeId == 3) && device.custom.ws == 2) { isLoading = true; } if (device.deviceTypeId == 13 && (device.custom.control.open1 || device.custom.control.open2 || device .custom.control.open3 || device.custom.control.open4)) { isLoading = true; } return isLoading; } /** * 设置倒计时数据 * @param {Object} device * @param {Object} array */ setLoadTimersData(device, array) { if (!this.isLoading(device)) return; this.loadTimerObj[device.deviceCode] = { arr: array, deviceTypeId: device.deviceTypeId, ct: device.custom.ct, }; if (!this.loadTimerState) { this.loadTimer(); } } // 控制中倒计时 loadTimer() { const that = this; if (Object.keys(that.loadTimerObj).length) { this.loadTimerState = true; let loadTimerArr = Object.entries(that.loadTimerObj).map(([key, value]) => ({ key, value })); loadTimerArr.forEach(item => { let arr = item.value.arr; let deviceTypeId = item.value.deviceTypeId; let ct = item.value.ct; if (deviceTypeId == 2 || deviceTypeId == 3) { let endS = calcEndTime(ct.time, ct.value); ct.endS = endS; if (endS <= 0) { delete that.loadTimerObj[item.key]; } } if (deviceTypeId == 13) { let allEnd = true; for (var key in ct) { let endS = calcEndTime(ct[key].time, ct[key].value); ct[key].endS = endS; if (endS > 0) { allEnd = false; } } if (allEnd) { delete that.loadTimerObj[item.key]; } } // if (arr.length == 1) { // groupDeviceList.value[arr[0]].device.custom.ct.endS = endS; // } if (arr.length == 2) { that.valves[arr[0]].children[arr[1]].device.custom.ct = ct; } }) setTimeout(() => { that.loadTimer(); }, 1000); } else { that.clearTimer(); } } // 清理计时器 clearTimer() { this.loadTimerState = false; this.loadTimerObj = {}; } /****************************** 数据筛选 ******************************/ /** * @param {Object} type 筛选状态:1在线 2离线 3警告 0/其他 全部 */ showType(key = "") { if (this.screenKey == key) return; this.screenKey = key; this.screenData(); } /** * 工作阀门 */ workingForward(value) { if (this.forward == value) return; this.forward = value; this.screenData(true); } /** * 筛选设备数据 * isSort 是否需要排序,默认false */ screenData(isSort = false) { // 如果在线就显示 不在线就隐藏 const that = this; let all = 0, online = 0, offline = 0, watering = 0, unwater = 0, error = 0; let screenKey = that.screenKey; let forward = that.forward; that.valves.forEach(item => { item.isOpen = false; if (false) { console.log("蝶阀暂不统计"); // if (type == 1) { // 在线 // if (item.deviceId && item.device && item.device.custom && item.device.custom // .online) { // item.isHide = that.#workingScreen(item, work); // } else { // item.isHide = true; // } // } else if (type == 2) { // 离线 // if (item.deviceId && item.device && item.device.custom && !item.device.custom // .online) { // item.isHide = that.#workingScreen(item, work); // } else { // item.isHide = true; // } // } else if (type == 3) { // 警告 // if (item.deviceId && item.device && item.device.custom && (!item.device.custom // .online || // item.device.custom.earlyWarning.showTips)) { // item.isHide = that.#workingScreen(item, work); // } else { // item.isHide = true; // } // } else { // 全部 // if (work == 1 || work == 2) { // if (item.deviceId && item.device && item.device.custom) { // item.isHide = that.#workingScreen(item, work); // } else { // item.isHide = true; // } // } else { // item.isHide = false; // } // } } if (item.children && item.children.length) { item.children.forEach(item2 => { item2.isOpen = false; let keys = []; // 状态数据统计 if (item2.device) { all++; if (item2.device.custom) { if (item2.device.custom.online) { online++; keys.push('online'); } else { offline++; error++; keys.push('offline'); keys.push('error'); } if (item2.device.custom.isOpen) { item2.isOpen = true; item.isOpen = true; watering++; keys.push('watering'); } else { unwater++; keys.push('unwater'); } } else { offline++; unwater++; error++; keys = ['offline', 'unwater', 'error']; } } // 显示状态判断 if (screenKey == "") { item2.isHide = false; } else if (screenKey == "all") { if (item2.device) { item2.isHide = false; } else { item2.isHide = true; } } else if (keys.includes(screenKey)) { item2.isHide = false; } else { item2.isHide = true; } }); // 如果阀门全部都不在线 就隐藏阀门组的折叠 // if (item.children.every(j => j.isHide == true) && item.isHide) { if (item.children.every(j => j.isHide == true)) { item.isHide = true; } else { item.isHide = false; } if (isSort) { if (forward) { item.children.sort((a, b) => Number(!a.isOpen) - Number(!b.isOpen)); } else { item.children.sort(commonUtil.compareDataNew("outletCode", "multiPortCode")); } } } else { if (screenKey == "") { item.isHide = false; } else { item.isHide = true; } } }) if (isSort) { if (forward) { that.valves.sort((a, b) => Number(!a.isOpen) - Number(!b.isOpen)); } else { that.valves.sort(commonUtil.compareDataNew("landGroup", "branchCanalCode")); } } this.screen.all = all; this.screen.online = online; this.screen.offline = offline; this.screen.watering = watering; this.screen.unwater = unwater; this.screen.error = error; } /** * 筛选设备数据 * value 阀门/出水口编号 */ screenDataByName(value) { // 如果在线就显示 不在线就隐藏 const that = this; that.forward = false; that.screenKey = ""; if (value == "" || !value) { value = null; } that.valves.forEach(item => { item.isOpen = false; if (item.children && item.children.length) { item.children.forEach(item2 => { if (value) { if (item2.rowKey.includes(value)) { item2.isHide = false; } else { if (item2.device) { if (item2.device.deviceCode.includes(value)) { item2.isHide = false; } else { item2.isHide = true; } } else { item2.isHide = true; } } } else { item2.isHide = false; } }); // 如果阀门全部都不在线 就隐藏阀门组的折叠 // if (item.children.every(j => j.isHide == true) && item.isHide) { if (item.children.every(j => j.isHide == true)) { item.isHide = true; } else { item.isHide = false; } } else { item.isHide = true; } }) } /****************************** 自动刷新 ******************************/ runningTimer = false; /** * 自动刷新页面数据 */ startRunningTimer() { if (this.runningTimer) return; const that = this; that.runningTimer = true; setTimeout(() => { that.onEventHandler(that.eventType.refreshApi, that.land.item.id) }, 20 * 1000); } // 数据解析 valveRefresh(e) { const arr = commonUtil.findIndexByIotId(this.valves, e.data.data.iotId); if (arr.length) { if (arr.length == 1) {} if (arr.length == 2) { let row = this.valves[arr[0]].children[arr[1]]; row.device.status = e.data.status; row.device.data = e.data.data; //数据解析 if (row.device.deviceTypeId == 11 || row.device.deviceTypeId == 12) { row.device = flapValveControl(row.device); } else if (row.device.deviceTypeId == 2 || row.device.deviceTypeId == 3) { row.device = threeWayValveControl(row.device); } else if (row.device.deviceTypeId == 13) { row.device = fiveWayValveControl(row.device); } this.valves[arr[0]].children[arr[1]].device = row.device; this.setLoadTimersData(row.device, arr) } } } /****************************** 消息回调 ******************************/ eventType = { interface: 0, send: 1, refresh: 2, refreshApi: 3, } //应用类型 type:0打开操作界面,1控制命令发送,2刷新命令发送 ,3接口刷新阀门数据 onEventHandler(type, params, item = null) { if (typeof this.eventHandler === 'function') { this.eventHandler(type, params, item); } } onMsgHandler(msg) { if (typeof this.msgHandler === 'function') { this.msgHandler(msg); } } } export default deviceControl /***************************** 公共方法 *****************************/ var commonUtil = { // 数据排序 compareDataNew(file1, file2) { return function(b, a) { if (b[file1] < a[file1]) { return -1; } else if (b[file1] > a[file1]) { return 1; } else { if (b[file2] < a[file2]) { return -1; } else if (b[file2] > a[file2]) { return 1; } else { return 0; } } } }, // 服务商 facilitator(type) { if (type) { if (type == "1") { return "移动" } else if (type == "2") { return "电信" } else if (type == "3") { return "联通" } } else { return "未知" } }, //格式化时间戳 getYMDHMS(timestamp) { let date = new Date(parseInt(timestamp)) let Year = date.getFullYear() let Moth = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1 let Day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate() let Hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours() let Minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes() let Sechond = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds() let GMT = Year + "-" + Moth + "-" + Day + " " + Hour + ":" + Minute + ":" + Sechond return GMT }, findIndexByIotId(arr, iotid) { if (arr.length) { var index1 = arr.findIndex(x => x.device && x.device.iotId == iotid); if (index1 != -1) { return [index1]; } else { let arrIndex = []; for (var i = 0; i < arr.length; i++) { if (arr[i].children && arr[i].children.length) { var j = arr[i].children.findIndex(x => x.device && x.device.iotId == iotid); if (j != -1) { arrIndex = [i, j]; break; } } } return arrIndex; } } else { return []; } } }