From ae99c452eb46d0f0c36a8e44e627994ce261a726 Mon Sep 17 00:00:00 2001 From: Aleksander Morgado Date: Fri, 11 Apr 2014 09:27:51 +0200 Subject: examples: add modem watcher example in JS --- configure.ac | 1 + examples/Makefile.am | 2 +- examples/modem-watcher-javascript/Makefile.am | 5 ++ examples/modem-watcher-javascript/README | 22 ++++++ examples/modem-watcher-javascript/main.js | 11 +++ .../modem-watcher-javascript | 20 +++++ examples/modem-watcher-javascript/modemWatcher.js | 89 ++++++++++++++++++++++ 7 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 examples/modem-watcher-javascript/Makefile.am create mode 100644 examples/modem-watcher-javascript/README create mode 100644 examples/modem-watcher-javascript/main.js create mode 100755 examples/modem-watcher-javascript/modem-watcher-javascript create mode 100644 examples/modem-watcher-javascript/modemWatcher.js diff --git a/configure.ac b/configure.ac index 296be30b..34b8dbf9 100644 --- a/configure.ac +++ b/configure.ac @@ -310,6 +310,7 @@ vapi/Makefile cli/Makefile examples/Makefile examples/modem-watcher-python/Makefile +examples/modem-watcher-javascript/Makefile ]) AC_OUTPUT diff --git a/examples/Makefile.am b/examples/Makefile.am index 26e5407f..53b0b70e 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1 +1 @@ -SUBDIRS = modem-watcher-python +SUBDIRS = modem-watcher-python modem-watcher-javascript diff --git a/examples/modem-watcher-javascript/Makefile.am b/examples/modem-watcher-javascript/Makefile.am new file mode 100644 index 00000000..2ab38b1a --- /dev/null +++ b/examples/modem-watcher-javascript/Makefile.am @@ -0,0 +1,5 @@ + +EXTRA_DIST = \ + main.js \ + modemWatcher.js \ + modem-watcher-javascript diff --git a/examples/modem-watcher-javascript/README b/examples/modem-watcher-javascript/README new file mode 100644 index 00000000..db77683d --- /dev/null +++ b/examples/modem-watcher-javascript/README @@ -0,0 +1,22 @@ + +The modem-watcher-javascript program makes use of the 'libmm-glib' library through +GObject Introspection to talk to ModemManager. + +The program will just print in stdout whenever: + * ModemManager is found in the bus + * ModemManager is lost in the bus + * A new modem is added to ModemManager + * A modem is removed from ModemManager + +The output will look like this: + +$ ./modem-watcher-javascript +[ModemWatcher] ModemManager service is available in bus +[ModemWatcher] Vodafone (Huawei) (K3772) modem managed by ModemManager [861320000017897]: /org/freedesktop/ModemManager1/Modem/1 +[ModemWatcher] modem unmanaged by ModemManager: /org/freedesktop/ModemManager1/Modem/1 +[ModemWatcher] ModemManager service not available in bus + +Note that the program requires ModemManager and libmm-glib to be installed in +the system and the introspection typelibs available in the standard paths. + +Have fun! \ No newline at end of file diff --git a/examples/modem-watcher-javascript/main.js b/examples/modem-watcher-javascript/main.js new file mode 100644 index 00000000..a4c35549 --- /dev/null +++ b/examples/modem-watcher-javascript/main.js @@ -0,0 +1,11 @@ + +const Mainloop = imports.mainloop; +const GLib = imports.gi.GLib; + +const modemWatcher = imports.modemWatcher; + +function start() { + let watcher = new modemWatcher.ModemWatcher(); + + Mainloop.run(); +} diff --git a/examples/modem-watcher-javascript/modem-watcher-javascript b/examples/modem-watcher-javascript/modem-watcher-javascript new file mode 100755 index 00000000..074b543e --- /dev/null +++ b/examples/modem-watcher-javascript/modem-watcher-javascript @@ -0,0 +1,20 @@ +#!/bin/sh + +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation; either version 2 of the License, or (at your option) any +# later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +# details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., 51 +# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Copyright (C) 2014 Aleksander Morgado +# + +exec gjs-console -I . -c "const Main = imports.main; Main.start();" diff --git a/examples/modem-watcher-javascript/modemWatcher.js b/examples/modem-watcher-javascript/modemWatcher.js new file mode 100644 index 00000000..f97ff483 --- /dev/null +++ b/examples/modem-watcher-javascript/modemWatcher.js @@ -0,0 +1,89 @@ +// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*- + +// +// This program is free software; you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License as published by the Free +// Software Foundation; either version 2 of the License, or (at your option) any +// later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +// details. +// +// You should have received a copy of the GNU Lesser General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., 51 +// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Copyright (C) 2014 Aleksander Morgado +// + +const Lang = imports.lang; +const Mainloop = imports.mainloop; +const Gio = imports.gi.Gio; +const ModemManager = imports.gi.ModemManager; + +// The ModemWatcher class +const ModemWatcher = new Lang.Class({ + Name: 'ModemWatcher', + + _init: function() { + // The manager + this._manager = ModemManager.Manager.new_sync(Gio.DBus.system, + Gio.DBusObjectManagerClientFlags.DO_NOT_AUTO_START, + null) + + // Follow changes in the ModemManager1 interface name owner + this._nameOwnerId = this._manager.connect('notify::name-owner', Lang.bind(this, this._ModemManagerNameOwnerChanged)); + this._ModemManagerNameOwnerChanged() + + // Follow added/removed objects + this._objectAddedId = this._manager.connect('object-added', Lang.bind(this, this._ModemAdded)); + this._objectRemovedId = this._manager.connect('object-removed', Lang.bind(this, this._ModemRemoved)); + + // Add initial objects + let modem_object_list = this._manager.get_objects(); + for (let i = 0; i < modem_object_list.length; i++) + this._ModemAdded(this._manager, modem_object_list[i]); + }, + + _ModemManagerNameOwnerChanged: function() { + if (this._manager.name_owner) + print('[ModemWatcher] ModemManager service is available in bus'); + else + print('[ModemWatcher] ModemManager service not available in bus'); + }, + + _ModemAdded: function(manager, object) { + let modem = object.get_modem(); + print('[ModemWatcher] ' + + modem.get_manufacturer() + + ' (' + + modem.get_model() + + ') modem managed by ModemManager [' + + modem.get_equipment_identifier() + + ']: ' + + object.get_object_path()); + }, + + _ModemRemoved: function(manager, object) { + print('[ModemWatcher] modem unmanaged by ModemManager: ' + object.get_object_path()); + }, + + destroy: function() { + if (this._nameOwnerId) { + this._manager.disconnect(this._nameOwnerId); + this._nameOwnerId = 0; + } + + if (this._objectAddedId) { + this._manager.disconnect(this._objectAddedId); + this._objectAddedId = 0; + } + + if (this._objectRemovedId) { + this._manager.disconnect(this._objectRemovedId); + this._objectRemovedId = 0; + } + } +}); -- cgit v1.2.3