Skip to content

Instantly share code, notes, and snippets.

@bryanburgers
Last active February 13, 2017 16:38
Show Gist options
  • Save bryanburgers/1b5b60c0da3d57df535dd27ae192087c to your computer and use it in GitHub Desktop.
Save bryanburgers/1b5b60c0da3d57df535dd27ae192087c to your computer and use it in GitHub Desktop.
Use promises with node-dbus 1.0.0.
'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;
'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());
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