7 from pprint import pprint
 
   9 from termios import tcflush, TCIOFLUSH
 
  11 from time import sleep
 
  13 from uuid import getnode
 
  18 from urllib import urlencode
 
  19 from StringIO import StringIO
 
  28 from threading import Timer
 
  34   print "Opening path "+path
 
  36   ser = serial.Serial(path,1200);
 
  40   ser = serial.Serial(path,baud,timeout=timeout)
 
  42     tcflush(ser,TCIOFLUSH);
 
  48     timeout_timer = Timer(timeout, thread.interrupt_main)
 
  52   except KeyboardInterrupt:
 
  55     timeout_timer.cancel()
 
  57 def read_loop(ser,callback):
 
  65       if line=="<<TIMEOUT>>":
 
  66         print "Reopening port..."
 
  71     except KeyboardInterrupt:
 
  80     system("logger -t weathermon \""+str+"\"")
 
  82 def submit_data(stype,sid,param,value):
 
  84   global url,username,password
 
  86   params = {'stype':stype, 'sid':sid, 'param':param, 'value':value}
 
  92     response_buffer = StringIO()
 
  95     curl.setopt(curl.URL, url)
 
  97       curl.setopt(curl.USERPWD, '%s:%s' % (username, password))
 
  98     curl.setopt(curl.WRITEFUNCTION, response_buffer.write)
 
  99     curl.setopt(curl.POST, 1)
 
 100     curl.setopt(curl.POSTFIELDS, urlencode(params))
 
 105     response_value = response_buffer.getvalue()
 
 107     print_log('Server response: '+response_value)
 
 113     exc_type, exc_value, exc_traceback = sys.exc_info()
 
 114     traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
 
 115     traceback.print_exception(exc_type, exc_value, exc_traceback,
 
 116                                   limit=2, file=sys.stdout)
 
 119 def process_str(str):
 
 120   print_log("Received: "+str)
 
 122     msg_type, msg_body = str.split(':')
 
 126     if msg_type == 'STATUS':
 
 127       print_log('Status: '+msg_body)
 
 128     elif msg_type == 'ERROR':
 
 129       print_log('Error: '+ msg_body)
 
 130     elif msg_type == 'SENSOR':
 
 131       sens = msg_body.split(',')
 
 136         key,value = rec.split('=')
 
 150           print_log('Type = '+sensor_type+', ID = '+sensor_id+', Param = '+key+', Value = '+sensor[key])
 
 151           submit_data(sensor_type,sensor_id,key,sensor[key])
 
 153           print_log('Error: got empty parameter value for '+sensor_type+'.'+sensor_id+'.'+key)
 
 155     print_log('Exception processing...')
 
 156     exc_type, exc_value, exc_traceback = sys.exc_info()
 
 157     traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
 
 158     traceback.print_exception(exc_type, exc_value, exc_traceback,
 
 159                               limit=5, file=sys.stdout)  
 
 165   ser = open_port(path)
 
 166   read_loop(ser,process_str)
 
 173   cfg = ConfigParser.RawConfigParser(allow_no_value=True)
 
 174   cfg.readfp(open('/etc/weathermon.conf'))
 
 175   url = cfg.get("web","url")
 
 176   path = cfg.get("serial","port");
 
 178     username = cfg.get("web","user");
 
 179     password = cfg.get("web","password");
 
 184     logging = cfg.get("logging","enabled")
 
 188     timeout = int(cfg.get("serial","timeout"))
 
 192     baud = int(cfg.get("serial","baud"))
 
 196     devid = cfg.get("web","devid")
 
 198     devid = "{:0>12X}".format(getnode())   
 
 202   print_log("Cannot intialize system")
 
 205 if __name__ == "__main__":
 
 208   sys.setdefaultencoding('utf-8')