8 socket = require "socket"
12 io.stdout:setvbuf('no')
15 if type(o) == 'table' then
17 for k,v in pairs(o) do
18 if type(k) ~= 'number' then k = '"'..k..'"' end
19 s = s .. '['..k..'] = ' .. dump(v) .. ','
27 function get_device_list(config_name)
32 cur.foreach(config_name, "device", function(s)
33 devices[#devices+1] = s[".name"]
40 function get_device(config_name,device_name)
45 cur.foreach(config_name, "device", function(s)
46 if s[".name"] == device_name then
55 function find_device(name,subsystem)
59 if subsystem == "iio" then
60 search_base = "/sys/bus/iio/devices"
61 for file in lfs.dir(search_base) do
62 if get_file_content(search_base.."/"..file.."/name") == name then
63 return search_base.."/"..file
66 elseif subsystem == "hwmon" then
67 search_base = "/sys/class/hwmon"
68 for file in lfs.dir(search_base) do
69 if get_file_content(search_base.."/"..file.."/device/name") == name then
70 return search_base.."/"..file.."/device"
79 function get_parameter(record)
80 return tonumber(get_file_content(record["path"])) * record["scale"] + record["correction"]
83 function string.fromhex(str)
84 return (str:gsub('..', function (cc)
85 return string.char(tonumber(cc, 16))
89 function string.tohex(str)
90 return (str:gsub('.', function (c)
91 return string.format('%02X', string.byte(c))
95 function get_mhz(record)
96 local p = record["rs232"]
98 p:write(string.fromhex("ff0186000000000079"))
99 local e, s = p:read(9,1000)
100 if (e == 0) and (s:len() == 9) and (s:byte(1) == 255) then
103 crc = crc + s:byte(i)
110 if crc == s:byte(9) then
111 return s:byte(3)*256+s:byte(4)
119 function init_serial_device(device,subsystem,parameters)
121 rs232 = require("luars232")
125 local e, port = rs232.open(device["port"])
127 if subsystem == "mhz" then
129 assert(port:set_baud_rate(rs232.RS232_BAUD_9600) == rs232.RS232_ERR_NOERROR)
130 assert(port:set_data_bits(rs232.RS232_DATA_8) == rs232.RS232_ERR_NOERROR)
131 assert(port:set_parity(rs232.RS232_PARITY_NONE) == rs232.RS232_ERR_NOERROR)
132 assert(port:set_stop_bits(rs232.RS232_STOP_1) == rs232.RS232_ERR_NOERROR)
133 assert(port:set_flow_control(rs232.RS232_FLOW_OFF) == rs232.RS232_ERR_NOERROR)
137 getparameter["rs232"] = port
138 getparameter["function"] = get_mhz
139 getparameter["name"] = "CO2_PPM"
140 getparameter["sensor"] = "MHZ19"
141 parameters[#parameters+1] = getparameter
149 function init_i2c_device(device,subsystem,parameters)
156 local f = io.open("/sys/class/i2c-dev/i2c-"..i2c_bus.."/device/new_device","w")
158 io.write(device["name"].." "..device["address"])
162 device_path=find_device(device["name"],subsystem)
164 if device_path and device["set_param"] then
165 for key,record in pairs(device["set_param"]) do
166 setparam = split(record,":")
167 setpath = device_path.."/"..setparam[1]
169 local f = io.open(setpath,"w")
171 io.write(setparam[2])
177 if device_path and device["parameter"] then
179 for key,record in pairs(device["parameter"]) do
181 getparam = split(record,":")
183 getparameter["path"] = device_path.."/"..getparam[1]
184 getparameter["name"] = getparam[2]
185 getscale = getparam[3]
186 getcorrection = getparam[4]
190 if not getcorrection then
193 getparameter["scale"] = tonumber(getscale)
194 getparameter["sensor"] = device["name"]:upper()
195 getparameter["correction"] = tonumber(getcorrection)
196 getparameter["function"] = get_parameter
197 parameters[#parameters+1] = getparameter
205 function init_device(device,parameters)
207 if device["module"] then
208 os.execute("modprobe "..device["module"])
211 if device["type"] then
213 devtype = split(device["type"],":")
215 subsystem = devtype[2]
217 if (bus == "i2c") then
219 init_i2c_device(device,subsystem,parameters)
221 elseif (bus == "serial") then
223 init_serial_device(device,subsystem,parameters)
231 function init(config_name)
235 i2c_bus = uci.get(config_name,"hardware","i2c_bus")
237 local devices = get_device_list(config_name)
239 for key,devname in pairs(devices) do
241 device = get_device(config_name,devname)
244 init_device(device,parameters)
253 function get_parameters(parameters)
255 for key,record in pairs(parameters) do
257 if not results[record["sensor"]] then
258 results[record["sensor"]] = {}
261 results[record["sensor"]][record["name"]] = record["function"](record)
269 if not config_name then
270 config_name = "weathermon"
273 web_id = get_devid(config_name)
275 parameters = init(config_name)
277 local delay = uci.get(config_name,"process","delay")
279 local working_dir = uci.get(config_name,"process","working_dir")
281 lfs.mkdir(working_dir)
289 values = get_parameters(parameters)
292 for key,record in pairs(values) do
293 records[web_id][key] = record
294 records[web_id]["timestamp"] = os.date("%Y-%m-%dT%H:%M:%S")
296 for key,value in pairs(values) do
297 value["device"] = key
298 print(json.encode(value))