8 socket = require "socket"
12 function get_device_list(config_name)
17 cur.foreach(config_name, "device", function(s)
18 devices[#devices+1] = s[".name"]
25 function get_device(config_name,device_name)
30 cur.foreach(config_name, "device", function(s)
31 if s[".name"] == device_name then
40 function find_device(name,subsystem)
44 if subsystem == "iio" then
45 search_base = "/sys/bus/iio/devices"
46 for file in lfs.dir(search_base) do
47 if get_file_content(search_base.."/"..file.."/name") == name then
48 return search_base.."/"..file
51 elseif subsystem == "hwmon" then
52 search_base = "/sys/class/hwmon"
53 for file in lfs.dir(search_base) do
54 if get_file_content(search_base.."/"..file.."/device/name") == name then
55 return search_base.."/"..file.."/device"
64 function init_device(device,parameters,i2c_bus)
70 if device["module"] then
71 os.execute("modprobe "..device["module"])
74 if device["type"] then
76 devtype = split(device["type"],":")
78 subsystem = devtype[2]
80 if (bus == "i2c") and device["address"] and device["name"] then
82 local f = io.open("/sys/class/i2c-dev/i2c-"..i2c_bus.."/device/new_device","w")
84 io.write(device["name"].." "..device["address"])
89 device_path=find_device(device["name"],subsystem)
91 if device_path and device["set_param"] then
92 for key,record in pairs(device["set_param"]) do
93 setparam = split(record,":")
94 setpath = device_path.."/"..setparam[1]
96 local f = io.open(setpath,"w")
104 if device_path and device["parameter"] then
106 for key,record in pairs(device["parameter"]) do
108 getparam = split(record,":")
110 getparameter["path"] = device_path.."/"..getparam[1]
111 getparameter["name"] = getparam[2]
112 getscale = getparam[3]
113 getcorrection = getparam[4]
117 if not getcorrection then
120 getparameter["scale"] = tonumber(getscale)
121 getparameter["sensor"] = device["name"]:upper()
122 getparameter["correction"] = tonumber(getcorrection)
124 parameters[#parameters+1] = getparameter
134 function init(config_name)
138 i2c_bus = uci.get(config_name,"hardware","i2c_bus")
140 local devices = get_device_list(config_name)
142 for key,devname in pairs(devices) do
144 device = get_device(config_name,devname)
147 init_device(device,parameters,i2c_bus)
156 function get_parameter(parameter)
157 return tonumber(get_file_content(parameter["path"])) * parameter["scale"] + parameter["correction"]
160 function get_parameters(parameters)
162 for key,record in pairs(parameters) do
163 if not results[record["sensor"]] then
164 results[record["sensor"]] = {}
166 results[record["sensor"]][record["name"]] = get_parameter(record)
172 if not config_name then
173 config_name = "weathermon"
176 web_id = get_devid(config_name)
178 parameters = init(config_name)
180 local delay = uci.get(config_name,"process","delay")
182 local working_dir = uci.get(config_name,"process","working_dir")
184 lfs.mkdir(working_dir)
191 io.stdout:setvbuf('no')
194 if pcall(function () values = get_parameters(parameters) end) then
197 for key,record in pairs(values) do
198 records[web_id][key] = record
199 records[web_id]["timestamp"] = os.date("%Y-%m-%dT%H:%M:%S")
201 for key,value in pairs(values) do
202 value["device"] = key
203 print(json.encode(value))