-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
5063 lines (4959 loc) · 146 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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
/*
* Created with @iobroker/create-adapter v1.31.0
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
// https://techsparx.com/nodejs/esnext/esm-to-cjs.html
// Load your modules here, e.g.:
// const fs = require("fs");
const md5 = require('md5');
const YamahaYXC = require('yamaha-yxc-nodejs').YamahaYXC;
let yamaha = null;
let yamaha2 = null;
const responses = [ {} ];
let onlineCheckTimer = null;
const onlineCheckInterval = 30;
const dpZoneCommands = {
power: 'power',
mute: 'mute',
surround: 'surround',
volume: 'setVolumeTo',
input: 'setInput',
bass_extension: 'setBassExtension',
enhancer: 'setEnhancer',
direct: 'setDirect',
pure_direct: 'setPureDirect',
sound_program: 'setSound',
bass: 'setBassTo',
treble: 'setTrebleTo',
balance: 'setBalance',
sleep: 'sleep',
clearVoice: 'setClearVoice',
link_control: 'setLinkControl',
link_audio_delay: 'setLinkAudioDelay',
link_audio_quality: 'setLinkAudioQuality'
};
const dpCommands = {
subwoofer_volume: 'setSubwooferVolumeTo',
presetrecallnumber: 'recallPreset',
recallRecentItem: 'recallRecentItem'
};
const dpToggleCommands = {
shuffle: 'toggleShuffle',
repeat: 'toggleRepeat'
};
class Musiccast extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: 'musiccast'
});
this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
// this.on('objectChange', this.onObjectChange.bind(this));
this.on('message', this.onMessage.bind(this));
this.on('unload', this.onUnload.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Initialize your adapter here
try {
//yamaha.discover
//yamaha.discoverYSP
//found devices crosscheck with config.devices
//new found devices to adapter.confg.devices //quit adapter and restart with found config
const obj = this.config.devices;
//check if something is not configured
await this.isOnline(true);
for (const anz in obj) {
//general structure setup
await this.defineMusicDevice(obj[anz].type, obj[anz].uid, obj[anz].name); //contains also the structure to musiccast.0._id_type_.
await this.defineMusicNetUsb(obj[anz].type, obj[anz].uid); //all devices are supporting netusb
//defineMClink basic structure
this.log.info('--------------------');
// undefined, what should it be?
this.log.info(JSON.stringify(YamahaYXC));
this.log.info('--------------------');
//get the inout list and create object
await this.defineMusicDeviceFeatures(obj[anz].ip, obj[anz].type, obj[anz].uid);
//yamaha.getNameText() evtl. um enum_room für die Zone zu setzen oder über setNameText enum_room aus admin setzen
//yamaha.getStatus('main'); initial status of device
//some reading from the devices
// get system data
await this.getMusicDeviceInfo(obj[anz].ip, obj[anz].type, obj[anz].uid);
// get main status
await this.getMusicZoneInfo(obj[anz].ip, obj[anz].type, obj[anz].uid, 'main'); //must be looped if more than main zone
/*
adapter.getStatesOf(adapter.namespace + "." + obj[anz].type + "_" + obj[anz].uid + ".zone2",function (err, channel) {
if (err) {
adapter.log.info('zone2 nicht existent für ');
}
else {
getMusicZoneInfo(obj[anz].ip, obj[anz].type, obj[anz].uid, 'zone2');
}
});
adapter.getStatesOf(adapter.namespace + "." + obj[anz].type + "_" + obj[anz].uid + ".zone3",function (err, channel) {
if (err){
adapter.log.info('zone3 nicht existent für ');}
else {
getMusicZoneInfo(obj[anz].ip, obj[anz].type, obj[anz].uid, 'zone3');
}
});
adapter.getStatesOf(adapter.namespace + "." + obj[anz].type + "_" + obj[anz].uid + ".zone4",function (err, channel) {
if (err) {
adapter.log.info('zone4 nicht existent für ');}
else {
getMusicZoneInfo(obj[anz].ip, obj[anz].type, obj[anz].uid, 'zone4');
}
});
*/
// get main lists status
await this.getMusicZoneLists(obj[anz].ip, obj[anz].type, obj[anz].uid); //
// get netusb status
await this.getMusicNetusbInfo(obj[anz].ip, obj[anz].type, obj[anz].uid);
await this.getMusicNetusbRecent(obj[anz].ip, obj[anz].type, obj[anz].uid);
await this.getMusicNetusbPreset(obj[anz].ip, obj[anz].type, obj[anz].uid);
//get CD initially
//get Clock initially
//get tuner initially
}
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
server.on('error', (err) => {
this.log.error('server error:' + err.stack);
server.close();
});
server.on('message', (msg, rinfo) => {
this.log.debug('server got:' + msg.toString() + ' from ' + rinfo.address);
//adapter.log.debug('server got:' + JSON.parse(msg.toString()) + 'from ' + rinfo.address );
const foundip = this.getConfigObjects(this.config.devices, 'ip', rinfo.address);
if (foundip.length === 0 || foundip.length !== 1) {
//nix oder mehr als eine Zuordnung
this.log.error('received telegram can not be processed, no config for this IP' + rinfo.address);
} else {
//try catch
this.gotUpdate(JSON.parse(msg.toString()), rinfo.address); //erstmal noch IP, device_id ist eine andere als die in ssdp übermittelte (letze Teil von UDN)
}
});
server.on('listening', () => {
this.log.info('musiccast socket listening ');
});
server.bind(41100);
//everything is configured, make cyclic updates
// make some artifical request to overcome the 20min autostop on updating
/*
function pollData() {
var interval = 300; // 5min
for (var anz in obj) { // für alle Objekte
adapter.getForeignState()
getMusicDeviceInfo(obj[anz].ip, obj[anz].type, obj[anz].uid);
}
adapter.log.debug("polling! keeping musiccast alive");
mcastTimeout = setTimeout(pollData, interval * 1000);
}
*/
// if(adapter.config.keepalive){pollData()}
// in this musiccast all states changes inside the adapters namespace are subscribed
this.subscribeStates('*');
} catch (err) {
this.log.error(`[main] error: ${err.message}, stack: ${err.stack}`);
}
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
// Here you must clear all timeouts or intervals that may still be active
clearTimeout(onlineCheckTimer);
// clearTimeout(timeout2);
// ...
// clearInterval(interval1);
callback();
} catch (e) {
this.log.error(e);
callback();
}
}
// If you need to react to object changes, uncomment the following block and the corresponding line in the constructor.
// You also need to subscribe to the objects with `this.subscribeObjects`, similar to `this.subscribeStates`.
// /**
// * Is called if a subscribed object changes
// * @param {string} id
// * @param {ioBroker.Object | null | undefined} obj
// */
// onObjectChange(id, obj) {
// if (obj) {
// // The object was changed
// this.log.info(`object ${id} changed: ${JSON.stringify(obj)}`);
// } else {
// // The object was deleted
// this.log.info(`object ${id} deleted`);
// }
// }
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
async onStateChange(id, state) {
if (state) {
// The state was changed
this.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
if (state && !state.ack) {
//hier erkennung einbauen um festzustellen ob 2 oder 3 stufige Objekthierarchie
const tmp = id.split('.');
const dp = tmp.pop(); //is the instance we are working on
const idx = tmp.pop(); //is zone, system or other item
const idy = tmp.pop(); // the device "type"_"uid"
this.log.info('MusicCast: ' + id + ' identified for command with ' + state.val);
//ermitteln der IP aus config
this.log.debug('device with uid = ' + idy.split('_')[1]);
const uid = idy.split('_')[1];
const IP = this.getConfigObjects(this.config.devices, 'uid', uid);
this.log.debug('config items : ' + JSON.stringify(this.config.devices));
this.log.debug('IP configured : ' + IP[0].ip + ' for UID ' + uid);
yamaha = new YamahaYXC(IP[0].ip);
const zone = idx;
// possible commands not yet implemented
// "extra_bass",
// "adaptive_drc",
// "dts_dialogue_control",
// "adaptive_dsp_level"
// work with boolCMD
//eslint, defs hier für case 'add_to_group', 'remove_from_group':
const groupID = md5(state.val);
var clientIP = null;
let clientpayload = null;
let masterpayload = null;
switch (dp) {
// calls with zone
case 'power':
case 'mute':
case 'surround':
case 'volume':
case 'input':
case 'bass_extension':
case 'enhancer':
case 'direct':
case 'pure_direct':
case 'sound_program':
case 'bass':
case 'treble':
case 'balance':
case 'sleep':
case 'clearVoice':
case 'link_control':
case 'link_audio_delay':
case 'link_audio_quality':
//command with Zone
try {
let value = state.val;
if (dp === 'power') {
value = state.val ? 'on' : 'standby';
}
const result = await yamaha[dpZoneCommands[dp]](value, zone);
if (result.response_code === 0) {
this.log.debug('sent ' + dp + ' succesfully to ' + zone + ' with ' + value);
//await this.setStateAsync(id, true, true);
} else {
this.log.debug('failure ' + dp + ' cmd ' + this.responseFailLog(result));
}
} catch (err) {
this.log.debug('API call failure ' + dp + ' cmd ' + err);
}
break;
//calls without zone
case 'subwoofer_volume':
try {
let value = state.val;
const result = await yamaha[dpZoneCommands[dp]](value);
if (result.response_code === 0) {
this.log.debug('sent ' + dp + ' succesfully with ' + value);
//await this.setStateAsync(id, true, true);
} else {
this.log.debug('failure ' + dp + ' cmd ' + this.responseFailLog(result));
}
} catch (err) {
this.log.debug('API call failure ' + dp + ' cmd ' + err);
}
break;
case 'presetrecallnumber':
/* angeblich soll mit zone der Aufruf gehen, dann muß der Datenpunkt aber in die zonen, ansonsten hat zone=netusb
yamaha.recallPreset(state.val, zone).then((result) => {
if (result.response_code === 0 ){
this.log.debug('recalled the Preset succesfully in zone ' + zone + ' to ' + state.val);
//await this.setStateAsync(id, true, true);
}
else {this.log.debug('failure recalling Preset' + this.responseFailLog(result));}
});
*/
try {
const result = await yamaha[dpCommands[dp]](state.val);
if (result.response_code === 0) {
this.log.debug('sent ' + dp + ' succesfully with ' + state.val);
//await this.setStateAsync(id, true, true);
} else {
this.log.debug('failure ' + dp + ' cmd ' + this.responseFailLog(result));
}
} catch (err) {
this.log.debug('API call failure ' + dp + ' cmd ' + err);
}
break;
case 'recallRecentItem':
try {
const result = await yamaha[dpCommands[dp]](state.val);
if (result.response_code === 0) {
this.log.debug('sent ' + dp + ' succesfully with ' + state.val);
//await this.setStateAsync(id, true, true);
} else {
this.log.debug('failure ' + dp + ' cmd ' + this.responseFailLog(result));
}
} catch (err) {
this.log.debug('API call failure ' + dp + ' cmd ' + err);
}
break;
case 'low':
try {
const result = await yamaha.setEqualizer(state.val, '', '', zone);
if (result.response_code === 0) {
this.log.debug('set equalizer LOW succesfully to ' + zone + ' with ' + state.val);
//await this.setStateAsync(id, true, true);
} else {
this.log.debug('failure setting EQ LOW ' + this.responseFailLog(result));
}
} catch (err) {
this.log.debug('API call failure ' + dp + ' cmd ' + err);
}
break;
case 'mid':
try {
const result = await yamaha.setEqualizer('', state.val, '', zone);
if (result.response_code === 0) {
this.log.debug('set equalizer MID succesfully to ' + zone + ' with ' + state.val);
//await this.setStateAsync(id, true, true);
} else {
this.log.debug('failure setting EQ MID ' + this.responseFailLog(result));
}
} catch (err) {
this.log.debug('API call failure ' + dp + ' cmd ' + err);
}
break;
case 'high':
try {
const result = await yamaha.setEqualizer('', '', state.val, zone);
if (result.response_code === 0) {
this.log.debug('set equalizer HIGH succesfully to ' + zone + ' with ' + state.val);
//await this.setStateAsync(id, true, true);
} else {
this.log.debug('failure setting EQ HIGH ' + this.responseFailLog(result));
}
} catch (err) {
this.log.debug('API call failure ' + dp + ' cmd ' + err);
}
break;
//playback calls with netusb or cd and the action
case 'prev':
case 'next':
case 'stop':
case 'play':
case 'pause':
case 'playPause':
try {
let action = dp;
if (dp === 'prev') action = 'previous';
if (dp === 'playPause') {
//ppstate can be 'stop' or 'play'
const ppstate = await this.getStateAsync(id.replace('playPause', 'playback'));
if (ppstate.val == 'stop') {
action = 'play';
} else {
action = 'stop';
}
}
const result = await yamaha.setPlayback(action, idx);
if (result.response_code === 0) {
this.log.debug('sent ' + dp + ' succesfully to ' + idx);
//await this.setStateAsync(id, true, true); at playback
} else {
this.log.debug('failure ' + dp + ' ' + action + ' cmd ' + this.responseFailLog(result));
}
} catch (err) {
this.log.debug('API call failure ' + dp + ' cmd ' + err);
}
break;
// calls with with netusb or cd
case 'repeat':
case 'shuffle':
try {
const result = await yamaha[dpToggleCommands[dp]](state.val, zone);
if (result.response_code === 0) {
this.log.debug('sent ' + dp + ' succesfully to ' + zone + ' with ' + state.val);
//await this.setStateAsync(id, true, true);
} else {
this.log.debug('failure ' + dp + ' cmd ' + this.responseFailLog(result));
}
} catch (err) {
this.log.debug('API call failure ' + dp + ' cmd ' + err);
}
break;
//distribution
case 'distr_state':
//Start/Stop distribution
//startDistribution(num) als Funktion aufrufen oder hier als
var num = 0;
if (state.val === true || state.val === 'true' || state.val === 'on') {
await yamaha.startDistribution(num).then((result) => {
if (result.response_code === 0) {
this.log.debug('sent Start Distribution');
//await this.setStateAsync(id, true, true);
} else {
this.log.debug('failure sending Start Distribution' + this.responseFailLog(result));
}
});
}
if (state.val === false || state.val === 'false' || state.val === 'off') {
await yamaha.stopDistribution(num).then((result) => {
if (result.response_code === 0) {
this.log.debug('sent Stop Distribution');
//await this.setStateAsync(id, true, true);
} else {
this.log.debug('failure sending Stop Distribution' + this.responseFailLog(result));
}
});
}
break;
case 'add_to_group':
case 'remove_from_group':
//state.val enthält die IP des Masters
// variablendefinition wegen eslint vor dem switch
if (dp === 'add_to_group') {
//addToGroup(state.val, IP[0].ip);
clientIP = IP[0].ip;
this.log.debug('clientIP ' + clientIP + 'ID ' + groupID);
clientpayload = { group_id: groupID, zone: [ 'main' ] };
masterpayload = {
group_id: groupID,
zone: 'main',
type: 'add',
client_list: [ clientIP ]
};
yamaha2 = new YamahaYXC(state.val);
await yamaha.setClientInfo(JSON.stringify(clientpayload)).then((result) => {
if (result.response_code === 0) {
this.log.debug('sent ClientInfo : ' + clientIP);
//await this.setStateAsync(id, true, true);
} else {
this.log.debug('failure sending ClientInfo' + this.responseFailLog(result));
}
});
await yamaha2.setServerInfo(JSON.stringify(masterpayload)).then((result) => {
if (result.response_code === 0) {
this.log.debug('sent ServerInfo ' + state.val);
//await this.setStateAsync(id, true, true);
} else {
this.log.debug('failure sending ServerInfo' + this.responseFailLog(result));
}
});
//Übergabewert soll der Nummer des links entsprechen?!
await yamaha2.startDistribution(0).then((result) => {
if (result.response_code === 0) {
this.log.debug('sent start ServerInfo ' + state.val);
//await this.setStateAsync(id, true, true);
} else {
this.log.debug('failure sending ServerInfo' + this.responseFailLog(result));
}
});
}
if (dp === 'remove_from_group') {
//removeFromGroup(state.val, IP[0].ip);
clientIP = IP[0].ip;
this.log.debug('clientIP ' + clientIP);
clientpayload = { group_id: '', zone: [ 'main' ] };
masterpayload = {
group_id: groupID,
zone: 'main',
type: 'remove',
client_list: [ clientIP ]
};
yamaha2 = new YamahaYXC(state.val);
/* stop distribution scheint zuviel
//Übergabewert soll der Nummer des links entsprechen?!
await yamaha2.stopDistribution(0).then((result) => {
if (result.response_code === 0) {
this.log.debug('sent Stop Distribution');
//await this.setStateAsync(id, true, true);
} else {
this.log.debug('failure sending Stop Distribution' + this.responseFailLog(result));
}
});
*/
await yamaha.setClientInfo(JSON.stringify(clientpayload)).then((result) => {
if (result.response_code === 0) {
this.log.debug('sent Client disconnect to : ' + clientIP);
//await this.setStateAsync(id, true, true);
} else {
this.log.debug('failure sending disconnect' + this.responseFailLog(result));
}
});
await yamaha2.setServerInfo(JSON.stringify(masterpayload)).then((result) => {
if (result.response_code === 0) {
this.log.debug('sent ServerInfo to ' + state.val);
//await this.setStateAsync(id, true, true);
} else {
this.log.debug('failure sending ServerInfo' + this.responseFailLog(result));
}
});
//Übergabewert soll der Nummer des links entsprechen?!
await yamaha2.startDistribution(0).then((result) => {
if (result.response_code === 0) {
this.log.debug('sent start ServerInfo ' + state.val);
//await this.setStateAsync(id, true, true);
} else {
this.log.debug('failure sending ServerInfo' + this.responseFailLog(result));
}
});
}
break;
default:
this.log.warn('Warning command is not processed (no case created for it) ' + dp);
}
} //if status
} else {
// The state was deleted
this.log.info(`state ${id} deleted`);
}
}
// If you need to accept messages in your adapter, uncomment the following block and the corresponding line in the constructor.
// /**
// * Some message was sent to this instance over message box. Used by email, pushover, text2speech, ...
// * Using this method requires "common.messagebox" property to be set to true in io-package.json
// * @param {ioBroker.Message} obj
// */
async onMessage(obj) {
let wait = false;
this.log.debug('messagebox received ' + JSON.stringify(obj));
if (typeof obj === 'object' && obj.message) {
if (obj.command === 'send') {
// e.g. send email or pushover or whatever
this.log.info('msg with obj.command for test received');
// Send response in callback if required
if (obj.callback) this.sendTo(obj.from, obj.command, 'Message received', obj.callback);
}
} else if (obj) {
let result = [];
switch (obj.command) {
case 'browse':
yamaha = new YamahaYXC();
try {
const res = await yamaha.discover();
this.log.debug('result ' + JSON.stringify(res));
result = res;
//result.push({ ip: res[0], name: res[1], type: res[2], uid: res[3] });
this.log.debug('result ' + JSON.stringify(result));
if (obj.callback) this.sendTo(obj.from, obj.command, result, obj.callback);
} catch (error) {
this.log.info('error in sendTo discover() -> ' + error);
if (obj.callback) this.sendTo(obj.from, obj.command, result, obj.callback);
}
wait = true;
break;
case 'jsonreq':
try {
this.log.info('Message SendTo: jsonreq');
const devarray = this.config.devices;
const res = await this.discoverAndGet(devarray);
result.push(res);
this.log.debug('result ' + JSON.stringify(result));
if (obj.callback) this.sendTo(obj.from, obj.command, result, obj.callback);
} catch (error) {
this.log.info('error in sendTo jsonreq() -> ' + error);
if (obj.callback) this.sendTo(obj.from, obj.command, result, obj.callback);
}
if (obj.callback) this.sendTo(obj.from, obj.command, responses, obj.callback); //responses wird sukzessive mit den get-Aufrufen befüllt
wait = true;
break;
default:
this.log.warn('Received Mesage with Unknown command: ' + obj.command);
break;
}
}
if (!wait && obj.callback) {
this.log.debug('messagebox landed in last evaluation wait=false and callback');
this.sendTo(obj.from, obj.command, obj.message, obj.callback);
}
return true;
}
async discoverAndGet(devicearray) {
let found = [];
try {
if (devicearray) {
await Promise.all(
devicearray.map(async (device) => {
let data = {};
data[device.name] = {};
const yamaha = new YamahaYXC(device.ip);
data[device.name]['system'] = {};
const getDeviceInfo = await yamaha.getDeviceInfo();
data[device.name]['system']['getDeviceInfo'] = getDeviceInfo;
const getNetworkStatus = await yamaha.getNetworkStatus();
data[device.name]['system']['getNetworkStatus'] = getNetworkStatus;
const getFuncStatus = await yamaha.getFuncStatus();
data[device.name]['system']['getFuncStatus'] = getFuncStatus;
const getLocationInfo = await yamaha.getLocationInfo();
data[device.name]['system']['getLocationInfo'] = getLocationInfo;
const getFeatures = await yamaha.getFeatures();
data[device.name]['system']['getFeatures'] = getFeatures;
data[device.name]['dist'] = {};
const getDistributionInfo = await yamaha.getDistributionInfo();
data[device.name]['dist']['getFeatures'] = getDistributionInfo;
if (getFeatures['netusb']) {
data[device.name]['netusb'] = {};
const getNetPlayInfo = await yamaha.getPlayInfo();
data[device.name]['netusb']['getPlayInfo'] = getNetPlayInfo;
const getPresetInfo = await yamaha.getPresetInfo();
data[device.name]['netusb']['getPresetInfo'] = getPresetInfo;
const getSettings = await yamaha.getSettings();
data[device.name]['netusb']['getSettings'] = getSettings;
const getRecentInfo = await yamaha.getRecentInfo();
data[device.name]['netusb']['getRecentInfo'] = getRecentInfo;
}
if (getFeatures['tuner']) {
data[device.name]['tuner'] = {};
const getTunerPlayInfo = await yamaha.getPlayInfo('tuner');
data[device.name]['tuner']['getPlayInfo'] = getTunerPlayInfo;
const getTunerPresetInfo = await yamaha.getTunerPresetInfo();
data[device.name]['tuner']['getPresetInfo'] = getTunerPresetInfo;
}
if (getFeatures['cd']) {
data[device.name]['cd'] = {};
const getCdPlayInfo = await yamaha.getPlayInfo('cd');
data[device.name]['cd']['getPlayInfo'] = getCdPlayInfo;
}
if (getFeatures['clock']) {
data[device.name]['clock'] = {};
const getClockSettings = await yamaha.getClockSettings();
data[device.name]['clock']['getSettings'] = getClockSettings;
}
if (getFeatures['zone']) {
await Promise.all(
getFeatures['zone'].map(async (zone) => {
data[device.name][zone.id] = {};
const getStatus = await yamaha.getStatus(zone.id);
data[device.name][zone.id]['getStatus'] = getStatus;
const getSoundProgramList = await yamaha.getSoundProgramList(zone.id);
data[device.name][zone.id]['getSoundProgramList'] = getSoundProgramList;
const getSignalInfo = await yamaha.getSignalInfo(zone.id);
data[device.name][zone.id]['getSignalInfo'] = getSignalInfo;
})
);
}
found.push(data);
})
);
return Promise.resolve(found);
}
} catch (error) {
return Promise.reject(error);
}
}
responseFailLog(fail) {
let errcode = '';
switch (fail.response_code) {
case 1:
errcode = 'Response : 1 Initializing';
break;
case 2:
errcode = 'Response : 2 Internal Error';
break;
case 3:
errcode = 'Response : 3 Invalid Request (A method did not exist, a method wasn’t appropriate etc.)';
break;
case 4:
errcode = 'Response : 4 Invalid Parameter (Out of range, invalid characters etc.)';
break;
case 5:
errcode = 'Response : 5 Guarded (Unable to setup in current status etc.)';
break;
case 6:
errcode = 'Response : 6 Time Out';
break;
case 99:
errcode = 'Response : 99 Firmware Updating';
break;
//Streaming Service Errors
case 100:
errcode = 'Response : 100 Access Error Streaming Service';
break;
case 101:
errcode = 'Response : 101 Other Errors Streaming Service';
break;
case 102:
errcode = 'Response : 102 Wrong User Name Streaming Service';
break;
case 103:
errcode = 'Response : 103 Wrong Password Streaming Service';
break;
case 104:
errcode = 'Response : 104 Account Expired Streaming Service';
break;
case 105:
errcode = 'Response : 105 Account Disconnected/Gone Off/Shut Down Streaming Service';
break;
case 106:
errcode = 'Response : 106 Account Number Reached to the Limit Streaming Service';
break;
case 107:
errcode = 'Response : 107 Server Maintenance Streaming Service';
break;
case 108:
errcode = 'Response : 108 Invalid Account Streaming Service';
break;
case 109:
errcode = 'Response : 109 License Error Streaming Service';
break;
case 110:
errcode = 'Response : 110 Read Only Mode Streaming Service';
break;
case 111:
errcode = 'Response : 111 Max Stations Streaming Service';
break;
case 112:
errcode = 'Response : 112 Access Denied Streaming Service';
break;
case 113:
errcode = 'Response : 113 There is a need to specify the additional destination Playlist';
break;
case 114:
errcode = 'Response : 114 There is a need to create a new Playlist';
break;
case 115:
errcode = 'Response : 115 Simultaneous logins has reached the upper limit';
break;
case 200:
errcode = 'Response : 200 Linking in progress';
break;
case 201:
errcode = 'Response : 115 Unlinking in progress';
break;
default:
errcode = 'unknown code';
}
return errcode;
}
/*
browse(callback) {
const result = [];
result.push({ ip: '192.168.178.52', name: 'Wohnzimmer', type: 'YSP-1600', uid: '0B587073' });
result.push({ ip: '192.168.178.56', name: 'Küche', type: 'WX-030', uid: '0E257883' });
if (callback) callback(result);
}
*/
getConfigObjects(Obj, where, what) {
const foundObjects = [];
for (const prop in Obj) {
if (Obj[prop][where] == what) {
foundObjects.push(Obj[prop]);
}
}
return foundObjects;
}
async defineMusicDevice(type, uid, name) {
this.log.info('Setting up System :' + type + '-' + uid);
await this.setObjectNotExistsAsync(type + '_' + uid, {
type: 'device',
common: {
name: 'MusicCast ' + type + ' ' + name,
role: 'device'
},
native: {
addr: uid
}
});
await this.setObjectNotExistsAsync(type + '_' + uid + '.system', {
type: 'channel',
common: {
name: 'MusicCast System Info',
role: 'sensor'
},
native: {
addr: uid
}
});
await this.setObjectNotExistsAsync(type + '_' + uid + '.online', {
type: 'state',
common: {
name: 'Online',
type: 'boolean',
read: true,
write: false,
role: 'value',
desc: 'Online',
def: false
},
native: {}
});
await this.setObjectNotExistsAsync(type + '_' + uid + '.system.api_version', {
type: 'state',
common: {
name: 'API Version',
type: 'number',
read: true,
write: false,
role: 'value',
desc: 'API Version'
},
native: {}
});
await this.setObjectNotExistsAsync(type + '_' + uid + '.system.system_version', {
type: 'state',
common: {
name: 'System Version',
type: 'number',
read: true,
write: false,
role: 'value',
desc: 'System Version'
},
native: {}
});
await this.setObjectNotExistsAsync(type + '_' + uid + '.system.system_id', {
type: 'state',
common: {
name: 'System ID',
type: 'string',
read: true,
write: false,
role: 'text',
desc: 'System ID'
},
native: {}
});
await this.setObjectNotExistsAsync(type + '_' + uid + '.system.device_id', {
type: 'state',
common: {
name: 'Device ID',
type: 'string',
read: true,
write: false,
role: 'text',
desc: 'Device ID'
},
native: {}
});
await this.setObjectNotExistsAsync(type + '_' + uid + '.system.getDeviceInfo', {
type: 'state',
common: {
name: 'Feedback of getDeviceInfo',
type: 'object',
read: true,
write: false,
role: 'list',
desc: 'Feedback of getDeviceInfo'
},
native: {}
});
await this.setObjectNotExistsAsync(type + '_' + uid + '.system.getFeatures', {
type: 'state',
common: {
name: 'Feedback of getFeatures',
type: 'object',
read: true,
write: false,
role: 'list',
desc: 'Feedback of getFeatures'
},
native: {}
});
}
async defineMusicZoneNew(type, uid, zone, zone_arr) {
this.log.info('Setting up Zone:' + zone + ' of ' + type + '-' + uid);
await this.setObjectNotExistsAsync(type + '_' + uid + '.' + zone, {
type: 'channel',
common: {
name: 'MusicCast Zone ' + type,
role: 'sensor'
},
native: {
addr: uid
}
});
this.log.info('Setting up Zone:' + zone + ' of ' + type + '-' + uid);
/*
if (zone_arr.zone_b){
this.log.debug('zone b dabei');
await this.setObjectNotExistsAsync(type + '_' + uid + '.' + zone + '.zone_b', {
type: 'state',
common: {
"name": "Zone B",
"type": "boolean",
"read": true,
"write": true,
"role": "value",
"desc": "Zone B"
},
native: {}
});
} else this.log.debug('zone b nicht dabei');
*/
await this.setObjectNotExistsAsync(type + '_' + uid + '.' + zone + '.getStatus', {
type: 'state',
common: {
name: 'Feedback of getStatus',
type: 'object',
read: true,
write: false,
role: 'list',
desc: 'Feedback of getStatus'
},
native: {}
});
await this.setObjectNotExistsAsync(type + '_' + uid + '.' + zone + '.disable_flags', {
type: 'state',
common: {
name: 'disable_flags',
type: 'number',
read: true,
write: false,
role: 'level',
desc: 'disable_flags'
},
native: {}
});
if (zone_arr.func_list.indexOf('volume') !== -1) {
await this.setObjectNotExistsAsync(type + '_' + uid + '.' + zone + '.volume', {
type: 'state',
common: {
name: 'Volume',
type: 'number',
min:
zone_arr.range_step[
zone_arr.range_step.findIndex(function(row) {
return row.id == 'volume';
})
].min,
max:
zone_arr.range_step[