6 from os.path import isfile, join
 
   8 from termios import tcflush, TCIOFLUSH
 
  10 from time import sleep,time
 
  12 from uuid import getnode
 
  14 from hashlib import md5
 
  21 from urllib import urlencode
 
  22 from StringIO import StringIO
 
  24 searchpath = '/dev/serial/by-id/'
 
  28 external_submit_interval = 320
 
  29 expire_interval = 1200
 
  40   files = listdir(searchpath)
 
  43       return join(searchpath,f)
 
  48   ser = serial.Serial(path,baud,timeout)
 
  50     tcflush(ser,TCIOFLUSH);
 
  58 def read_loop(ser,callback):
 
  66     except KeyboardInterrupt:
 
  71 def submit_narodmon(queue):
 
  73   param = { 'ID':"{:X}".format(getnode())}
 
  76     value = submit_queue[sensor]['val']
 
  77     timestamp = submit_queue[sensor]['timestamp']
 
  78     digest = md5(sensor).hexdigest()[:18]
 
  79     param[digest] = value;
 
  83   url = "http://narodmon.ru/post.php"
 
  87     response_buffer = StringIO()
 
  90     curl.setopt(curl.URL, url)
 
  91     curl.setopt(curl.WRITEFUNCTION, response_buffer.write)   
 
  92     curl.setopt(curl.POST, 1)
 
  93     curl.setopt(curl.POSTFIELDS, urlencode(param))
 
  98     response_value = response_buffer.getvalue()
 
 100     print 'Content: ', response_value
 
 106     exc_type, exc_value, exc_traceback = sys.exc_info()
 
 107     traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
 
 108     traceback.print_exception(exc_type, exc_value, exc_traceback,
 
 109                               limit=2, file=sys.stdout)  
 
 112 def submit_owm(queue):
 
 114   url = "http://openweathermap.org/data/post"
 
 115   params = {'name':owm_station, 'lat':owm_lat, 'long':owm_lon}
 
 120       params['temp'] = queue[owm_temp]['val']
 
 121       params['pressure'] = queue[owm_pres]['val']
 
 122       params['humidity'] = queue[owm_humi]['val']
 
 126     response_buffer = StringIO()
 
 129     curl.setopt(curl.URL, url)
 
 130     curl.setopt(curl.USERPWD, '%s:%s' % (owmuser, owmpasswd))
 
 131     curl.setopt(curl.WRITEFUNCTION, response_buffer.write)
 
 132     curl.setopt(curl.POST, 1)
 
 133     curl.setopt(curl.POSTFIELDS, urlencode(params))
 
 138     response_value = response_buffer.getvalue()
 
 140     print 'Content: ', response_value
 
 146     exc_type, exc_value, exc_traceback = sys.exc_info()
 
 147     traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
 
 148     traceback.print_exception(exc_type, exc_value, exc_traceback,
 
 149                                   limit=2, file=sys.stdout)
 
 155   for key in submit_queue:
 
 156     if submit_queue[key]['timestamp'] < time()-expire_interval:
 
 157       print "Expired value for "+key
 
 162 def submit_data(sensor_type,sensor_id,sensor_param,param_value):
 
 165   c = database.cursor()
 
 166   c.execute('CALL meteo.submit_value(%s,%s,%s,%s)', (sensor_type,sensor_id,sensor_param,param_value))
 
 168   submit_queue[sensor_type+'.'+sensor_id+'.'+sensor_param]={'val':param_value,'timestamp':time()}
 
 169   if time()>submit_time+external_submit_interval:
 
 170     if submit_narodmon(submit_queue):
 
 172         submit_owm(submit_queue)
 
 173       print 'Purging queue...'
 
 178 def process_str(str):
 
 180     msg_type, msg_body = str.split(':')
 
 181     if msg_type == 'STATUS':
 
 182       print 'Status: ', msg_body
 
 183     elif msg_type == 'ERROR':
 
 184       print 'Error: ', msg_body
 
 185     elif msg_type == 'SENSOR':
 
 186       sens = msg_body.split(',')
 
 191         key,value = rec.split('=')
 
 202         print 'Type = ', sensor_type, ', ID = ', sensor_id, ', Param = ', key, ', Value = ', sensor[key]
 
 203         submit_data(sensor_type,sensor_id,key,sensor[key])
 
 205     print 'Exception processing...'
 
 218   ser = open_port(path)
 
 219   read_loop(ser,process_str)
 
 226     database = MySQLdb.connect(host=dbhost,user=dbuser,passwd=dbpasswd,use_unicode=True,connect_timeout=10)
 
 227     database.set_character_set('utf8')
 
 228     c = database.cursor()
 
 229     c.execute('SET NAMES utf8;')
 
 230     print "Database connected..."
 
 234     print "Error connecting database"
 
 242   cfg = ConfigParser.RawConfigParser(allow_no_value=True)
 
 243   cfg.readfp(open('/etc/weathermon.conf'))
 
 244   dbhost = cfg.get("mysql","host")
 
 245   dbuser = cfg.get("mysql","user")
 
 246   dbpasswd = cfg.get("mysql","passwd")
 
 247   serialnum = cfg.get("serial","id")
 
 248   owmuser = cfg.get("openweathermap","user")
 
 249   owmpasswd = cfg.get("openweathermap",'passwd')
 
 251     owm_temp = cfg.get("openweathermap",'temp')
 
 252     owm_pres = cfg.get("openweathermap",'pres')
 
 253     owm_humi = cfg.get("openweathermap",'humi')
 
 254     owm_lat = cfg.get("openweathermap",'lat')
 
 255     owm_lon = cfg.get("openweathermap",'lon')
 
 256     owm_station = cfg.get("openweathermap",'station')
 
 261   print "Cannot intialize system"
 
 264 if __name__ == "__main__":
 
 267   sys.setdefaultencoding('utf-8')