8 socket = require "socket"
12 io.stdout:setvbuf('no')
14 function get_device_list(config_name)
19 cur.foreach(config_name, "device", function(s)
20 devices[#devices+1] = s[".name"]
27 function get_device(config_name,device_name)
32 cur.foreach(config_name, "device", function(s)
33 if s[".name"] == device_name then
42 function find_device(name,subsystem)
46 if subsystem == "iio" then
47 search_base = "/sys/bus/iio/devices"
48 for file in lfs.dir(search_base) do
49 if get_file_content(search_base.."/"..file.."/name") == name then
50 return search_base.."/"..file
53 elseif subsystem == "hwmon" then
54 search_base = "/sys/class/hwmon"
55 for file in lfs.dir(search_base) do
56 if get_file_content(search_base.."/"..file.."/device/name") == name then
57 return search_base.."/"..file.."/device"
66 function get_parameter(record)
67 return tonumber(get_file_content(record["path"])) * record["scale"] + record["correction"]
70 function string.fromhex(str)
71 return (str:gsub('..', function (cc)
72 return string.char(tonumber(cc, 16))
76 function string.tohex(str)
77 return (str:gsub('.', function (c)
78 return string.format('%02X', string.byte(c))
82 function get_mhz(record)
83 local p = record["rs232"]
84 p:write(string.fromhex("ff0186000000000079"))
85 local e, s = p:read(9,1000)
86 return s:byte(3)*256+s:byte(4)
89 function init_serial_device(device,subsystem,parameters)
91 rs232 = require("luars232")
95 local e, port = rs232.open(device["port"])
97 if subsystem == "mhz" then
99 assert(port:set_baud_rate(rs232.RS232_BAUD_9600) == rs232.RS232_ERR_NOERROR)
100 assert(port:set_data_bits(rs232.RS232_DATA_8) == rs232.RS232_ERR_NOERROR)
101 assert(port:set_parity(rs232.RS232_PARITY_NONE) == rs232.RS232_ERR_NOERROR)
102 assert(port:set_stop_bits(rs232.RS232_STOP_1) == rs232.RS232_ERR_NOERROR)
103 assert(port:set_flow_control(rs232.RS232_FLOW_OFF) == rs232.RS232_ERR_NOERROR)
107 getparameter["rs232"] = port
108 getparameter["function"] = get_mhz
109 getparameter["name"] = "CO2_PPM"
110 getparameter["sensor"] = "MHZ19"
111 parameters[#parameters+1] = getparameter
119 function init_i2c_device(device,subsystem,parameters)
126 local f = io.open("/sys/class/i2c-dev/i2c-"..i2c_bus.."/device/new_device","w")
128 io.write(device["name"].." "..device["address"])
132 device_path=find_device(device["name"],subsystem)
134 if device_path and device["set_param"] then
135 for key,record in pairs(device["set_param"]) do
136 setparam = split(record,":")
137 setpath = device_path.."/"..setparam[1]
139 local f = io.open(setpath,"w")
141 io.write(setparam[2])
147 if device_path and device["parameter"] then
149 for key,record in pairs(device["parameter"]) do
151 getparam = split(record,":")
153 getparameter["path"] = device_path.."/"..getparam[1]
154 getparameter["name"] = getparam[2]
155 getscale = getparam[3]
156 getcorrection = getparam[4]
160 if not getcorrection then
163 getparameter["scale"] = tonumber(getscale)
164 getparameter["sensor"] = device["name"]:upper()
165 getparameter["correction"] = tonumber(getcorrection)
166 getparameter["function"] = get_parameter
167 parameters[#parameters+1] = getparameter
175 function init_device(device,parameters)
177 if device["module"] then
178 os.execute("modprobe "..device["module"])
181 if device["type"] then
183 devtype = split(device["type"],":")
185 subsystem = devtype[2]
187 if (bus == "i2c") then
189 init_i2c_device(device,subsystem,parameters)
191 elseif (bus == "serial") then
193 init_serial_device(device,subsystem,parameters)
201 function init(config_name)
205 i2c_bus = uci.get(config_name,"hardware","i2c_bus")
207 local devices = get_device_list(config_name)
209 for key,devname in pairs(devices) do
211 device = get_device(config_name,devname)
214 init_device(device,parameters)
223 function get_parameters(parameters)
225 for key,record in pairs(parameters) do
227 if not results[record["sensor"]] then
228 results[record["sensor"]] = {}
231 results[record["sensor"]][record["name"]] = record["function"](record)
239 if not config_name then
240 config_name = "weathermon"
243 web_id = get_devid(config_name)
245 parameters = init(config_name)
247 local delay = uci.get(config_name,"process","delay")
249 local working_dir = uci.get(config_name,"process","working_dir")
251 lfs.mkdir(working_dir)
259 values = get_parameters(parameters)
262 for key,record in pairs(values) do
263 records[web_id][key] = record
264 records[web_id]["timestamp"] = os.date("%Y-%m-%dT%H:%M:%S")
266 for key,value in pairs(values) do
267 value["device"] = key
268 print(json.encode(value))