Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strong typed events #140

Merged
merged 4 commits into from Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion .vscode/launch.json
Expand Up @@ -13,7 +13,9 @@
"outFiles": ["${workspaceFolder}/lib/**/*.js"],
"env": {
"DEBUG":"sonos:*",
"SONOS_HOST":"192.168.96.56"
"SONOS_HOST":"192.168.96.56",
"SONOS_TTS_ENDPOINT": "https://xxx/api/generate",
"SONOS_TTS_LANG": "nl-NL"
},
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
Expand Down
10 changes: 10 additions & 0 deletions examples/mute.js
@@ -0,0 +1,10 @@
const SonosDevice = require('../lib').SonosDevice

const sonos = new SonosDevice(process.env.SONOS_HOST || '192.168.96.56')
sonos.RenderingControlService.GetMute({ InstanceID: 0, Channel: 'Master' })
.then(data => {
console.log('Device muted', data);
})
.catch(err => {
console.log(err)
})
19 changes: 12 additions & 7 deletions examples/play-notification.js
Expand Up @@ -9,32 +9,37 @@ sonos.Events.on('currentTrack', (track) => {

setTimeout(async () => {
// Add a second notification (by some other event)
sonos.PlayNotification({
await sonos.PlayNotification({
trackUri: 'https://cdn.smartersoft-group.com/various/someone-at-the-door.mp3', // Cached text-to-speech file.
onlyWhenPlaying: false, // make sure that it only plays when you're listening to music. So it won't play when you're sleeping.
notificationFired: (played) => {
console.log('Second notification %o', played);
},
timeout: 10, // If the events don't work (to see when it stops playing) or if you turned on a stream, it will revert back after this amount of seconds.
volume: 16, // Set the volume for the notification (and revert back afterwards)
delayMs: 100 // Pause between commands in ms, (when sonos fails to play notification often).
volume: 20, // Set the volume for the notification (and revert back afterwards)
delayMs: 700 // Pause between commands in ms, (when sonos fails to play notification often).
});
await sonos.PlayTTS({
text: 'Iemand aan de deur', // Text to speech input
timeout: 10, // If the events don't work (to see when it stops playing) or if you turned on a stream, it will revert back after this amount of seconds.
volume: 20, // Set the volume for the notification (and revert back afterwards)
delayMs: 700 // Pause between commands in ms, (when sonos fails to play notification often).
})
}, 500)
}, 1000)

sonos.PlayNotification({
trackUri: 'https://cdn.smartersoft-group.com/various/pull-bell-short.mp3', // Can be any uri sonos understands
// trackUri: 'https://cdn.smartersoft-group.com/various/someone-at-the-door.mp3', // Cached text-to-speech file.
onlyWhenPlaying: false, // make sure that it only plays when you're listening to music. So it won't play when you're sleeping.
timeout: 10, // If the events don't work (to see when it stops playing) or if you turned on a stream, it will revert back after this amount of seconds.
volume: 15, // Set the volume for the notification (and revert back afterwards)
delayMs: 100 // Pause between commands in ms, (when sonos fails to play notification often).
delayMs: 700 // Pause between commands in ms, (when sonos fails to play notification often).
})
.then(played => {
console.log('Played notification(s) %o', played)
sonos.CancelEvents();
setTimeout(() => {
process.exit(0)
}, 1000)
}, 5000)
})
.catch(console.error)

Expand Down
62 changes: 31 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -54,7 +54,7 @@
"guid-typescript": "^1.0.9",
"html-entities": "^2.3.2",
"node-fetch": "^2.6.1",
"strict-event-emitter-types": "^2.0.0"
"typed-emitter": "^1.3.1"
},
"files": [
"README.md",
Expand Down
3 changes: 2 additions & 1 deletion src/models/sonos-state.ts
Expand Up @@ -2,8 +2,9 @@ import { GetMediaInfoResponse, GetPositionInfoResponse } from '../services';
import { TransportState } from './transport-state';

export interface SonosState {
transportState: TransportState;
mediaInfo: GetMediaInfoResponse;
muted: boolean;
positionInfo: GetPositionInfoResponse;
transportState: TransportState;
volume: number;
}
6 changes: 3 additions & 3 deletions src/services/base-service.ts
Expand Up @@ -4,7 +4,7 @@ import { parse } from 'fast-xml-parser';
import { Guid } from 'guid-typescript';
import { EventEmitter } from 'events';
import debug, { Debugger } from 'debug';
import StrictEventEmitter from 'strict-event-emitter-types';
import TypedEmitter from 'typed-emitter';
import SoapHelper from '../helpers/soap-helper';
import XmlHelper from '../helpers/xml-helper';
import { Track } from '../models/track';
Expand Down Expand Up @@ -36,7 +36,7 @@ export default abstract class BaseService <TServiceEvent> {
return this.debugger;
}

private events?: StrictEventEmitter<EventEmitter, ServiceEvent<TServiceEvent>>;
private events?: TypedEmitter<ServiceEvent<TServiceEvent>>;

/**
* Control URL is the relative endpoint to send command to.
Expand Down Expand Up @@ -338,7 +338,7 @@ export default abstract class BaseService <TServiceEvent> {
* @type {EventEmitter}
* @memberof BaseService
*/
public get Events(): StrictEventEmitter<EventEmitter, ServiceEvent<TServiceEvent>> {
public get Events(): TypedEmitter<ServiceEvent<TServiceEvent>> {
if (this.events === undefined) {
this.events = new EventEmitter();
this.events.on('removeListener', async (eventName: string | symbol) => {
Expand Down