8 socket = require "socket"
10 function split(str, pat)
11 local t = {} -- NOTE: use {n = 0} in Lua-5.0
12 local fpat = "(.-)" .. pat
14 local s, e, cap = str:find(fpat, 1)
16 if s ~= 1 or cap ~= "" then
20 s, e, cap = str:find(fpat, last_end)
22 if last_end <= #str then
23 cap = str:sub(last_end)
30 return (s:gsub("^%s*(.-)%s*$", "%1"))
33 function get_device_list(config_name)
38 cur.foreach(config_name, "device", function(s)
39 devices[#devices+1] = s[".name"]
46 function get_device(config_name,device_name)
51 cur.foreach(config_name, "device", function(s)
52 if s[".name"] == device_name then
61 function get_file_content(name)
62 local f = io.open(name,"r")
64 local content = trim(f:read("*all"))
72 function find_device(name,subsystem)
76 if subsystem == "iio" then
77 search_base = "/sys/bus/iio/devices"
78 for file in lfs.dir(search_base) do
79 if get_file_content(search_base.."/"..file.."/name") == name then
80 return search_base.."/"..file
83 elseif subsystem == "hwmon" then
84 search_base = "/sys/class/hwmon"
85 for file in lfs.dir(search_base) do
86 if get_file_content(search_base.."/"..file.."/device/name") == name then
87 return search_base.."/"..file.."/device"
96 function init_device(device,parameters,i2c_bus)
102 if device["module"] then
103 os.execute("modprobe "..device["module"])
106 if device["type"] then
108 devtype = split(device["type"],":")
110 subsystem = devtype[2]
112 if (bus == "i2c") and device["address"] and device["name"] then
114 local f = io.open("/sys/class/i2c-dev/i2c-"..i2c_bus.."/device/new_device","w")
116 io.write(device["name"].." "..device["address"])
121 device_path=find_device(device["name"],subsystem)
123 if device_path and device["set_param"] then
124 for key,record in pairs(device["set_param"]) do
125 setparam = split(record,":")
126 setpath = device_path.."/"..setparam[1]
128 local f = io.open(setpath,"w")
130 io.write(setparam[2])
136 if device_path and device["parameter"] then
138 for key,record in pairs(device["parameter"]) do
140 getparam = split(record,":")
142 getparameter["path"] = device_path.."/"..getparam[1]
143 getparameter["name"] = getparam[2]
144 getscale = getparam[3]
145 getcorrection = getparam[4]
149 if not getcorrection then
152 getparameter["scale"] = tonumber(getscale)
153 getparameter["sensor"] = device["name"]:upper()
154 getparameter["correction"] = tonumber(getcorrection)
156 parameters[#parameters+1] = getparameter
166 function init(config_name)
170 i2c_bus = uci.get(config_name,"hardware","i2c_bus")
172 local devices = get_device_list(config_name)
174 for key,devname in pairs(devices) do
176 device = get_device(config_name,devname)
179 init_device(device,parameters,i2c_bus)
188 function get_parameter(parameter)
189 return tonumber(get_file_content(parameter["path"])) * parameter["scale"] + parameter["correction"]
192 function get_parameters(parameters)
194 for key,record in pairs(parameters) do
195 if not results[record["sensor"]] then
196 results[record["sensor"]] = {}
198 results[record["sensor"]][record["name"]] = get_parameter(record)
204 if not config_name then
205 config_name = "weathermon"
208 parameters = init(config_name)
210 local delay = uci.get(config_name,"process","delay")
212 local working_dir = uci.get(config_name,"process","working_dir")
213 local dump_file = uci.get(config_name,"process","dump_file")
215 lfs.mkdir(working_dir)
223 values = get_parameters(parameters)
224 for key,record in pairs(values) do
227 print(json.encode(dump))
230 local f = io.open(dump_file,"w")
232 io.write(json.encode(values))