82 lines
4.3 KiB
JavaScript
82 lines
4.3 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(); }
|
|
PushToTalk(pressed) { daemon.emit('PushToTalk', Boolean(pressed)); }
|
|
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; }
|
|
EnrollWake(phrase) { daemon.emit('EnrollWake', String(phrase)); }
|
|
IngestPath(filePath) { daemon.emit('IngestPath', String(filePath)); }
|
|
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)); }
|
|
WakeHeard(phrase) { this.emit('WakeHeard', String(phrase)); }
|
|
PartialTranscript(text) { this.emit('PartialTranscript', String(text)); }
|
|
FinalTranscript(text) { this.emit('FinalTranscript', String(text)); }
|
|
SpeakingLevel(rms) { this.emit('SpeakingLevel', Number(rms)); }
|
|
ListeningLevel(rms) { this.emit('ListeningLevel', Number(rms)); }
|
|
ChipOffered(id, label, payload) { this.emit('ChipOffered', String(id), String(label), String(payload)); }
|
|
JobProgress(id, pct, label) { this.emit('JobProgress', String(id), Number(pct), String(label)); }
|
|
ComputerStep(json) { this.emit('ComputerStep', String(json)); }
|
|
ComputerHighlight(json) { this.emit('ComputerHighlight', String(json)); }
|
|
}
|
|
Interface.configureMembers(Session, {
|
|
Arm: { inSignature: '', outSignature: '', method: 'Arm' },
|
|
Sleep: { inSignature: '', outSignature: '', method: 'Sleep' },
|
|
Shutdown: { inSignature: '', outSignature: '', method: 'Shutdown' },
|
|
PushToTalk: { inSignature: 'b', outSignature: '', method: 'PushToTalk' },
|
|
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' },
|
|
EnrollWake: { inSignature: 's', outSignature: '', method: 'EnrollWake' },
|
|
IngestPath: { inSignature: 's', outSignature: '', method: 'IngestPath' },
|
|
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 },
|
|
WakeHeard: { signature: 's', signal: true },
|
|
PartialTranscript: { signature: 's', signal: true },
|
|
FinalTranscript: { signature: 's', signal: true },
|
|
SpeakingLevel: { signature: 'd', signal: true },
|
|
ListeningLevel: { signature: 'd', signal: true },
|
|
ChipOffered: { signature: 'sss', signal: true },
|
|
JobProgress: { signature: 'sds', signal: true },
|
|
ComputerStep: { signature: 's', signal: true },
|
|
ComputerHighlight: { signature: 's', 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));
|
|
for (const signal of ['WakeHeard', 'PartialTranscript', 'FinalTranscript', 'SpeakingLevel', 'ListeningLevel', 'ChipOffered', 'JobProgress', 'ComputerStep', 'ComputerHighlight']) {
|
|
daemon.on(signal, (...args) => iface[signal](...args));
|
|
}
|
|
return bus;
|
|
}
|
|
|
|
export { BUS_NAME, OBJECT, IFACE };
|