require "log" require "sys" require "utils" require "patch" require "pack" siganl = require 'posix.signal' syswait = require 'posix.sys.wait' unistd = require 'posix.unistd' mqtt=require "mosquitto" base64=require("base64") dyutils=require("dyutils") lpack=require("lua_pack") require "internal_api" LOG_LEVEL = log.LOGLEVEL_WARN -- LOG_LEVEL = log.LOGLEVEL_INFO do_status={ DO0=0, DO1=0, DO2=0, DO3=0, DO4=0, DO5=0, DO6=0, DO7=0, DO8=0, DO9=0, DO10=0, DO11=0, } dio_status={ DI0=0, DI1=0, DI2=0, DI3=0, DI4=0, DI5=0, DI6=0, DI7=1, DI8=0, DI9=0, DI10=0, DI11=0, DI12=0, DI13=0, DI14=0, DI15=0, } ai_status={ aag1=8400, aag2=8400, aag3=8400, aag4=8400, } function service_response(gw_sn,dev_sn,port,identifier,param,mi,time) local topic = string.format( "ipc/%s/%s/device/%s/data/service/%s",gw_sn,port,dev_sn,identifier) local obj_send={} local json_str obj_send['mi'] = mi or 0 -- obj_send['len'] = #data obj_send['identifier'] = identifier obj_send['sn'] = dev_sn obj_send['time'] = time or os.time() obj_send["data_type"] = 2 obj_send["requester"] = "local" obj_send["tag_node"] = cjson.encode(param) json_str = cjson.encode(obj_send) log.info("service_response",topic,json_str) client:publish(topic, json_str) end local function ai_report() while true do sys.wait(3000) service_response(gw_sn,string.format("%s_AI",gw_sn),"RS485_3","ReadData",ai_status,0) service_response(gw_sn,string.format("%s_AI",gw_sn),"RS485_3","Read_DCvoltage",{aag6 = 15000},0) end end local function dio_report() while true do sys.wait(3000) service_response(gw_sn,string.format("%s_DIO",gw_sn),"RS485_3","RData",dio_status,0) end end local function env_report() while true do sys.wait(3000) service_response(gw_sn,string.format("%s_ENV",gw_sn),"RS485_3","shujushangbao1",{temperature=28,humidity=88},0) end end local function meter_report() while true do sys.wait(3000) service_response(gw_sn,string.format("%s_METER",gw_sn),"RS485_3","post",{APhaseVoltage=220,BPhaseVoltage=224,CPhaseVoltage=217},0) end end local function iot2app(topic,payload) log.info("iot2app",topic,payload) local obj = cjson.decode(payload) if string.find(topic,"_DIO") then if string.find(obj.identifier,"DO") then if obj.identifier == "DO4" or obj.identifier == "DO5" or obj.identifier == "DO6" then return end if obj.identifier == "DO8" then dio_status["DI11"] = obj.open else local ch = string.gsub(obj.identifier,".*(%d+)","%1" ) dio_status[string.format("DI%d",tonumber(ch))] = obj.open end do_status[obj.identifier] = obj.open -- dio_status["DI0"] = 0 log.warn(os.date("%Y-%m-%d %H:%M:%S",os.time()),string.format("DI0~3 [%d,%d,%d,%d] DI11 [%d] DO0~3[%d,%d,%d,%d] DO8 [%d]", dio_status.DI0,dio_status.DI1,dio_status.DI2,dio_status.DI3,dio_status.DI11,do_status.DO0,do_status.DO1,do_status.DO2,do_status.DO3,do_status.DO8)) end elseif string.find(topic,"_AI") then if obj.identifier == "Read_DCleakagecurrent" then service_response(gw_sn,obj.sn,"RS485_3",obj.identifier,{aag5 = 15000},obj.mi) end end end local function pp2app(topic,payload) -- -- print("iot2app",topic,payload) end local function handler() local pid, status, code = syswait.wait(-1, syswait.WNOHANG) log.error('system exit', pid, status, code) --打印不出来 os.exit(0) end function main(nodes_cfg,sn) log.info("main_loop", "=============== Direct Current RUN 1.0 ============") gw_sn = sn if not gw_sn then gw_sn = io.getGatewayID() -- gw_sn = "218800000010" end local devs = { string.format("%s_DIO",gw_sn),string.format("%s_DIO",gw_sn),string.format("%s_AI",gw_sn) } log.info("device list:",cjson.encode(devs)) log.info("main_loop", string.format("Try Run At Shell\nsn='%s'\nnodes='%s'\nmain_loop(nodes,sn)",gw_sn or "NOT SN",nodes_cfg or "")) siganl.signal(siganl.SIGKILL,handler) siganl.signal(siganl.SIGHUP,handler) internal_api.local_mqtt_session(devs,"APP",iot2app,pp2app) sys.taskInit(ai_report) sys.taskInit(dio_report) sys.taskInit(env_report) sys.taskInit(meter_report) --启动系统框架 sys.init(0, 0) sys.run() end main(arg[1],arg[2])