Last active
February 13, 2017 16:38
-
-
Save bryanburgers/1b5b60c0da3d57df535dd27ae192087c to your computer and use it in GitHub Desktop.
Use promises with node-dbus 1.0.0.
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
'use strict'; | |
const Promise = require('bluebird'); | |
const DBus = require('dbus'); | |
const Bus = require('dbus/lib/bus'); | |
const Interface = require('dbus/lib/interface'); | |
Promise.promisifyAll(Bus.prototype); | |
Promise.promisifyAll(Interface.prototype); | |
const oldInit = Interface.prototype.init; | |
Interface.prototype.init = function(...args) { | |
oldInit.apply(this, args); | |
Promise.promisifyAll(this); | |
} | |
module.exports = DBus; |
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
'use strict'; | |
const SERVICE = 'org.freedesktop.systemd1'; | |
const MANAGER_OBJECT = '/org/freedesktop/systemd1'; | |
const MANAGER_INTERFACE = 'org.freedesktop.systemd1.Manager'; | |
const UNIT_INTERFACE = 'org.freedesktop.systemd1.Unit'; | |
const DBus = require('./dbus-promise'); | |
const bus = DBus.getBus('system'); | |
bus.getInterfaceAsync(SERVICE, MANAGER_OBJECT, MANAGER_INTERFACE) | |
.then(manager => manager.GetUnitAsync('ssh.service')) | |
.then(objectPath => bus.getInterfaceAsync(SERVICE, objectPath, UNIT_INTERFACE)) | |
.then(unit => unit.getPropertyAsync('ActiveState')) | |
//.then(unit => unit.getPropertiesAsync()) | |
.then(state => console.log(state)) | |
.catch(err => console.log(err)) | |
.finally(() => bus.disconnect()); |
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
const DBus = require('./dbus-promise'); | |
const bus = DBus.getBus('session'); | |
bus.getInterfaceAsync('org.freedesktop.DBus', '/', 'org.freedesktop.DBus') | |
.then(iface => { | |
return iface.ListNamesAsync({ timeout: 5000 }); | |
}) | |
.then(names => { | |
console.log(names); | |
}) | |
.catch(err => { | |
console.log(err); | |
}) | |
.finally(() => { | |
bus.disconnect(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment