3 local json = require("json")
7 function getConfig(configname)
11 local uci=require("uci")
12 local cur=uci.cursor()
20 web_url = cur.get(config,"web","url")
21 web_user = cur.get(config,"web","user")
22 web_timeout = cur.get(config,"web","timeout")
23 web_pass = cur.get(config,"web","password")
25 if not web_timeout then
29 web_devid = get_devid(config)
31 logging = cur.get(config,"logging","enabled")
32 touch_file = cur.get(config,"logging","touch_file")
34 backlogdb = cur.get(config,"process","backlogdb")
35 logdb = cur.get(config,"process","logdb")
37 serial_port = cur.get(config,"serial","port")
38 serial_baud = cur.get(config,"serial","baud")
40 input_file = cur.get(config,"input","file")
41 input_exec = cur.get(config,"input","exec")
42 alarm_exec = cur.get(config,"alarm","exec")
46 command = "stty -F "..serial_port.." "..serial_baud
51 mqtt_host = cur.get(config,"mqtt","host")
52 mqtt_port = cur.get(config,"mqtt","port")
53 mqtt_id = cur.get(config,"mqtt","id")
54 mqtt_topic = cur.get(config,"mqtt","topic")
55 mqtt_alarm_topic = cur.get(config,"mqtt","alarm_topic")
57 mqtt_user = cur.get(config,"mqtt","user")
58 mqtt_passwd = cur.get(config,"mqtt","password")
60 if mqtt_host and not mqtt_id then
61 mqtt_id="weather-"..web_devid
64 if mqtt_host and not mqtt_port then
68 if mqtt_host and not mqtt_topic then
69 mqtt_topic = 'weathermon/{dev}/{type}/{id}/{param}'
72 if mqtt_host and not mqtt_alarm_topic then
73 mqtt_alarm_topic = 'alarm/{dev}/{type}/{id}'
76 dump_file = cur.get(config,"process","dump_file")
80 function printLog(str)
82 capture("logger -t weathermon "..str)
84 elseif logging=="syslog" then
85 capture("logger -t weathermon "..str)
86 elseif logging=="stdout" then
91 function submitValue(type,id,param,val)
95 if web_url and val then
97 local url = web_url.."?stype="..url_encode(type).."&sid="..url_encode(id).."¶m="..url_encode(param).."&value="..url_encode(val)
100 url = url:gsub("//","//"..web_user..":"..web_pass.."@",1)
103 local result,code = http.request (url)
105 if code ~= 200 and backlog_con then
106 printLog("writing record to backlog...")
107 backlog_con:execute(string.format("INSERT INTO queue(time_stamp,sensor_id,sensor,param,value) VALUES (datetime('now','localtime'),'%s','%s','%s',%f)",id,type,param,val))
113 log_con:execute(string.format("INSERT INTO log(time_stamp,sensor_id,sensor,param,value) VALUES (datetime('now','localtime'),'%s','%s','%s',%f)",id,type,param,val))
118 function storeRecord(id,sensor,param,value)
120 if not records[id] then
124 records[id]["timestamp"] = os.date("%Y-%m-%dT%H:%M:%S")
126 if not records[id][sensor] then
127 records[id][sensor] = {}
130 records[id][sensor][param] = value
134 function processJson(str)
140 for key,value in pairs(msg) do
142 if key=="model" or key=="device" then
144 elseif key=="id" then
146 elseif key=='time' then
154 if not sensor_id then
155 sensor_id = web_devid
158 if not (sensor_type==nil or sensor_id==nil or sensor_type=='' or sensor_id=='') then
159 if next(sensor)==nil then
160 sensor["command"]="alarm"
163 for k,v in pairs(sensor) do
164 storeRecord(sensor_id,sensor_type,k,v)
165 printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = \""..v.."\"")
167 submitValue(sensor_type,sensor_id,k,v)
170 mqtt_path=string.gsub(mqtt_topic,"{(.-)}",
173 return mqtt_encode(web_devid)
174 elseif name=="type" then
175 return mqtt_encode(sensor_type)
176 elseif name=="id" then
177 return mqtt_encode(sensor_id)
178 elseif name=="param" then
181 return '{'..name..'}'
184 if not mqtt_client:socket() then
185 mqtt_client:reconnect()
187 mqtt_client:publish(mqtt_path,v,0,0)
192 printLog("Cannot parse sensor input: "..str)
197 function processLine(str)
200 msg_type=msg[1] or ''
201 msg_body=msg[2] or ''
202 if msg_type=="STATUS" then
203 printLog("Status: "..msg_body)
204 elseif msg_type=="ERROR" then
205 printLog("Error: "..msg_body)
206 elseif msg_type=="SENSOR" then
207 printLog("SENSOR: "..msg_body)
208 sens = split(msg_body,",")
212 sensor_id = web_devid
213 for i,rec in ipairs(sens) do
220 elseif key=="ID" then
227 if not (sensor_type==nil or sensor_id==nil or sensor_type=='' or sensor_id=='') then
229 for k,v in pairs(sensor) do
230 storeRecord(sensor_id,sensor_type,k,v)
231 printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = "..v)
233 submitValue(sensor_type,sensor_id,k,v)
236 mqtt_path=string.gsub(mqtt_topic,"{(.-)}",
240 elseif name=="type" then
242 elseif name=="id" then
244 elseif name=="param" then
247 return '{'..name..'}'
250 mqtt_client:publish(mqtt_path,v,0,0)
255 printLog("Cannot parse sensor input: "..msg_body)
257 elseif msg_type=="ALARM" then
258 printLog("ALARM: "..msg_body)
259 sens = split(msg_body,",")
263 sensor_id = web_devid
265 for i,rec in ipairs(sens) do
272 elseif key=="ID" then
277 if not (alarm_type==nil or alarm_id==nil or alarm_type=='' or alarm_id=='') then
279 mqtt_path=string.gsub(mqtt_alarm_topic,"{(.-)}",
283 elseif name=="type" then
285 elseif name=="id" then
288 return '{'..name..'}'
291 mqtt_client:publish(mqtt_path,msg_body,0,0)
296 " \""..string.gsub(alarm_type,"\"","\\\"")..
297 "\" \""..string.gsub(alarm_id,"\"","\\\"")..
298 "\" \""..string.gsub(msg_body,"\"","\\\"").."\""
302 printLog("Cannot parse alarm input: "..msg_body)
310 if backlogdb or logdb then
311 local dbdriver = require "luasql.sqlite3"
312 env = assert(dbdriver.sqlite3())
316 if not file_exists(backlogdb) then
319 backlog_con = assert(env:connect(backlogdb))
320 backlog_con:execute("CREATE TABLE queue(time_stamp datetime,sensor_id varchar(16),sensor varchar(16),param varchar(16),value float)")
324 if not file_exists(logdb) then
327 log_con = assert(env:connect(logdb))
328 log_con:execute("CREATE TABLE log(time_stamp datetime,sensor_id varchar(16),sensor varchar(16),param varchar(16),value float)")
329 log_con:execute("CREATE INDEX log_idx ON log(sensor_id,sensor,param,time_stamp)")
333 http = require("socket.http")
334 http.TIMEOUT = web_timeout
338 MQTT = require "mosquitto"
339 mqtt_client = MQTT.new(mqtt_id)
341 mqtt_client:login_set(mqtt_user, mqtt_passwd)
343 mqtt_client:connect(mqtt_host,mqtt_port)
347 serialin=io.open(serial_port,"r")
348 elseif input_file == "-" then
350 elseif input_file then
351 serialin=io.open(input_file,"r")
352 elseif input_exec then
353 serialin=io.popen(input_exec,"r")
355 printLog("No input selected")
359 serialin:setvbuf('no')
365 line=serialin:read("*l")
373 printLog("Received: "..line)
374 if startswith(line,'{') then
388 local f = io.open(dump_file,"w")
390 io.write(json.encode(records))