X-Git-Url: https://git.rvb.name/weathermon.git/blobdiff_plain/42bd3e809b0fa95d3a463462e242caf40d98bf79..decb6723ffef77eb4e5ac3ceea675cd7fe0450b0:/weathermon?ds=inline diff --git a/weathermon b/weathermon index 68798a1..8470f63 100755 --- a/weathermon +++ b/weathermon @@ -64,6 +64,7 @@ def open_port(path): if path[0] == "=": import subprocess + sleep(3) command=path[1:] try: command,args=command.split(' ',1) @@ -83,8 +84,14 @@ def read_port(ser): 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 "<>" finally: @@ -100,11 +107,15 @@ def read_loop(ser,callback): line=read_port(ser) if line=="<>": print "Reopening port..." + print line ser.close() if proc: - print "Terminating process..." - proc.terminate() - sleep(5) + try: + print "Terminating process..." + proc.terminate() + sleep(5) + finally: + None ser=open_port(path) if line: callback(line) @@ -347,68 +358,76 @@ def reconnect(): def main(): weather_mon() -try: +def init(): + + global dbhost,dbuser,dbpasswd,path,serialnum,logging; + 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: + 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()