Добавлен touch-файл для мониторинга степени живости, добавлены опции для протоколиров...
[weathermon.git] / weathermon
index 68798a10dfe4b35b64b8eaba1ac66b5a3bb4d339..19688cb9607ed4c95f086eb9d13195353a8b82fa 100755 (executable)
@@ -55,15 +55,17 @@ def find_port():
 
 def open_port(path):
 
 
 def open_port(path):
 
-  global proc
+  global proc, debug
 
 
-  print "Opening path "+path
+  if debug>0:
+    print "Opening path "+path
 
   if path == "-":
     return sys.stdin
 
   if path[0] == "=":
     import subprocess
 
   if path == "-":
     return sys.stdin
 
   if path[0] == "=":
     import subprocess
+    sleep(3)
     command=path[1:]
     try:
       command,args=command.split(' ',1)
     command=path[1:]
     try:
       command,args=command.split(' ',1)
@@ -83,8 +85,14 @@ def read_port(ser):
   try: 
     timeout_timer = Timer(timeout, thread.interrupt_main)
     timeout_timer.start()
   try: 
     timeout_timer = Timer(timeout, thread.interrupt_main)
     timeout_timer.start()
-    line = ser.readline()
-    return line.strip()
+
+    line=''
+    while line=='':
+      line = ser.readline()
+      line = line.strip()
+      
+    return line
+    
   except KeyboardInterrupt:
     return "<<TIMEOUT>>"
   finally:
   except KeyboardInterrupt:
     return "<<TIMEOUT>>"
   finally:
@@ -99,12 +107,18 @@ def read_loop(ser,callback):
     try:
       line=read_port(ser)
       if line=="<<TIMEOUT>>":
     try:
       line=read_port(ser)
       if line=="<<TIMEOUT>>":
-        print "Reopening port..."
+        if debug>0:
+          print "Reopening port..."
+          print line
         ser.close()
         if proc:
         ser.close()
         if proc:
-          print "Terminating process..."
-          proc.terminate()
-          sleep(5)
+          try:
+            if debug>0:
+              print "Terminating process..."
+            proc.terminate()
+            sleep(5)
+          finally:
+            None  
         ser=open_port(path)
       if line:
         callback(line)
         ser=open_port(path)
       if line:
         callback(line)
@@ -115,7 +129,8 @@ def read_loop(ser,callback):
 
 def print_log(str):
   global logging
 
 def print_log(str):
   global logging
-  print str
+  if debug>0:
+    print str
   if logging == "on":
     system("logger -t weathermon \""+str+"\"")
 
   if logging == "on":
     system("logger -t weathermon \""+str+"\"")
 
@@ -140,11 +155,14 @@ def submit_narodmon():
 
   queue=c.fetchall()
 
 
   queue=c.fetchall()
 
-  pprint(queue)
+  if debug>1:
+    pprint(queue)
 
   for (sensor,value) in queue:
     param[sensor] = value  
 
   for (sensor,value) in queue:
     param[sensor] = value  
-  pprint (param)
+  
+  if debug>1:
+    pprint (param)
 
   url = "http://narodmon.ru/post.php"
 
 
   url = "http://narodmon.ru/post.php"
 
@@ -197,11 +215,13 @@ def submit_owm():
 
   queue=c.fetchall()
 
 
   queue=c.fetchall()
 
-  pprint(queue)
+  if debug>1:
+    pprint(queue)
 
   for (sensor,value) in queue:
     params[sensor]=value
 
   for (sensor,value) in queue:
     params[sensor]=value
-  pprint (params)
+  if debug>1:  
+    pprint (params)
 
   try:
 
 
   try:
 
@@ -347,68 +367,80 @@ def reconnect():
 def main():
   weather_mon()
 
 def main():
   weather_mon()
 
-try:
+def init():
+
+  global dbhost,dbuser,dbpasswd,path,serialnum,logging,debug;
+  global timeout,baud,narmon,devid;
+  global owmuser,owmpasswd,owm_temp,owm_pres,owm_humi,owm_lat,owm_lon,owm_station;
+  global alarm_script;
 
 
-  cfg = ConfigParser.RawConfigParser(allow_no_value=True)
-  cfg.readfp(open('/etc/weathermon.conf'))
-  dbhost = cfg.get("mysql","host")
-  dbuser = cfg.get("mysql","user")
-  dbpasswd = cfg.get("mysql","passwd")
-  try:
-    path = cfg.get("serial","port");
-  except:
-    path = None
-  try:    
-    serialnum = cfg.get("serial","id")
-  except:
-    serialnum = None  
-  try:
-    logging = cfg.get("logging","enabled")
-  except:
-    logging = None
-  try:
-    timeout = int(cfg.get("serial","timeout"))
-  except:
-    timeout = 120
-  try:
-    baud = int(cfg.get("serial","baud"))
-  except:
-    baud = 57600
-  try:
-    narmon = cfg.get("narodmon","enable")
-  except:
-    narmon = 'off'  
-  try:
-    devid = cfg.get("narodmon","devid")
-  except:
-    devid = "{:0>12X}".format(getnode())   
-  try:  
-    owmuser = cfg.get("openweathermap","user")
-    owmpasswd = cfg.get("openweathermap",'passwd')
-  except:
-    owmuser = None  
-  if owmuser:
-    owm_temp = cfg.get("openweathermap",'temp')
-    owm_pres = cfg.get("openweathermap",'pres')
-    owm_humi = cfg.get("openweathermap",'humi')
-    owm_lat = cfg.get("openweathermap",'lat')
-    owm_lon = cfg.get("openweathermap",'lon')
-    owm_station = cfg.get("openweathermap",'station')
   try:
   try:
-    alarm_script = cfg.get("alarm","exec")
-    import subprocess
-  except:
-    alarm_script = None
-      
-  reconnect()
+
+    cfg = ConfigParser.RawConfigParser(allow_no_value=True)
+    cfg.readfp(open('/etc/weathermon.conf'))
+    dbhost = cfg.get("mysql","host")
+    dbuser = cfg.get("mysql","user")
+    dbpasswd = cfg.get("mysql","passwd")
+    try:
+      debug = cfg.get("logging","debug")
+    except:
+      debug = 0  
+    try:
+      path = cfg.get("serial","port");
+    except:
+      path = None
+    try:    
+      serialnum = cfg.get("serial","id")
+    except:
+      serialnum = None  
+    try:
+      logging = cfg.get("logging","enabled")
+    except:
+      logging = None
+    try:
+      timeout = int(cfg.get("serial","timeout"))
+    except:
+      timeout = 120
+    try:
+      baud = int(cfg.get("serial","baud"))
+    except:
+      baud = 57600
+    try:
+      narmon = cfg.get("narodmon","enable")
+    except:
+      narmon = 'off'  
+    try:
+      devid = cfg.get("narodmon","devid")
+    except:
+      devid = "{:0>12X}".format(getnode())   
+    try:  
+      owmuser = cfg.get("openweathermap","user")
+      owmpasswd = cfg.get("openweathermap",'passwd')
+    except:
+      owmuser = None  
+    if owmuser:
+      owm_temp = cfg.get("openweathermap",'temp')
+      owm_pres = cfg.get("openweathermap",'pres')
+      owm_humi = cfg.get("openweathermap",'humi')
+      owm_lat = cfg.get("openweathermap",'lat')
+      owm_lon = cfg.get("openweathermap",'lon')
+      owm_station = cfg.get("openweathermap",'station')
+    try:
+      alarm_script = cfg.get("alarm","exec")
+      import subprocess
+    except:
+      alarm_script = None
+        
+    reconnect()
  
  
-except:
+  except:
 
 
-  print_log("Cannot intialize system")
-  exit() 
+    print_log("Cannot intialize system")
+    exit() 
   
 if __name__ == "__main__":
   import sys
   reload(sys)
   sys.setdefaultencoding('utf-8')
   
 if __name__ == "__main__":
   import sys
   reload(sys)
   sys.setdefaultencoding('utf-8')
+  init()
   main()
   main()