Repository navigation
Expand file tree
/
Copy pathwhere-am-i.py
More file actions
executable file
·167 lines (136 loc) · 5.62 KB
/
Copy pathwhere-am-i.py
File metadata and controls
executable file
·167 lines (136 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
import sys
import dbus
import traceback
from gi.repository import Gtk
from gi.repository import GtkClutter
from gi.repository import GObject
from gi.repository import Champlain
from dbus.mainloop.glib import DBusGMainLoop
system_bus = None
GEOCLUE2_BUS_NAME = 'org.freedesktop.GeoClue2'
MANAGER_INTERFACE_NAME = GEOCLUE2_BUS_NAME + '.Manager'
CLIENT_INTERFACE_NAME = GEOCLUE2_BUS_NAME + '.Client'
LOCATION_INTERFACE_NAME = GEOCLUE2_BUS_NAME + '.Location'
PROPERTIES_INTERFACE_NAME = 'org.freedesktop.DBus.Properties'
def get_object(bus, name, object_path):
'''
This function returns a dbus proxy object located at the path
provided in object_path
'''
try:
return bus.get_object(name, object_path)
except dbus.DBusException:
traceback.print_exc()
sys.exit(1)
def get_interface(proxy_object, interface_name):
'''
This function takes a proxy object and returns the interface whose name
is provided in interface_name
'''
try:
return dbus.Interface(proxy_object, interface_name)
except dbus.DBusException:
traceback.print_exc()
sys.exit(1)
def get_property(properties_interface, interface_name, property_name):
'''
This function gets the value of a property. Parameters are -
the property interface, interface name where the property is located,
and the name of the property to be fetched
'''
try:
return properties_interface.Get(interface_name, property_name)
except dbus.DBusException:
traceback.print_exc()
sys.exit(1)
def set_property(properties_interface, interface_name, property_name, value):
'''
This function gets the value of a property. Parameters are -
the property interface, interface name where the property is located,
the name of the property to be fetched and the value to be set
'''
try:
return properties_interface.Set(interface_name, property_name, value)
except dbus.DBusException:
traceback.print_exc()
sys.exit(1)
def location_updated(old_path, new_path):
'''
When 'LocationUpdated' signal gets emitted, this function gets called
It fetches the location object located at the new path, and prints the
location details
'''
location_object = get_object(system_bus, GEOCLUE2_BUS_NAME, new_path)
location_properties = get_interface(location_object,
PROPERTIES_INTERFACE_NAME)
latitude = get_property(location_properties, LOCATION_INTERFACE_NAME,
'Latitude')
longitude = get_property(location_properties, LOCATION_INTERFACE_NAME,
'Longitude')
accuracy = get_property(location_properties, LOCATION_INTERFACE_NAME,
'Accuracy')
description = get_property(location_properties, LOCATION_INTERFACE_NAME,
'Description')
print "Latitude = " + str(latitude)
print "Longitude = " + str(longitude)
print "Accuracy = " + str(accuracy)
print "Description = " + str(description)
show_position_on_map(latitude, longitude)
def show_position_on_map(latitude, longitude):
GtkClutter.init([])
window = Gtk.Window()
window.connect("delete-event", Gtk.main_quit)
window.set_default_size(500, 350)
map_to_show = get_map(latitude, longitude)
window.add(map_to_show)
window.show_all()
Gtk.main()
def get_map(latitude, longitude):
view = Champlain.View()
view.set_kinetic_mode(True)
view.set_property("zoom-level", 12)
view.set_reactive(True)
view.set_size(500, 350)
view.center_on(latitude, longitude)
embed = GtkClutter.Embed.new()
embed.realize()
stage = embed.get_stage()
stage.add_actor(view)
return embed
def main():
print "This code will print your location and exit after 10 seconds"
global system_bus
# In order to make asynchronous calls, we need to setup an event loop
# Go ahead and try removing this, the 'connect_to_signal' method
# at the bottom won't work
dbus_loop = DBusGMainLoop(set_as_default = True)
# We connect to the system bus as GeoClue2 is located there
system_bus = dbus.SystemBus(mainloop = dbus_loop)
# We get the proxy object and then the interface
manager_object = get_object(system_bus, GEOCLUE2_BUS_NAME,
'/org/freedesktop/GeoClue2/Manager')
manager = get_interface(manager_object, MANAGER_INTERFACE_NAME)
# GetClient() returns the path to the newly created client object
client_path = manager.GetClient()
client_object = get_object(system_bus, GEOCLUE2_BUS_NAME, client_path)
# We set the 'DistanceThreshold' property before starting client.
# This property decides how often 'LocationUpdated' signal is emitted
# If the distance moved is below threshold, the signal won't be emitted
# We have set this to 1000 meters
client_properties = get_interface(client_object, PROPERTIES_INTERFACE_NAME)
set_property(client_properties, CLIENT_INTERFACE_NAME,
'DistanceThreshold', dbus.UInt32(1000))
client = get_interface(client_object, CLIENT_INTERFACE_NAME)
# Connect to the 'LocationUpdated' signal before starting the client
client.connect_to_signal('LocationUpdated', location_updated)
# Start receiving events about current location
client.Start()
loop = GObject.MainLoop()
# Quit after 10 seconds
GObject.timeout_add(10000, loop.quit)
loop.run()
client.Stop()
print "If no location was printed, run geoclue as root in another terminal -"
print "Path to the executable is in 'org.freedesktop.GeoClue2.service' file"
main()