55 lines
2.5 KiB
JavaScript
55 lines
2.5 KiB
JavaScript
const IFACE = 'io.qvac.Jarvis.Session';
|
|
const OBJECT = '/io/qvac/Jarvis';
|
|
const BUS_NAME = 'io.qvac.Jarvis';
|
|
|
|
export async function serveOnSessionBus(daemon) {
|
|
const dbus = await import('dbus-next');
|
|
const { Interface } = dbus.interface;
|
|
class Session extends Interface {
|
|
constructor() { super(IFACE); }
|
|
Arm() { daemon.arm(); }
|
|
Sleep() { daemon.sleep(); }
|
|
Shutdown() { daemon.close(); }
|
|
Say(text) { return daemon.say?.(text); }
|
|
Ask(text) { return daemon.ask(text); }
|
|
Cancel() { daemon.cancel(); }
|
|
SetMode(mode) { daemon.mode = mode; daemon.emit('ModeChanged', mode); }
|
|
GetState() { return daemon.state; }
|
|
ComputerGrant(persist) { daemon.computerGrant(persist); }
|
|
ComputerRevoke() { daemon.computerRevoke(); }
|
|
ComputerStatus() { return JSON.stringify(daemon.computer.status()); }
|
|
StateChanged(state) { this.emit('StateChanged', state); }
|
|
Reply(text) { this.emit('Reply', String(text)); }
|
|
Token(text) { this.emit('Token', String(text)); }
|
|
Error(code, message) { this.emit('Error', String(code), String(message)); }
|
|
}
|
|
Interface.configureMembers(Session, {
|
|
Arm: { inSignature: '', outSignature: '', method: 'Arm' },
|
|
Sleep: { inSignature: '', outSignature: '', method: 'Sleep' },
|
|
Shutdown: { inSignature: '', outSignature: '', method: 'Shutdown' },
|
|
Say: { inSignature: 's', outSignature: '', method: 'Say' },
|
|
Ask: { inSignature: 's', outSignature: '', method: 'Ask' },
|
|
Cancel: { inSignature: '', outSignature: '', method: 'Cancel' },
|
|
SetMode: { inSignature: 's', outSignature: '', method: 'SetMode' },
|
|
GetState: { inSignature: '', outSignature: 's', method: 'GetState' },
|
|
ComputerGrant: { inSignature: 'b', outSignature: '', method: 'ComputerGrant' },
|
|
ComputerRevoke: { inSignature: '', outSignature: '', method: 'ComputerRevoke' },
|
|
ComputerStatus: { inSignature: '', outSignature: 's', method: 'ComputerStatus' },
|
|
StateChanged: { signature: 's', signal: true },
|
|
Reply: { signature: 's', signal: true },
|
|
Token: { signature: 's', signal: true },
|
|
Error: { signature: 'ss', signal: true },
|
|
});
|
|
const bus = dbus.sessionBus();
|
|
await bus.requestName(BUS_NAME);
|
|
const iface = new Session();
|
|
bus.export(OBJECT, iface);
|
|
daemon.on('StateChanged', (state) => iface.StateChanged(state));
|
|
daemon.on('Reply', (reply) => iface.Reply(reply));
|
|
daemon.on('Token', (token) => iface.Token(token));
|
|
daemon.on('Error', (code, message) => iface.Error(code, message));
|
|
return bus;
|
|
}
|
|
|
|
export { BUS_NAME, OBJECT, IFACE };
|