Подчистка
[weathermon.git] / weathermon.lua~
1 #!/usr/bin/lua
2
3 function getConfig()
4
5   local uci=require("uci")
6   local cur=uci.cursor()
7   local config="weathermon"
8
9   web_url  = cur.get(config,"web","url")
10   web_user = cur.get(config,"web","user")
11   web_pass = cur.get(config,"web","password")
12   web_devid = cur.get(config,"web","devid")
13
14   web_iface = cur.get(config,"web","iface")
15
16   if web_iface then
17   
18     command = '/sbin/ifconfig '..web_iface..' | grep \'\\<inet\\>\' | sed -n \'1p\' | tr -s \' \' | cut -d \' \' -f3 | cut -d \':\' -f2'
19     f=io.popen(command,'r')
20     ip_addr=f:read()
21   
22   end
23
24   if not web_devid then
25   
26     if web_iface then
27       io.input("/sys/class/net/"..web_iface.."/address")
28     else
29       io.input("/sys/class/net/eth0/address")
30     end
31
32     mac = io.read("*line")
33     mac = mac:gsub(":","")
34     mac = mac:upper()
35
36     web_devid = mac
37
38   end
39
40   logging = cur.get(config,"logging","enabled") 
41
42   serial_port = cur.get(config,"serial","port")
43   serial_baud = cur.get(config,"serial","baud")
44
45   input_file = cur.get(config,"input","file")
46   input_exec = cur.get(config,"input","exec")
47   alarm_exec = cur.get(config,"alarm","exec")
48
49   if serial_port then
50
51     command = "stty -F  "..serial_port.." "..serial_baud
52     os.execute(command)
53
54   end
55
56   mqtt_host = cur.get(config,"mqtt","host")
57   mqtt_port = cur.get(config,"mqtt","port")
58   mqtt_id = cur.get(config,"mqtt","id")
59   mqtt_topic = cur.get(config,"mqtt","topic")
60   mqtt_alarm_topic = cur.get(config,"mqtt","alarm_topic")
61
62   mqtt_user = cur.get(config,"mqtt","user")
63   mqtt_passwd = cur.get(config,"mqtt","password")
64
65   if mqtt_host and not mqtt_id then
66     mqtt_id="weather-"..web_devid
67   end
68
69   if mqtt_host and not mqtt_port then
70     mqtt_port = 1883
71   end
72
73   if mqtt_host and not mqtt_topic then
74     mqtt_topic = 'weathermon/{dev}/{type}/{id}/{param}'
75   end
76
77   if mqtt_host and not mqtt_alarm_topic then
78     mqtt_alarm_topic = 'alarm/{dev}/{type}/{id}'
79   end
80
81 end
82
83 require "socket"
84
85 function sleep(sec)
86   socket.select(nil, nil, sec)
87 end
88
89 function splitStr(str,char)
90
91   local res = {}
92   local idx = 1
93
94
95   while str:len()>0 do
96     pos = str:find(char); 
97     if pos == nil then
98       res[idx]=str
99       str=""
100     else
101       res[idx]=str:sub(1,pos-1)
102       idx=idx+1
103       str=str:sub(pos+1)
104     end
105   end
106
107   return res
108
109 end
110
111 function printLog(str)
112   print(str)
113   if logging=="on" then
114     os.execute("logger -t weathermon "..str)
115   end 
116 end
117
118 function submitValue(type,id,param,val)
119
120   url = web_url.."?stype="..type.."&sid="..id.."&param="..param.."&value="..val
121
122   command = "curl"
123
124   if web_iface then
125     command = command.." --interface "..ip_addr
126   end
127
128   if web_user then
129     command = command.." -u "..web_user..":"..web_pass
130   end
131
132   command = command.." \""..url.."\""
133
134   os.execute(command)
135   print()
136
137 end
138
139 function processLine(str)
140
141   msg=splitStr(line,':')
142   msg_type=msg[1] or ''
143   msg_body=msg[2] or ''
144   if msg_type=="STATUS" then
145     printLog("Status: "..msg_body)
146   elseif msg_type=="ERROR" then
147     printLog("Error: "..msg_body)  
148   elseif msg_type=="SENSOR" then
149     printLog("SENSOR: "..msg_body)  
150     sens = splitStr(msg_body,",")
151     sensor = {}
152     idx = 1
153     sensor_type = nil
154     sensor_id = web_devid
155     for i,rec in ipairs(sens) do
156       recrd=splitStr(rec,'=')
157       key=recrd[1] or ''
158       value=recrd[2] or ''
159       if value then
160         if key=="TYPE" then
161           sensor_type=value
162         elseif key=="ID" then
163           sensor_id=value
164         else
165           sensor[key]=value
166         end
167       end
168     end
169     if not (sensor_type==nil or sensor_id==nil or sensor_type=='' or sensor_id=='') then
170       for k,v in pairs(sensor) do
171         printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = "..v)
172         submitValue(sensor_type,sensor_id,k,v)
173         if mqtt_client then
174           mqtt_path=string.gsub(mqtt_topic,"{(.-)}", 
175             function (name) 
176               if name=="dev" then
177                 return web_devid
178               elseif name=="type" then
179                 return sensor_type
180               elseif name=="id" then
181                 return sensor_id
182               elseif name=="param" then
183                 return k
184               else
185                 return '{'..name..'}'
186               end      
187             end)
188           mqtt_client:publish(mqtt_path,v)
189         end  
190       end
191     else
192       printLog("Cannot parse sensor input: "..msg_body)
193     end
194   elseif msg_type=="ALARM" then
195     printLog("ALARM: "..msg_body)  
196     sens = splitStr(msg_body,",")
197     sensor = {}
198     idx = 1
199     sensor_type = nil
200     sensor_id = web_devid
201     mqtt_param = {}
202     for i,rec in ipairs(sens) do
203       recrd=splitStr(rec,'=')
204       key=recrd[1] or ''
205       value=recrd[2] or ''
206       if value then
207         if key=="TYPE" then
208           alarm_type=value
209         elseif key=="ID" then
210           alarm_id=value
211         end
212       end
213     end
214     if not (alarm_type==nil or alarm_id==nil or alarm_type=='' or alarm_id=='') then
215       if mqtt_client then
216         mqtt_path=string.gsub(mqtt_alarm_topic,"{(.-)}", 
217           function (name) 
218             if name=="dev" then
219               return web_devid
220             elseif name=="type" then
221               return sensor_type
222             elseif name=="id" then
223               return sensor_id
224             else
225               return '{'..name..'}'
226             end      
227           end)
228         mqtt_client:publish(mqtt_path,msg_body)
229       end
230       if alarm_exec then
231         command=alarm_exec..
232           " \""..string.gsub(alarm_type,"\"","\\\"")..
233           "\" \""..string.gsub(alarm_id,"\"","\\\"")..
234           "\" \""..string.gsub(msg_body,"\"","\\\"").."\""
235         os.execute(command)
236       end
237     else
238       printLog("Cannot parse alarm input: "..msg_body)
239     end
240   end
241
242 end
243
244 getConfig()
245
246 if mqtt_host then
247   MQTT = require "paho.mqtt"
248   mqtt_client = MQTT.client.create(mqtt_host, mqtt_port)
249   if mqtt_user then
250     mqtt_client:auth(mqtt_user, mqtt_passwd)
251   end
252   mqtt_client:connect(mqtt_id)
253   json = require( "json" )
254 end
255
256 if serial_port then
257   serialin=io.open(serial_port,"r")
258 elseif input_file == "-" then
259   serialin=io.stdin;
260 elseif input_file then
261   serialin=io.open(input_file,"r")
262 elseif input_exec then
263   serialin=io.popen(input_exec,"r")
264 else
265   printLog("No input selected")
266   return
267 end  
268 while 1 do
269   line=serialin:read()
270   print(line)
271   processLine(line)
272 end
273