From d63010ef6c3cc76ac2a035c0249c87a78e38d980 Mon Sep 17 00:00:00 2001 From: Roman Bazalevsky Date: Tue, 13 Nov 2018 10:55:29 +0300 Subject: [PATCH] =?utf8?q?=D0=9E=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA?= =?utf8?q?=D0=B0=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D1=8F?= =?utf8?q?=20=D0=BE=D1=82=20=D0=BF=D1=80=D0=BE=D1=86=D0=B5=D1=81=D1=81?= =?utf8?q?=D0=BE=D0=B2=20=D0=B7=D0=B0=D0=B2=D0=B5=D1=80=D0=BD=D1=83=D1=82?= =?utf8?q?=D0=B0=20=D0=B2=20pcall=20=D0=B4=D0=BB=D1=8F=20=D0=B1=D0=BE?= =?utf8?q?=D0=BB=D1=8C=D1=88=D0=B5=D0=B9=20=D0=BF=D0=B0=D0=B4=D0=B5=D0=B6?= =?utf8?q?=D0=B5=D1=83=D1=81=D1=82=D0=BE=D0=B9=D1=87=D0=B8=D0=B2=D0=BE?= =?utf8?q?=D1=81=D1=82=D0=B8=20=D0=B2=20=D1=81=D0=BB=D1=83=D1=87=D0=B0?= =?utf8?q?=D0=B5=20=D0=BF=D1=80=D0=B8=D0=B5=D0=B7=D0=B6=D0=B0=D0=BD=D0=B8?= =?utf8?q?=D1=8F=20=D0=BD=D0=B5=D0=BA=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82?= =?utf8?q?=D0=BD=D1=8B=D1=85=20=D1=81=D1=82=D1=80=D0=BE=D0=BA.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- bin/weather-display | 168 ++++++++++++++++++++++++-------------------- bin/weathermon | 31 ++++---- bin/weathermon-iio | 21 +++--- 3 files changed, 120 insertions(+), 100 deletions(-) diff --git a/bin/weather-display b/bin/weather-display index 1e0993b..ef210ff 100755 --- a/bin/weather-display +++ b/bin/weather-display @@ -160,103 +160,115 @@ while true do local timestr = os.date(timestr_template) - local mod = lfs.attributes(watch_file,"modification") - local muted_mod = lfs.attributes(mute_file,"modification") - local muted_beep = muted_mod and (muted_mod > (os.time() - mute_time)) - - if mod then + pcall(function () + + local mod = lfs.attributes(watch_file,"modification") + local muted_mod = lfs.attributes(mute_file,"modification") + local muted_beep = muted_mod and (muted_mod > (os.time() - mute_time)) - if last < mod then - last = mod - res,sensor_data = pcall(function () - local f = io.open(watch_file,"r") - content = f:read("*all") - io.close(f) - return json.decode(content) - end) + if mod then + + if last < mod then + last = mod + res,sensor_data = pcall(function () + local f = io.open(watch_file,"r") + content = f:read("*all") + io.close(f) + return json.decode(content) + end) - write_file(w_led,w_engage) + write_file(w_led,w_engage) - else + else - write_file(w_led,w_disengage) + write_file(w_led,w_disengage) - end - - local values = {} - local printable = {} - - for sensor,sensor_params in pairs(sensor_data[web_id]) do - if sensor ~= "timestamp" then - for param,value in pairs(sensor_params) do - local name = sensor.."."..param - values[name]=value - if a_formats[name] then - printable[name]=string.format("%"..a_formats[name][1],value*a_formats[name][2]) - end + end + + local values = {} + local printable = {} + + for sensor,sensor_params in pairs(sensor_data[web_id]) do + if sensor ~= "timestamp" then + for param,value in pairs(sensor_params) do + local name = sensor.."."..param + values[name]=value + if a_formats[name] then + printable[name]=string.format("%"..a_formats[name][1],value*a_formats[name][2]) + end + end + end + end + + level = 1 + alarms = "" + + if not muted_beep then + for key,value in pairs(values) do + value_level = check_limit(key,value,limits) + if value_level > level then + level = value_level + alarms = a_names[key] + elseif value_level == level then + alarms = alarms.." "..a_names[key] + end end end - end - - level = 1 - alarms = "" - - if not muted_beep then - for key,value in pairs(values) do - value_level = check_limit(key,value,limits) - if value_level > level then - level = value_level - alarms = a_names[key] - elseif value_level == level then - alarms = alarms.." "..a_names[key] - end - end - end - leds_engage = {} + leds_engage = {} - for key,value in pairs(a_engage[level]) do - leds_engage[value] = a_engage_mode[level] - end - - for led,mode in pairs(a_leds) do - if leds_engage[led] then - write_file(led,leds_engage[led]) - else - write_file(led,mode) + for key,value in pairs(a_engage[level]) do + leds_engage[value] = a_engage_mode[level] + end + + for led,mode in pairs(a_leds) do + if leds_engage[led] then + write_file(led,leds_engage[led]) + else + write_file(led,mode) + end end - end - alarms = trim(alarms) - alarmstr = a_levels[level]..": "..alarms + alarms = trim(alarms) + alarmstr = a_levels[level]..": "..alarms - if alarmstr:len()>alarmstr_len then - alarmstr=alarmstr:sub(1,alarmstr_len) - elseif alarmstr:len()alarmstr_len then + alarmstr=alarmstr:sub(1,alarmstr_len) + elseif alarmstr:len()=alarm_raise) then - data = alarmstr..sensstr - else + if onoff and (level>=alarm_raise) then + data = alarmstr..sensstr + else data = timestr..sensstr - end + end - if old_data ~= data then + if old_data ~= data then - old_data = data - print(data) - write_file(out_file,data) + old_data = data + print(data) + write_file(out_file,data) - end + end + end + + end) + + local t = os.date("*t",os.time()+60) + t["sec"] = 0 + local sec = os.time(t)-os.time() + if sec>0 then + if sec>5 then + sec=5 + end + socket.sleep(sec) end - - socket.sleep(1) end diff --git a/bin/weathermon b/bin/weathermon index 03fad42..6980cc8 100755 --- a/bin/weathermon +++ b/bin/weathermon @@ -388,21 +388,28 @@ end records = {} while 1 do + line=serialin:read("*l") + if line == nil then break end - printLog("Received: "..line); - if startswith(line,'{') then - processJson(line) - else - processLine(line) - end - if dump_file then - local f = io.open(dump_file,"w") - io.output(f) - io.write(json.encode(records)) - io.close(f) - end + pcall(function () + + printLog("Received: "..line) + if startswith(line,'{') then + processJson(line) + else + processLine(line) + end + + if dump_file then + local f = io.open(dump_file,"w") + io.output(f) + io.write(json.encode(records)) + io.close(f) + end + end) + end diff --git a/bin/weathermon-iio b/bin/weathermon-iio index 7fe5f68..df32c3a 100755 --- a/bin/weathermon-iio +++ b/bin/weathermon-iio @@ -189,16 +189,17 @@ if not delay then end while true do - values = get_parameters(parameters) - records = {} - records[web_id] = {} - for key,record in pairs(values) do - records[web_id][key] = record - records[web_id]["timestamp"] = os.date("%Y-%m-%dT%H:%M:%S") - end - for key,value in pairs(values) do - value["device"] = key - print(json.encode(value)) + if pcall(function () values = get_parameters(parameters) end) then + records = {} + records[web_id] = {} + for key,record in pairs(values) do + records[web_id][key] = record + records[web_id]["timestamp"] = os.date("%Y-%m-%dT%H:%M:%S") + end + for key,value in pairs(values) do + value["device"] = key + print(json.encode(value)) + end end socket.sleep(delay) end -- 2.34.1