Last active
December 10, 2016 10:37
-
-
Save DanSnow/ee0a0b4b3d045832e82c6492f2d4a1ef to your computer and use it in GitHub Desktop.
DBus: Receive singal and get wifi SSID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DBus from 'dbus' | |
import Promise from 'bluebird' | |
const dbus = new DBus() | |
const bus = dbus.getBus('system') | |
const NM_SERVICE = 'org.freedesktop.NetworkManager' | |
const NM_OBJECT = '/org/freedesktop/NetworkManager' | |
const NM_INTERFACE = NM_SERVICE | |
const NM_ACTIVE_CONN_INTERFACE = 'org.freedesktop.NetworkManager.Connection.Active' | |
Promise.fromCallback( | |
(cb) => bus.getInterface(NM_SERVICE, NM_OBJECT, NM_INTERFACE, cb) | |
).then((iface) => { | |
// DBus interface doc: https://developer.gnome.org/NetworkManager/0.9/spec.html#org.freedesktop.NetworkManager | |
return new Promise((resolve) => { | |
const propChangeListener = ({ PrimaryConnection, PrimaryConnectionType = '', State = 0 }) => { | |
if (PrimaryConnectionType.includes('wireless') && State === 70 /* NM_STATE_CONNECTED_GLOBAL: Conntected, with global network */) { | |
iface.removeListener('PropertiesChanged', propChangeListener) | |
// Get active connection's interface | |
resolve(Promise.fromCallback( | |
(cb) => bus.getInterface(NM_SERVICE, PrimaryConnection, NM_ACTIVE_CONN_INTERFACE, cb) | |
)) | |
} | |
} | |
// Listen to signal | |
iface.on('PropertiesChanged', propChangeListener) | |
}) | |
}).then((iface) => { | |
// doc: https://developer.gnome.org/NetworkManager/0.9/spec.html#org.freedesktop.NetworkManager.Connection.Active | |
// Actually, prop `Id` is the connection config's name | |
// But it is same as SSID by default | |
return Promise.fromCallback( | |
(cb) => iface.getProperty('Id', cb) | |
) | |
}).then((ssid) => { | |
console.log(`Connect to ${ssid}`) | |
}).catch((err) => { | |
console.error(err) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment