Package elisa :: Package plugins :: Package bad :: Package weather :: Module weather_activity
[hide private]
[frames] | no frames]

Source Code for Module elisa.plugins.bad.weather.weather_activity

  1  # -*- coding: utf-8 -*- 
  2  # Elisa - Home multimedia server 
  3  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
  4  # All rights reserved. 
  5  # 
  6  # This file is available under one of two license agreements. 
  7  # 
  8  # This file is licensed under the GPL version 3. 
  9  # See "LICENSE.GPL" in the root of this distribution including a special 
 10  # exception to use Elisa with Fluendo's plugins. 
 11  # 
 12  # The GPL part of Elisa is also available under a commercial licensing 
 13  # agreement from Fluendo. 
 14  # See "LICENSE.Elisa" in the root directory of this distribution package 
 15  # for details on that license. 
 16   
 17   
 18  __maintainer__ = 'Benjamin Kampmann <benjamin@fluendo.com>' 
 19   
 20  from elisa.core.common import application 
 21  from elisa.base_components.activity import Activity 
 22   
 23  from sun import suntime 
 24   
 25   
 26  from elisa.core.media_uri import MediaUri 
 27  from elisa.core import common 
 28   
 29  from weather_model import WeatherModel 
 30   
 31  import os 
 32  import time, calendar, datetime ## do we need all this 
 33  import csv ### and this? 
 34   
 35  try: 
 36      from elisa.extern.metar import Metar 
 37  except ImportError: 
 38      Metar = None 
 39   
 40  import string 
 41  #import rllib 
 42  import math 
 43   
 44   
 45   
 46  BASE_URL = "http://weather.noaa.gov/pub/data/observations/metar/stations" 
 47   
48 -class WeatherActivity(Activity):
49 """ The weather activity is retrieving the weather informations with metar 50 """ 51 config_doc = {'ICAO': 'Four letters designing your location; ' \ 52 'a complete list can be found at ' \ 53 'http://en.wikipedia.org/wiki/ICAO_airport_code' 54 } 55 56 default_config = {"ICAO": "LEBL" 57 } 58 59 60 # translation table between weather indications returned by pymetar to 61 # custom pixmaps; these are in data/weather/ and supposed to be png files 62 weather_to_pixmap = {"day": { 63 "sun": "clear", 64 "suncloud": "clear_cloud", 65 "cloud": "cloud", 66 "clear": "clear", 67 "fog": "fog", 68 "storm": "storm", 69 "rain": "rain", 70 "snow": "snow", 71 "unknown": "clear" 72 }, 73 "night": { 74 "sun": "night_clear", 75 "suncloud": "night_clear_cloud", 76 "cloud": "night_cloud", 77 "clear": "night_clear", 78 "fog": "night_fog", 79 "storm": "night_storm", 80 "rain": "night_rain", 81 "snow": "night_snow", 82 "unknown": "night_clear" 83 } 84 }
85 - def initialize(self):
86 Activity.initialize(self) 87 self._station_code = self.config["ICAO"] 88 stations_path = self.plugin.get_resource_file('/data/stations.csv') 89 90 self._location = "couldn't look it up" 91 # If there is no location name found, we've to display it 92 self._latitude = 0 93 self._longitude = 0 94 # FIXME: do not check each time when weather config is made 95 try: 96 stations = csv.reader(open(stations_path, 'rb')) 97 for row in stations: 98 if len(row) > 7: 99 if (row[3] == self._station_code): # Looking up the location name 100 self._location = row[2] + ',' + row[0] 101 self._latitude = float(row[6]) 102 self._longitude = float(row[7]) 103 break 104 except: 105 self.warning("The location couldn't be looked up."\ 106 "May be the station file %s is corrupt..." % 107 stations_path)
108
109 - def get_model(self):
110 # Create a MenuNodeModel first 111 registry = common.application.plugin_registry 112 menu_node = registry.create_component('base:menu_node_model') 113 menu_node.text = "Weather" 114 menu_node.theme_icon = "weather_icon" 115 menu_node.activity = self 116 117 return menu_node
118
119 - def retrieve_weather(self):
120 """ Fetches the weather information on the Internet thanks to metar 121 and fill in the weather report self._report 122 """ 123 self._report = None 124 url = "%s/%s.TXT" % (BASE_URL, self._station_code) 125 handler = application.media_manager.open(MediaUri(url)) 126 127 if not handler: 128 self.warning("No handler for http:// found") 129 return 130 131 report = '' 132 for line in handler.read(-1).splitlines(): 133 if line.startswith(self._station_code): 134 report = line.strip() 135 self._report = Metar.Metar(line) 136 break
137 # FIXME: There is something good needed 138
139 - def get_icon_from_weather(self, weather_report):
140 """ Returns a string for a icon corresponding to a given weather and time 141 142 weather_report: report to consider containing weather and time 143 """ 144 145 t = datetime.datetime.now() 146 147 rise_time = suntime('rise', float(self._latitude), 148 float(self._longitude), 1, time.localtime()[7]) 149 set_time = suntime('set', float(self._latitude), 150 float(self._longitude), 1, time.localtime()[7]) 151 current_time = time.localtime()[3:5] 152 153 night_time = (current_time > set_time) or (current_time < rise_time) 154 155 weather = None 156 157 for i in self._report.sky: 158 for j in i: 159 if (j == "SKC") or (j == "CLR"): 160 sky_weather = "sun" 161 elif (j == "FEW") or (j == "SCT"): 162 sky_weather = "suncloud" 163 elif (j == "FEW") or (j == "SCT"): 164 weather = "cloud" 165 break 166 167 for i in self._report.weather: 168 for j in i: 169 if (j == "TS"): 170 weather = "storm" 171 elif (j == "DZ") or (j == "RA"): 172 weather = "rain" 173 break 174 elif (j == "SN") or (j == "SG") or (j == "IC") \ 175 or (j == "PL") or (j == "GS"): 176 weather = "snow" 177 break 178 elif (j == "BR") or (j == "FG") or (j == "FU"): 179 weather = "fog" 180 181 if weather == None: 182 weather = "unknown" 183 184 if not night_time: 185 pixmap = self.weather_to_pixmap["day"][weather] 186 else: 187 pixmap = self.weather_to_pixmap["night"][weather] 188 189 return pixmap
190
191 - def get_weather_data(self):
192 """ Show/Hide a WeatherWidget containing an updated weather report """ 193 self.retrieve_weather() 194 195 # Labels initialisation 196 model = application.plugin_registry.create_component("weather:weather_model") 197 198 # Metar lib does not handle exceptions yet 199 if not self._report or not hasattr(self._report.temp, "string"): 200 return model 201 202 model.location = self._location 203 model.temp = self._report.temp.value(units='C') 204 205 # compute humidity: http://www.faqs.org/faqs/meteorology/temp-dewpoint/ 206 dp = self._report.dewpt.value(units="K") 207 t = self._report.temp.value(units="K") 208 e = 6.11 * math.exp((2500000 / 461.5) * ((1 / 273.15) - (1 / dp))) 209 es = 6.11 * math.exp((2500000 / 461.5) * ((1 / 273.15) - (1 / t))) 210 model.relHumidity = float((e / es) * 100) 211 212 if self._report.wind_speed is not None: 213 model.wind_speed = self._report.wind_speed.value(units="KMH") 214 215 if self._report.wind_dir is not None: 216 model.wind_dir = self._report.wind_dir.value() 217 218 if self._report.press is not None: 219 model.pressure = self._report.press.value(units="HPA") 220 221 model.dew = self._report.dewpt.value(units="C") 222 223 model.sky = self._report.sky_conditions("") 224 225 model.icon = self.get_icon_from_weather(self._report) 226 227 return model
228