Добавлено разумности при определении состояния временной базы данных (теперь нужен...
[weathermon.git] / bin / weather-display
index 1e0993b31e2cd4c2c16514f906deeccab82be047..ef210ff5a68c17c383e07d64e25d992978d149cd 100755 (executable)
@@ -160,103 +160,115 @@ while true do
 
   local timestr = os.date(timestr_template)
 
 
   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
       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
-    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
-      local delta = alarmstr_len - alarmstr:len()
-      local before = math.floor(delta/2)
-      local after = delta - before
-      alarmstr = string.rep(" ",before)..alarmstr..string.rep(" ",after)
-    end
+      if alarmstr:len()>alarmstr_len then
+        alarmstr=alarmstr:sub(1,alarmstr_len)
+      elseif alarmstr:len()<alarmstr_len then
+        local delta = alarmstr_len - alarmstr:len()
+        local before = math.floor(delta/2)
+        local after = delta - before
+        alarmstr = string.rep(" ",before)..alarmstr..string.rep(" ",after)
+      end
 
 
-    sensstr = string.gsub(senstemplate,"%{.-%}", function (s) return printable[s:sub(2,s:len()-1)]; end)
+      sensstr = string.gsub(senstemplate,"%{.-%}", function (s) return printable[s:sub(2,s:len()-1)]; end)
 
 
-    if onoff and (level>=alarm_raise) then
-       data = alarmstr..sensstr
-    else
+      if onoff and (level>=alarm_raise) then
+       data = alarmstr..sensstr
+      else
         data = timestr..sensstr
         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
   end
-  
-  socket.sleep(1)
 
 end
 
 end