3 from pprint import pprint
7 from urllib import urlencode
8 from StringIO import StringIO
20 system("logger -t weathermon \""+str+"\"")
22 def submit_narodmon():
26 param = { 'ID':devid }
31 select nm_id,avg(value) from meteo.ext_sensors e,meteo.sensor_values v
33 e.sensor_id=v.sensor_id and e.param_id=v.parameter_id
34 and v.timestamp>DATE_SUB(CURRENT_TIMESTAMP(),INTERVAL 15 MINUTE)
35 and e.nm_id is not null
36 group by e.sensor_id,e.param_id,e.nm_id,e.owm_id
45 for (sensor,value) in queue:
51 url = "http://narodmon.ru/post.php"
55 response_buffer = StringIO()
58 curl.setopt(curl.URL, url)
59 curl.setopt(curl.WRITEFUNCTION, response_buffer.write)
60 curl.setopt(curl.POST, 1)
61 curl.setopt(curl.POSTFIELDS, urlencode(param))
66 response_value = response_buffer.getvalue()
68 print_log('Narodmon response: '+response_value)
74 exc_type, exc_value, exc_traceback = sys.exc_info()
75 traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
76 traceback.print_exception(exc_type, exc_value, exc_traceback,
77 limit=2, file=sys.stdout)
84 url = "http://openweathermap.org/data/post"
85 params = {'name':owm_station, 'lat':owm_lat, 'long':owm_lon}
90 select owm_id,value from
92 SELECT sensor_id,parameter_id,avg(timestamp) timestamp,round(avg(value),1) value FROM meteo.sensor_values
94 timestamp>=date_sub(current_timestamp(), INTERVAL 15 MINUTE)
95 group by sensor_id,parameter_id
96 ) v,meteo.ext_sensors e
97 where v.sensor_id=e.sensor_id and v.parameter_id=e.param_id
98 and owm_id is not null
107 for (sensor,value) in queue:
114 response_buffer = StringIO()
117 curl.setopt(curl.URL, url)
118 curl.setopt(curl.USERPWD, '%s:%s' % (owmuser, owmpasswd))
119 curl.setopt(curl.WRITEFUNCTION, response_buffer.write)
120 curl.setopt(curl.POST, 1)
121 curl.setopt(curl.POSTFIELDS, urlencode(params))
126 response_value = response_buffer.getvalue()
128 print_log('Openweathermap response: '+response_value)
134 exc_type, exc_value, exc_traceback = sys.exc_info()
135 traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
136 traceback.print_exception(exc_type, exc_value, exc_traceback,
137 limit=2, file=sys.stdout)
146 global logging,debug;
148 global dbhost,dbuser,dbpasswd;
149 global owmuser,owmpasswd,owm_temp,owm_pres,owm_humi,owm_lat,owm_lon,owm_station;
153 cfg = ConfigParser.RawConfigParser(allow_no_value=True)
154 cfg.readfp(open('/etc/weathermon.conf'))
157 logging = cfg.get("logging","enabled")
161 debug = int(cfg.get("logging","debug"))
165 dbhost = cfg.get("mysql","host")
166 dbuser = cfg.get("mysql","user")
167 dbpasswd = cfg.get("mysql","passwd")
169 narmon = cfg.get("narodmon","enable")
173 devid = cfg.get("narodmon","devid")
175 devid = "{:0>12X}".format(getnode())
177 owmuser = cfg.get("openweathermap","user")
178 owmpasswd = cfg.get("openweathermap",'passwd')
183 owm_station=cfg.get("openweathermap","station")
184 owm_lat=cfg.get("openweathermap","lat")
185 owm_lon=cfg.get("openweathermap","lon")
190 database = MySQLdb.connect(host=dbhost,user=dbuser,passwd=dbpasswd,use_unicode=True,connect_timeout=10)
191 database.set_character_set('utf8')
192 c = database.cursor()
193 c.execute('SET NAMES utf8;')
194 print_log("Database connected...")
199 print_log("Cannot intialize system")
202 if __name__ == "__main__":
205 sys.setdefaultencoding('utf-8')