summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2015-05-04 23:11:21 +0200
committerMatěj Cepl <mcepl@cepl.eu>2015-05-06 11:48:15 +0200
commit8bf1b5b2c999d25f5d8a61d8ce03cde46b112b32 (patch)
tree9b98452a1147cac4bc7c0efecca0b111e828eb4f
parent6e58fdc0bb60e33081f75a97bd1669776acbf579 (diff)
downloadISS_Above-8bf1b5b2c999d25f5d8a61d8ce03cde46b112b32.tar.gz
Rewrite into object
Fix the signals business so it doesn't throw exceptions.
-rwxr-xr-x[-rw-r--r--]geoclue.js0
-rwxr-xr-x[-rw-r--r--]iss_get_data.js133
2 files changed, 91 insertions, 42 deletions
diff --git a/geoclue.js b/geoclue.js
index bcd0775..bcd0775 100644..100755
--- a/geoclue.js
+++ b/geoclue.js
diff --git a/iss_get_data.js b/iss_get_data.js
index 03c2ec5..9ba7069 100644..100755
--- a/iss_get_data.js
+++ b/iss_get_data.js
@@ -1,11 +1,10 @@
#!/usr/bin/gjs
+/* jshint moz: true, multistr: true */
/* global imports */
-
-const Json = imports.gi.Json;
-const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
-const Lang = imports.lang;
const Mainloop = imports.mainloop;
+const GObject = imports.gi.GObject;
+const Lang = imports.lang;
const AccuracyLevel = {
COUNTRY: 1,
@@ -15,6 +14,8 @@ const AccuracyLevel = {
EXACT: 8
};
+const API_URL = 'http://api.open-notify.org/iss-now.json';
+
const ManagerIface = '<node> \
<interface name="org.freedesktop.GeoClue2.Manager"> \
<method name="GetClient"> \
@@ -51,43 +52,91 @@ const LocationInterface = '<node> \
</node>';
const LocationProxy = Gio.DBusProxy.makeProxyWrapper(LocationInterface);
-function get_ISS_coords() {
- const API_URL = 'http://api.open-notify.org/iss-now.json';
- let iss_api = Gio.file_new_for_uri(API_URL);
- iss_api.load_contents_async(null, function(iss_api, result) {
- let iss_coords = JSON.parse(iss_api.load_contents_finish(result)[1]);
- print(Object.keys(iss_coords));
- print(iss_coords.toSource());
- });
-}
-
-function onLocationUpdated(proxy, sender, [oldPath, newPath]) {
- let geoclueLocation = new LocationProxy(Gio.DBus.system,
- "org.freedesktop.GeoClue2",
- newPath);
- print('location:');
- print('\tlatitude = ' + geoclueLocation.Latitude);
- print('\tlongitude = ' + geoclueLocation.Longitude);
- print('\taccuracy = ' + geoclueLocation.Accuracy);
- print('\tdescription = ' + geoclueLocation.Description);
- //this._updateLocation(location);
- Mainloop.quit();
-}
-
-const _managerProxy = new ManagerProxy(Gio.DBus.system,
- 'org.freedesktop.GeoClue2', '/org/freedesktop/GeoClue2/Manager');
-
-const [clientAddr] = _managerProxy.GetClientSync();
-
-const clientProxy = new ClientProxy(Gio.DBus.system,
- 'org.freedesktop.GeoClue2', clientAddr);
-clientProxy.DesktopId = 'gnome-shell-ISS_Above';
-clientProxy.DistanceThreshold = 10000;
-clientProxy.RequestedAccuracyLevel = AccuracyLevel.EXACT;
-let updatedId = clientProxy.connectSignal('LocationUpdated',
- onLocationUpdated);
-clientProxy.StartRemote();
-
-get_ISS_coords();
+const ISS_Above = new GObject.Class({
+ Name: 'ISS_Above',
+ Extends: GObject.Object,
+ Signals: {
+ 'processed_data': {
+ param_types: [
+ // GObject.TYPE_INT
+ ]
+ }
+ },
+
+ _init: function(params) {
+ this.parent(params);
+
+ this.managerProxy = new ManagerProxy(Gio.DBus.system,
+ 'org.freedesktop.GeoClue2', '/org/freedesktop/GeoClue2/Manager');
+
+ [this.clientAddr] = this.managerProxy.GetClientSync();
+ print('this.clientAddr = ' + this.clientAddr);
+
+ this.current_location = {};
+ this.iss_coords = null;
+ this.task_queue = 2; // number of async process to complete
+
+ this.clientProxy = new ClientProxy(Gio.DBus.system,
+ 'org.freedesktop.GeoClue2', this.clientAddr);
+ this.clientProxy.DesktopId = 'gnome-shell-ISS_Above';
+ this.clientProxy.DistanceThreshold = 10000;
+ this.clientProxy.RequestedAccuracyLevel = AccuracyLevel.EXACT;
+ this.clientProxy.connectSignal('LocationUpdated',
+ Lang.bind(this, this.onLocationUpdated));
+ this.clientProxy.StartRemote();
+ this.connect('processed_data', Lang.bind(this, this.processResult));
+ },
+
+ run: function() {
+ this.getISScoords();
+ },
+
+ getISScoords: function () {
+ let self = this;
+ let iss_api = Gio.file_new_for_uri(API_URL);
+ iss_api.load_contents_async(null, function(iss_api, result) {
+ self.iss_coords = JSON.parse(iss_api.load_contents_finish(result)[1]);
+ print(self.iss_coords.toSource());
+ self.emit('processed_data');
+ });
+ },
+
+ onLocationUpdated: function (proxy, sender, [oldPath, newPath]) {
+ let geoclueLocation = new LocationProxy(Gio.DBus.system,
+ "org.freedesktop.GeoClue2",
+ newPath);
+ print('location:');
+ print('\tlatitude = ' + geoclueLocation.latitude);
+ print('\tlongitude = ' + geoclueLocation.longitude);
+ print('\taccuracy = ' + geoclueLocation.accuracy);
+ print('\tdescription = ' + geoclueLocation.description);
+
+ // this.current_location.latitude = geoclueLocation.latitude;
+ // this.current_location.longitude = geoclueLocation.longitude;
+ // this.current_location.accuracy = geoclueLocation.accuracy;
+ // this.current_location.description = geoclueLocation.description;
+
+ this.emit('processed_data');
+ },
+
+ processResult: function() {
+ this.task_queue--;
+ print('this.task_queue = ' + this.task_queue);
+
+ if (this.task_queue === 0) {
+ print('location:');
+ print('\tlatitude = ' + this.current_location.latitude);
+ print('\tlongitude = ' + this.current_location.longitude);
+ print('\taccuracy = ' + this.current_location.accuracy);
+ print('\tdescription = ' + this.current_location.description);
+ print('\n\niss coordinates: ' + this.iss_coords.toSource());
+
+ Mainloop.quit();
+ }
+ }
+});
+
+let client = new ISS_Above();
+client.run();
Mainloop.run();