forked from ioBroker/ioBroker.sayit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
903 lines (802 loc) · 32.6 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
/* jshint -W097 */
/* jshint strict: false */
/* jslint node: true */
'use strict';
const utils = require('@iobroker/adapter-core'); // Get common adapter utils
const engines = require('./admin/engines.js');
const Text2Speech = require('./lib/text2speech');
const Speech2Device = require('./lib/speech2device');
const adapterName = require('./package.json').name.split('.').pop();
const fs = require('fs');
const path = require('path');
const sayitOptions = engines.sayitOptions;
let dataDir = path.join(utils.getAbsoluteDefaultDataDir(), 'sayit');
process.on('SIGINT', stop);
let processMessageTimeout;
let timeoutRunning;
let crypto;
let adapter;
let lang;
const options = {
sayLastVolume: null,
webLink: '',
cacheDir: '',
outFileExt: 'mp3',
};
let sayLastGeneratedText = '';
let lastSay = null;
let fileExt = 'mp3';
let text2speech = null;
let speech2device = null;
let MP3FILE;
const tasks = [];
let processing = false;
let helloCounter = 1;
function startAdapter(options) {
options = options || {};
Object.assign(options, {name: adapterName, unload: callback => stop(true, callback)});
adapter = new utils.Adapter(options);
adapter.on('stateChange', async (id, state) => {
if (state && !state.ack) {
if (id === `${adapter.namespace}.tts.clearQueue`) {
if (tasks.length > 1) {
tasks.splice(1);
adapter.setState('tts.clearQueue', false, true);
}
} else if (id === `${adapter.namespace}.tts.volume`) {
if (adapter.config.type === 'system') {
speech2device && speech2device.sayItSystemVolume(state.val);
} else {
options.sayLastVolume = state.val;
}
} else if (id === `${adapter.namespace}.tts.text`) {
if (typeof state.val !== 'string') {
if (state.val === null || state.val === undefined || state.val === '') {
return adapter.log.warn('Cannot cache empty text');
}
state.val = state.val.toString();
}
addToQueue(state.val);
} else if (id === `${adapter.namespace}.tts.cachetext`) {
if (typeof state.val !== 'string') {
if (state.val === null || state.val === undefined || state.val === '') {
return adapter.log.warn('Cannot cache empty text');
}
state.val = state.val.toString();
}
addToQueue(state.val, null, null, true);
}
}
});
adapter.on('objectChange', (id, obj) => {
if (id === `system.adapter.${adapter.config.webInstance}`) {
options.webLink = getWebLink(obj, adapter.config.webServer, adapter.config.webInstance);
}
});
adapter.on('ready', async () => await main());
adapter.on('message', obj => processMessage(obj));
try {
// create directory
!fs.existsSync(dataDir) && fs.mkdirSync(dataDir);
} catch (err) {
adapter.log.error(`Could not create Storage directory: ${err}`);
dataDir = __dirname;
}
return adapter;
}
function processMessage(obj) {
if (obj) {
if (obj.command === 'stopInstance') {
stop(false, () =>
obj.callback && adapter.sendTo(obj.from, obj.command, null, obj.callback));
} else if (obj.callback && obj.command === 'browseGoogleHome') {
// look for chromecast devices
try {
const mdns = require('mdns');
let browser = mdns.createBrowser(mdns.tcp('googlecast'));
const result = [];
browser.on('serviceUp', service => result.push({name: service.name, ip: service.addresses[0]}));
browser.on('error', err => adapter.log.error(`Error on MDNS discovery: ${err}`));
processMessageTimeout = setTimeout(() => {
processMessageTimeout = null;
browser.stop();
browser = null;
if (obj.command === 'browseGoogleHome') {
adapter.sendTo(obj.from, obj.command, result.map(s => ({label: `${s.name}[${s.ip}]`, value: s.ip})), obj.callback);
} else {
adapter.sendTo(obj.from, obj.command, result, obj.callback);
}
}, 2000);
browser.start();
} catch (e) {
adapter.log.error(e);
adapter.sendTo(obj.from, obj.command, null, obj.callback);
}
} else if (obj.callback && obj.command === 'browseChromecast') {
adapter.getObjectView('system', 'device', {startkey: 'chromecast.', endkey: 'chromecast.\u9999'}, (err, res) => {
const list = [];
if (!err && res) {
for (let i = 0; i < res.rows.length; i++) {
let name = res.rows[i].value && res.rows[i].value.common && res.rows[i].value.common.name;
if (typeof name === 'object') {
name = name[lang] || name.en;
}
list.push({value: res.rows[i].id, label: `${name} [${res.rows[i].id}]`});
}
}
adapter.sendTo(obj.from, obj.command, list, obj.callback);
});
} else if (obj.callback && obj.command === 'browseHeos') {
adapter.getObjectView('system', 'device', {startkey: 'heos.', endkey: 'heos.\u9999'}, (err, res) => {
const list = [];
for (let i = 0; i < res.rows.length; i++) {
let name = res.rows[i].value && res.rows[i].value.common && res.rows[i].value.common.name;
if (typeof name === 'object') {
name = name[lang] || name.en;
}
if (res.rows[i].id.includes('.players.')) {
list.push({value: res.rows[i].id, label: `${res.rows[i].id.replace(/^heos\.\d+\.players\./, '')} [${name}]`});
}
}
adapter.sendTo(obj.from, obj.command, list, obj.callback);
});
} else if (obj.callback && obj.command === 'browseSonos') {
adapter.getObjectView('system', 'device', {startkey: 'sonos.', endkey: 'heos.\u9999'}, (err, res) => {
const list = [];
for (let i = 0; i < res.rows.length; i++) {
let name = res.rows[i].value && res.rows[i].value.common && res.rows[i].value.common.name;
if (typeof name === 'object') {
name = name[lang] || name.en;
}
if (res.rows[i].id.includes('.players.')) {
list.push({value: res.rows[i].id, label: `${res.rows[i].id.replace(/^sonos\.\d+\.root\./, '')} [${name}]`});
}
}
adapter.sendTo(obj.from, obj.command, list, obj.callback);
});
} else if (obj.callback && obj.command === 'test') {
const language = (obj.message.engine || adapter.config.engine).substring(0, 2);
let text = 'Hello';
if (language === 'de') {
text = 'Hallo';
} else if (language === 'pl') {
text = 'Cześć';
} else if (language === 'uk') {
text = 'Привіт';
} else if (language === 'ru') {
text = 'Привет';
} else if (language === 'it') {
text = 'Ciao';
} else if (language === 'pt') {
text = 'Olá';
} else if (language === 'es') {
text = 'Hola';
} else if (language === 'fr') {
text = 'Bonjour';
} else if (language === 'nl') {
text = 'Hallo';
} else if (language === 'zh') {
text = '你好';
}
text += ` ${helloCounter++}`;
const opts = {...obj.message};
if (obj.callback) {
opts.callback = error => {
adapter.sendTo(obj.from, obj.command, {error, result: error ? undefined : 'Ok'}, obj.callback);
};
}
addToQueue(text, null, null, null, opts);
}
}
}
function stop(unload, callback) {
processMessageTimeout && clearTimeout(processMessageTimeout);
processMessageTimeout = null;
timeoutRunning && clearTimeout(timeoutRunning);
timeoutRunning = null;
try {
adapter && adapter.log && adapter.log.info && adapter.log.info('stopping...');
} catch (e) {
// ignore
}
typeof callback === 'function' && callback();
if (!unload) {
setTimeout(() => adapter.terminate ? adapter.terminate() : process.exit(), 500);
}
}
function mkpathSync(rootpath, dirpath) {
// Remove filename
dirpath = dirpath.split('/');
dirpath.pop();
if (!dirpath.length) {
return;
}
for (let i = 0; i < dirpath.length; i++) {
rootpath += `${dirpath[i]}/`;
if (!fs.existsSync(rootpath)) {
if (dirpath[i] !== '..') {
fs.mkdirSync(rootpath);
} else {
throw `Cannot create ${rootpath}${dirpath.join('/')}`;
}
}
}
}
function addToQueue(text, language, volume, onlyCache, testOptions) {
// Extract language from "en;volume;Text to say"
if (text.includes(';')) {
const arr = text.split(';', 3);
// If language;text or volume;text
if (arr.length === 2) {
// If number
if (parseInt(arr[0]).toString() === arr[0].toString()) {
volume = arr[0].trim();
} else {
language = arr[0].trim();
}
text = arr[1].trim();
} else if (arr.length === 3) {
// If language;volume;text or volume;language;text
// If number
if (parseInt(arr[0]).toString() === arr[0].toString()) {
volume = arr[0].trim();
language = arr[1].trim();
} else {
volume = arr[1].trim();
language = arr[0].trim();
}
text = arr[2].trim();
}
}
// Workaround for double text
// find all similar texts with interval less han 500 ms
const combined = [text, language, volume].filter(t => t).join(';');
if (tasks.find(task => task.combined === combined && Date.now() - task.ts < 500)) {
// ignore it
return;
}
const highPriority = text.startsWith('!');
volume = parseInt(volume || adapter.config.volume, 10);
if (Number.isNaN(volume)) {
volume = undefined;
}
let announce = testOptions && testOptions.announce !== undefined ? testOptions.announce : adapter.config.announce;
const annoTimeout = parseInt(testOptions && testOptions.annoTimeout !== undefined ? testOptions.annoTimeout : adapter.config.annoTimeout, 10);
const task = {text, language, volume, onlyCache, ts: Date.now(), combined, testOptions};
// If more time than 15 seconds till last text, add announcement
if (!onlyCache && announce && !tasks.length && (!lastSay || (Date.now() - lastSay > annoTimeout * 1000))) {
testOptions && prepareAnnounceFiles(testOptions);
const annoVolume = parseInt(testOptions && testOptions.annoVolume !== undefined ? testOptions.annoVolume : adapter.config.annoVolume, 10);
announce = testOptions && testOptions.announce !== undefined ? testOptions.announce : adapter.config.announce;
// place as first the announcement mp3
tasks.push({
text: announce,
language,
volume: Math.round((volume || 70) / 100 * (parseInt(annoVolume, 10) || 50)),
ts: task.ts,
testOptions
});
// and then text
tasks.push(task);
} else if (!onlyCache && highPriority) {
tasks.unshift(task);
} else {
tasks.push(task);
}
processTasks()
.catch(() => {});
}
function getCachedFileName(dir, text, fileExt) {
crypto = crypto || require('crypto');
return path.normalize(path.join(dir, `${crypto.createHash('md5').update(text).digest('hex')}.${fileExt}`));
}
function isCached(cacheDir, text, fileExt, cacheExpiryDays) {
const md5filename = getCachedFileName(options.cacheDir, text, fileExt);
if (fs.existsSync(md5filename)) {
if (cacheExpiryDays) {
const fileStat = fs.statSync(md5filename);
if (fileStat.ctime && (Date.now() - new Date(fileStat.ctime).getTime() > cacheExpiryDays * 1000 * 60 * 60 * 24)) {
this.adapter.log.info('Cached File expired, remove and re-generate');
fs.unlinkSync(md5filename);
return false;
}
}
return md5filename;
}
return false;
}
async function processTasks() {
if (processing) {
return;
}
processing = true;
let {text, language, volume, onlyCache, testOptions} = tasks[0];
let error;
if (text[0] === '!') {
text = text.substring(1);
}
const type = (testOptions && testOptions.type) || adapter.config.type;
if (volume === undefined || volume === null) {
try {
const state = await adapter.getForeignStateAsync(`${adapter.namespace}.tts.volume`);
if (state && state.val) {
volume = state.val;
}
} catch (e) {
// ignore
}
}
volume = parseInt(volume || (testOptions && testOptions.volume) || adapter.config.volume, 10);
if (Number.isNaN(volume)) {
volume = undefined;
}
let fileName;
// find out if say.mp3 must be generated
const isGenerate = !Speech2Device.isPlayFile(text) && sayitOptions[type].mp3Required;
language = language || (testOptions && testOptions.engine) || adapter.config.engine;
// if no text => do not process
if (isGenerate && text.length && text2speech && speech2device) {
// Check: may be it is a file from DB filesystem, like /vis.0/main/img/door-bell.mp3
if (text[0] === '/') {
if (!testOptions && (adapter.config.cache || onlyCache)) {
fileName = isCached(options.cacheDir, text, fileExt, adapter.config.cacheExpiryDays);
}
if (!fileName) {
const parts = text.split('/');
const _adapter = parts[0];
parts.shift();
const _path = parts.join('/');
let data;
try {
data = await adapter.readFileAsync(_adapter, _path);
} catch (e) {
// adapter.log.error(`Cache file does not exist "${text}": ${e.toString()}`);
}
if (!data) {
// may be the file is from real FS
if (fs.existsSync(text)) {
try {
data = fs.readFileSync(text);
} catch (e) {
adapter.log.error(`Cannot read file "${text}": ${e.toString()}`);
}
} else {
adapter.log.warn(`File "${text}" not found`);
}
}
if (data) {
try {
// Cache the file
if (adapter.config.cache || onlyCache) {
// get file name for cache
fileName = getCachedFileName(options.cacheDir, text, fileExt);
} else {
fileName = MP3FILE;
}
fs.writeFileSync(fileName, data);
} catch (e) {
adapter.log.error(`Cannot write file "${MP3FILE}": ${e.toString()}`);
}
}
}
}
adapter.log.info(`saying: ${text}`);
// If text first must be generated, and it is not the same as last one
if (!fileName && isGenerate) {
// do not cache if test options active, to test the voice generation too
if (sayLastGeneratedText !== `[${language}]${text}` || testOptions) {
if (adapter.config.cache && !testOptions) {
let md5filename = isCached(options.cacheDir, `${language};${text}`, fileExt, adapter.config.cacheExpiryDays);
if (md5filename) {
fileName = md5filename;
}
}
if (!fileName) {
try {
fileName = await text2speech.sayItGetSpeech(text, language, volume, testOptions);
sayLastGeneratedText = `[${language}]${text}`;
} catch (e) {
fileName = null;
error = `Cannot generate speech file: ${e}`;
adapter.log.error(error);
}
}
} else {
fileName = MP3FILE;
}
}
}
let duration = 0;
if (!onlyCache && text.length) {
await adapter.setStateAsync('tts.playing', true, true);
try {
// play file
if (fileName) {
duration = await text2speech.getDuration(fileName);
duration = await speech2device.playFile(type, fileName, language, volume, duration, testOptions);
} else if (!isGenerate) {
if (Speech2Device.isPlayFile(text)) {
duration = await text2speech.getDuration(text);
}
duration = await speech2device.playFile(type, text, language, volume, duration, testOptions);
}
lastSay = Date.now();
} catch (e) {
error = `Cannot play file: ${e}`;
adapter.log.error(error);
}
await adapter.setStateAsync('tts.playing', false, true);
}
if (tasks[0] && tasks[0].testOptions && tasks[0].testOptions.callback) {
tasks[0].testOptions.callback(error);
tasks[0].testOptions.callback = null;
}
tasks.shift();
if (tasks.length) {
timeoutRunning = setTimeout(() => {
timeoutRunning = null;
processing = false;
processTasks();
}, 100 + duration * 1000);
} else {
processing = false;
}
}
async function uploadFile(file) {
try {
const stat = fs.statSync(path.join(`${__dirname}/mp3/`, file));
if (!stat.isFile()) {
// ignore not a file
return;
}
} catch (e) {
// ignore not a file
return;
}
let data;
try {
data = await adapter.readFileAsync(adapter.namespace, `tts.userfiles/${file}`);
} catch (error) {
// ignore error
}
if (!data) {
try {
await adapter.writeFileAsync(adapter.namespace, `tts.userfiles/${file}`, fs.readFileSync(path.join(`${__dirname}/mp3/`, file)));
} catch (e) {
adapter.log.error(`Cannot write file "${__dirname}/mp3/${file}": ${e.toString()}`);
}
}
}
async function uploadFiles() {
if (fs.existsSync(`${__dirname}/mp3`)) {
adapter.log.info('Upload announce mp3 files');
let obj;
try {
obj = await adapter.getForeignObjectAsync(adapter.namespace);
} catch (e) {
// ignore
}
if (!obj) {
await adapter.setForeignObjectAsync(adapter.namespace, {
type: 'meta',
common: {
name: 'User files for SayIt',
type: 'meta.user',
},
native: {},
});
}
const files = fs.readdirSync(`${__dirname}/mp3`);
for (let f = 0; f < files.length; f++) {
await uploadFile(files[f]);
}
}
}
async function prepareAnnounceFiles(config) {
if (config.announce) {
config.annoDuration = parseInt(config.annoDuration) || 0;
config.annoTimeout = parseInt(config.annoTimeout) || 15;
config.annoVolume = parseInt(config.annoVolume) || 70; // percent from actual volume
// remove "tts.userfiles/" from file name
const fileName = config.announce.split('/').pop();
if (!fs.existsSync(path.join(__dirname, fileName))) {
try {
const data = await adapter.readFileAsync(adapter.namespace, `tts.userfiles/${fileName}`);
if (data) {
try {
fs.writeFileSync(path.join(__dirname, fileName), data);
config.announce = path.join(__dirname, fileName);
} catch (e) {
adapter.log.error(`Cannot write file: ${e.toString()}`);
config.announce = '';
}
}
} catch (e) {
adapter.log.error(`Cannot read file: ${e.toString()}`);
config.announce = '';
}
} else {
config.announce = path.join(__dirname, fileName);
}
}
}
async function start() {
if (!adapter.config.convertedV1toV2) {
const newConfig = JSON.parse(JSON.stringify(adapter.config));
if (newConfig.type === 'system') {
newConfig.systemCommand = newConfig.command;
newConfig.systemPlayer = newConfig.player;
} else if (newConfig.type === 'mp24ftp') {
newConfig.mp24Server = newConfig.server;
newConfig.ftpUser = newConfig.user;
newConfig.ftpPort = newConfig.port;
newConfig.ftpPassword = newConfig.pass;
} else if (newConfig.type === 'mp24') {
newConfig.mp24Server = newConfig.server;
} else if (newConfig.type === 'chromecast') {
newConfig.chromecastDevice = newConfig.cDevice;
} else if (newConfig.type === 'googleHome') {
newConfig.googleHomeServer = newConfig.server;
} else if (newConfig.type === 'sonos') {
newConfig.sonosDevice = newConfig.device;
} else if (newConfig.type === 'browser') {
newConfig.browserInstance = newConfig.instance;
} else if (newConfig.type === 'mpd') {
newConfig.mpdInstance = newConfig.mpd_device;
} else if (newConfig.type === 'heos') {
newConfig.heosDevice = newConfig.heos_device;
}
newConfig.webInstance = newConfig.web;
delete newConfig.server;
delete newConfig.mpd_device;
delete newConfig.heos_device;
delete newConfig.web;
delete newConfig.command;
delete newConfig.player;
delete newConfig.user;
delete newConfig.port;
delete newConfig.pass;
delete newConfig.cDevice;
delete newConfig.instance;
delete newConfig.sonos;
delete newConfig.googleHome;
delete newConfig.device;
if (newConfig.engine === 'ru_YA_CLOUD') {
newConfig.yandexKey = newConfig.key;
newConfig.yandexCloudVoice = newConfig.voice;
newConfig.yandexFolderID = newConfig.folderID;
newConfig.yandexEmotion = newConfig.emotion;
} else if (newConfig.engine === 'ru_YA') {
newConfig.yandexKey = newConfig.key;
newConfig.yandexVoice = newConfig.voice;
newConfig.yandexEmotion = newConfig.emotion;
newConfig.yandexDrunk = newConfig.drunk;
newConfig.yandexIll = newConfig.ill;
newConfig.yandexRobot = newConfig.robot;
} else if (newConfig.engine.includes('_CLOUD_')) {
newConfig.cloudInstance = newConfig.cloud;
} else if (newConfig.engine.includes('_AP_')) {
newConfig.awsAccessKey = newConfig.accessKey;
newConfig.awsSecretKey = newConfig.secretKey;
newConfig.awsRegion = newConfig.region;
}
delete newConfig.accessKey;
delete newConfig.secretKey;
delete newConfig.region;
delete newConfig.robot;
delete newConfig.ill;
delete newConfig.drunk;
delete newConfig.emotion;
delete newConfig.voice;
delete newConfig.key;
delete newConfig.folderID;
delete newConfig.cloud;
newConfig.convertedV1toV2 = true;
const configObj = await adapter.getForeignObjectAsync(`system.adapter.${adapter.namespace}`);
configObj.native = newConfig;
await adapter.setForeignObjectAsync(configObj._id, configObj);
// wait for restart
return;
}
if (adapter.config.browserVis === undefined) {
const configObj = await adapter.getForeignObjectAsync(`system.adapter.${adapter.namespace}`);
configObj.native.browserVis = '';
await adapter.setForeignObjectAsync(configObj._id, configObj);
// wait for restart
return;
}
const systemConfig = await adapter.getForeignObjectAsync('system.config');
if (!adapter.config.engine) {
adapter.config.engine = (systemConfig && systemConfig.common && systemConfig.common.language) || 'de';
}
lang = (systemConfig && systemConfig.common && systemConfig.common.language) || 'de';
if (adapter.config.engine === 'ru_YA_CLOUD') {
fileExt = 'ogg';
} else {
fileExt = 'mp3';
}
adapter.config.dataDir = dataDir;
MP3FILE = path.normalize(path.join(adapter.config.dataDir, `${adapter.namespace}.say.${fileExt}`));
options.outFileExt = fileExt;
await prepareAnnounceFiles(adapter.config);
// If cache enabled
if (adapter.config.cache) {
if (adapter.config.cacheDir && (adapter.config.cacheDir[0] === '/' || adapter.config.cacheDir[0] === '\\')) {
adapter.config.cacheDir = adapter.config.cacheDir.substring(1);
}
options.cacheDir = path.join(__dirname, adapter.config.cacheDir);
if (options.cacheDir) {
options.cacheDir = options.cacheDir.replace(/\\/g, '/');
if (options.cacheDir[options.cacheDir.length - 1] === '/') {
options.cacheDir = options.cacheDir.substring(0, options.cacheDir.length - 1);
}
} else {
options.cacheDir = '';
}
const parts = options.cacheDir.split('/');
let i = 0;
while (i < parts.length) {
if (parts[i] === '..') {
parts.splice(i - 1, 2);
i--;
} else {
i++;
}
}
options.cacheDir = parts.join('/');
// Create cache directory, if does not exist
if (!fs.existsSync(options.cacheDir)) {
try {
mkpathSync(`${__dirname}/`, adapter.config.cacheDir);
} catch (e) {
adapter.log.error(`Cannot create "${options.cacheDir}": ${e.message}`);
}
} else {
let engine = '';
// Read the old engine
if (fs.existsSync(path.join(options.cacheDir, 'engine.txt'))) {
try {
engine = fs.readFileSync(path.join(options.cacheDir, 'engine.txt')).toString();
} catch (e) {
adapter.log.error(`Cannot read file "${path.join(options.cacheDir, 'engine.txt')}: ${e.toString()}`);
}
}
// If engine changed
if (engine !== adapter.config.engine) {
// Delete all files in this directory
const files = fs.readdirSync(options.cacheDir);
for (let f = 0; f < files.length; f++) {
if (files[f] === 'engine.txt') continue;
try {
if (fs.existsSync(path.join(options.cacheDir, files[f])) && fs.lstatSync(path.join(options.cacheDir, files[f])).isDirectory()) {
fs.unlinkSync(path.join(options.cacheDir, files[f]));
}
} catch (e) {
adapter.log.error(`Cannot remove cache file "${path.join(options.cacheDir, files[f])}: ${e.toString()}`);
}
}
try {
fs.writeFileSync(path.join(options.cacheDir, 'engine.txt'), adapter.config.engine);
} catch (e) {
adapter.log.error(`Cannot write file "${path.join(options.cacheDir, 'engine.txt')}: ${e.toString()}`);
}
}
}
}
// initialize tts.text
await adapter.setStateAsync('tts.playing', false, true);
// calculate weblink for devices that require it
if ((adapter.config.type === 'sonos') ||
(adapter.config.type === 'heos') ||
(adapter.config.type === 'chromecast') ||
(adapter.config.type === 'mpd') ||
(adapter.config.type === 'googleHome')
) {
const obj = await adapter.getForeignObjectAsync(`system.adapter.${adapter.config.webInstance}`);
options.webLink = getWebLink(obj, adapter.config.webServer, adapter.config.webInstance);
// update web link on changes
await adapter.subscribeForeignObjectsAsync(`system.adapter.${adapter.config.webInstance}`);
}
// initialize tts.text
let textState;
try {
textState = await adapter.getStateAsync('tts.text');
} catch (e) {
// ignore
}
if (!textState) {
await adapter.setStateAsync('tts.text', '', true);
}
// create Text2Speech and Speech2Device
try {
options.addToQueue = addToQueue;
options.getCachedFileName = getCachedFileName;
options.isCached = isCached;
options.getWebLink = getWebLink;
options.MP3FILE = MP3FILE;
text2speech = new Text2Speech(adapter, options);
speech2device = new Speech2Device(adapter, options);
} catch (e) {
adapter.log.error(`Cannot initialize engines: ${e.toString()}`);
return;
}
// initialize tts.volume
let volumeState;
try {
volumeState = await adapter.getStateAsync('tts.volume');
} catch (e) {
// ignore
}
if (!volumeState) {
await adapter.setStateAsync('tts.volume', 70, true);
if (adapter.config.type !== 'system') {
options.sayLastVolume = 70;
} else {
await speech2device.sayItSystemVolume(70);
}
} else {
if (adapter.config.type !== 'system') {
options.sayLastVolume = volumeState.val;
} else {
await speech2device.sayItSystemVolume(volumeState.val);
}
}
adapter.subscribeStates('*');
}
function getWebLink(obj, webServer, webInstance) {
let webLink = '';
if (obj && obj.native) {
webLink = 'http';
if (obj.native.auth) {
adapter.log.error(`Cannot use server "${obj._id}" with authentication for sonos/heos/chromecast. Select other or create another one.`);
} else {
if (obj.native.secure) {
webLink += 's';
}
webLink += '://';
if (obj.native.bind === 'localhost' || obj.native.bind === '127.0.0.1') {
adapter.log.error(`Selected web server "${obj._id}" is only on local device available. Select other or create another one.`);
} else {
if (obj.native.bind === '0.0.0.0') {
webLink += webServer || adapter.config.webServer;
} else {
webLink += obj.native.bind;
}
}
webLink += `:${obj.native.port}`;
}
} else {
adapter.log.error(`Cannot read information about "${webInstance || adapter.config.webInstance}". No web server is active`);
}
return webLink;
}
async function main() {
if (
(process.argv && process.argv.includes('--install')) ||
(
(!process.argv || !process.argv.includes('--force')) && // If no arguments or no --force
(!adapter.common || !adapter.common.enabled) && // And adapter is not enabled
!process.argv.includes('--debug') // and not debug
)
) {
adapter.log.info('Install process. Upload files and stop.');
// Check if files exists in data storage
await uploadFiles();
if (adapter.stop) {
adapter.stop()
} else {
process.exit();
}
} else {
// Check if files exists in data storage
await uploadFiles();
await start();
}
}
// If started as allInOne/compact mode => return function to create instance
if (module && module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
}