For questions, read the onePK Forums
Contents
This tutorial demonstrates the Location Service Set functionality - how to get, set the location information and add listeners for location change events.
NOTE: Available on IOS-XE (ASR1K) only.
The code used in this tutorial is available in the LocationTutorial.py file located under <SDK Location>/python/tutorials/location/LocationTutorial.py.
These steps assume the application can connect properly to a network element. Please see the Connecting to a Network Element tutorial for information on how to make the initial connection.
Create a civic location object.
civic_location = CivicLocation(CivicType.LOCATION_CIVIC_POSTAL_CODE, '95035')
Create a geo location object.
geo_location = GeoLocation(37.41, -121.91, altitude = 26, alt_type = AltType.LOCATION_ALT_TYPE_METERS)
Create a location object.
location = Location(civic_list=civic_location_list, custom_list=custom_location_list, geo=geo_location)
Add a civic location to the location object.
location.add_civic_location(CivicType.LOCATION_CIVIC_TYPE_OF_PLACE, 'office')
Add a custom location to the location object.
intf_location.add_custom_location('rack', '367')
Set the location of the network element.
tutorial.network_element.set_location(location)
Set the location of a network interface.
intf.set_location(intf_location)
Create a location change Listener that listens to location change events.
class ExampleLocationChangeListener(LocationChangeListener): def handle_event(self, event, client_data): if event.has_subtype(LocationSubtype.LOCATION_TYPE_GEO): logger.info('Geo location information changed.') if event.has_subtype(LocationSubtype.LOCATION_TYPE_CUSTOM): logger.info('Custom location information changed.') if event.has_subtype(LocationSubtype.LOCATION_TYPE_CIVIC): logger.info('Civic location information changed.') logger.info('The location is changed to: \n' +str(event.location))
Next, create the filter to filter the required events. Add the event subtypes to only catch the location changes which you want.
loc_filter = LocationChangeFilter() loc_filter.add_subtype(LocationSubtype.LOCATION_TYPE_GEO) loc_filter.add_subtype(LocationSubtype.LOCATION_TYPE_CUSTOM) loc_filter.add_subtype(LocationSubtype.LOCATION_TYPE_CIVIC)
Apply the location change listener created earlier.
event_handle = location.add_change_listener(loc_listener, loc_filter, 'client_data')
To create the event modify the location on the Network element.
Get the location of the network element.
location = tutorial.network_element.get_location()
Remove the geo location information from the location object.
location.remove_geo_location()
Remove the custom location information list from the location object.
location.remove_custom_location_list()
Remove a particular civic location information from the location object.
location.remove_civic_location(CivicType.LOCATION_CIVIC_TYPE_OF_PLACE)
Finally, at the end of the tutorial, remove the location change Listener.
location.remove_change_listener(event_handle)
Congratulations! You are now able to use your onePK application to get, set location information and receive notification for location change events.
To try out this tutorial code by running it, you can find the code located in the directory <SDK Location>/tutorials/python/location/LocationTutorial.py