From 8bf1b5b2c999d25f5d8a61d8ce03cde46b112b32 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Mon, 4 May 2015 23:11:21 +0200 Subject: Rewrite into object Fix the signals business so it doesn't throw exceptions. --- iss_get_data.js | 133 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 91 insertions(+), 42 deletions(-) mode change 100644 => 100755 iss_get_data.js (limited to 'iss_get_data.js') diff --git a/iss_get_data.js b/iss_get_data.js old mode 100644 new mode 100755 index 03c2ec5..9ba7069 --- 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 = ' \ \ \ @@ -51,43 +52,91 @@ const LocationInterface = ' \ '; 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(); -- cgit