3 from pprint import pprint
7 from urllib import urlencode
8 from StringIO import StringIO
22 system("logger -t weathermon \""+str+"\"")
24 def submit_narodmon():
28 param = { 'ID':devid }
33 select nm_id,avg(value) from meteo.ext_sensors e,meteo.sensor_values v
35 e.sensor_id=v.sensor_id and e.param_id=v.parameter_id
36 and v.timestamp>DATE_SUB(CURRENT_TIMESTAMP(),INTERVAL 15 MINUTE)
37 and e.nm_id is not null
38 group by e.sensor_id,e.param_id,e.nm_id,e.owm_id
47 for (sensor,value) in queue:
53 url = "http://narodmon.ru/post.php"
57 response_buffer = StringIO()
60 curl.setopt(curl.URL, url)
61 curl.setopt(curl.WRITEFUNCTION, response_buffer.write)
62 curl.setopt(curl.POST, 1)
63 curl.setopt(curl.POSTFIELDS, urlencode(param))
68 response_value = response_buffer.getvalue()
70 print_log('Narodmon response: '+response_value)
76 exc_type, exc_value, exc_traceback = sys.exc_info()
77 traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
78 traceback.print_exception(exc_type, exc_value, exc_traceback,
79 limit=2, file=sys.stdout)
86 url = "http://openweathermap.org/data/post"
87 params = {'name':owm_station, 'lat':owm_lat, 'long':owm_lon}
92 select owm_id,value from
94 SELECT sensor_id,parameter_id,avg(timestamp) timestamp,round(avg(value),1) value FROM meteo.sensor_values
96 timestamp>=date_sub(current_timestamp(), INTERVAL 15 MINUTE)
97 group by sensor_id,parameter_id
98 ) v,meteo.ext_sensors e
99 where v.sensor_id=e.sensor_id and v.parameter_id=e.param_id
100 and owm_id is not null
109 for (sensor,value) in queue:
116 response_buffer = StringIO()
119 curl.setopt(curl.URL, url)
120 curl.setopt(curl.USERPWD, '%s:%s' % (owmuser, owmpasswd))
121 curl.setopt(curl.WRITEFUNCTION, response_buffer.write)
122 curl.setopt(curl.POST, 1)
123 curl.setopt(curl.POSTFIELDS, urlencode(params))
128 response_value = response_buffer.getvalue()
130 print_log('Openweathermap response: '+response_value)
136 exc_type, exc_value, exc_traceback = sys.exc_info()
137 traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
138 traceback.print_exception(exc_type, exc_value, exc_traceback,
139 limit=2, file=sys.stdout)
148 global logging,debug;
150 global dbhost,dbuser,dbpasswd;
151 global owmuser,owmpasswd,owm_temp,owm_pres,owm_humi,owm_lat,owm_lon,owm_station;
155 cfg = ConfigParser.RawConfigParser(allow_no_value=True)
156 cfg.readfp(open('/etc/weathermon.conf'))
159 logging = cfg.get("logging","enabled")
163 debug = int(cfg.get("logging","debug"))
167 dbhost = cfg.get("mysql","host")
168 dbuser = cfg.get("mysql","user")
169 dbpasswd = cfg.get("mysql","passwd")
171 narmon = cfg.get("narodmon","enable")
175 devid = cfg.get("narodmon","devid")
177 devid = "{:0>12X}".format(getnode())
179 owmuser = cfg.get("openweathermap","user")
180 owmpasswd = cfg.get("openweathermap",'passwd')
185 owm_station=cfg.get("openweathermap","station")
186 owm_lat=cfg.get("openweathermap","lat")
187 owm_lon=cfg.get("openweathermap","lon")
192 database = MySQLdb.connect(host=dbhost,user=dbuser,passwd=dbpasswd,use_unicode=True,connect_timeout=10)
193 database.set_character_set('utf8')
194 c = database.cursor()
195 c.execute('SET NAMES utf8;')
196 print_log("Database connected...")
201 print_log("Cannot intialize system")
204 if __name__ == "__main__":
207 sys.setdefaultencoding('utf-8')