6 function startswith(String,Start)
8 return string.sub(String,1,string.len(Start))==Start
14 function url_encode(str)
16 str = string.gsub (str, "\n", "\r\n")
17 str = string.gsub (str, "([^%w %-%_%.%~])",
18 function (c) return string.format ("%%%02X", string.byte(c)) end)
19 str = string.gsub (str, " ", "+")
24 function capture(cmd, raw)
25 local f = assert(io.popen(cmd, 'r'))
26 local s = assert(f:read('*a'))
28 if raw then return s end
29 s = string.gsub(s, '^%s+', '')
30 s = string.gsub(s, '%s+$', '')
31 s = string.gsub(s, '[\n\r]+', ' ')
35 function mqtt_encode(str)
37 str = string.gsub (str, "\n", "")
38 str = string.gsub (str, "/", "-")
43 function getConfig(configname)
45 local uci=require("uci")
46 local cur=uci.cursor()
54 web_url = cur.get(config,"web","url")
55 web_user = cur.get(config,"web","user")
56 web_pass = cur.get(config,"web","password")
57 web_devid = cur.get(config,"web","devid")
59 web_iface = cur.get(config,"web","iface")
63 command = '/sbin/ifconfig '..web_iface..' | grep \'\\<inet\\>\' | sed -n \'1p\' | tr -s \' \' | cut -d \' \' -f3 | cut -d \':\' -f2'
64 f=io.popen(command,'r')
72 io.input("/sys/class/net/"..web_iface.."/address")
74 io.input("/sys/class/net/eth0/address")
77 mac = io.read("*line")
78 mac = mac:gsub(":","")
85 logging = cur.get(config,"logging","enabled")
86 touch_file = cur.get(config,"logging","touch_file")
88 serial_port = cur.get(config,"serial","port")
89 serial_baud = cur.get(config,"serial","baud")
91 input_file = cur.get(config,"input","file")
92 input_exec = cur.get(config,"input","exec")
93 alarm_exec = cur.get(config,"alarm","exec")
97 command = "stty -F "..serial_port.." "..serial_baud
102 mqtt_host = cur.get(config,"mqtt","host")
103 mqtt_port = cur.get(config,"mqtt","port")
104 mqtt_id = cur.get(config,"mqtt","id")
105 mqtt_topic = cur.get(config,"mqtt","topic")
106 mqtt_alarm_topic = cur.get(config,"mqtt","alarm_topic")
108 mqtt_user = cur.get(config,"mqtt","user")
109 mqtt_passwd = cur.get(config,"mqtt","password")
111 if mqtt_host and not mqtt_id then
112 mqtt_id="weather-"..web_devid
115 if mqtt_host and not mqtt_port then
119 if mqtt_host and not mqtt_topic then
120 mqtt_topic = 'weathermon/{dev}/{type}/{id}/{param}'
123 if mqtt_host and not mqtt_alarm_topic then
124 mqtt_alarm_topic = 'alarm/{dev}/{type}/{id}'
131 local file = io.open(touch_file, 'w')
137 socket.select(nil, nil, sec)
140 function splitStr(str,char)
146 pos = str:find(char);
151 res[idx]=str:sub(1,pos-1)
161 function printLog(str)
162 if logging=="on" then
163 capture("logger -t weathermon "..str)
165 elseif logging=="syslog" then
166 capture("logger -t weathermon "..str)
167 elseif logging=="stdout" then
172 function submitValue(type,id,param,val)
174 url = web_url.."?stype="..url_encode(type).."&sid="..url_encode(id).."¶m="..url_encode(param).."&value="..url_encode(val)
179 command = command.." --interface "..ip_addr
183 command = command.." -u "..web_user..":"..web_pass
186 command = command.." \""..url.."\" 2>&1"
188 result = capture(command)
194 function processJson(str)
200 for key,value in pairs(msg) do
202 if key=="model" or key=="device" then
204 elseif key=="id" then
206 elseif key=='time' then
214 if not (sensor_type==nil or sensor_id==nil or sensor_type=='' or sensor_id=='') then
215 if next(sensor)==nil then
216 sensor["command"]="alarm"
218 for k,v in pairs(sensor) do
219 printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = \""..v.."\"")
220 submitValue(sensor_type,sensor_id,k,v)
222 mqtt_path=string.gsub(mqtt_topic,"{(.-)}",
225 return mqtt_encode(web_devid)
226 elseif name=="type" then
227 return mqtt_encode(sensor_type)
228 elseif name=="id" then
229 return mqtt_encode(sensor_id)
230 elseif name=="param" then
233 return '{'..name..'}'
236 mqtt_client:publish(mqtt_path,v)
240 printLog("Cannot parse sensor input: "..msg_body)
245 function processLine(str)
247 msg=splitStr(line,':')
248 msg_type=msg[1] or ''
249 msg_body=msg[2] or ''
250 if msg_type=="STATUS" then
251 printLog("Status: "..msg_body)
252 elseif msg_type=="ERROR" then
253 printLog("Error: "..msg_body)
254 elseif msg_type=="SENSOR" then
255 printLog("SENSOR: "..msg_body)
256 sens = splitStr(msg_body,",")
260 sensor_id = web_devid
261 for i,rec in ipairs(sens) do
262 recrd=splitStr(rec,'=')
268 elseif key=="ID" then
275 if not (sensor_type==nil or sensor_id==nil or sensor_type=='' or sensor_id=='') then
276 for k,v in pairs(sensor) do
277 printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = "..v)
278 submitValue(sensor_type,sensor_id,k,v)
280 mqtt_path=string.gsub(mqtt_topic,"{(.-)}",
284 elseif name=="type" then
286 elseif name=="id" then
288 elseif name=="param" then
291 return '{'..name..'}'
294 mqtt_client:publish(mqtt_path,v)
298 printLog("Cannot parse sensor input: "..msg_body)
300 elseif msg_type=="ALARM" then
301 printLog("ALARM: "..msg_body)
302 sens = splitStr(msg_body,",")
306 sensor_id = web_devid
308 for i,rec in ipairs(sens) do
309 recrd=splitStr(rec,'=')
315 elseif key=="ID" then
320 if not (alarm_type==nil or alarm_id==nil or alarm_type=='' or alarm_id=='') then
322 mqtt_path=string.gsub(mqtt_alarm_topic,"{(.-)}",
326 elseif name=="type" then
328 elseif name=="id" then
331 return '{'..name..'}'
334 mqtt_client:publish(mqtt_path,msg_body)
338 " \""..string.gsub(alarm_type,"\"","\\\"")..
339 "\" \""..string.gsub(alarm_id,"\"","\\\"")..
340 "\" \""..string.gsub(msg_body,"\"","\\\"").."\""
344 printLog("Cannot parse alarm input: "..msg_body)
353 MQTT = require "mosquitto"
354 mqtt_client = MQTT.new(mqtt_id)
356 mqtt_client:login_set(mqtt_user, mqtt_passwd)
358 mqtt_client:connect(mqtt_host,mqtt_port)
362 serialin=io.open(serial_port,"r")
363 elseif input_file == "-" then
365 elseif input_file then
366 serialin=io.open(input_file,"r")
367 elseif input_exec then
368 serialin=io.popen(input_exec,"r")
370 printLog("No input selected")
378 printLog("Received: "..line);
379 if startswith(line,'{') then