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
weather_reporter_node.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 rospy
22 
23 from weather_reporter_factory import WeatherReporterFactory
24 
25 from rapp_utilities import RappUtilities
26 from rapp_exceptions import RappError
27 
28 from rapp_platform_ros_communications.msg import WeatherForecastMsg
29 
30 from rapp_platform_ros_communications.srv import (
31  WeatherReporterCurrentSrv,
32  WeatherReporterCurrentSrvResponse,
33  WeatherReporterForecastSrv,
34  WeatherReporterForecastSrvResponse
35  )
36 
37 
38 ## @class WeatherReporterNode
39 # @brief Evaluates weather conditions
40 class WeatherReporterNode(object):
41 
42  ## @brief Constructor
43  def __init__(self):
44  ## Factory that returns proper weather_reporter
45  self._weather_reporter_factory = WeatherReporterFactory()
46 
47  if rospy.has_param('rapp_weather_current_topic'):
48  srv_topic = \
49  rospy.get_param("rapp_weather_current_topic")
50  else:
51  srv_topic = ''
52  RappUtilities.rapp_print('Weather Reporter Current Weather topic not found!', 'ERROR')
53 
54  current_weather_service = rospy.Service(
55  srv_topic, WeatherReporterCurrentSrv, self.fetch_current_weather_srv_callback
56  )
57 
58  if rospy.has_param('rapp_weather_forecast_topic'):
59  srv_topic = \
60  rospy.get_param("rapp_weather_forecast_topic")
61  else:
62  srv_topic = ''
63  RappUtilities.rapp_print('Weather Reporter Forecast topic not found!', 'ERROR')
64 
65  forecast_weather_service = rospy.Service(
66  srv_topic, WeatherReporterForecastSrv, self.fetch_forecast_srv_callback
67  )
68 
69  ## @brief The callback to fetch current weather
70  #
71  # @param req
72  # [rapp_platform_ros_communications::WeatherReporter::WeatherReporterCurrentSrv]
73  # The service request
74  #
75  # @return res
76  # [rapp_platform_ros_communications::WeatherReporter::WeatherReporterCurrentSrvResponse]
77  # The service response
79  response = WeatherReporterCurrentSrvResponse()
80 
81  try:
82  w_reporter = self._weather_reporter_factory.\
83  select_weather_reporter(req.weather_reporter)
84  except RappError as err:
85  response.error = str(err)
86  return response
87  try:
88  results = w_reporter.fetch_current_weather(req)
89  except RappError as err:
90  response.error = str(err)
91  return response
92 
93  return self._create_current_service_response(results)
94 
95  ## @brief The callback to fetch weather forecast
96  #
97  # @param req
98  # [rapp_platform_ros_communications::WeatherReporter::WeatherReporterForecastSrv]
99  # The service request
100  #
101  # @return res
102  # [rapp_platform_ros_communications::WeatherReporter::WeatherReporterCurrentSrvResponse]
103  # The service response
105  response = WeatherReporterForecastSrvResponse()
106 
107  try:
108  w_reporter = self._weather_reporter_factory.\
109  select_weather_reporter(req.weather_reporter)
110  except RappError as err:
111  response.error = str(err)
112  return response
113  try:
114  results = w_reporter.fetch_weather_forecast(req)
115  except RappError as err:
116  response.error = str(err)
117  return response
118 
119  return self._create_forecast_service_response(results)
120 
121  ## @brief Creates proper response
122  #
123  # @param results [dict]
124  # The server results containing the weather information
125  #
126  # @return res
127  # [rapp_platform_ros_communications::WeatherReporter::WeatherReporterCurrentSrvResponse]
128  # The service response
130  response = WeatherReporterCurrentSrvResponse()
131 
132  response.date = result['date']
133  response.temperature = result['temperature']
134  response.weather_description = result['weather_description']
135  response.humidity = result['humidity']
136  response.visibility = result['visibility']
137  response.pressure = result['pressure']
138  response.wind_speed = result['wind_speed']
139  response.wind_temperature = result['wind_temperature']
140  response.wind_direction = result['wind_direction']
141 
142  return response
143 
145  response = WeatherReporterForecastSrvResponse()
146 
147  for res in result:
148  msg = WeatherForecastMsg()
149  msg.high_temperature = res['high_temperature']
150  msg.low_temperature = res['low_temperature']
151  msg.description = res['description']
152  msg.date = res['date']
153  response.forecast.append(msg)
154  return response
155 
156 if __name__ == "__main__":
157  rospy.init_node('WeatherReporter')
158  weather_reporter_node = WeatherReporterNode()
159  RappUtilities.rapp_print("Weather Reporter node initialized")
160  rospy.spin()
def fetch_current_weather_srv_callback
The callback to fetch current weather.
_weather_reporter_factory
Factory that returns proper weather_reporter.
def fetch_forecast_srv_callback
The callback to fetch weather forecast.