Макроподстановки в топике MQTT для удобства связывания с OpenHAB
[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 nil
143   msg_body=msg[2] or nil
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 nil
158       value=recrd[2] or nil
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     for k,v in pairs(sensor) do
170       printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = "..v)
171       submitValue(sensor_type,sensor_id,k,v)
172       if mqtt_client then
173         mqtt_path=string.gsub(mqtt_topic,"{(.-)}", 
174           function (name) 
175             if name=="dev" then
176               return web_devid
177             elseif name=="type" then
178               return sensor_type
179             elseif name=="id" then
180               return sensor_id
181             elseif name=="param" then
182               return k
183             else
184               return '{'..name..'}'
185             end      
186           end)
187         mqtt_client:publish(mqtt_path,v)
188       end  
189     end
190   elseif msg_type=="ALARM" then
191     printLog("ALARM: "..msg_body)  
192     sens = splitStr(msg_body,",")
193     sensor = {}
194     idx = 1
195     sensor_type = nil
196     sensor_id = web_devid
197     mqtt_param = {}
198     for i,rec in ipairs(sens) do
199       recrd=splitStr(rec,'=')
200       key=recrd[1] or nil
201       value=recrd[2] or nil
202       if value then
203         if key=="TYPE" then
204           alarm_type=value
205         elseif key=="ID" then
206           alarm_id=value
207         end
208       end
209     end
210     if mqtt_client then
211       mqtt_path=string.gsub(mqtt_alarm_topic,"{(.-)}", 
212         function (name) 
213           if name=="dev" then
214             return web_devid
215           elseif name=="type" then
216             return sensor_type
217           elseif name=="id" then
218             return sensor_id
219           else
220             return '{'..name..'}'
221           end      
222         end)
223       mqtt_client:publish(mqtt_path,msg_body)
224     end
225     if alarm_exec then
226       command=alarm_exec..
227         " \""..string.gsub(alarm_type,"\"","\\\"")..
228         "\" \""..string.gsub(alarm_id,"\"","\\\"")..
229         "\" \""..string.gsub(msg_body,"\"","\\\"").."\""
230       os.execute(command)
231     end
232   end
233
234 end
235
236 getConfig()
237
238 if mqtt_host then
239   MQTT = require "paho.mqtt"
240   mqtt_client = MQTT.client.create(mqtt_host, mqtt_port)
241   if mqtt_user then
242     mqtt_client:auth(mqtt_user, mqtt_passwd)
243   end
244   mqtt_client:connect(mqtt_id)
245   json = require( "json" )
246 end
247
248 if serial_port then
249   serialin=io.open(serial_port,"r")
250 elseif input_file == "-" then
251   serialin=io.stdin;
252 elseif input_file then
253   serialin=io.open(input_file,"r")
254 elseif input_exec then
255   serialin=io.popen(input_exec,"r")
256 else
257   printLog("No input selected")
258   return
259 end  
260 while 1 do
261   line=serialin:read()
262   print(line)
263   processLine(line)
264 end