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
+ sleep(3)
command=path[1:]
try:
command,args=command.split(' ',1)
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:
try:
line=read_port(ser)
if line=="<<TIMEOUT>>":
- print "Reopening port..."
+ if debug>0:
+ print "Reopening port..."
+ print line
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)
def print_log(str):
global logging
- print str
+ if debug>0:
+ print str
if logging == "on":
system("logger -t weathermon \""+str+"\"")
queue=c.fetchall()
- pprint(queue)
+ if debug>1:
+ pprint(queue)
for (sensor,value) in queue:
param[sensor] = value
- pprint (param)
+
+ if debug>1:
+ pprint (param)
url = "http://narodmon.ru/post.php"
queue=c.fetchall()
- pprint(queue)
+ if debug>1:
+ pprint(queue)
for (sensor,value) in queue:
params[sensor]=value
- pprint (params)
+ if debug>1:
+ pprint (params)
try:
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:
- 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')
+ init()
main()