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 search_rs232_const(rs232,prefix,value)
121 for k,v in pairs(rs232) do
123 if k == prefix..value:upper() then
133 function init_serial_device(device,subsystem,parameters)
135 rs232 = require("luars232")
139 local e, port = rs232.open(device["port"])
141 local baud = device["baud"]; if baud == nil then baud = 9600; end
142 local bits = device["bits"]; if bits == nil then bits = 8; end
143 local stop_bits = device["stop_bits"]; if stop_bits == nil then stop_bits = 1; end
144 local parity = device["parity"]; if parity == nil then parity = "NONE"; end
145 local flowctl = device["flowctl"]; if flowctl == nil then flowctl = "OFF"; end
147 assert(port:set_baud_rate(search_rs232_const(rs232,"RS232_BAUD_",baud)) == rs232.RS232_ERR_NOERROR)
148 assert(port:set_data_bits(search_rs232_const(rs232,"RS232_DATA_",bits)) == rs232.RS232_ERR_NOERROR)
149 assert(port:set_parity(search_rs232_const(rs232,"RS232_PARITY_",parity)) == rs232.RS232_ERR_NOERROR)
150 assert(port:set_stop_bits(search_rs232_const(rs232,"RS232_STOP_",stop_bits)) == rs232.RS232_ERR_NOERROR)
151 assert(port:set_flow_control(search_rs232_const(rs232,"RS232_FLOW_",flowctl)) == rs232.RS232_ERR_NOERROR)
155 getparameter["rs232"] = port
157 if subsystem == "mhz" then
159 getparameter["function"] = get_mhz
160 getparameter["name"] = "CO2_PPM"
161 getparameter["sensor"] = "MHZ19"
162 parameters[#parameters+1] = getparameter
166 parameters[#parameters+1] = getparameter
172 function init_i2c_device(device,subsystem,parameters)
179 local f = io.open("/sys/class/i2c-dev/i2c-"..i2c_bus.."/device/new_device","w")
181 io.write(device["name"].." "..device["address"])
185 device_path=find_device(device["name"],subsystem)
187 if device_path and device["set_param"] then
188 for key,record in pairs(device["set_param"]) do
189 setparam = split(record,":")
190 setpath = device_path.."/"..setparam[1]
192 local f = io.open(setpath,"w")
194 io.write(setparam[2])
200 if device_path and device["parameter"] then
202 for key,record in pairs(device["parameter"]) do
204 getparam = split(record,":")
206 getparameter["path"] = device_path.."/"..getparam[1]
207 getparameter["name"] = getparam[2]
208 getscale = getparam[3]
209 getcorrection = getparam[4]
213 if not getcorrection then
216 getparameter["scale"] = tonumber(getscale)
217 getparameter["sensor"] = device["name"]:upper()
218 getparameter["correction"] = tonumber(getcorrection)
219 getparameter["function"] = get_parameter
220 parameters[#parameters+1] = getparameter
228 function init_device(device,parameters)
230 if device["module"] then
231 os.execute("modprobe "..device["module"])
234 if device["type"] then
236 devtype = split(device["type"],":")
238 subsystem = devtype[2]
240 if (bus == "i2c") then
242 init_i2c_device(device,subsystem,parameters)
244 elseif (bus == "serial") then
246 init_serial_device(device,subsystem,parameters)
254 function init(config_name)
258 i2c_bus = uci.get(config_name,"hardware","i2c_bus")
260 local devices = get_device_list(config_name)
262 for key,devname in pairs(devices) do
264 device = get_device(config_name,devname)
267 init_device(device,parameters)
276 function get_parameters(parameters)
278 for key,record in pairs(parameters) do
280 if not results[record["sensor"]] then
281 results[record["sensor"]] = {}
284 results[record["sensor"]][record["name"]] = record["function"](record)
292 if not config_name then
293 config_name = "weathermon"
296 web_id = get_devid(config_name)
298 parameters = init(config_name)
300 local delay = uci.get(config_name,"process","delay")
302 local working_dir = uci.get(config_name,"process","working_dir")
304 lfs.mkdir(working_dir)
312 values = get_parameters(parameters)
315 for key,record in pairs(values) do
316 records[web_id][key] = record
317 records[web_id]["timestamp"] = os.date("%Y-%m-%dT%H:%M:%S")
319 for key,value in pairs(values) do
320 value["device"] = key
321 print(json.encode(value))