RAPP Platform  v0.6.0
RAPP Platform is a collection of ROS nodes and back-end processes that aim to deliver ready-to-use generic services to robots
 All Classes Namespaces Files Functions Variables Macros
yweather_reporter.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- encode: utf-8 -*-
3 
4 #Copyright 2015 RAPP
5 
6 #Licensed under the Apache License, Version 2.0 (the "License");
7 #you may not use this file except in compliance with the License.
8 #You may obtain a copy of the License at
9 
10  #http://www.apache.org/licenses/LICENSE-2.0
11 
12 #Unless required by applicable law or agreed to in writing, software
13 #distributed under the License is distributed on an "AS IS" BASIS,
14 #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #See the License for the specific language governing permissions and
16 #limitations under the License.
17 
18 # Authors: Aris Thallas
19 # contact: aris.thallas@{iti.gr, gmail.com}
20 
21 import yweather
22 import urllib2
23 import xml
24 
26  WeatherReporterBase,
27  RappUtilities,
28  RappError
29  )
30 
31 
32 ## @class YWeatherReporter
33 # @brief Yweather weather reporter
35 
36  ## @brief Constructor
37  def __init__(self):
38  WeatherReporterBase.__init__(self)
39  self._client = yweather.Client()
40 
41 
42  def _fetch_city_id(self, city_str):
43  counter = 4
44  while counter != 0:
45  try:
46  city_id = self._client.fetch_woeid(city_str)
47  except urllib2.URLError as err:
48  RappUtilities.rapp_print(err, 'ERROR')
49  counter -= 1
50  except ( xml.etree.ElementTree.ParseError) as err:
51  RappUtilities.rapp_print(err, 'ERROR')
52  raise RappError(err)
53  else:
54  if city_id is None:
55  counter -= 1
56  else:
57  return city_id
58 
59  if city_id is None:
60  err = 'City provided is wrong or not supported'
61  else:
62  err = 'Yweather server error'
63  RappUtilities.rapp_print(err, 'ERROR')
64  raise RappError(err)
65 
66 
67  def _fetch_yweather_report(self, city_id, param_metric='True'):
68  try:
69  return self._client.fetch_weather(city_id, metric=param_metric)
70  except (urllib2.URLError, xml.etree.ElementTree.ParseError) as err:
71  RappUtilities.rapp_print(err, 'ERROR')
72  raise RappError(err)
73 
74  ## @brief Fetch the current weather
75  #
76  # @param req
77  # [rapp_platform_ros_communications::WeatherReporter::WeatherReporterCurrentSrv]
78  # The service request
79  #
80  # @return [dict] The server results
81  def fetch_current_weather(self, req):
82 
83  city_id = self._fetch_city_id(req.city)
84  weather_xml = self._fetch_yweather_report(city_id, req.metric)
85  return self._handle_current_weather_report(weather_xml)
86 
87  ## @brief Fetch the current weather
88  #
89  # @param req
90  # [rapp_platform_ros_communications::WeatherReporter::WeatherReporterCurrentSrv]
91  # The service request
92  #
93  # @return [dict] The server results
94  def fetch_weather_forecast(self, req):
95  city_id = self._fetch_city_id(req.city)
96  weather_xml = self._fetch_yweather_report(city_id, req.metric)
97  return self._handle_weather_forecast_report(weather_xml)
98 
99  ## @brief Handles the server's response
100  #
101  # @param response [] The server's response to the request module.
102  #
103  # @return values [dict] The final values
105  response = {}
106 
107  response['date'] = report['lastBuildDate']
108  response['temperature'] = report['condition']['temp']
109  response['weather_description'] = report['condition']['text']
110  response['humidity'] = report['atmosphere']['humidity']
111  response['visibility'] = report['atmosphere']['visibility']
112  response['pressure'] = report['atmosphere']['pressure']
113  response['wind_speed'] = report['wind']['speed']
114  response['wind_temperature'] = report['wind']['chill']
115  response['wind_direction'] = report['wind']['compass']
116 
117  return response
118 
120  response = []
121  for forecast in report['forecast']:
122  fore = {}
123  fore['high_temperature'] = forecast['high']
124  fore['low_temperature'] = forecast['low']
125  fore['description'] = forecast['text']
126  fore['date'] = forecast['date']
127  response.append(fore)
128  return response
def _handle_current_weather_report
Handles the server's response.