def run(self): while True: try: debug.info("Refreshing EC current observations weather") ecData = ECData(coordinates=(self.data.latlng)) self.data.wx_updated = True self.network_issues = False # except (requests.exceptions) as e: # #raise ValueError(e) # debug.error("Unable to get EC data error:{0}".format(e)) # self.data.wx_updated = False # self.network_issues = True # pass except Exception as e: debug.error("Unable to get EC data error:{0}".format(e)) self.data.wx_updated = False self.network_issues = True pass if self.data.wx_updated: #Set up units [temp, wind speed,precip, storm distance] #Use these eventhough some are included in data feed if self.data.config.weather_units == "metric": self.data.wx_units = [ "C", "kph", "mm", "miles", "hPa", "ca" ] else: self.data.wx_units = [ "F", "mph", "in", "miles", "MB", "us" ] curr_cond = ecData.conditions #Uncomment next line if you want to see what is being returned back from EC #debug.info(curr_cond) if self.time_format == "%H:%M": wx_timestamp = datetime.datetime.now().strftime( "%m/%d %H:%M") else: wx_timestamp = datetime.datetime.now().strftime( "%m/%d %I:%M %p") #Check current temperature to determine if using windchill or heat for apparent temperature #Make sure we have a value. Sometimes, the feed will not contain a value curr_temp = curr_cond.get("temperature").get("value", {}) curr_humidity = curr_cond.get("humidity").get("value", {}) if curr_humidity == None: curr_humidity = "0" wx_humidity = "N/A" else: wx_humidity = curr_humidity + "%" if curr_temp != None: curr_temp = float(curr_cond["temperature"]["value"]) check_windchill = 10.0 if self.data.config.weather_units == "imperial": curr_temp = temp_f(curr_temp) check_windchill = 50.0 if curr_temp < check_windchill: windchill = round( wind_chill( float(curr_cond["temperature"]["value"]), float(curr_cond["wind_speed"]["value"]), self.data.wx_units[1]), 1) wx_app_temp = str(windchill) + self.data.wx_units[0] else: humidex = round( cadhumidex(curr_temp, int(curr_humidity)), 1) wx_app_temp = str(humidex) + self.data.wx_units[0] wx_temp = str(round(curr_temp, 1)) + self.data.wx_units[0] else: wx_temp = "N/A" wx_app_temp = "N/A" if curr_cond.get("icon_code").get("value", "90") == None: wx_icon = '\uf07b' else: #Get condition and icon from dictionary for row in range(len(self.icons)): if int(self.icons[row]["Code"]) == int( curr_cond.get("icon_code").get("value", "90")): wx_icon = self.icons[row]['font'] break else: wx_icon = '\uf07b' wx_summary = curr_cond.get("condition").get("value", "N/A") if wx_summary == None: wx_summary = "Curr Cond N/A" curr_dewpoint = curr_cond.get("dewpoint").get("value", "0.0") if curr_dewpoint == None: curr_dewpoint = 0.0 else: curr_dewpoint = float(curr_dewpoint) if self.data.config.weather_units == "imperial": curr_dewpoint = round(temp_f(curr_dewpoint), 1) if curr_dewpoint == 0.0: wx_dewpoint = "N/A" else: wx_dewpoint = str(curr_dewpoint) + self.data.wx_units[0] self.data.wx_current = [ wx_timestamp, wx_icon, wx_summary, wx_temp, wx_app_temp, wx_humidity, wx_dewpoint ] winddir = degrees_to_direction( float(curr_cond.get("wind_bearing").get("value", "0"))) curr_windspeed = float( curr_cond.get("wind_speed").get("value", "0.0")) if self.data.config.weather_units == "imperial": curr_windspeed = round(wind_mph(curr_windspeed), 1) wx_windspeed = str( curr_windspeed) + " " + self.data.wx_units[1] if curr_cond.get("wind_gust").get("value", "0.0") != None: curr_windgust = float( curr_cond.get("wind_gust").get("value", "0.0")) if self.data.config.weather_units == "imperial": curr_windgust = round(wind_mph(curr_windgust), 1) wx_windgust = str( curr_windgust) + " " + self.data.wx_units[1] else: wx_windgust = "0.0 " + self.data.wx_units[1] wx_pressure = str( round(float(curr_cond.get("pressure").get("value", "0")), 1) * 10) + " " + self.data.wx_units[4] for row in range(len(self.icons)): if self.icons[row]["Description"].lower() == curr_cond.get( "tendency").get("value", "N/A"): wx_tendency = self.icons[row]['font'] break else: wx_tendency = '\uf07b' if curr_cond.get("visibility").get("value", "24") == None: if self.data.config.weather_units == "imperial": wx_visibility = "14.9 mi" else: wx_visibility = "24.1 km" else: if self.data.config.weather_units == "imperial": imp_visibility = round( float( curr_cond.get("visibility").get("value", "24")) * 0.621371, 1) wx_visibility = str(imp_visibility) + " mi" else: wx_visibility = curr_cond.get("visibility").get( "value", "24") + " " + curr_cond.get("visibility").get( "unit", "km") self.data.wx_curr_wind = [ wx_windspeed, winddir[0], winddir[1], wx_windgust, wx_pressure, wx_tendency, wx_visibility ] else: debug.error("Unable to get EC data error") debug.info(self.data.wx_current) debug.info(self.data.wx_curr_wind) # Run every 'x' minutes sleep(60 * self.weather_frequency)
def run(self): if self.data.config.weather_units == "metric": self.data.wx_units = ["C","kph","mm","miles","hPa","ca"] else: self.data.wx_units = ["F","mph","in","miles","MB","us"] while True: lat = self.data.latlng[0] lon = self.data.latlng[1] #Testing #lat = 36.50 #lon = -94.62 try: debug.info("Refreshing OWM current observations weather") obs = self.owm.weather_at_coords(lat,lon) self.network_issues = False self.data.wx_updated = True except pyown.exceptions as e: #raise ValueError(e) debug.error("Unable to get OWM data error:{0}".format(e)) self.data.wx_updated = False self.network_issues = True pass except Exception as e: debug.error("Unable to get OWM data error:{0}".format(e)) self.data.wx_updated = False self.network_issues = True pass if not self.network_issues: wx = obs.get_weather() if self.time_format == "%H:%M": wx_timestamp = datetime.datetime.now().strftime("%m/%d %H:%M") else: wx_timestamp = datetime.datetime.now().strftime("%m/%d %I:%M %p") owm_wxcode = wx.get_weather_code() if owm_wxcode in range(200,299): # Thunderstorm Class owm_icon = 200 elif owm_wxcode in range(300,399): # Drizzle Class owm_icon = 300 elif owm_wxcode in range(500,599): # Rain Class owm_icon = 500 elif owm_wxcode in range(600,699): # Rain Class owm_icon = 600 elif owm_wxcode in range(801,804): # Rain Class owm_icon = 801 else: owm_icon = owm_wxcode #Get condition and icon from dictionary for row in range(len(self.icons)): if int(self.icons[row]["OWMCode"]) == owm_icon: wx_icon = self.icons[row]['font'] break else: wx_icon = '\uf07b' wx_summary = wx.get_detailed_status().title() # Get wind information. owm_windspeed = wx.get_wind()['speed'] owm_windgust = wx.get_wind().get("gust",0.0) if self.data.config.weather_units != "metric": owm_windspeed = wx.get_wind(unit='miles_hour')['speed'] owm_windgust = wx.get_wind(unit='miles_hour').get("gust",0.0) else: # Convert m/s to km/h owm_windspeed = wind_kmph(owm_windspeed) owm_windgust = wind_kmph(owm_windgust) # Get icon and text wind direction owm_winddir = wx.get_wind().get("deg",0.0) winddir = degrees_to_direction(owm_winddir) wx_windgust = str(round(owm_windgust,1))+ self.data.wx_units[1] wx_windspeed = str(round(owm_windspeed,1)) + self.data.wx_units[1] # Get temperature and apparent temperature based on weather units if self.data.config.weather_units == "metric": owm_temp = wx.get_temperature(unit="celsius")['temp'] check_windchill = 10.0 else: owm_temp = wx.get_temperature(unit="fahrenheit")['temp'] check_windchill = 50.0 if float(owm_temp) < check_windchill: windchill = round(wind_chill(float(owm_temp),float(owm_windspeed),"mps"),1) wx_app_temp = str(windchill) + self.data.wx_units[0] wx_temp = str(round(owm_temp,1)) + self.data.wx_units[0] else: if self.data.config.weather_units == "metric": wx_app_temp = wx.get_humidex() else: wx_app_temp = wx.get_heat_index() if wx_app_temp == None: app_temp = usaheatindex(float(wx.get_temperature(unit="celsius")['temp']),wx.get_humidity()) wx_app_temp = str(round(temp_f(app_temp),1)) + self.data.wx_units[0] wx_temp = str(round(owm_temp,1)) + self.data.wx_units[0] wx_humidity = str(wx.get_humidity()) + "%" wx_dewpoint = str(round(dew_point(float(owm_temp),wx.get_humidity()),1))+ self.data.wx_units[0] wx_pressure = str(wx.get_pressure()['press']) + " " +self.data.wx_units[4] # Always set icon to N/A as owm doesn't return tendency for pressure wx_tendency = '\uf07b' vis_distance = wx.get_visibility_distance() if vis_distance == None: vis_distance = 24100 if self.data.config.weather_units == "metric": owm_visibility = round(vis_distance/1000,1) wx_visibility = str(owm_visibility) + " km" else: owm_visibility = round(vis_distance*0.000621371,1) wx_visibility = str(owm_visibility) + " mi" self.data.wx_current = [wx_timestamp,wx_icon,wx_summary,wx_temp ,wx_app_temp ,wx_humidity,wx_dewpoint] self.data.wx_curr_wind = [wx_windspeed,winddir[0],winddir[1],wx_windgust,wx_pressure,wx_tendency,wx_visibility] debug.info(self.data.wx_current) debug.info(self.data.wx_curr_wind) # Run every 'x' minutes sleep(60 * self.weather_frequency)