aboutsummaryrefslogtreecommitdiff
path: root/src/Location.vala
blob: 5d64e150c00da1c4a5b8896d7dea1985604ec796 (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
FIXME: Remember to unref the needed things!

*/
using ContextProvider;

namespace ContextKit {

	namespace Location2 {
		// Data type for the locally stored location data
		struct LocationData {
			public double latitude;
			public double longitude;
			public double altitude;

			public bool latlong_valid;
			public bool altitude_valid;
		}

		public class Provider : GLib.Object, ContextProvider.Provider {
			ProviderMixins.SubscriberList subscribed;

			// contextd keys this plugin takes care of
			const Key[] keys = {
				{
					"Context.Environment.Location.latitude",
					ValueType.DOUBLE
				},
				{
					"Context.Environment.Location.longitude",
					ValueType.DOUBLE
				},
				{
					"Context.Environment.Location.altitude",
					ValueType.DOUBLE
				}
			};

			StringSet location_keys;

			StringSet subscribed_keys = new StringSet();

			// Stores the last known location
			static LocationData current_location;

			// Device for using liblocation
			Location.GPSDevice m_device;

			void subscription_removed (ProviderMixins.SubscriberList l, Subscriber s) {
				// TODO: Check here whether the connection to the loc service should be closed? 

				// FIXME: Do we have to remove it from the subscription list manually? Should the subscription list be part of the superclass?
			}

			/* Constructor
			Connect the liblocation signals to correct functions.
			Possibly: Indicate to liblocation that we're intrested in location data and start listening.
			*/
			public Provider () {

				// Create plugin-related data structures
				subscribed = new ProviderMixins.SubscriberList();
				subscribed.removed += subscription_removed;

				location_keys = new StringSet.from_array (new string[] {
						"Context.Environment.Location.latitude",
						"Context.Environment.Location.longitude",
						"Context.Environment.Location.altitude"
						});

				// Invalidate the initial data
				current_location.latlong_valid = false;
				current_location.altitude_valid = false;

				// Fetch an instance of the LocationGPSDControl
				Location.GPSDControl control;
				control = control.get_default();

				// Connect signals from LocationGPSDControl
				control.error += CB_Error;
				control.gpsd_running += CB_Running;
				control.gpsd_stopped += CB_Stopped;

				// Indicate that we want to use only ACWP to avoid starting the GPS if it is not already running.
				control.preferred_method = Location.Method.ACWP;

				control.start(); // FIXME: This is only for testing.

				// Create a LocationGPSDevice
				m_device = new Location.GPSDevice();

				// Connect signals from device
				m_device.changed += CB_Changed;
				m_device.connected += CB_Connected;
				m_device.disconnected += CB_Disconnected;

				m_device.start(); // FIXME: This is only for testing

				// FIXME: Do we need to unref control? Or does Vala handle it automatically?

			}

			// Destructor
			~Provider() {
				// FIXME: Deleting / freeing / ... the device? How? Or does Vala handle it automatically?
			}

			// Handling signals from GPS control
			static void CB_Error(Location.GPSDControl? control) {
				debug("Error");
			}

			static void CB_Running(Location.GPSDControl? control) {
				debug("Running");
			}

			static void CB_Stopped(Location.GPSDControl? control) {
				debug("Stopped");
			}

			// Handling signals from GPS device
			void CB_Changed(Location.GPSDevice? device) {

				bool values_changed = false;
				if (device != null && device.fix != null) { 
					debug("Changed %f %f %f", device.fix.latitude, device.fix.longitude, device.fix.altitude);
					if ((device.fix.fields & Location.GPS_DEVICE_LATLONG_SET) != 0) {

						if (current_location.latitude != device.fix.latitude) {
							values_changed = true;
						}
						if (current_location.longitude != device.fix.longitude) {
							values_changed = true;
						}
						current_location.latitude = device.fix.latitude;
						current_location.longitude = device.fix.longitude;
						current_location.latlong_valid = true;
					}

					if ((device.fix.fields & Location.GPS_DEVICE_ALTITUDE_SET) != 0) {
						if (current_location.altitude != device.fix.altitude) {
							values_changed = true;
						}

						current_location.altitude = device.fix.altitude;
						current_location.altitude_valid = true;
					}
					// TODO: Tresholds? If the values change only a little, ignore the change?
				}

				HashTable<string, TypedVariant?> ret = new HashTable<string, TypedVariant?> (str_hash,str_equal);

				if (subscribed_keys.is_member ("Context.Environment.Location.latitude")) {
					insert_double_key("Context.Environment.Location.latitude", ret, current_location.latitude, current_location.latlong_valid);
				}

				if (subscribed_keys.is_member ("Context.Environment.Location.longitude")) {
					insert_double_key("Context.Environment.Location.longitude", ret, current_location.longitude, current_location.latlong_valid);
				}

				if (subscribed_keys.is_member ("Context.Environment.Location.altitude")) {
					insert_double_key("Context.Environment.Location.altitude", ret, current_location.altitude, current_location.altitude_valid);
				}

				// TODO: Should we check whether we need the location service any more?
				// If we stop the connecton, we won't be able to answer future Get:s accurately.

				/* Note: We always emit the same kind of changed signal, not depending on which values have
				actually changed. */

				if (values_changed) {
					// Emit the "Changed" signal
					for (int i=0; i < subscribed.size; i++) {
						weak Subscriber s = subscribed.get(i);
						s.emit_changed(ret);
					}
					// TODO: Emit only for those who are intrested in the changed keys?
				}
			}

			static void CB_Connected(Location.GPSDevice device) {
				debug("Connected");
			}

			static void CB_Disconnected(Location.GPSDevice device) {
				debug("Disconnected");
			}

			void insert_double_key (string key, HashTable<string, TypedVariant?> ret, double double_value, bool do_insert) {
				Value v = Value (typeof(double));

				if (do_insert) {
					v.set_double(double_value);
					ret.insert (key, TypedVariant (ValueType.DOUBLE, v));
				}
				else {
					v.set_double (0);
					ret.insert (key, TypedVariant (ValueType.UNDETERMINED, v));
				}
			}


			public void Get (StringSet keys, HashTable<string, TypedVariant?> ret) {
				if (keys.is_disjoint (location_keys)) {
					return;
				}

				/*
				Note: If this is the first Get (and we don't start the service before this), the data may not be valid.
				*/

				// Copy the internally stored information to the map
				if (keys.is_member ("Context.Environment.Location.latitude")) {
					insert_double_key("Context.Environment.Location.latitude", ret, current_location.latitude, current_location.latlong_valid);
				}

				if (keys.is_member ("Context.Environment.Location.longitude")) {
					insert_double_key("Context.Environment.Location.longitude", ret, current_location.longitude, current_location.latlong_valid);
				}

				if (keys.is_member ("Context.Environment.Location.altitude")) {
					insert_double_key("Context.Environment.Location.altitude", ret, current_location.altitude, current_location.altitude_valid);
				}
			}

			public void Subscribe (StringSet keys, ContextProvider.Subscriber s) {
				// FIXME: These are common to all plugins --> move to some upper level
				subscribed.add (s);
				subscribed_keys = new StringSet.union (subscribed_keys, keys);
				// FIXME: Also an intersection is needed.
			}

			public Key[] Keys() {
				return keys;
			}
		}
	}
}