def render(self, data={}, context={}): try: assert self.username is not None, "'MetofficeWOW.siteid' must be set" assert self.password is not None, "'MetofficeWOW.siteAuthenticationKey' must be set" assert self.period is not None, "'MetofficeWOW.period' must be set" self.logger.info("Initializing MetOffice WOW Upload (user %s)" % self.username) self.alive = True accu = AccumulatorDatasource() accu.slice = 'hour' accu.span = 2 accu.storage = self.storage accu.formulas = {'current': { 'temp' : LastFormula('temp'), 'hum' : LastFormula('hum'), 'pressure' : LastFormula('pressure'), 'wind' : LastFormula('wind'), 'wind_deg' : LastFormula('wind_dir'), 'rain' : SumFormula('rain'), 'utctime' : LastFormula('utctime') } } while self.alive: try: data = accu.execute()['current']['series'] index = len(data['lbl'])-1 args = { 'dateutc': data['utctime'][index].strftime('%Y-%m-%d %H:%M:%S'), # Some ARGs are hashed out here as Metoffice WOW needs them in the correct order or it will reject the post # I found that if I used the args they are in a random order so define them on the URL encode instead #'siteAuthenticationKey': str(self.password), #'softwaretype': "Wfrog", 'humidity': int(round(data['hum'][index])), 'tempf': str(CToF(data['temp'][index])), #'siteid': str(self.username), 'winddir': int(round(data['wind_deg'][index])), 'windspeedmph': str(MpsToMph(data['wind'][index])), 'baromin': str(HPaToInHg(data['pressure'][index])), 'rainin': str(MmToIn(data['rain'][index])) } self.logger.info("Publishing Metoffice WOW data: %s " % urlencode(args)) self._publish(args, 'wow.metoffice.gov.uk', '/automaticreading') except Exception, e: if (str(e) == "'NoneType' object has no attribute 'strftime'") or (str(e) == "a float is required"): self.logger.error('Could not publish: no valid values at this time. Retry next run...') else: self.logger.error('Got unexpected error. Retry next run. Error: %s' % e) time.sleep(self.period) except Exception, e: self.logger.exception(e) raise
def render(self, data={}, context={}): try: assert self.stationId is not None, "'wettercom.stationId' must be set" assert self.password is not None, "'wettercom.password' must be set" assert self.period is not None, "'wettercom.period' must be set" self.logger.info("Initializing Wetter.com (stationID %s)" % self.stationId) self.alive = True accu = AccumulatorDatasource() accu.slice = 'hour' accu.span = 1 accu.storage = self.storage accu.formulas = {'current': { 'temp' : LastFormula('temp'), 'hum' : LastFormula('hum'), 'hum2' : LastFormula('hum2'), 'pressure' : LastFormula('pressure'), 'wind' : LastFormula('wind'), 'wind_deg' : LastFormula('wind_dir'), 'rain' : LastFormula('rain'), 'localtime' : LastFormula('localtime'), 'utctime' : LastFormula('utctime'), 'dew_point' : LastFormula('dew_point') } } while self.alive: try: data = accu.execute()['current']['series'] self.logger.debug("Got data from accumulator: %s" % data) index = len(data['lbl'])-1 try: # If date is NoneType (see except for this try), we need to wait for a new value in wfrog.csv args = { 'sid' : 'wfrog', 'id' : str(self.stationId), 'pwd' : str(self.password), 'dt' : data['localtime'][index].strftime('%Y%m%d%H%M'), 'dtutc' : data['utctime'][index].strftime('%Y%m%d%H%M'), # we need both, dt (date) and dtutc (date in UTC). 'hu' : int(round(data['hum'][index])), 'te' : str(data['temp'][index]), 'dp' : str(data['dew_point'][index]), 'wd' : int(round(data['wind_deg'][index])), 'ws' : str(data['wind'][index]), 'pr' : str(data['pressure'][index]), 'pa' : str(data['rain'][index]) } if self.test: args["test"] = "true" self.logger.info('#!#!#!#!#!#!#!#!#!#! >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Running in test-mode!! The data wont be stored. #!#!#!#!#!#!#!#!#!#!') self.logger.debug("Publishing wettercom data: %s " % args) rawResponse = self._publish(args, 'interface.wetterarchiv.de', '/weather/') self.logger.debug('Server response: Code: %s Status: %s API-Answer: %s' % rawResponse) # Ok, now create an JSON-object response = json.loads(rawResponse[2]) # With the new API, checking for any error is very easy! if (response["status"] == "success"): self.logger.info('Data published successfully!') else: self.logger.error('Data publishing fails! Code: %s | Description: %s' % response["errorcode"], response["errormessage"]) except Exception, e: if (str(e) == "'NoneType' object has no attribute 'strftime'") or (str(e) == "a float is required"): self.logger.error('Could not publish: no valid values at this time. Retry next run...') else: self.logger.error('Got unexpected error. Retry next run. Error: %s' % e) raise except Exception, e: self.logger.exception(e) time.sleep(self.period) except Exception, e: self.logger.exception(e) raise
def render(self, data={}, context={}): try: assert self.id is not None, "'wunderground.id' must be set" assert self.password is not None, "'wunderground.password' must be set" assert self.period is not None, "'wunderground.period' must be set" self.real_time = self.real_time and self.period < 30 rtfreq = None if self.real_time: rtfreq = self.period self.logger.info( "Initializing Wunderground publisher (station %s)" % self.id) import weather.services self.publisher = weather.services.Wunderground( self.id, self.password, rtfreq) self.alive = True if not self.real_time: accu = AccumulatorDatasource() accu.slice = 'day' accu.span = 1 accu.storage = self.storage accu.formulas = { 'current': { 'temp': LastFormula('temp'), 'dew_point': LastFormula('dew_point'), 'hum': LastFormula('hum'), 'pressure': LastFormula('pressure'), 'wind': LastFormula('wind'), 'wind_deg': LastFormula('wind_dir'), 'gust': LastFormula('wind_gust'), 'gust_deg': LastFormula('wind_gust_dir'), 'rain_rate': LastFormula('rain_rate'), 'rain_fall': SumFormula('rain'), 'utctime': LastFormula('utctime') } } while self.alive: try: data = accu.execute()['current']['series'] index = len(data['lbl']) - 1 params = { # <float> pressure: in inches of Hg 'pressure': HPaToInHg(data['pressure'][index]), # <float> dewpoint: in Fahrenheit 'dewpoint': CToF(data['dew_point'][index]), # <float> humidity: between 0.0 and 100.0 inclusive 'humidity': data['hum'][index], # <float> tempf: in Fahrenheit 'tempf': CToF(data['temp'][index]), # <float> rainin: inches/hour of rain 'rainin': MmToIn(data['rain_rate'][index]), # <float> rainday: total rainfall in day (localtime) 'rainday': MmToIn(data['rain_fall'][index]), # <string> dateutc: date "YYYY-MM-DD HH:MM:SS" in GMT timezone 'dateutc': data['utctime'][index].strftime( '%Y-%m-%d %H:%M:%S'), # <float> windspeed: in mph 'windspeed': MpsToMph(data['wind'][index]), # <float> winddir: in degrees, between 0.0 and 360.0 'winddir': data['wind_deg'][index], # <float> windgust: in mph 'windgust': MpsToMph(data['gust'][index]), # <float> windgustdir: in degrees, between 0.0 and 360.0 'windgustdir': data['gust_deg'][index] } # Do not send parameters that are null (None). # from above only dateutc is a mandatory parameter. params = dict( filter(lambda (p, v): v, [(p, v) for p, v in params.iteritems()])) self.logger.info( "Publishing Wunderground data (normal server): %s " % str(params)) self.publisher.set(**params) response = self.publisher.publish() self.logger.info('Result Wunderground publisher: %s' % str(response)) except Exception, e: self.logger.exception(e) time.sleep(self.period) else:
def render(self, data={}, context={}): try: assert self.username is not None, "'openweathermap.id' must be set" assert self.password is not None, "'openweathermap.password' must be set" assert self.name is not None, "'openweathermap.name' must be set" assert self.latitude is not None, "'openweathermap.latitude' must be set" assert self.longitude is not None, "'openweathermap.longitude' must be set" assert self.altitude is not None, "'openweathermap.altitude' must be set" self.logger.info("Initializing openweathermap.com (user %s)" % self.username) self.alive = True accu = AccumulatorDatasource() accu.slice = 'day' accu.span = 1 accu.storage = self.storage accu.formulas = {'current': { 'temp' : LastFormula('temp'), 'hum' : LastFormula('hum'), 'pressure' : LastFormula('pressure'), 'dew_point' : LastFormula('dew_point'), 'wind' : LastFormula('wind'), 'wind_gust' : LastFormula('wind_gust'), 'wind_deg' : LastFormula('wind_dir'), 'rain' : SumFormula('rain'), 'utctime' : LastFormula('utctime') } } if self.send_uv: accu.formulas['current']['uv'] = LastFormula('uv') if self.send_radiation: accu.formulas['current']['solar_rad'] = LastFormula('solar_rad') accu24h = AccumulatorDatasource() accu24h.slice = 'hour' accu24h.span = 24 accu24h.storage = self.storage accu24h.formulas = {'current': {'rain': SumFormula('rain')} } accu60min = AccumulatorDatasource() accu60min.slice = 'minute' accu60min.span = 60 accu60min.storage = self.storage accu60min.formulas = {'current': {'rain': SumFormula('rain')} } last_timestamp = None while self.alive: try: data = accu.execute()['current']['series'] index = len(data['lbl'])-1 rain_1h = sum(map(lambda x: x if x is not None else 0, accu60min.execute()['current']['series']['rain'][:60])) rain_24h = sum(map(lambda x: x if x is not None else 0, accu24h.execute()['current']['series']['rain'][:24])) if last_timestamp == None or last_timestamp < data['utctime'][index]: last_timestamp = data['utctime'][index] args = { 'wind_dir': int(round(data['wind_deg'][index])), # grad 'wind_speed': str(data['wind'][index]), # mps 'wind_gust': str(data['wind_gust'][index]), # mps 'temp': str(data['temp'][index]), # grad C #'dewpoint': str(data['dew_point'][index]), # NOT WORKING PROPERLY 'humidity': int(round(data['hum'][index])), # relative humidity % 'pressure': str(data['pressure'][index]), # mb 'rain_1h': rain_1h, # mm 'rain_24h': rain_24h, # mm 'rain_today': str(data['rain'][index]), # mm 'lat': self.latitude, 'long': self.longitude, 'alt': self.altitude, 'name': self.name } if self.send_uv: args['uv'] = str(data['uv'][index]) if self.send_radiation: args['lum'] = str(data['solar_rad'][index]) self.logger.debug("Publishing openweathermap data: %s " % urllib.urlencode(args)) response = self._publish(args, 'openweathermap.org', '/data/post') if response[0] == 200: self.logger.info('Data published successfully') self.logger.debug('Code: %s Status: %s Answer: %s' % response) else: self.logger.error('Error publishing data. Code: %s Status: %s Answer: %s' % response) except Exception, e: if (str(e) == "'NoneType' object has no attribute 'strftime'") or (str(e) == "a float is required"): self.logger.error('Could not publish: no valid values at this time. Retry next run...') else: self.logger.exception(e) time.sleep(60) # each minute we check for new records to send to openweathermap except Exception, e: self.logger.exception(e) raise
def render(self, data={}, context={}): try: assert self.id is not None, "'wunderground.id' must be set" assert self.password is not None, "'wunderground.password' must be set" assert self.period is not None, "'wunderground.period' must be set" self.real_time = self.real_time and self.period < 30 rtfreq = None if self.real_time: rtfreq = self.period self.logger.info("Initializing Wunderground publisher (station %s)" % self.id) import weather.services self.publisher = weather.services.Wunderground(self.id, self.password, rtfreq) self.alive = True if not self.real_time: accu = AccumulatorDatasource() accu.slice = 'day' accu.span = 1 accu.storage = self.storage accu.formulas = {'current': { 'temp' : LastFormula('temp'), 'dew_point': LastFormula('dew_point'), 'hum' : LastFormula('hum'), 'pressure' : LastFormula('pressure'), 'wind' : LastFormula('wind'), 'wind_deg' : LastFormula('wind_dir'), 'gust' : LastFormula('wind_gust'), 'gust_deg' : LastFormula('wind_gust_dir'), 'rain_rate' : LastFormula('rain_rate'), 'rain_fall' : SumFormula('rain'), 'utctime' : LastFormula('utctime') } } while self.alive: try: data = accu.execute()['current']['series'] index = len(data['lbl'])-1 params = { # <float> pressure: in inches of Hg 'pressure' : HPaToInHg(data['pressure'][index]), # <float> dewpoint: in Fahrenheit 'dewpoint' : CToF(data['dew_point'][index]), # <float> humidity: between 0.0 and 100.0 inclusive 'humidity' : data['hum'][index], # <float> tempf: in Fahrenheit 'tempf' : CToF(data['temp'][index]), # <float> rainin: inches/hour of rain 'rainin' : MmToIn(data['rain_rate'][index]), # <float> rainday: total rainfall in day (localtime) 'rainday' : MmToIn(data['rain_fall'][index]), # <string> dateutc: date "YYYY-MM-DD HH:MM:SS" in GMT timezone 'dateutc' : data['utctime'][index].strftime('%Y-%m-%d %H:%M:%S'), # <float> windspeed: in mph 'windspeed' : MpsToMph(data['wind'][index]), # <float> winddir: in degrees, between 0.0 and 360.0 'winddir' : data['wind_deg'][index], # <float> windgust: in mph 'windgust' : MpsToMph(data['gust'][index]), # <float> windgustdir: in degrees, between 0.0 and 360.0 'windgustdir' : data['gust_deg'][index] } # Do not send parameters that are null (None). # from above only dateutc is a mandatory parameter. params = dict(filter(lambda (p,v): v, [(p,v) for p,v in params.iteritems()])) self.logger.info("Publishing Wunderground data (normal server): %s " % str(params)) self.publisher.set(**params) response = self.publisher.publish() self.logger.info('Result Wunderground publisher: %s' % str(response)) except Exception, e: self.logger.exception(e) time.sleep(self.period) else:
def render(self, data={}, context={}): try: assert self.username is not None, "'MetofficeWOW.siteid' must be set" assert self.password is not None, "'MetofficeWOW.siteAuthenticationKey' must be set" assert self.period is not None, "'MetofficeWOW.period' must be set" self.logger.info("Initializing MetOffice WOW Upload (user %s)" % self.username) self.alive = True accu = AccumulatorDatasource() accu.slice = 'hour' accu.span = 2 accu.storage = self.storage accu.formulas = { 'current': { 'temp': LastFormula('temp'), 'hum': LastFormula('hum'), 'pressure': LastFormula('pressure'), 'wind': LastFormula('wind'), 'wind_deg': LastFormula('wind_dir'), 'rain': SumFormula('rain'), 'utctime': LastFormula('utctime') } } while self.alive: try: data = accu.execute()['current']['series'] index = len(data['lbl']) - 1 args = { 'dateutc': data['utctime'][index].strftime('%Y-%m-%d %H:%M:%S'), # Some ARGs are hashed out here as Metoffice WOW needs them in the correct order or it will reject the post # I found that if I used the args they are in a random order so define them on the URL encode instead #'siteAuthenticationKey': str(self.password), #'softwaretype': "Wfrog", 'humidity': int(round(data['hum'][index])), 'tempf': str(CToF(data['temp'][index])), #'siteid': str(self.username), 'winddir': int(round(data['wind_deg'][index])), 'windspeedmph': str(MpsToMph(data['wind'][index])), 'baromin': str(HPaToInHg(data['pressure'][index])), 'rainin': str(MmToIn(data['rain'][index])) } self.logger.info("Publishing Metoffice WOW data: %s " % urlencode(args)) self._publish(args, 'wow.metoffice.gov.uk', '/automaticreading') except Exception, e: if (str(e) == "'NoneType' object has no attribute 'strftime'" ) or (str(e) == "a float is required"): self.logger.error( 'Could not publish: no valid values at this time. Retry next run...' ) else: self.logger.error( 'Got unexpected error. Retry next run. Error: %s' % e) time.sleep(self.period) except Exception, e: self.logger.exception(e) raise
def render(self, data={}, context={}): try: assert self.id is not None, "'pws.id' must be set" assert self.password is not None, "'pws.password' must be set" assert self.period is not None, "'pws.period' must be set" self.logger.info("Initializing PWS publisher (station %s)" % self.id) import weather.services self.publisher = weather.services.PwsWeather(self.id, self.password) self.alive = True accu = AccumulatorDatasource() accu.slice = "day" accu.span = 1 accu.storage = self.storage accu.formulas = { "current": { "temp": LastFormula("temp"), "dew_point": LastFormula("dew_point"), "hum": LastFormula("hum"), "pressure": LastFormula("pressure"), "wind": LastFormula("wind"), "wind_deg": LastFormula("wind_dir"), "gust": LastFormula("wind_gust"), "gust_deg": LastFormula("wind_gust_dir"), "rain_rate": LastFormula("rain_rate"), "rain_fall": SumFormula("rain"), "utctime": LastFormula("utctime"), } } accu_month = AccumulatorDatasource() accu_month.slice = "month" accu_month.span = 1 accu_month.storage = self.storage accu_month.formulas = {"current": {"rain_fall": SumFormula("rain")}} accu_year = AccumulatorDatasource() accu_year.slice = "year" accu_year.span = 1 accu_year.storage = self.storage accu_year.formulas = {"current": {"rain_fall": SumFormula("rain")}} while self.alive: try: data = accu.execute()["current"]["series"] index = len(data["lbl"]) - 1 data_month = accu_month.execute()["current"]["series"] index_month = len(data_month["lbl"]) - 1 data_year = accu_year.execute()["current"]["series"] index_year = len(data_year["lbl"]) - 1 params = { # <float> pressure: in inches of Hg "pressure": HPaToInHg(data["pressure"][index]), # <float> dewpoint: in Fahrenheit "dewpoint": CToF(data["dew_point"][index]), # <float> humidity: between 0.0 and 100.0 inclusive "humidity": data["hum"][index], # <float> tempf: in Fahrenheit "tempf": CToF(data["temp"][index]), # <float> rainin: inches/hour of rain "rainin": MmToIn(data["rain_rate"][index]), # <float> rainday: total rainfall in day (localtime) "rainday": MmToIn(data["rain_fall"][index]), # <float> rainmonth: total rainfall for month (localtime) "rainmonth": MmToIn(data_month["rain_fall"][index_month]), # <float> rainyear: total rainfall for year (localtime) "rainyear": MmToIn(data_year["rain_fall"][index_year]), # <string> dateutc: date "YYYY-MM-DD HH:MM:SS" in GMT timezone "dateutc": data["utctime"][index].strftime("%Y-%m-%d %H:%M:%S"), # <float> windgust: in mph "windgust": MpsToMph(data["gust"][index]), # <float> windspeed: in mph "windspeed": MpsToMph(data["wind"][index]), # <float> winddir: in degrees, between 0.0 and 360.0 "winddir": data["wind_deg"][index], } # Do not send parameters that are null (None), but don't remove zeroes (0.0) # from above only dateutc is a mandatory parameter. params = dict(filter(lambda (p, v): v is not None, [(p, v) for p, v in params.iteritems()])) self.logger.info("Publishing PWS data: %s " % str(params)) self.publisher.set(**params) response = self.publisher.publish() self.logger.info("Result PWS publisher: %s" % str(response)) except Exception, e: self.logger.exception(e) time.sleep(self.period) except Exception, e: self.logger.exception(e) raise