diff options
author | Aleksander Morgado <aleksander@aleksander.es> | 2014-04-11 09:27:51 +0200 |
---|---|---|
committer | Aleksander Morgado <aleksander@aleksander.es> | 2014-04-11 10:31:02 +0200 |
commit | ae99c452eb46d0f0c36a8e44e627994ce261a726 (patch) | |
tree | e793f373e790498dd324750b24d88f563c5c6507 | |
parent | ebff76cfaa0e5809ef5883d7588f55dc3e11f828 (diff) |
examples: add modem watcher example in JS
-rw-r--r-- | configure.ac | 1 | ||||
-rw-r--r-- | examples/Makefile.am | 2 | ||||
-rw-r--r-- | examples/modem-watcher-javascript/Makefile.am | 5 | ||||
-rw-r--r-- | examples/modem-watcher-javascript/README | 22 | ||||
-rw-r--r-- | examples/modem-watcher-javascript/main.js | 11 | ||||
-rwxr-xr-x | examples/modem-watcher-javascript/modem-watcher-javascript | 20 | ||||
-rw-r--r-- | examples/modem-watcher-javascript/modemWatcher.js | 89 |
7 files changed, 149 insertions, 1 deletions
diff --git a/configure.ac b/configure.ac index 296be30b..34b8dbf9 100644 --- a/configure.ac +++ b/configure.ac | |||
@@ -310,6 +310,7 @@ vapi/Makefile | |||
310 | cli/Makefile | 310 | cli/Makefile |
311 | examples/Makefile | 311 | examples/Makefile |
312 | examples/modem-watcher-python/Makefile | 312 | examples/modem-watcher-python/Makefile |
313 | examples/modem-watcher-javascript/Makefile | ||
313 | ]) | 314 | ]) |
314 | AC_OUTPUT | 315 | AC_OUTPUT |
315 | 316 | ||
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 @@ | |||
1 | |||
2 | EXTRA_DIST = \ | ||
3 | main.js \ | ||
4 | modemWatcher.js \ | ||
5 | 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 @@ | |||
1 | |||
2 | The modem-watcher-javascript program makes use of the 'libmm-glib' library through | ||
3 | GObject Introspection to talk to ModemManager. | ||
4 | |||
5 | The program will just print in stdout whenever: | ||
6 | * ModemManager is found in the bus | ||
7 | * ModemManager is lost in the bus | ||
8 | * A new modem is added to ModemManager | ||
9 | * A modem is removed from ModemManager | ||
10 | |||
11 | The output will look like this: | ||
12 | |||
13 | $ ./modem-watcher-javascript | ||
14 | [ModemWatcher] ModemManager service is available in bus | ||
15 | [ModemWatcher] Vodafone (Huawei) (K3772) modem managed by ModemManager [861320000017897]: /org/freedesktop/ModemManager1/Modem/1 | ||
16 | [ModemWatcher] modem unmanaged by ModemManager: /org/freedesktop/ModemManager1/Modem/1 | ||
17 | [ModemWatcher] ModemManager service not available in bus | ||
18 | |||
19 | Note that the program requires ModemManager and libmm-glib to be installed in | ||
20 | the system and the introspection typelibs available in the standard paths. | ||
21 | |||
22 | 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 @@ | |||
1 | |||
2 | const Mainloop = imports.mainloop; | ||
3 | const GLib = imports.gi.GLib; | ||
4 | |||
5 | const modemWatcher = imports.modemWatcher; | ||
6 | |||
7 | function start() { | ||
8 | let watcher = new modemWatcher.ModemWatcher(); | ||
9 | |||
10 | Mainloop.run(); | ||
11 | } | ||
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 @@ | |||
1 | #!/bin/sh | ||
2 | |||
3 | # This program is free software; you can redistribute it and/or modify it under | ||
4 | # the terms of the GNU Lesser General Public License as published by the Free | ||
5 | # Software Foundation; either version 2 of the License, or (at your option) any | ||
6 | # later version. | ||
7 | # | ||
8 | # This program is distributed in the hope that it will be useful, but WITHOUT | ||
9 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
10 | # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
11 | # details. | ||
12 | # | ||
13 | # You should have received a copy of the GNU Lesser General Public License along | ||
14 | # with this program; if not, write to the Free Software Foundation, Inc., 51 | ||
15 | # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
16 | # | ||
17 | # Copyright (C) 2014 Aleksander Morgado <aleksander@aleksander.es> | ||
18 | # | ||
19 | |||
20 | 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 @@ | |||
1 | // -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*- | ||
2 | |||
3 | // | ||
4 | // This program is free software; you can redistribute it and/or modify it under | ||
5 | // the terms of the GNU Lesser General Public License as published by the Free | ||
6 | // Software Foundation; either version 2 of the License, or (at your option) any | ||
7 | // later version. | ||
8 | // | ||
9 | // This program is distributed in the hope that it will be useful, but WITHOUT | ||
10 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
11 | // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
12 | // details. | ||
13 | // | ||
14 | // You should have received a copy of the GNU Lesser General Public License along | ||
15 | // with this program; if not, write to the Free Software Foundation, Inc., 51 | ||
16 | // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
17 | // | ||
18 | // Copyright (C) 2014 Aleksander Morgado <aleksander@aleksander.es> | ||
19 | // | ||
20 | |||
21 | const Lang = imports.lang; | ||
22 | const Mainloop = imports.mainloop; | ||
23 | const Gio = imports.gi.Gio; | ||
24 | const ModemManager = imports.gi.ModemManager; | ||
25 | |||
26 | // The ModemWatcher class | ||
27 | const ModemWatcher = new Lang.Class({ | ||
28 | Name: 'ModemWatcher', | ||
29 | |||
30 | _init: function() { | ||
31 | // The manager | ||
32 | this._manager = ModemManager.Manager.new_sync(Gio.DBus.system, | ||
33 | Gio.DBusObjectManagerClientFlags.DO_NOT_AUTO_START, | ||
34 | null) | ||
35 | |||
36 | // Follow changes in the ModemManager1 interface name owner | ||
37 | this._nameOwnerId = this._manager.connect('notify::name-owner', Lang.bind(this, this._ModemManagerNameOwnerChanged)); | ||
38 | this._ModemManagerNameOwnerChanged() | ||
39 | |||
40 | // Follow added/removed objects | ||
41 | this._objectAddedId = this._manager.connect('object-added', Lang.bind(this, this._ModemAdded)); | ||
42 | this._objectRemovedId = this._manager.connect('object-removed', Lang.bind(this, this._ModemRemoved)); | ||
43 | |||
44 | // Add initial objects | ||
45 | let modem_object_list = this._manager.get_objects(); | ||
46 | for (let i = 0; i < modem_object_list.length; i++) | ||
47 | this._ModemAdded(this._manager, modem_object_list[i]); | ||
48 | }, | ||
49 | |||
50 | _ModemManagerNameOwnerChanged: function() { | ||
51 | if (this._manager.name_owner) | ||
52 | print('[ModemWatcher] ModemManager service is available in bus'); | ||
53 | else | ||
54 | print('[ModemWatcher] ModemManager service not available in bus'); | ||
55 | }, | ||
56 | |||
57 | _ModemAdded: function(manager, object) { | ||
58 | let modem = object.get_modem(); | ||
59 | print('[ModemWatcher] ' + | ||
60 | modem.get_manufacturer() + | ||
61 | ' (' + | ||
62 | modem.get_model() + | ||
63 | ') modem managed by ModemManager [' + | ||
64 | modem.get_equipment_identifier() + | ||
65 | ']: ' + | ||
66 | object.get_object_path()); | ||
67 | }, | ||
68 | |||
69 | _ModemRemoved: function(manager, object) { | ||
70 | print('[ModemWatcher] modem unmanaged by ModemManager: ' + object.get_object_path()); | ||
71 | }, | ||
72 | |||
73 | destroy: function() { | ||
74 | if (this._nameOwnerId) { | ||
75 | this._manager.disconnect(this._nameOwnerId); | ||
76 | this._nameOwnerId = 0; | ||
77 | } | ||
78 | |||
79 | if (this._objectAddedId) { | ||
80 | this._manager.disconnect(this._objectAddedId); | ||
81 | this._objectAddedId = 0; | ||
82 | } | ||
83 | |||
84 | if (this._objectRemovedId) { | ||
85 | this._manager.disconnect(this._objectRemovedId); | ||
86 | this._objectRemovedId = 0; | ||
87 | } | ||
88 | } | ||
89 | }); | ||