require "main.global" local tools = require "main.tools" local schedule = require "storage.schedule" local output = require "application.output" local sensors = require "application.sensors" local sensor_monitor = require "application.sensor_monitor" local bit = require "bit" local sys = require "sys" local est = {} local LEN_PER_SEG = 6 local lastSche = {} local lastDatetime local nowSchedule local prevSchedule local ocTimer = nil local STRATEGY_CONT = 7 -- 单次更新策略天数 local conta_sche_flag = 0 local conta_strategy_flag = false local conta_EstConfig local conta_tempManual = {} local strategy_buff = {} -- 序号1~7,7天策略缓存buff local multi_sche_flag = 0 local multi_strategy_flag = {} local multi_EstConfig local multi_tempManual = {} local multi_node_sn = {"MultiSwitch1", "MultiSwitch2", "MultiSwitch3", "MultiSwitch4", "MultiSwitch5", "MultiSwitch6", "MultiSwitch7", "MultiSwitch8", "MultiSwitch9", "MultiSwitch10"} -- 多路开关控制器sn local safe_node_sn = {"SafeElectricity1", "SafeElectricity2", "SafeElectricity3", "SafeElectricity4", "SafeElectricity5", "SafeElectricity6", "SafeElectricity7", "SafeElectricity8", "SafeElectricity9", "SafeElectricity10", "SafeElectricity11", "SafeElectricity12", "SafeElectricity13", "SafeElectricity14", "SafeElectricity15", "SafeElectricity16", "SafeElectricity17", "SafeElectricity18", "SafeElectricity19", "SafeElectricity20", "SafeElectricity21", "SafeElectricity22", "SafeElectricity23", "SafeElectricity24", "SafeElectricity25", "SafeElectricity26", "SafeElectricity27", "SafeElectricity28", "SafeElectricity29", "SafeElectricity30", "SafeElectricity31", "SafeElectricity32"} -- 安全用电模块sn local function getTime(offset) return os.date("*t", os.time() + (offset or 0)) end local function m_to_hm(min) local h, s = math.modf(min / 60) local m = min % 60 return h, m end -- 解析策略 0630001800AA -->每个状态长度6 local function decodeSchedule(data) if data == nil then return -1 end local segCnt = string.len(data) / 6 local segs = {} for i = 0, segCnt - 1, 1 do segs[i + 1] = {} segs[i + 1].offset = tonumber(string.sub(data, i * LEN_PER_SEG + 1, i * LEN_PER_SEG + 2)) * 60 segs[i + 1].offset = segs[i + 1].offset + tonumber(string.sub(data, i * LEN_PER_SEG + 3, i * LEN_PER_SEG + 4)) -- hh:mm->分钟 segs[i + 1].value = tonumber(string.sub(data, i * LEN_PER_SEG + 5, i * LEN_PER_SEG + 6), 16) -- value 00 表示关(断路) , 非00 表示开(闭合) end return segs end function est.updateSchedule() lastDatetime = getTime() local time = lastDatetime local dayIndex = schedule.toDayIndex(time.month, time.day) --local prevIndex = dayIndex > 2 and dayIndex - 1 or 366 -- lua三目运算符 --local nextIndex = dayIndex < 366 and dayIndex + 1 or 1 for i = 0, STRATEGY_CONT-1, 1 do -- 每次读取策略,连续读出7天 local month, date = schedule.fromDayIndex(dayIndex + i) strategy_buff[i+1] = schedule.readDay(month, date) end --month, date = schedule.fromDayIndex(prevIndex) --prevSchedule = schedule.readDay(month, date) -- 当天没有策略或者策略时间没到就执行前一天的最后一条策略 end function est.contactor_poll() local time = getTime() local Strategy = protocol.isStrategy() if time.day ~= lastDatetime.day or Strategy[0] == true then est.updateSchedule() conta_sche_flag = 1 -- 置位下发当天策略标志(所有回路) protocol.resetStrategy(0) end for n = 1, TOTAL_CH, 1 do if Strategy[n] == true then if n >= 1 and n <= 8 then conta_strategy_flag = true --log_print.info(" conta 1 ::::::::::::::::", n) end est.updateSchedule() protocol.resetStrategy(n) conta_sche_flag = 2 -- 置位下发通道对应控制器策略标志(单回路) end end Strategy = {} for m = 1, STRATEGY_CONT, 1 do -- 接触器连续解析7天策略 for i = 1, TOTAL_CH, 1 do local segs = decodeSchedule(strategy_buff[m][tostring(i)]) if segs == -1 then break end local ka = "KA" .. string.format("%d", i) local cont = #segs > 2 and 2 or #segs for j = 1, cont, 1 do -- 每天最多设置 4 个开关灯时间,对于plc而言协议只支持2个开关灯时间,因此不管几个时间段只取前2个 local hh, mm = m_to_hm(segs[j].offset) -- 时,分 if segs[j].value == 0 then Conta_Strategy[m][ka].g.hh = hh Conta_Strategy[m][ka].g.mm = mm Conta_Strategy[m][ka].g.time = segs[j].offset -- else Conta_Strategy[m][ka].k.hh = hh Conta_Strategy[m][ka].k.mm = mm Conta_Strategy[m][ka].k.time = segs[j].offset -- end end end end end -- 接触器远程手动模式下,控制任意回路的断开和闭合 function est.contactor_control(chn, value) if chn == 0 then for i = 1, TOTAL_CH, 1 do conta_EstConfig[i].manualVal = value conta_tempManual[i] = true end else conta_EstConfig[chn].manualVal = value conta_tempManual[chn] = true end end function est.contactor_doPoll() local time = getTime() -- "独立"更新接触器本地策略 下发策略,设备运行模式不改变,接触器不区分下发的是全部通道策略和单独通道策略,因为只有一个PLC if conta_sche_flag == 1 then log_print.info(" down strategy continuous 7 days ... ^_^ ", "PLC") for i = 1, STRATEGY_CONT, 1 do output.contactor_set_strategy(Conta_Strategy[i]) end conta_sche_flag = 0 elseif conta_sche_flag == 2 then if conta_strategy_flag == true then log_print.info(" down strategy continuous 7 days ... ^_^ ", "PLC") for i = 1, STRATEGY_CONT, 1 do -- 接触器连续下发7天策略 output.contactor_set_strategy(Conta_Strategy[i]) end conta_sche_flag = 0 -- 清零下发当天策略标志 conta_strategy_flag = false end end -- 切换运行模式 if protocol.isAuto() then if RUN_MODE == 0 then -- 1.远程手动模式,1).切换至远程手动状态,不执行策略,下发策略,设备运行模式不改变;2).主要用于业务联动保护时,切断回路后不会自动恢复 -- 执行一次远程控制,控制设备全关,设备模式切换至手动,模式0 sensors.set_contactor_mode(0) sensors.contactor_set_do(0x00) elseif RUN_MODE == 1 then -- 2.多路开关控制器运行策略 for i = 1, TOTAL_CH, 1 do conta_tempManual[i] = false end -- 执行运行模式切换指令,模式1 sensors.set_contactor_mode(1) end protocol.resetAuto(false) end -- 远程手动控制 local conta_sta = false local conta_val = 0 local sensor_status = sensor_monitor.get_status() for chn = 1, TOTAL_CH, 1 do if sensor_status.ADIO.tags.DO then if conta_tempManual[chn] == true then if conta_sta == false then -- 确保只获取一次DO sensors.get_do_status() conta_val = sensor_status.ADIO.tags.DO conta_sta = true -- end log_print.info(" chn :", chn, "value :", conta_EstConfig[chn].manualVal == 0 and 0 or 1) local channel = chn % 8 if channel == 0 then conta_val = bit.bor(bit.band(conta_val, bit.bnot(bit.lshift(1, 7))),bit.lshift(conta_EstConfig[chn].manualVal == 0 and 0 or 1, 7)) else conta_val = bit.bor(bit.band(conta_val, bit.bnot(bit.lshift(1, channel - 1))),bit.lshift(conta_EstConfig[chn].manualVal == 0 and 0 or 1, channel - 1)) end conta_tempManual[chn] = false end end end -- 发送接触器控制 if conta_sta == true then log_print.info(" Control conta ") output.contactor_set_do(conta_val) end -- ~~清~~ if true then conta_sta = false conta_val = 0 end end function est.contactor_init() conta_EstConfig = {} for i = 0, TOTAL_CH, 1 do conta_EstConfig[i] = {} end for i = 0, TOTAL_CH, 1 do Strategy_chn[i] = false end est.updateSchedule() end ---------------------------------------------------------------------------------------------------------------- function est.multi_poll() local time = getTime() local Strategy = protocol.isStrategy() if time.day ~= lastDatetime.day or Strategy[0] == true then est.updateSchedule() multi_sche_flag = 1 -- 置位下发当天策略标志(所有回路) protocol.resetStrategy(0) end for n = 1, TOTAL_CH, 1 do if Strategy[n] == true then if n >= 1 and n <= 6 then multi_strategy_flag[1] = true --log_print.info(" multi 1 ::::::::::::::::", n) elseif n >= 7 and n <= 12 then multi_strategy_flag[2] = true --log_print.info(" multi 2 ::::::::::::::::", n) elseif n >= 13 and n <= 18 then multi_strategy_flag[3] = true --log_print.info(" multi 3 ::::::::::::::::", n) elseif n >= 19 and n <= 24 then multi_strategy_flag[4] = true --log_print.info(" multi 4 ::::::::::::::::", n) elseif n >= 25 and n <= 30 then multi_strategy_flag[5] = true --log_print.info(" multi 5 ::::::::::::::::", n) elseif n >= 31 and n <= 36 then multi_strategy_flag[6] = true --log_print.info(" multi 6 ::::::::::::::::", n) elseif n >= 37 and n <= 42 then multi_strategy_flag[7] = true --log_print.info(" multi 7 ::::::::::::::::", n) elseif n >= 43 and n <= 48 then multi_strategy_flag[8] = true --log_print.info(" multi 8 ::::::::::::::::", n) elseif n >= 49 and n <= 54 then multi_strategy_flag[9] = true --log_print.info(" multi 9 ::::::::::::::::", n) elseif n >= 55 and n <= 60 then multi_strategy_flag[10] = true --log_print.info(" multi 10 ::::::::::::::::", n) end est.updateSchedule() protocol.resetStrategy(n) multi_sche_flag = 2 -- 置位下发通道对应控制器策略标志(单回路) end end Strategy = {} for m = 1, STRATEGY_CONT, 1 do -- 多路开关控制器连续解析7天策略 local v = 0 for i = 1, TOTAL_CH, 1 do v = v + 1 local segs = decodeSchedule(strategy_buff[m][tostring(i)]) if segs == -1 then break end --print(cjson.encode(segs), offsetNow) if i <= 6 then for j = 1, #segs, 1 do -- 每天最多设置 4 个开关灯时间 if j == 1 then Multi_Strategy[m].CTRL1.K1.time = segs[j].offset Multi_Strategy[m].CTRL1.K1.value[v] = segs[j].value elseif j == 2 then Multi_Strategy[m].CTRL1.K2.time = segs[j].offset Multi_Strategy[m].CTRL1.K2.value[v] = segs[j].value elseif j == 3 then Multi_Strategy[m].CTRL1.K3.time = segs[j].offset Multi_Strategy[m].CTRL1.K3.value[v] = segs[j].value elseif j == 4 then Multi_Strategy[m].CTRL1.K4.time = segs[j].offset Multi_Strategy[m].CTRL1.K4.value[v] = segs[j].value end end Multi_Strategy[m].CTRL1.k_cont = #segs elseif i > 6 and i <= 12 then for j = 1, #segs, 1 do -- 每天最多设置 4 个开关灯时间 if j == 1 then Multi_Strategy[m].CTRL2.K1.time = segs[j].offset Multi_Strategy[m].CTRL2.K1.value[v] = segs[j].value elseif j == 2 then Multi_Strategy[m].CTRL2.K2.time = segs[j].offset Multi_Strategy[m].CTRL2.K2.value[v] = segs[j].value elseif j == 3 then Multi_Strategy[m].CTRL2.K3.time = segs[j].offset Multi_Strategy[m].CTRL2.K3.value[v] = segs[j].value elseif j == 4 then Multi_Strategy[m].CTRL2.K4.time = segs[j].offset Multi_Strategy[m].CTRL2.K4.value[v] = segs[j].value end end Multi_Strategy[m].CTRL2.k_cont = #segs elseif i > 12 and i <= 18 then for j = 1, #segs, 1 do -- 每天最多设置 4 个开关灯时间 if j == 1 then Multi_Strategy[m].CTRL3.K1.time = segs[j].offset Multi_Strategy[m].CTRL3.K1.value[v] = segs[j].value elseif j == 2 then Multi_Strategy[m].CTRL3.K2.time = segs[j].offset Multi_Strategy[m].CTRL3.K2.value[v] = segs[j].value elseif j == 3 then Multi_Strategy[m].CTRL3.K3.time = segs[j].offset Multi_Strategy[m].CTRL3.K3.value[v] = segs[j].value elseif j == 4 then Multi_Strategy[m].CTRL3.K4.time = segs[j].offset Multi_Strategy[m].CTRL3.K4.value[v] = segs[j].value end end Multi_Strategy[m].CTRL3.k_cont = #segs elseif i > 18 and i <= 24 then for j = 1, #segs, 1 do -- 每天最多设置 4 个开关灯时间 if j == 1 then Multi_Strategy[m].CTRL4.K1.time = segs[j].offset Multi_Strategy[m].CTRL4.K1.value[v] = segs[j].value elseif j == 2 then Multi_Strategy[m].CTRL4.K2.time = segs[j].offset Multi_Strategy[m].CTRL4.K2.value[v] = segs[j].value elseif j == 3 then Multi_Strategy[m].CTRL4.K3.time = segs[j].offset Multi_Strategy[m].CTRL4.K3.value[v] = segs[j].value elseif j == 4 then Multi_Strategy[m].CTRL4.K4.time = segs[j].offset Multi_Strategy[m].CTRL4.K4.value[v] = segs[j].value end end Multi_Strategy[m].CTRL4.k_cont = #segs elseif i > 24 and i <= 30 then for j = 1, #segs, 1 do -- 每天最多设置 4 个开关灯时间 if j == 1 then Multi_Strategy[m].CTRL5.K1.time = segs[j].offset Multi_Strategy[m].CTRL5.K1.value[v] = segs[j].value elseif j == 2 then Multi_Strategy[m].CTRL5.K2.time = segs[j].offset Multi_Strategy[m].CTRL5.K2.value[v] = segs[j].value elseif j == 3 then Multi_Strategy[m].CTRL5.K3.time = segs[j].offset Multi_Strategy[m].CTRL5.K3.value[v] = segs[j].value elseif j == 4 then Multi_Strategy[m].CTRL5.K4.time = segs[j].offset Multi_Strategy[m].CTRL5.K4.value[v] = segs[j].value end end Multi_Strategy[m].CTRL5.k_cont = #segs elseif i > 30 and i <= 36 then for j = 1, #segs, 1 do -- 每天最多设置 4 个开关灯时间 if j == 1 then Multi_Strategy[m].CTRL6.K1.time = segs[j].offset Multi_Strategy[m].CTRL6.K1.value[v] = segs[j].value elseif j == 2 then Multi_Strategy[m].CTRL6.K2.time = segs[j].offset Multi_Strategy[m].CTRL6.K2.value[v] = segs[j].value elseif j == 3 then Multi_Strategy[m].CTRL6.K3.time = segs[j].offset Multi_Strategy[m].CTRL6.K3.value[v] = segs[j].value elseif j == 4 then Multi_Strategy[m].CTRL6.K4.time = segs[j].offset Multi_Strategy[m].CTRL6.K4.value[v] = segs[j].value end end Multi_Strategy[m].CTRL6.k_cont = #segs elseif i > 36 and i <= 42 then for j = 1, #segs, 1 do -- 每天最多设置 4 个开关灯时间 if j == 1 then Multi_Strategy[m].CTRL7.K1.time = segs[j].offset Multi_Strategy[m].CTRL7.K1.value[v] = segs[j].value elseif j == 2 then Multi_Strategy[m].CTRL7.K2.time = segs[j].offset Multi_Strategy[m].CTRL7.K2.value[v] = segs[j].value elseif j == 3 then Multi_Strategy[m].CTRL7.K3.time = segs[j].offset Multi_Strategy[m].CTRL7.K3.value[v] = segs[j].value elseif j == 4 then Multi_Strategy[m].CTRL7.K4.time = segs[j].offset Multi_Strategy[m].CTRL7.K4.value[v] = segs[j].value end end Multi_Strategy[m].CTRL7.k_cont = #segs elseif i > 42 and i <= 48 then for j = 1, #segs, 1 do -- 每天最多设置 4 个开关灯时间 if j == 1 then Multi_Strategy[m].CTRL8.K1.time = segs[j].offset Multi_Strategy[m].CTRL8.K1.value[v] = segs[j].value elseif j == 2 then Multi_Strategy[m].CTRL8.K2.time = segs[j].offset Multi_Strategy[m].CTRL8.K2.value[v] = segs[j].value elseif j == 3 then Multi_Strategy[m].CTRL8.K3.time = segs[j].offset Multi_Strategy[m].CTRL8.K3.value[v] = segs[j].value elseif j == 4 then Multi_Strategy[m].CTRL8.K4.time = segs[j].offset Multi_Strategy[m].CTRL8.K4.value[v] = segs[j].value end end Multi_Strategy[m].CTRL8.k_cont = #segs elseif i > 48 and i <= 54 then for j = 1, #segs, 1 do -- 每天最多设置 4 个开关灯时间 if j == 1 then Multi_Strategy[m].CTRL9.K1.time = segs[j].offset Multi_Strategy[m].CTRL9.K1.value[v] = segs[j].value elseif j == 2 then Multi_Strategy[m].CTRL9.K2.time = segs[j].offset Multi_Strategy[m].CTRL9.K2.value[v] = segs[j].value elseif j == 3 then Multi_Strategy[m].CTRL9.K3.time = segs[j].offset Multi_Strategy[m].CTRL9.K3.value[v] = segs[j].value elseif j == 4 then Multi_Strategy[m].CTRL9.K4.time = segs[j].offset Multi_Strategy[m].CTRL9.K4.value[v] = segs[j].value end end Multi_Strategy[m].CTRL9.k_cont = #segs elseif i > 54 and i <= 60 then for j = 1, #segs, 1 do -- 每天最多设置 4 个开关灯时间 if j == 1 then Multi_Strategy[m].CTRL10.K1.time = segs[j].offset Multi_Strategy[m].CTRL10.K1.value[v] = segs[j].value elseif j == 2 then Multi_Strategy[m].CTRL10.K2.time = segs[j].offset Multi_Strategy[m].CTRL10.K2.value[v] = segs[j].value elseif j == 3 then Multi_Strategy[m].CTRL10.K3.time = segs[j].offset Multi_Strategy[m].CTRL10.K3.value[v] = segs[j].value elseif j == 4 then Multi_Strategy[m].CTRL10.K4.time = segs[j].offset Multi_Strategy[m].CTRL10.K4.value[v] = segs[j].value end end Multi_Strategy[m].CTRL10.k_cont = #segs end if v % 6 == 0 then v = 0 end end end end -- 多路开关远程手动模式下,控制任意回路的断开和闭合 function est.multi_control(chn, value) if chn == 0 then for i = 1, TOTAL_CH, 1 do multi_EstConfig[i].manualVal = value multi_tempManual[i] = true end else multi_EstConfig[chn].manualVal = value multi_tempManual[chn] = true end end function est.multi_doPoll() -- "独立"更新多路开关控制器本地策略 下发策略,设备运行模式不改变 if multi_sche_flag == 1 then for i = 1, MULTI_CONT, 1 do local ctrl = "CTRL" .. string.format("%d", i) log_print.info(" down strategy continuous 7 days ... ^_^ ", ctrl) for m = 1, STRATEGY_CONT, 1 do -- 多路开关控制器连续下发7天策略 local offset = (m - 1) * 3600 * 24 local time = getTime(offset) output.multi_set_strategy(multi_node_sn[i], time.month, time.day, Multi_Strategy[m][ctrl], Multi_Strategy[m][ctrl].k_cont) end end multi_sche_flag = 0 -- 清零下发当天策略标志(所有控制器) elseif multi_sche_flag == 2 then for i = 1, MULTI_CONT, 1 do if multi_strategy_flag[i] then local ctrl = "CTRL" .. string.format("%d", i) log_print.info(" down strategy continuous 7 days ... ^_^ ", ctrl) for m = 1, STRATEGY_CONT, 1 do local offset = (m - 1) * 3600 * 24 local time = getTime(offset) output.multi_set_strategy(multi_node_sn[i], time.month, time.day, Multi_Strategy[m][ctrl], Multi_Strategy[m][ctrl].k_cont) end multi_strategy_flag[i] = false end end multi_sche_flag = 0 -- 清零下发当天策略标志 (某个/多个控制器) end -- 切换运行模式 if protocol.isAuto() then if RUN_MODE == 0 then -- 1.远程手动模式,1).切换至远程手动状态,不执行策略,下发策略,设备运行模式不改变;2).主要用于业务联动保护时,切断回路后不会自动恢复 -- 执行一次远程控制,控制设备全关,设备模式切换至远程手动,模式00A5 for i = 1, MULTI_CONT, 1 do output.multi_set_do(multi_node_sn[i], 0x00) end elseif RUN_MODE == 1 then -- 2.多路开关控制器运行策略 for i = 1, TOTAL_CH, 1 do multi_tempManual[i] = false end -- 执行运行模式切换指令,模式0000 for i = 1, MULTI_CONT, 1 do sensors.set_multi_order(multi_node_sn[i], 0x02) end end protocol.resetAuto(false) end -- 远程手动控制 local muti_sta = {} local muti_val = {} for i = 1, MULTI_CONT, 1 do muti_sta[i] = false muti_val[i] = 0 end local sensor_status = sensor_monitor.get_status() for chn = 1, TOTAL_CH, 1 do if sensor_status.MultiSwitch.tags.MSDO[1] then if multi_tempManual[chn] == true and (chn >= 1 and chn <= 6) then if muti_sta[1] == false then -- 确保只获取一次MSDO sensors.get_multi_do_current(multi_node_sn[1]) muti_val[1] = sensor_status.MultiSwitch.tags.MSDO[1] muti_sta[1] = true -- end log_print.info(" chn :", chn, "value :", multi_EstConfig[chn].manualVal == 0 and 0 or 1) local channel = chn % 6 if channel == 0 then muti_val[1] = bit.bor(bit.band(muti_val[1], bit.bnot(bit.lshift(1, 5))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, 5)) else muti_val[1] = bit.bor(bit.band(muti_val[1], bit.bnot(bit.lshift(1, channel - 1))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, channel - 1)) end multi_tempManual[chn] = false end end if sensor_status.MultiSwitch.tags.MSDO[2] then if multi_tempManual[chn] == true and (chn >= 7 and chn <= 12) then if muti_sta[2] == false then -- 确保只获取一次MSDO sensors.get_multi_do_current(multi_node_sn[2]) muti_val[2] = sensor_status.MultiSwitch.tags.MSDO[2] muti_sta[2] = true -- end log_print.info(" chn :", chn, "value :", multi_EstConfig[chn].manualVal == 0 and 0 or 1) local channel = chn % 6 if channel == 0 then muti_val[2] = bit.bor(bit.band(muti_val[2], bit.bnot(bit.lshift(1, 5))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, 5)) else muti_val[2] = bit.bor(bit.band(muti_val[2], bit.bnot(bit.lshift(1, channel - 1))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, channel - 1)) end multi_tempManual[chn] = false end end if sensor_status.MultiSwitch.tags.MSDO[3] then if multi_tempManual[chn] == true and (chn >= 13 and chn <= 18) then if muti_sta[3] == false then -- 确保只获取一次MSDO sensors.get_multi_do_current(multi_node_sn[3]) muti_val[3] = sensor_status.MultiSwitch.tags.MSDO[3] muti_sta[3] = true -- end log_print.info(" chn :", chn, "value :", multi_EstConfig[chn].manualVal == 0 and 0 or 1) local channel = chn % 6 if channel == 0 then muti_val[3] = bit.bor(bit.band(muti_val[3], bit.bnot(bit.lshift(1, 5))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, 5)) else muti_val[3] = bit.bor(bit.band(muti_val[3], bit.bnot(bit.lshift(1, channel - 1))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, channel - 1)) end multi_tempManual[chn] = false end end if sensor_status.MultiSwitch.tags.MSDO[4] then if multi_tempManual[chn] == true and (chn >= 19 and chn <= 24) then if muti_sta[4] == false then -- 确保只获取一次MSDO sensors.get_multi_do_current(multi_node_sn[4]) muti_val[4] = sensor_status.MultiSwitch.tags.MSDO[4] muti_sta[4] = true -- end log_print.info(" chn :", chn, "value :", multi_EstConfig[chn].manualVal == 0 and 0 or 1) local channel = chn % 6 if channel == 0 then muti_val[4] = bit.bor(bit.band(muti_val[4], bit.bnot(bit.lshift(1, 5))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, 5)) else muti_val[4] = bit.bor(bit.band(muti_val[4], bit.bnot(bit.lshift(1, channel - 1))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, channel - 1)) end multi_tempManual[chn] = false end end if sensor_status.MultiSwitch.tags.MSDO[5] then if multi_tempManual[chn] == true and (chn >= 25 and chn <= 30) then if muti_sta[5] == false then -- 确保只获取一次MSDO sensors.get_multi_do_current(multi_node_sn[5]) muti_val[5] = sensor_status.MultiSwitch.tags.MSDO[5] muti_sta[5] = true -- end log_print.info(" chn :", chn, "value :", multi_EstConfig[chn].manualVal == 0 and 0 or 1) local channel = chn % 6 if channel == 0 then muti_val[5] = bit.bor(bit.band(muti_val[5], bit.bnot(bit.lshift(1, 5))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, 5)) else muti_val[5] = bit.bor(bit.band(muti_val[5], bit.bnot(bit.lshift(1, channel - 1))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, channel - 1)) end multi_tempManual[chn] = false end end if sensor_status.MultiSwitch.tags.MSDO[6] then if multi_tempManual[chn] == true and (chn >= 31 and chn <= 36) then if muti_sta[6] == false then -- 确保只获取一次MSDO sensors.get_multi_do_current(multi_node_sn[6]) muti_val[6] = sensor_status.MultiSwitch.tags.MSDO[6] muti_sta[6] = true -- end log_print.info(" chn :", chn, "value :", multi_EstConfig[chn].manualVal == 0 and 0 or 1) local channel = chn % 6 if channel == 0 then muti_val[6] = bit.bor(bit.band(muti_val[6], bit.bnot(bit.lshift(1, 5))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, 5)) else muti_val[6] = bit.bor(bit.band(muti_val[6], bit.bnot(bit.lshift(1, channel - 1))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, channel - 1)) end multi_tempManual[chn] = false end end if sensor_status.MultiSwitch.tags.MSDO[7] then if multi_tempManual[chn] == true and (chn >= 37 and chn <= 42) then if muti_sta[7] == false then -- 确保只获取一次MSDO sensors.get_multi_do_current(multi_node_sn[7]) muti_val[7] = sensor_status.MultiSwitch.tags.MSDO[7] muti_sta[7] = true -- end log_print.info(" chn :", chn, "value :", multi_EstConfig[chn].manualVal == 0 and 0 or 1) local channel = chn % 6 if channel == 0 then muti_val[7] = bit.bor(bit.band(muti_val[7], bit.bnot(bit.lshift(1, 5))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, 5)) else muti_val[7] = bit.bor(bit.band(muti_val[7], bit.bnot(bit.lshift(1, channel - 1))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, channel - 1)) end multi_tempManual[chn] = false end end if sensor_status.MultiSwitch.tags.MSDO[8] then if multi_tempManual[chn] == true and (chn >= 43 and chn <= 48) then if muti_sta[8] == false then -- 确保只获取一次MSDO sensors.get_multi_do_current(multi_node_sn[8]) muti_val[8] = sensor_status.MultiSwitch.tags.MSDO[8] muti_sta[8] = true -- end log_print.info(" chn :", chn, "value :", multi_EstConfig[chn].manualVal == 0 and 0 or 1) local channel = chn % 6 if channel == 0 then muti_val[8] = bit.bor(bit.band(muti_val[8], bit.bnot(bit.lshift(1, 5))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, 5)) else muti_val[8] = bit.bor(bit.band(muti_val[8], bit.bnot(bit.lshift(1, channel - 1))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, channel - 1)) end multi_tempManual[chn] = false end end if sensor_status.MultiSwitch.tags.MSDO[9] then if multi_tempManual[chn] == true and (chn >= 49 and chn <= 54) then if muti_sta[9] == false then -- 确保只获取一次MSDO sensors.get_multi_do_current(multi_node_sn[9]) muti_val[9] = sensor_status.MultiSwitch.tags.MSDO[9] muti_sta[9] = true -- end log_print.info(" chn :", chn, "value :", multi_EstConfig[chn].manualVal == 0 and 0 or 1) local channel = chn % 6 if channel == 0 then muti_val[9] = bit.bor(bit.band(muti_val[9], bit.bnot(bit.lshift(1, 5))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, 5)) else muti_val[9] = bit.bor(bit.band(muti_val[9], bit.bnot(bit.lshift(1, channel - 1))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, channel - 1)) end multi_tempManual[chn] = false end end if sensor_status.MultiSwitch.tags.MSDO[10] then if multi_tempManual[chn] == true and (chn >= 55 and chn <= 60) then if muti_sta[10] == false then -- 确保只获取一次MSDO sensors.get_multi_do_current(multi_node_sn[10]) muti_val[10] = sensor_status.MultiSwitch.tags.MSDO[10] muti_sta[10] = true -- end log_print.info(" chn :", chn, "value :", multi_EstConfig[chn].manualVal == 0 and 0 or 1) local channel = chn % 6 if channel == 0 then muti_val[10] = bit.bor(bit.band(muti_val[10], bit.bnot(bit.lshift(1, 5))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, 5)) else muti_val[10] = bit.bor(bit.band(muti_val[10], bit.bnot(bit.lshift(1, channel - 1))),bit.lshift(multi_EstConfig[chn].manualVal == 0 and 0 or 1, channel - 1)) end multi_tempManual[chn] = false end end end -- 发送多路开关控制器控制 for i = 1, MULTI_CONT, 1 do if muti_sta[i] == true then log_print.info(" Control muti ", i) output.multi_set_do(multi_node_sn[i], muti_val[i]) end end -- ~~清~~ if true then for i = 1, MULTI_CONT, 1 do muti_sta[i] = false muti_val[i] = 0 end end end function est.multi_init() multi_EstConfig = {} for i = 0, TOTAL_CH, 1 do multi_EstConfig[i] = {} end for i = 1, MULTI_CONT, 1 do multi_strategy_flag[i] = false end for i = 0, TOTAL_CH, 1 do Strategy_chn[i] = false end est.updateSchedule() end --[[ -- 多路开关控制器远程手动模式下,控制任意回路的断开和闭合 function est.multi_control(chn, value) local time = getTime() --if RUN_MODE == 0 then if chn == 0 then for i = 1, MULTI_CONT, 1 do if value == 1 then sensors.set_multi_do_output(multi_node_sn[i], 0xFF) else sensors.set_multi_do_output(multi_node_sn[i], 0x00) end sys.wait(1) end else local index, s = math.modf(chn / 6) local channel = chn % 6 if channel == 0 then sensors.get_multi_do_current(multi_node_sn[index]) local sensor_status = sensor_monitor.get_status() local do_output = bit.bor(bit.band(sensor_status.MultiSwitch.tags.MSDO[index], bit.bnot(bit.lshift(1, 5))),bit.lshift(value == 0 and 0 or 1, 5)) sensors.set_multi_do_output(multi_node_sn[index], do_output) else sensors.get_multi_do_current(multi_node_sn[index + 1]) local sensor_status = sensor_monitor.get_status() local do_output = bit.bor(bit.band(sensor_status.MultiSwitch.tags.MSDO[index + 1], bit.bnot(bit.lshift(1, channel - 1))),bit.lshift(value == 0 and 0 or 1, channel - 1)) sensors.set_multi_do_output(multi_node_sn[index + 1], do_output) end end --end end ]] -- 同步 接触器 & 多路开关控制器 rtc function est.sync_rtc() local time = getTime() log_print.info("Now Time: ", string.format("%04d-%02d-%02d %02d:%02d:%02d wday: %02d", time.year, time.month, time.day, time.hour, time.min, time.sec, time.wday)) -- 接触器rtc -- wday`** (weekday, 1–7, Sunday is 1) local week = 0 if time.wday == 1 then week = 7 else week = time.wday - 1 end sensors.set_contactor_sync_rtc(time.year, time.month, time.day, time.hour, time.min, time.sec, week) -- 多路开关控制器rtc for i = 1, MULTI_CONT, 1 do sensors.set_multi_sync_rtc(multi_node_sn[i], time.year, time.month, time.day, time.hour, time.min, time.sec) end end function est.safe_electricity() local time = getTime() if protocol.isSetWarn() then log_print.info(" Start set safe threshold ") for i = 1, SAFE_CONT, 1 do sensors.safe_set_threshold(safe_node_sn[i], WarnConfig.ChOverVoltage[i], WarnConfig.ChOverCurrent[i], WarnConfig.ChTemperature[i], WarnConfig.ChResidualCurrent[i], WarnConfig.ChOverPower[i]) sys.wait(1) end protocol.resetWarn(false) end end return est