Добавлена корректная обработка падения процесса-поставщика данных (в моем случае...
[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     io.input("/sys/class/net/"..web_iface.."/address")
18   else
19     io.input("/sys/class/net/eth0/address")
20   end
21
22   mac = io.read("*line")
23   mac = mac:gsub(":","")
24   mac = mac:upper()
25
26   web_devid = web_devid or mac
27   logging = cur.get(config,"logging","enabled") 
28
29   serial_port = cur.get(config,"serial","port")
30   serial_baud = cur.get(config,"serial","baud")
31
32   command = "stty -F  "..serial_port.." "..serial_baud
33   os.execute(command)
34
35 end
36
37 require "socket"
38
39 function sleep(sec)
40   socket.select(nil, nil, sec)
41 end
42
43 function splitStr(str,char)
44
45   local res = {}
46   local idx = 1
47
48
49   while str:len()>0 do
50     pos = str:find(char); 
51     if pos == nil then
52       res[idx]=str
53       str=""
54     else
55       res[idx]=str:sub(1,pos-1)
56       idx=idx+1
57       str=str:sub(pos+1)
58     end
59   end
60
61   return res
62
63 end
64
65 function printLog(str)
66   print(str)
67   if logging=="on" then
68     os.execute("logger -t weathermon "..str)
69   end 
70 end
71
72 function submitValue(type,id,param,val)
73
74   url = web_url.."?stype="..type.."&sid="..id.."&param="..param.."&value="..val
75
76   command = "curl"
77
78   if web_iface then
79     command = command.." --interface "..web_iface
80   end
81
82   if web_user then
83     command = command.." -u "..web_user..":"..web_pass
84   end
85
86   command = command.." \""..url.."\""
87
88   print(command)
89   os.execute(command)
90   print("")
91
92 end
93
94 function processLine(str)
95
96   msg=splitStr(line,':')
97   msg_type=msg[1] or nil
98   msg_body=msg[2] or nil
99   if msg_type=="STATUS" then
100     printLog("Status: "..msg_body)
101   elseif msg_type=="ERROR" then
102     printLog("Error: "..msg_body)  
103   elseif msg_type=="SENSOR" then
104     printLog("SENSOR: "..msg_body)  
105     sens = splitStr(msg_body,",")
106     sensor = {}
107     idx = 1
108     sensor_type = nil
109     sensor_id = web_devid
110     for i,rec in ipairs(sens) do
111       recrd=splitStr(rec,'=')
112       key=recrd[1] or nil
113       value=recrd[2] or nil
114       if value then
115         if key=="TYPE" then
116           sensor_type=value
117         elseif key=="ID" then
118           sensor_id=value
119         else
120           sensor[key]=value
121         end
122       end
123     end
124     for k,v in pairs(sensor) do
125       printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = "..v)
126       submitValue(sensor_type,sensor_id,k,v)
127     end
128   end
129
130 end
131
132 getConfig()
133
134 serialin=io.open(serial_port,"r")
135 while 1 do
136   line=serialin:read()
137   print(line)
138   processLine(line)
139 end