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
geolocator_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 geolocator_factory import GeolocatorFactory
24 
25 from rapp_utilities import RappUtilities
26 from rapp_exceptions import RappError
27 
28 from rapp_platform_ros_communications.srv import (
29  GeolocatorSrv,
30  GeolocatorSrvResponse
31  )
32 
33 
34 ## @class GeolocatorNode
35 # @brief Calculates geolocation via IP
36 class GeolocatorNode(object):
37 
38  ## @brief Constructor
39  def __init__(self):
40  ## Factory that returns proper geolocator
41  self._geolocator_factory = GeolocatorFactory()
42 
43  if rospy.has_param('rapp_geolocator_locate_topic'):
44  srv_topic = \
45  rospy.get_param("rapp_geolocator_locate_topic")
46  else:
47  srv_topic = ''
48  RappUtilities.rapp_print('Geolocator topic not found!', 'ERROR')
49 
50  fetch_service = rospy.Service(
51  srv_topic, GeolocatorSrv, self.fetch_location_srv_callback
52  )
53 
54  ## @brief The callback to calculate geolocation
55  #
56  # @param req
57  # [rapp_platform_ros_communications::Geolocator::GeolocatorSrv]
58  # The service request
59  #
60  # @return res
61  # [rapp_platform_ros_communications::Geolocator::GeolocatorSrvResponse]
62  # The service response
64  response = GeolocatorSrvResponse()
65 
66  try:
67  locator = \
68  self._geolocator_factory.select_geolocator(req.geolocator)
69  except RappError as err:
70  response.error = str(err)
71  return response
72  try:
73  results = locator.fetch_geolocation(req)
74  except RappError as err:
75  response.error = str(err)
76  return response
77 
78  return self._create_service_response(results)
79 
80  ## @brief The callback to geolocalize
81  #
82  # @param results [dict]
83  # The server results containing the location information
84  #
85  # @return res
86  # [rapp_platform_ros_communications::Geolocator::GeolocatorSrvResponse]
87  # The service response
88  def _create_service_response(self, result):
89  response = GeolocatorSrvResponse()
90  response.city = result['city']
91  response.country = result['country']
92  response.countryCode = result['countryCode']
93  response.regionName = result['regionName']
94  response.timezone = result['timezone']
95  response.zip = result['zip']
96  response.longtitude = result['longtitude']
97  response.latitude = result['latitude']
98  return response
99 
100 
101 if __name__ == "__main__":
102  rospy.init_node('Geolocator')
103  geolocator_node = GeolocatorNode()
104  RappUtilities.rapp_print("Geolocator node initialized")
105  rospy.spin()
def fetch_location_srv_callback
The callback to calculate geolocation.
_geolocator_factory
Factory that returns proper geolocator.
def _create_service_response
The callback to geolocalize.