local sys = require "sys" local cjson = require "cjson" local devices = require "main.devices" local mqtt_client = require "application.mqtt_client" local schedule = require "storage.schedule" require "main.global" local est = require "application.est" local configuration = require "storage.configuration" local fileOpt = require "storage.fileOpt" local event = require "main.event" local sensor_monitor = require "application.sensor_monitor" local sensors = require "application.sensors" local function load_ini(fileName) assert(type(fileName) == 'string', 'Parameter "fileName" must be a string.'); local file = assert(io.open(fileName, 'r'), 'Error loading file : ' .. fileName); local data = {}; local section; for line in file:lines() do local tempSection = line:match('^%[([^%[%]]+)%]$'); if (tempSection) then section = tonumber(tempSection) and tonumber(tempSection) or tempSection; data[section] = data[section] or {}; end local param, value = line:match('^([%w|_]+)%s-=%s-(.+)$'); if (param and value ~= nil) then if (tonumber(value)) then value = tonumber(value); elseif (value == 'true') then value = true; elseif (value == 'false') then value = false; end if (tonumber(param)) then param = tonumber(param); end data[section][param] = value; end end file:close(); return data; end local function read_ini(IniPath, Section, Key) local data = load_ini(IniPath) return data[Section][Key] end local function startTasks() sys.taskInit(function() ubuslog.log_reinit('smart_distribution_box') ubuslog.loglevel_set(ubuslog.LOG_DEBUG) log_print.info("==================== smart distribution box ====================") -- 智能配电箱 log_print.info("application started") log_print.info("version", VERSION) GW_SN = string.gsub(read_ini("/app/config/fac.ini", "factory", "sn"), " ", "") -- 加载 模版 & 设备 信息 devices.load() MULTI_CONT = devices.computeNodes("MultiSwitch") SAFE_CONT = devices.computeNodes("SafeElectricity") log_print.info("MultiSwitch cont", MULTI_CONT, "SafeElectricity cont", SAFE_CONT) SDB_SN = GW_SN .. "_" .. devices.get_sdb_sn() log_print.info("virtual smart_distribution_box SN: ", SDB_SN) -- 策略 & 配置参数 初始化 fileOpt.init() configuration.init() schedule.init() mqtt_client.start() -- 下挂设备告警检测,数据变化上报 sensor_monitor.init() sensors.set_lock_delay(SdbConfig.LockDelay) sensors.get_asw_exsist() -- 1. 上电自检并执行照明类型 0-接触器;1-多路开关 if SdbConfig.LightType == 1 then LIGHT_TYPE = 1 log_print.info("Multiplex switch controller Control type") else LIGHT_TYPE = 0 log_print.info("Contactor control type") end if LIGHT_TYPE == 0 then TOTAL_CH = 8 -- 接触器控制--8通道 est.contactor_init() else TOTAL_CH = MULTI_CONT * 6 -- 多路开关控制器控制--最多支持60通道(下挂10个多路开关控制器) est.multi_init() end sys.wait(10 * 1000) -- 2. 上电自检并执行相应的模式 本地旋钮开关 固定DI15 手动;自动 if sensor_monitor.isDiMannual() then RUN_MODE = 2 log_print.info("Enter Local manual mode...") else if SdbConfig.Auto == 1 then RUN_MODE = 1 log_print.info("Enter Remote automatic mode...") else RUN_MODE = 0 log_print.info("Enter Remote manual mode...") end end while true do sys.wait(10 * 1000) end end) -- 摄像头联动任务 sys.taskInit(function() sys.wait(10 * 1000) while true do sensor_monitor.camera_linkage() sys.wait(1 * 1000) end end) -- 接触器 & 多路开关策略任务 sys.taskInit(function() sys.waitUntil("", 10 * 1000) while true do if LIGHT_TYPE == 0 then est.contactor_poll() est.contactor_doPoll() else est.multi_poll() est.multi_doPoll() end est.safe_electricity() -- 设置安全用电模块阈值 sys.wait(3 * 1000) end end) -- 定时上报 & 同步时间任务 sys.taskInit(function() sys.wait(60 * 1000) -- event.wait("", 60000) while true do protocol.report() est.sync_rtc() sys.wait(SdbConfig.ReportIntv * 1000) -- 定时上报周期 end end) --sys.timerLoopStart(function() -- --log_print.info("ram usage", collectgarbage("count")) -- collectgarbage("collect") --end, 10 * 1000) -- 启动一个定时器 --sys.timerLoopStart(function() -- log_print.info(" Start sync multi rtc ") -- est.multi_sync_rtc() --end, 60 * 1000) -- sys.timerLoopStart(function() -- os.execute("python3 cameraconfig/arpspoof.py -i br-lan --attackermac AC:10:5F:00:00:39 --targetmac 4C:F5:DC:4E:E9:2C --gateip 192.168.1.1 192.168.1.64 &") -- end, 1000) end startTasks()