iobroker.sayit 5.1.0 → 5.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,7 +16,9 @@ class Speech2Device {
16
16
  adapter;
17
17
  options;
18
18
  MP3FILE;
19
+ /** True if the vis.0 objects exist. Null if it was not checked yet */
19
20
  vis1exist = null;
21
+ /** True if the vis-2.0 objects exist. Null if it was not checked yet */
20
22
  vis2exist = null;
21
23
  config;
22
24
  constructor(adapter, options) {
@@ -25,6 +27,12 @@ class Speech2Device {
25
27
  this.MP3FILE = options.MP3FILE;
26
28
  this.config = adapter.config;
27
29
  }
30
+ /**
31
+ * Try to read a file from the ioBroker file storage, like "sayit.0/tts.userfiles/gong.mp3"
32
+ *
33
+ * @param fileName Name of the file in the ioBroker file storage
34
+ * @returns Content of the file or null if it is not a file in the ioBroker file storage
35
+ */
28
36
  async #getFileInStates(fileName) {
29
37
  if (fileName.match(/^\/?[-_\w]+\.\d+\//)) {
30
38
  if (fileName.startsWith('/')) {
@@ -46,24 +54,37 @@ class Speech2Device {
46
54
  }
47
55
  return null;
48
56
  }
57
+ /**
58
+ * Execute a command in the shell and wait till it is finished.
59
+ * A shell is required here, because the user could configure his own play command.
60
+ *
61
+ * @param cmd Command to execute
62
+ * @param args Optional arguments, which will be appended to the command
63
+ * @param cwd Working directory for the command
64
+ */
49
65
  async #exec(cmd, args, cwd) {
50
66
  return new Promise((resolve, reject) => {
51
67
  try {
52
68
  const _cmd = `${cmd}${args?.length ? ` ${args.join(' ')}` : ''}`;
53
- this.adapter.log.debug(`Execute ${cmd} ${args?.length ? args.join(' ') : ''}`);
54
- const ls = (0, node_child_process_1.exec)(_cmd, { cwd }, code => {
55
- if (!code) {
69
+ this.adapter.log.debug(`Execute ${_cmd}`);
70
+ const ls = (0, node_child_process_1.exec)(_cmd, { cwd }, error => {
71
+ if (!error) {
56
72
  resolve?.();
57
73
  }
58
74
  else {
59
- reject?.(new Error(`Exit code: ${code.code}`));
75
+ reject?.(new Error(`Exit code ${error.code}: ${error.message}`));
60
76
  }
61
77
  reject = null;
62
78
  resolve = null;
63
79
  });
64
- ls.on('error', e => this.adapter.log.error(`sayIt.play: there was an error while playing the file: ${e.toString()}`));
80
+ ls.on('error', e => {
81
+ this.adapter.log.error(`sayIt.play: there was an error while playing the file: ${e.toString()}`);
82
+ reject?.(e);
83
+ reject = null;
84
+ resolve = null;
85
+ });
65
86
  ls.stdout?.on('data', data => this.adapter.log.debug(`stdout: ${data}`));
66
- ls.stderr?.on('data', data => this.adapter.log.error(`stderr: ${data}`));
87
+ ls.stderr?.on('data', data => this.adapter.log.debug(`stderr: ${data}`));
67
88
  }
68
89
  catch (e) {
69
90
  reject?.(e);
@@ -72,14 +93,26 @@ class Speech2Device {
72
93
  }
73
94
  });
74
95
  }
96
+ /**
97
+ * Execute an external program without a shell and wait till it is finished
98
+ *
99
+ * @param cmd Name of the executable
100
+ * @param args Arguments of the executable
101
+ * @param ignoreErrorCode If true, a non-zero exit code will not be treated as an error
102
+ */
75
103
  async #spawn(cmd, args, ignoreErrorCode) {
76
104
  return new Promise((resolve, reject) => {
77
105
  try {
78
106
  this.adapter.log.debug(`Execute ${cmd} ${args.join(' ')}`);
79
107
  const ls = (0, node_child_process_1.spawn)(cmd, args);
80
- ls.on('error', e => this.adapter.log.error(`sayIt.play: there was an error while playing the file: ${e.toString()}`));
108
+ ls.on('error', e => {
109
+ this.adapter.log.debug(`Cannot execute "${cmd}": ${e.toString()}`);
110
+ reject?.(new Error(`Cannot execute "${cmd}": ${e.toString()}`));
111
+ reject = null;
112
+ resolve = null;
113
+ });
81
114
  ls.stdout.on('data', data => this.adapter.log.debug(`stdout: ${data}`));
82
- ls.stderr.on('data', data => this.adapter.log.error(`stderr: ${data}`));
115
+ ls.stderr.on('data', data => this.adapter.log.debug(`stderr: ${data}`));
83
116
  ls.on('close', code => {
84
117
  if (!code || ignoreErrorCode) {
85
118
  resolve?.();
@@ -98,7 +131,14 @@ class Speech2Device {
98
131
  }
99
132
  });
100
133
  }
101
- // Function to fill the .tts.mp3 state
134
+ /**
135
+ * Store the file to say in the "tts.mp3" (or "tts.ogg") file of this instance,
136
+ * so it could be played by a browser or by an external device over the web server.
137
+ *
138
+ * @param text Text to say or the name of the file to play
139
+ * @param saveToDisk If true, the file will be stored on the disk too (required for FTP and system player)
140
+ * @returns Link to the file in the ioBroker file storage and the name of the file on the disk
141
+ */
102
142
  async #uploadToStates(text, saveToDisk) {
103
143
  let fileData;
104
144
  let fileName;
@@ -106,7 +146,7 @@ class Speech2Device {
106
146
  if (data) {
107
147
  if (saveToDisk) {
108
148
  try {
109
- (0, node_fs_1.writeFileSync)(this.MP3FILE, data);
149
+ (0, node_fs_1.writeFileSync)(this.MP3FILE, typeof data === 'string' ? Buffer.from(data, 'binary') : data);
110
150
  }
111
151
  catch (e) {
112
152
  throw new Error(`Cannot save file "${this.MP3FILE}" to disk: ${e.toString()}`);
@@ -136,6 +176,12 @@ class Speech2Device {
136
176
  fileOnDisk: fileName,
137
177
  };
138
178
  }
179
+ /**
180
+ * Check if the given text is a name of a sound file and must be played instead of being spoken
181
+ *
182
+ * @param text Text to say or the name of the file to play
183
+ * @returns True if the text is a name of a mp3, wav or ogg file
184
+ */
139
185
  static isPlayFile(text) {
140
186
  if (text.length > 4) {
141
187
  const ext = text.substring(text.length - 4).toLowerCase();
@@ -145,6 +191,12 @@ class Speech2Device {
145
191
  }
146
192
  return false;
147
193
  }
194
+ /**
195
+ * Play the file in all browsers, where vis or vis-2 is opened
196
+ *
197
+ * @param props Text, type and options of the task
198
+ * @returns True if the file was sent to at least one vis version
199
+ */
148
200
  async #sayItBrowser(props) {
149
201
  const result = await this.#uploadToStates(props.text);
150
202
  const browserInstance = props.testOptions?.browserInstance || this.config.browserInstance;
@@ -155,7 +207,7 @@ class Speech2Device {
155
207
  if (this.vis1exist === null) {
156
208
  this.vis1exist = !!(await this.adapter.getForeignObjectAsync('vis.0.control.command'));
157
209
  if (!this.vis1exist) {
158
- this.adapter.log.error('Cannot control browser via vis1, because vis.0.* objects found');
210
+ this.adapter.log.error('Cannot control browser via vis(1), because no vis.0.* objects found');
159
211
  }
160
212
  }
161
213
  if (this.vis1exist) {
@@ -174,8 +226,7 @@ class Speech2Device {
174
226
  if (this.vis2exist === null) {
175
227
  this.vis2exist = !!(await this.adapter.getForeignObjectAsync('vis-2.0.control.command'));
176
228
  if (!this.vis2exist) {
177
- this.adapter.log.error('Cannot control browser via vis1, because vis-2.0.* objects found');
178
- return false;
229
+ this.adapter.log.error('Cannot control browser via vis(2), because no vis-2.0.* objects found');
179
230
  }
180
231
  }
181
232
  if (this.vis2exist) {
@@ -187,25 +238,37 @@ class Speech2Device {
187
238
  }
188
239
  }
189
240
  catch (e) {
190
- this.adapter.log.error(`Cannot control vis(1): ${e}`);
241
+ this.adapter.log.error(`Cannot control vis(2): ${e}`);
191
242
  this.vis2exist = false;
192
243
  }
193
244
  }
194
- return !(!this.vis1exist && !this.vis2exist);
245
+ return !!this.vis1exist || !!this.vis2exist;
195
246
  }
247
+ /**
248
+ * Get the link to the web server. If test options are used, the link will be built for the given web instance.
249
+ *
250
+ * @param testOptions Options provided by the admin test dialog or by a "say" message
251
+ * @returns Link like "http://192.168.1.1:8082" or an empty string if no web server is available
252
+ */
196
253
  async #getWebLink(testOptions) {
197
254
  if (testOptions) {
198
- const webServer = testOptions?.webServer || this.config.webServer;
199
- const webInstance = testOptions?.webInstance || this.config.webInstance;
255
+ const webServer = testOptions.webServer || this.config.webServer;
256
+ const webInstance = testOptions.webInstance || this.config.webInstance;
200
257
  if (!this.options.webLink ||
201
258
  webServer !== this.config.webServer ||
202
259
  webInstance !== this.config.webInstance) {
203
- const obj = await this.adapter.getForeignObjectAsync(`system.adapter.${testOptions.webInstance}`);
260
+ const obj = await this.adapter.getForeignObjectAsync(`system.adapter.${webInstance}`);
204
261
  return this.options.getWebLink(obj, webServer, webInstance);
205
262
  }
206
263
  }
207
264
  return this.options.webLink;
208
265
  }
266
+ /**
267
+ * Play the file on a sonos device
268
+ *
269
+ * @param props Text, volume and options of the task
270
+ * @returns True if the file was sent to the sonos adapter
271
+ */
209
272
  async #sayItSonos(props) {
210
273
  props.volume ||= props.testOptions?.volume
211
274
  ? parseInt(props.testOptions.volume, 10)
@@ -228,6 +291,12 @@ class Speech2Device {
228
291
  }
229
292
  return true;
230
293
  }
294
+ /**
295
+ * Play the file on a HEOS device
296
+ *
297
+ * @param props Text, volume and options of the task
298
+ * @returns True if the file was sent to the heos adapter
299
+ */
231
300
  async #sayItHeos(props) {
232
301
  props.volume ||= props.testOptions?.volume
233
302
  ? parseInt(props.testOptions.volume, 10)
@@ -250,6 +319,12 @@ class Speech2Device {
250
319
  }
251
320
  return true;
252
321
  }
322
+ /**
323
+ * Play the file on a MPD (music player daemon) device
324
+ *
325
+ * @param props Text, volume and options of the task
326
+ * @returns True if the file was sent to the mpd adapter
327
+ */
253
328
  async #sayItMpd(props) {
254
329
  props.volume ||= props.testOptions?.volume
255
330
  ? parseInt(props.testOptions.volume, 10)
@@ -272,6 +347,12 @@ class Speech2Device {
272
347
  }
273
348
  return true;
274
349
  }
350
+ /**
351
+ * Play the file on a chromecast device and wait till the playback is finished
352
+ *
353
+ * @param props Text, volume, duration and options of the task
354
+ * @returns True if the announcement was played
355
+ */
275
356
  async #sayItChromecast(props) {
276
357
  props.volume ||= props.testOptions?.volume
277
358
  ? parseInt(props.testOptions.volume, 10)
@@ -294,45 +375,62 @@ class Speech2Device {
294
375
  // Check every 500 ms if the announcement has finished playing
295
376
  return await new Promise((resolve, reject) => {
296
377
  let count = 0;
378
+ let checking = false;
297
379
  let intervalHandler = setInterval(async () => {
380
+ // Do not start the next check if the previous one is not finished yet
381
+ if (checking || !intervalHandler) {
382
+ return;
383
+ }
384
+ checking = true;
298
385
  count++;
299
- // We are checking every 500 ms, expected length of playback is defined in variable duration in seconds
300
- // Thus, we have to count to duration*2 to be able to wait long enough for the expected playback duration.
301
- // We add two additional seconds, to cover delays before the playback starts.
302
- if (count > (props.duration + 2) * 2) {
386
+ /** Stop the interval, so no further checks will be done */
387
+ const stop = () => {
303
388
  if (intervalHandler) {
304
389
  clearInterval(intervalHandler);
305
390
  intervalHandler = null;
306
391
  }
392
+ };
393
+ // We are checking every 500 ms, expected length of playback is defined in variable duration in seconds
394
+ // Thus, we have to count to duration*2 to be able to wait long enough for the expected playback duration.
395
+ // We add two additional seconds, to cover delays before the playback starts.
396
+ if (count > (props.duration + 2) * 2) {
397
+ stop();
307
398
  this.adapter.log.error(`Error while checking if ${chromecastAnnouncementDev} finished playing announcement: ${announcementJSON}: TIMEOUT`);
308
399
  reject(new Error('Timeout by checking of announcement finished playing'));
309
400
  return;
310
401
  }
311
402
  try {
312
403
  const state = await this.adapter.getForeignStateAsync(chromecastAnnouncementDev);
313
- if (state && state.ack) {
404
+ if (state?.ack) {
314
405
  this.adapter.log.debug(`${chromecastAnnouncementDev} finished playing announcement: ${announcementJSON}`);
315
- if (intervalHandler) {
316
- clearInterval(intervalHandler);
317
- intervalHandler = null;
318
- }
406
+ stop();
319
407
  resolve(true);
320
408
  }
321
409
  }
322
410
  catch (err) {
323
411
  this.adapter.log.error(`Error while checking if ${chromecastAnnouncementDev} finished playing announcement: ${announcementJSON}: ${err}`);
412
+ stop();
324
413
  reject(new Error(`Error by checking of announcement finished playing: ${err}`));
325
414
  }
415
+ finally {
416
+ checking = false;
417
+ }
326
418
  }, 500);
327
419
  });
328
420
  }
329
421
  this.adapter.log.warn('Web server is unavailable!');
330
422
  return false;
331
423
  }
424
+ /**
425
+ * Start the default media receiver on the google home device and play the given URL
426
+ *
427
+ * @param client Connected castv2 client
428
+ * @param url URL of the mp3 file
429
+ */
332
430
  #launchGoogleHome(client, url) {
333
- return new Promise((resolve, reject) => client.launch(castv2_client_1.DefaultMediaReceiver, (err, player) => {
431
+ return new Promise((resolve, reject) => client.launch(castv2_client_1.DefaultMediaReceiver, (launchError, player) => {
334
432
  if (!player) {
335
- return reject(new Error('Player not available.'));
433
+ return reject(new Error(`Player not available: ${launchError?.toString() || 'unknown error'}`));
336
434
  }
337
435
  const media = {
338
436
  contentId: url,
@@ -351,33 +449,57 @@ class Speech2Device {
351
449
  });
352
450
  }));
353
451
  }
452
+ /**
453
+ * Connect to a google home device, set the volume and play the given URL
454
+ *
455
+ * @param host IP address or host name of the google home device
456
+ * @param url URL of the mp3 file
457
+ * @param volume Volume in percent (0..100)
458
+ */
354
459
  async #sendToGoogleHome(host, url, volume) {
355
460
  const client = new castv2_client_1.Client();
356
461
  return new Promise((resolve, reject) => {
357
462
  client.connect(host, async () => {
358
- if (volume !== undefined) {
463
+ try {
464
+ if (volume !== undefined) {
465
+ try {
466
+ await new Promise((_resolve, _reject) => client.setVolume({ level: volume / 100 }, (err) => err ? _reject(new Error(err.toString())) : _resolve()));
467
+ }
468
+ catch (err) {
469
+ this.adapter.log.error(`there was an error setting the volume: ${err}`);
470
+ }
471
+ }
472
+ await this.#launchGoogleHome(client, url);
473
+ resolve?.();
474
+ }
475
+ catch (err) {
476
+ reject?.(err);
477
+ }
478
+ finally {
479
+ reject = null;
480
+ resolve = null;
359
481
  try {
360
- await new Promise((_resolve, _reject) => client.setVolume({ level: volume / 100 }, (err) => err ? _reject(new Error(err.toString())) : _resolve()));
482
+ client.close();
361
483
  }
362
484
  catch (err) {
363
- this.adapter.log.error(`there was an error setting the volume: ${err}`);
485
+ this.adapter.log.error(`there was an error closing the client: ${err}`);
364
486
  }
365
487
  }
366
- await this.#launchGoogleHome(client, url);
367
- try {
368
- client.close();
369
- }
370
- catch (err) {
371
- this.adapter.log.error(`there was an error closing the client: ${err}`);
372
- }
373
- resolve();
374
488
  });
375
489
  client.on('error', (err) => {
376
490
  client.close();
377
- reject(new Error(err.toString()));
491
+ reject?.(new Error(err.toString()));
492
+ reject = null;
493
+ resolve = null;
378
494
  });
379
495
  });
380
496
  }
497
+ /**
498
+ * Play the file on a google home device
499
+ *
500
+ * @param props Text, volume and options of the task
501
+ * @returns True if the file was played
502
+ */
381
503
  async #sayItGoogleHome(props) {
382
504
  props.volume ||= this.options.sayLastVolume;
383
505
  const result = await this.#uploadToStates(props.text);
@@ -393,46 +515,49 @@ class Speech2Device {
393
515
  }
394
516
  return true;
395
517
  }
518
+ /**
519
+ * Let the android app "MediaPlayer24" say the text or play a file, which is stored on the android device
520
+ *
521
+ * @param props Text and options of the task
522
+ * @returns True if the request was sent
523
+ */
396
524
  async #sayItMP24(props) {
397
525
  const mp24Server = props.testOptions?.mp24Server || this.config.mp24Server;
398
526
  if (mp24Server) {
399
- if (!_a.isPlayFile(props.text)) {
400
- // say
401
- this.adapter.log.debug(`Request MediaPlayer24 "http://${mp24Server}:50000/tts=${encodeURI(props.text)}"`);
402
- try {
403
- const response = await axios_1.default.get(`http://${mp24Server}:50000/tts=${encodeURI(props.text)}`);
404
- this.adapter.log.debug(`Response from MediaPlayer24 "${mp24Server}": ${response.data}`);
405
- }
406
- catch (e) {
407
- if (e.message === 'Parse Error') {
408
- this.adapter.log.debug('Played successfully');
409
- }
410
- else {
411
- this.adapter.log.error(`Cannot say text on MediaPlayer24 "${mp24Server}": ${e.message}`);
412
- throw e;
413
- }
414
- }
527
+ // "tts=" says the text, "track=" plays a file, which is stored on the android device.
528
+ // The file name must stay unencoded, because it is a path on the android device
529
+ const url = _a.isPlayFile(props.text)
530
+ ? `http://${mp24Server}:50000/track=${props.text}`
531
+ : `http://${mp24Server}:50000/tts=${encodeURIComponent(props.text)}`;
532
+ this.adapter.log.debug(`Request MediaPlayer24 "${url}"`);
533
+ try {
534
+ const response = await axios_1.default.get(url);
535
+ this.adapter.log.debug(`Response from MediaPlayer24 "${mp24Server}": ${response.data}`);
415
536
  }
416
- else {
417
- // play local file
418
- try {
419
- const response = await axios_1.default.get(`http://${mp24Server}:50000/track=${props.text}`);
420
- this.adapter.log.debug(`Response from MediaPlayer24 "${mp24Server}": ${response.data}`);
537
+ catch (e) {
538
+ // MediaPlayer24 answers with an invalid HTTP response, so this error means success
539
+ if (e.message === 'Parse Error') {
540
+ this.adapter.log.debug('Played successfully');
421
541
  }
422
- catch (e) {
423
- if (e.message === 'Parse Error') {
424
- this.adapter.log.debug('Played successfully');
425
- }
426
- else {
427
- this.adapter.log.error(`Cannot say text on MediaPlayer24 "${mp24Server}": ${e.message}`);
428
- throw e;
429
- }
542
+ else {
543
+ this.adapter.log.error(`Cannot say text on MediaPlayer24 "${mp24Server}": ${e.message}`);
544
+ throw e;
430
545
  }
431
546
  }
432
547
  }
548
+ else {
549
+ this.adapter.log.warn('No MediaPlayer24 server defined');
550
+ return false;
551
+ }
433
552
  props.duration += 2;
434
553
  return true;
435
554
  }
555
+ /**
556
+ * Upload the mp3 file per FTP to an android device and let the app "MediaPlayer24" play it
557
+ *
558
+ * @param props Text and options of the task
559
+ * @returns True if the file was uploaded and played
560
+ */
436
561
  async #sayItMP24ftp(props) {
437
562
  const result = await this.#uploadToStates(props.text, true);
438
563
  const mp24Server = props.testOptions?.mp24Server || this.config.mp24Server;
@@ -447,11 +572,12 @@ class Speech2Device {
447
572
  user: ftpUser || 'anonymous', // defaults to 'anonymous'
448
573
  pass: ftpPassword || 'anonymous', // defaults to 'anonymous'
449
574
  });
575
+ const fileOnDisk = result.fileOnDisk;
450
576
  await new Promise((resolve, reject) => {
451
577
  try {
452
578
  // Copy file to FTP server
453
579
  const fileNameOnFTP = `${this.adapter.namespace}.say.${this.options.outFileExt}`;
454
- ftp.put(result.fileOnDisk, fileNameOnFTP, hadError => {
580
+ ftp.put(fileOnDisk, fileNameOnFTP, hadError => {
455
581
  // send quit command
456
582
  ftp.raw('quit', async (err) => {
457
583
  // , data
@@ -482,14 +608,24 @@ class Speech2Device {
482
608
  });
483
609
  });
484
610
  }
485
- catch {
486
- throw new Error(`Cannot upload file to ${mp24Server}:${ftpPort}`);
611
+ catch (e) {
612
+ reject(new Error(`Cannot upload file to ${mp24Server}:${ftpPort}: ${e.toString()}`));
487
613
  }
488
614
  });
489
615
  }
616
+ else {
617
+ this.adapter.log.warn('No FTP server or port defined');
618
+ return false;
619
+ }
490
620
  props.duration += 2;
491
621
  return true;
492
622
  }
623
+ /**
624
+ * Play the file with the system player of the host, where the adapter is running
625
+ *
626
+ * @param props Text, volume and options of the task
627
+ * @returns True if the file was played
628
+ */
493
629
  async #sayItSystem(props) {
494
630
  const result = await this.#uploadToStates(props.text, true);
495
631
  const systemCommand = props.testOptions?.systemCommand || this.config.systemCommand;
@@ -524,13 +660,13 @@ class Speech2Device {
524
660
  else if (p === 'linux') {
525
661
  // linux
526
662
  if (systemPlayer === 'omxplayer') {
527
- cmd = `omxplayer -o local ${result.fileOnDisk}`;
663
+ cmd = `omxplayer -o local "${result.fileOnDisk}"`;
528
664
  }
529
665
  else if (systemPlayer === 'mpg321') {
530
- cmd = `mpg321 -g ${this.options.sayLastVolume} ${result.fileOnDisk}`;
666
+ cmd = `mpg321 -g ${this.options.sayLastVolume} "${result.fileOnDisk}"`;
531
667
  }
532
668
  else {
533
- cmd = `mplayer ${result.fileOnDisk} -volume ${this.options.sayLastVolume}`;
669
+ cmd = `mplayer "${result.fileOnDisk}" -volume ${this.options.sayLastVolume}`;
534
670
  }
535
671
  try {
536
672
  await this.#exec(cmd);
@@ -543,29 +679,39 @@ class Speech2Device {
543
679
  else if (p.match(/^win/)) {
544
680
  // windows
545
681
  try {
546
- await this.#exec('cmdmp3.exe', [`"${result.fileOnDisk}"`], (0, node_path_1.normalize)(`${__dirname}/../cmdmp3/`));
682
+ await this.#spawn((0, node_path_1.normalize)((0, node_path_1.join)(__dirname, '..', 'cmdmp3', 'cmdmp3.exe')), [result.fileOnDisk]);
547
683
  }
548
684
  catch (e) {
549
- this.adapter.log.error(`Cannot play:${e}`);
685
+ this.adapter.log.error(`Cannot play: ${e}`);
550
686
  return false;
551
687
  }
552
688
  }
553
689
  else if (p === 'darwin') {
554
690
  // mac osx
555
691
  try {
556
- await this.#exec('/usr/bin/afplay', [result.fileOnDisk]);
692
+ await this.#spawn('/usr/bin/afplay', [result.fileOnDisk]);
557
693
  }
558
694
  catch (e) {
559
- this.adapter.log.error(`Cannot play:${e}`);
695
+ this.adapter.log.error(`Cannot play: ${e}`);
560
696
  return false;
561
697
  }
562
698
  }
699
+ else {
700
+ this.adapter.log.error(`Unsupported platform: ${p}`);
701
+ return false;
702
+ }
563
703
  }
564
704
  if (props.text !== this.config.announce) {
565
705
  props.duration += 2;
566
706
  }
567
707
  return true;
568
708
  }
709
+ /**
710
+ * Say the text with the windows own text to speech engine
711
+ *
712
+ * @param props Text, volume and options of the task
713
+ * @returns True if the text was said
714
+ */
569
715
  async #sayItWindows(props) {
570
716
  // If mp3 file
571
717
  if (_a.isPlayFile(props.text)) {
@@ -573,13 +719,13 @@ class Speech2Device {
573
719
  }
574
720
  // Call windows own text 2 speech
575
721
  const p = (0, node_os_1.platform)();
576
- if (props.volume || props.volume === 0) {
722
+ if (props.volume !== undefined) {
577
723
  await this.sayItSystemVolume(props.volume, props.testOptions);
578
724
  }
579
725
  if (p.match(/^win/)) {
580
726
  // windows
581
727
  try {
582
- await this.#exec((0, node_path_1.normalize)(`${__dirname}/../say/SayStatic.exe`), [`"${props.text}"`]);
728
+ await this.#spawn((0, node_path_1.normalize)((0, node_path_1.join)(__dirname, '..', 'say', 'SayStatic.exe')), [props.text]);
583
729
  }
584
730
  catch (error) {
585
731
  this.adapter.log.error(`sayItWindows: ${error}`);
@@ -593,11 +739,20 @@ class Speech2Device {
593
739
  props.duration += 2;
594
740
  return true;
595
741
  }
742
+ /**
743
+ * Set the volume of the host, where the adapter is running
744
+ *
745
+ * @param level Volume in percent (0..100)
746
+ * @param testOptions Options provided by the admin test dialog or by a "say" message
747
+ */
596
748
  async sayItSystemVolume(level, testOptions) {
597
- if (!level && level !== 0) {
749
+ if (level === undefined || level === null || level === '') {
598
750
  return;
599
751
  }
600
752
  level = parseInt(level, 10);
753
+ if (Number.isNaN(level)) {
754
+ return;
755
+ }
601
756
  if (level < 0) {
602
757
  level = 0;
603
758
  }
@@ -612,9 +767,9 @@ class Speech2Device {
612
767
  const p = (0, node_os_1.platform)();
613
768
  const systemPlayer = testOptions?.systemPlayer || this.config.systemPlayer;
614
769
  if (p === 'linux' && systemPlayer !== 'mpg321') {
615
- // linux
770
+ // linux. The arguments are given without quotes, because no shell is involved
616
771
  try {
617
- await this.#spawn('amixer', ['cset', 'name="Master Playback Volume"', '--', `${level}%`]);
772
+ await this.#spawn('amixer', ['cset', 'name=Master Playback Volume', '--', `${level}%`]);
618
773
  }
619
774
  catch {
620
775
  this.adapter.log.error('amixer is not available, so you may hear no audio. Install manually!');
@@ -625,7 +780,7 @@ class Speech2Device {
625
780
  // windows volume is from 0 to 65535
626
781
  level = Math.round((65535 * level) / 100); // because this level is from 0 to 100
627
782
  try {
628
- await this.#spawn((0, node_path_1.normalize)(`${__dirname}/../nircmd/nircmdc.exe`), ['setsysvolume', level.toString()], true);
783
+ await this.#spawn((0, node_path_1.normalize)((0, node_path_1.join)(__dirname, '..', 'nircmd', 'nircmdc.exe')), ['setsysvolume', level.toString()], true);
629
784
  }
630
785
  catch {
631
786
  this.adapter.log.error('nircmd is not available, so you may hear no audio.');
@@ -634,13 +789,19 @@ class Speech2Device {
634
789
  else if (p === 'darwin') {
635
790
  // mac osx
636
791
  try {
637
- await this.#spawn('sudo', ['osascript', '-e', `"set Volume ${Math.round(level / 10)}"`]);
792
+ await this.#spawn('sudo', ['osascript', '-e', `set Volume ${Math.round(level / 10)}`]);
638
793
  }
639
794
  catch {
640
795
  this.adapter.log.error('osascript is not available, so you may hear no audio.');
641
796
  }
642
797
  }
643
798
  }
799
+ /**
800
+ * Play the generated file or say the text on the configured device
801
+ *
802
+ * @param props Text, type, volume, duration and options of the task
803
+ * @returns True if the file was played
804
+ */
644
805
  async playFile(props) {
645
806
  const type = props.testOptions?.type || props.type;
646
807
  if (type === 'browser') {