Корректное закрытие курсора после выполнения запроса.
[weathermon.git] / bin / weather-display
index 1e0993b31e2cd4c2c16514f906deeccab82be047..58b8ae528266457d7f74677f1ccc71022066a96c 100755 (executable)
@@ -98,7 +98,7 @@ if not config_name then
   config_name = "weathermon"
 end
 
-print("loading config...")
+web_id = get_devid(config_name)
 
 local a_levels, a_engage, a_engage_mode, a_disengage_mode, levels_idx = get_levels_list(config_name)
 
@@ -152,111 +152,121 @@ if not alarmstr_len then
 end
 alarmstr_len=tonumber(alarmstr_len)
 
-web_id = uci.get(config_name,"web","devid")
-
 while true do
 
   onoff = not onoff
 
   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
-      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
-    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