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
forecastio_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 os
22 from forecastiopy import *
23 import geocoder
24 
26  WeatherReporterBase,
27  RappUtilities,
28  RappError
29  )
30 
31 
32 ## @class ForecastIOReporter
33 # @brief ForecastIO weather reporter
35 
36  ## @brief Constructor
37  def __init__(self):
38  WeatherReporterBase.__init__(self)
39  key_path = os.path.join(os.environ['HOME'],
40  '.config/rapp_platform/api_keys/forecast.io')
41  with open(key_path) as key_fd:
42  self._api_key = key_fd.readline().strip()
43 
44  ## @brief Perfom request for weather data
45  #
46  # @param city [string] The city name
47  #
48  # @return fio [object] The weather report object
49  def _get_fio_object(self, city, metric):
50  geocode = geocoder.google(city)
51 
52  if metric == 0:
53  fio = ForecastIO.ForecastIO(self._api_key,
54  units=ForecastIO.ForecastIO.UNITS_US,
55  latitude=geocode.latlng[0],
56  longitude=geocode.latlng[1])
57  else:
58  fio = ForecastIO.ForecastIO(self._api_key,
59  units=ForecastIO.ForecastIO.UNITS_SI,
60  latitude=geocode.latlng[0],
61  longitude=geocode.latlng[1])
62 
63  return fio
64 
65  ## @brief Fetch the current weather
66  #
67  # @param req
68  # [rapp_platform_ros_communications::WeatherReporter::WeatherReporterCurrentSrv]
69  # The service request
70  #
71  # @return [dict] The server results
72  def fetch_current_weather(self, req):
73  fio = self._get_fio_object(req.city, req.metric)
74  if fio.has_currently() is True:
75  currently = FIOCurrently.FIOCurrently(fio)
76  return self._handle_current_weather_report(currently)
77  else:
78  raise RappError('Could not fetch current weather')
79 
80  ## @brief Fetch the current weather
81  #
82  # @param req
83  # [rapp_platform_ros_communications::WeatherReporter::WeatherReporterCurrentSrv]
84  # The service request
85  #
86  # @return [dict] The server results
87  def fetch_weather_forecast(self, req):
88  fio = self._get_fio_object(req.city, req.metric)
89  if fio.has_daily() is True:
90  daily = FIODaily.FIODaily(fio)
91  return self._handle_weather_forecast_report(daily)
92  else:
93  raise RappError('Could not fetch weather forecast')
94 
95  ## @brief Handles the server's response
96  #
97  # @param response [] The server's response to the request module.
98  #
99  # @return values [dict] The final values
101  response = {}
102 
103  response['date'] = str(report.time)
104  response['temperature'] = str(report.temperature)
105  response['weather_description'] = str(report.summary)
106  response['humidity'] = str(report.humidity)
107  response['visibility'] = str(report.visibility)
108  response['pressure'] = str(report.pressure)
109  response['wind_speed'] = str(report.windSpeed)
110  response['wind_temperature'] = ''
111  response['wind_direction'] = ''
112 
113  return response
114 
116  response = []
117  for day in xrange(0, report.days()):
118  fore = {}
119  fore['high_temperature'] = \
120  str(report.get_day(day)['temperatureMax'])
121  fore['low_temperature'] = \
122  str(report.get_day(day)['temperatureMin'])
123  fore['description'] = str(report.get_day(day)['summary'])
124  fore['date'] = str(report.get_day(day)['time'])
125  response.append(fore)
126  return response