Улучшена обработка ошибок при работе с проводными датчиками.
[weathermon.git] / weathermon
index f7b2bd52aca3c26300bcde5d771de4b0d8de4ab0..66d6ac4390603399baeec4f11401fba71839a832 100755 (executable)
@@ -29,8 +29,10 @@ path = None
 timeout = 5
 
 external_submit_interval = 320
 timeout = 5
 
 external_submit_interval = 320
+owm_submit_interval = 320
 expire_interval = 1200
 submit_time = time()
 expire_interval = 1200
 submit_time = time()
+submit_time_owm = time()
 submit_queue = {}
 
 import MySQLdb
 submit_queue = {}
 
 import MySQLdb
@@ -109,7 +111,7 @@ def submit_narodmon(queue):
                                                                           
     response_value = response_buffer.getvalue()
                                                                               
                                                                           
     response_value = response_buffer.getvalue()
                                                                               
-    print_log('Content: '+response_value)
+    print_log('Narodmon response: '+response_value)
                                                                                   
     return True
                                                                                       
                                                                                   
     return True
                                                                                       
@@ -149,7 +151,7 @@ def submit_owm(queue):
     
     response_value = response_buffer.getvalue()
     
     
     response_value = response_buffer.getvalue()
     
-    print_log('Content: '+response_value)
+    print_log('Openweathermap response: '+response_value)
   
     return True
   
   
     return True
   
@@ -173,18 +175,21 @@ def purge_queue():
   
 def submit_data(sensor_type,sensor_id,sensor_param,param_value):
   global submit_time
   
 def submit_data(sensor_type,sensor_id,sensor_param,param_value):
   global submit_time
+  global submit_time_owm
+  global external_submit_interval
+  global owm_submit_interval
   global submit_queue
   c = database.cursor()
   c.execute('CALL meteo.submit_value(%s,%s,%s,%s,NULL)', (sensor_type,sensor_id,sensor_param,param_value))
   database.commit()
   submit_queue[sensor_type+'.'+sensor_id+'.'+sensor_param]={'val':param_value,'timestamp':time()}
   if time()>submit_time+external_submit_interval:
   global submit_queue
   c = database.cursor()
   c.execute('CALL meteo.submit_value(%s,%s,%s,%s,NULL)', (sensor_type,sensor_id,sensor_param,param_value))
   database.commit()
   submit_queue[sensor_type+'.'+sensor_id+'.'+sensor_param]={'val':param_value,'timestamp':time()}
   if time()>submit_time+external_submit_interval:
-    if submit_narodmon(submit_queue):
-      if owmuser:
-        submit_owm(submit_queue)
-      print_log('Purging queue...')
-      submit_time=time()
-      purge_queue()
+    submit_narodmon(submit_queue)
+    submit_time=time()
+  if owmuser and time()>submit_time_owm+owm_submit_interval:
+    submit_owm(submit_queue)
+    submit_time_owm=time()
+  purge_queue()
         
  
 def process_str(str):
         
  
 def process_str(str):
@@ -205,17 +210,19 @@ def process_str(str):
       sensor_id = None
       for rec in sens:
         key,value = rec.split('=')
       sensor_id = None
       for rec in sens:
         key,value = rec.split('=')
-        if key == 'TYPE':
-          sensor_type = value
-        elif key == 'ID':
-          sensor_id = value  
-        else:  
-          sensor[key] = value
+       value=value.strip()
+       if len(value)>0:
+          if key == 'TYPE':
+            sensor_type = value
+          elif key == 'ID':
+            sensor_id = value  
+          else:  
+            sensor[key] = value
       if sensor_type:    
         if not sensor_id:
           sensor_id='DEFAULT';    
       for key in sensor:
       if sensor_type:    
         if not sensor_id:
           sensor_id='DEFAULT';    
       for key in sensor:
-       if sensor[key] is not None:
+       if sensor[key]:
           print_log('Type = '+sensor_type+', ID = '+sensor_id+', Param = '+key+', Value = '+sensor[key])
           submit_data(sensor_type,sensor_id,key,sensor[key])
         else:
           print_log('Type = '+sensor_type+', ID = '+sensor_id+', Param = '+key+', Value = '+sensor[key])
           submit_data(sensor_type,sensor_id,key,sensor[key])
         else: