local syslog=require("syslog") local math=require("math") local cjson=require("cjson") require "sys" require "utils" require "patch" -------------------------[输入设备名称以及标识符]------------------------- gw_sn=io.getGatewayID() device_a=string.format("%s_RS485_1_02",gw_sn) device_b=string.format("%s_RS485_1_03",gw_sn) device_c=string.format("%s_RS485_1_04",gw_sn) device_d=string.format("%s_GPS",gw_sn) --print(type(gw_sn),gw_sn,device_a,device_b,device_c,device_d) --device_a="202103180001" --device_b="202103180002" --device_c="202103180003" --device_d="GPSJZX00001" value_name="chenjiangceliangzhi" syslog.openlog("chenjiang syslog", syslog.LOG_PERROR + syslog.LOG_ODELAY, "LOG_USER") --出口 function process_data(source_a1,source_a2,source_a3,source_a4,source_a5,source_a6,source_a7,source_a8,source_a9,source_a10,source_a11,source_a12,source_a13,source_b1,source_b2,source_c1,source_c2,source_c3,source_c4, source_d1, source_d2) local curr_timestamp=os.time() --local json_str=cjson.encode(raw_data) local new_data={} new_data["data_type"] = 0 --改为0是属性,服务2上报,1是event new_data['timestamp']=os.time() new_data['date']=os.date("%Y%m%d%H%M%S", os.time()+8*3600) if source_a1~=nil and source_a1>=0 and source_a1<=20 then new_data['dio']=source_a1 end if source_a2~=nil and source_a2>=0 and source_a2<=1000 then new_data['turb']=source_a2 end if source_a3~=nil and source_a3>=0 and source_a3<=100 then new_data['ec']=source_a3 end if source_a4~=nil and source_a4>=0 and source_a4<=14 then new_data['ph']=source_a4 end new_data['temp']=source_a5 new_data['ORP']=source_a6 new_data['Chl']=source_a7 new_data['BGA']=source_a8 new_data['SN']=source_a9 new_data['Error']=source_a10 new_data['zhuan']=source_a11 new_data['Vol']=source_a12 new_data['Salinity']=source_a13 new_data['TOC']=source_b1 if source_b2~=nil and source_b2>=0 and source_b2<=450 then new_data['COD']=source_b2 end if source_c1~=nil and source_c1>=0 and source_c1<=100 then new_data['NH3N']=source_c1 end new_data['K']=source_c2 new_data['NH4']=source_c3 new_data['LGNH4']=source_c4 new_data['longitude']= source_d1 new_data['latitude']= source_d2 local json_str=cjson.encode(new_data) syslog.syslog("LOG_WARNING", json_str .. #json_str) return 0,json_str,#json_str end function parser_input(obj) local source_a1,source_a2,source_a3,source_a4,source_a5,source_a6,source_a7,source_a8,source_a9,source_a10,source_a11,source_a12,source_a13 local source_b1,source_b2 local source_c1,source_c2,source_c3,source_c4 local source_d1, source_d2 for i,v in pairs(obj["datas"]) do if(v["sn"] == device_a) then tag = v["tags"] if tag then source_a1 = tag["dio"] --溶解氧 source_a2 = tag["turb"] --浊度 source_a3 = tag["ec"] --电导率 source_a4 = tag["ph"] --酸碱度 source_a5 = tag["temp"] --温度 source_a6 = tag["ORP"] --ORP source_a7 = tag["Chl"] --叶绿素 source_a8 = tag["BGA"] --蓝绿藻 source_a9 = tag["SN"] --SN source_a10 = tag["Error"] --报错类型 source_a11 = tag["zhuan"] --转动时间 source_a12 = tag["Vol"] --电源电压 source_a13 = tag["Salinity"] --盐度 end elseif(v["sn"] == device_b) then tag = v["tags"] if tag then source_b1 = tag["TOC"] --TOC有机物总量 source_b2 = tag["COD"] --COD化学需氧量 end elseif(v["sn"] == device_c) then tag = v["tags"] if tag then source_c1 = tag["NH3N"] --NH3N source_c2 = tag["K"] --K+ source_c3 = tag["NH4"] --NH4 source_c4 = tag["LGNH4"] --LGNH4 end elseif(v["sn"] == device_d) then tag = v["tags"] if tag then source_d1 = tag["longitude"] --longitude source_d2 = tag["latitude"] --latitude end end end return source_a1,source_a2,source_a3,source_a4,source_a5,source_a6,source_a7,source_a8,source_a9,source_a10,source_a11,source_a12,source_a13,source_b1,source_b2,source_c1,source_c2,source_c3,source_c4, source_d1, source_d2 end function protocol_decode(input_str, len) --出参格式化-- local obj = cjson.decode(input_str) if obj["datas"] == nil then return -1, nil, nil end --获取输入变量-- local source_a1,source_a2,source_a3,source_a4,source_a5,source_a6,source_a7,source_a8,source_a9,source_a10,source_a11,source_a12,source_a13,source_b1,source_b2,source_c1,source_c2,source_c3,source_c4, source_d1, source_d2 = parser_input(obj) --syslog.syslog("LOG_WARNING",string.format( "source data[%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f]",source_a1,source_a2,source_a3,source_a4,source_a5,source_a6,source_a7,source_a8,source_a9,source_a10,source_a11,source_a12,source_a13,source_b1,source_b2,source_c1,source_c2,source_c3,source_c4)) --local json_obj={} return process_data(source_a1,source_a2,source_a3,source_a4,source_a5,source_a6,source_a7,source_a8,source_a9,source_a10,source_a11,source_a12,source_a13,source_b1,source_b2,source_c1,source_c2,source_c3,source_c4, source_d1, source_d2) --return err_code,out_str,out_len end -- protocol_decode('{"datas":[{"sn":"201955555555","time":1574933970,"state":0,"quality":0,"identifier":"post","tags":{"chenjiangceliangzhi":10}},{"sn":"201944444444","time":1574933967,"state":0,"quality":0,"identifier":"post","tags":{"chenjiangceliangzhi":10}}]}', 0) syslog.closelog()