-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
index.js
2635 lines (2136 loc) · 90.4 KB
/
index.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
import * as fs from 'fs';
import mkdirp from 'mkdirp';
import LgTvController from './lib/LgTvController.js';
import Events from './lib/Events.js';
let Service, Characteristic, Homebridge, Accessory, HapStatusError, HAPStatus, HAPStorage;
const PLUGIN_NAME = 'homebridge-webos-tv';
const PLATFORM_NAME = 'webostv';
const PLUGIN_VERSION = '2.4.6';
// General constants
const NOT_EXISTING_INPUT = 999999;
const DEFAULT_INPUT_SOURCES_LIMIT = 45;
const BUTTON_RESET_TIMEOUT = 20; // in milliseconds
const AUTOMATIONS_TRIGGER_TIMEOUT = 400; // in milliseconds
export default (homebridge) => {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
Homebridge = homebridge;
Accessory = homebridge.platformAccessory;
HapStatusError = homebridge.hap.HapStatusError;
HAPStatus = homebridge.hap.HAPStatus;
HAPStorage = homebridge.hap.HAPStorage;
homebridge.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, webosTvPlatform, true);
};
class webosTvDevice {
constructor(log, config, api) {
this.log = log;
this.api = api;
// check if we have mandatory device info
try {
if (!config.ip) throw new Error(`TV ip address is required for ${config.name}`);
if (!config.mac) throw new Error(`TV mac address is required for ${config.name}`);
} catch (error) {
this.logError(error);
this.logError(`Failed to create platform device, missing mandatory information!`);
this.logError(`Please check your device config!`);
return;
}
// configuration
this.name = config.name || 'webOS TV';
this.ip = config.ip;
this.mac = config.mac;
this.broadcastAdr = config.broadcastAdr || '255.255.255.255';
this.keyFile = config.keyFile;
this.prefsDir = config.prefsDir || api.user.storagePath() + '/.webosTv/';
this.alivePollingInterval = config.pollingInterval || 5;
this.alivePollingInterval = this.alivePollingInterval * 1000;
this.deepDebugLog = config.deepDebugLog;
this.silentLog = config.silentLog;
if (this.deepDebugLog === undefined) {
this.deepDebugLog = false;
}
if (this.silentLog === undefined) {
this.silentLog = false;
}
this.inputSourcesLimit = config.inputSourcesLimit || DEFAULT_INPUT_SOURCES_LIMIT;
this.isHideTvService = config.hideTvService;
if (this.isHideTvService === undefined) {
this.isHideTvService = false;
}
this.volumeLimit = config.volumeLimit;
if (this.volumeLimit === undefined || isNaN(this.volumeLimit) || this.volumeLimit < 0) {
this.volumeLimit = 100;
}
this.volumeControl = config.volumeControl;
if (this.volumeControl === undefined) {
this.volumeControl = "both";
}
this.channelControl = config.channelControl;
if (this.channelControl === undefined) {
this.channelControl = true;
}
this.mediaControl = config.mediaControl;
if (this.mediaControl === undefined) {
this.mediaControl = false;
}
this.screenControl = config.screenControl;
if (this.screenControl === undefined) {
this.screenControl = false;
}
this.screenSaverControl = config.screenSaverControl;
if (this.screenSaverControl === undefined) {
this.screenSaverControl = false;
}
this.serviceMenuButton = config.serviceMenuButton;
if (this.serviceMenuButton === undefined) {
this.serviceMenuButton = false;
}
this.ezAdjustButton = config.ezAdjustButton;
if (this.ezAdjustButton === undefined) {
this.ezAdjustButton = false;
}
this.backlightControl = config.backlightControl;
if (this.backlightControl === undefined) {
this.backlightControl = false;
}
this.brightnessControl = config.brightnessControl;
if (this.brightnessControl === undefined) {
this.brightnessControl = false;
}
this.colorControl = config.colorControl;
if (this.colorControl === undefined) {
this.colorControl = false;
}
this.contrastControl = config.contrastControl;
if (this.contrastControl === undefined) {
this.contrastControl = false;
}
this.ccRemoteRemap = config.ccRemoteRemap;
if (this.ccRemoteRemap === undefined) {
this.ccRemoteRemap = {};
}
this.appButtons = config.appButtons;
this.channelButtons = config.channelButtons;
this.notificationButtons = config.notificationButtons;
this.remoteControlButtons = config.remoteControlButtons;
this.soundOutputButtons = config.soundOutputButtons;
this.remoteSequenceButtons = config.remoteSequenceButtons;
this.pictureModeButtons = config.pictureModeButtons;
this.soundModeButtons = config.soundModeButtons;
this.systemSettingsButtons = config.systemSettingsButtons;
this.triggers = config.triggers;
this.logInfo(`Init - got TV configuration, initializing device with name: ${this.name}`);
// check if input sources limit is within a reasonable range
if (this.inputSourcesLimit < 10) {
this.inputSourcesLimit = 10;
}
if (this.inputSourcesLimit > 65) {
this.inputSourcesLimit = 65;
}
// check if prefs directory ends with a /, if not then add it
if (this.prefsDir.endsWith('/') === false) {
this.prefsDir = this.prefsDir + '/';
}
// check if the tv preferences directory exists, if not then create it
if (fs.existsSync(this.prefsDir) === false) {
mkdirp(this.prefsDir);
}
// generate the key file name for the TV if not specified
if (this.keyFile === undefined) {
this.keyFile = this.prefsDir + 'keyFile_' + this.ip.split('.').join('') + '_' + this.mac.split(':').join('');
}
// prepare file paths
this.tvInfoFile = this.prefsDir + 'info_' + this.mac.split(':').join('');
this.tvAvailableInputsFile = this.prefsDir + 'inputsAvailable_' + this.mac.split(':').join('');
this.tvInputConfigFile = this.prefsDir + 'inputsConfg_' + this.mac.split(':').join('');
//prepare variables
this.dummyInputSourceServices = [];
this.configuredInputs = {};
this.tvInputsConfig = {};
// connect to the TV
this.connectToTv();
// init the tv accessory
this.initTvAccessory();
}
/*----------========== SETUP TV DEVICE ==========----------*/
connectToTv() {
// create new tv instance and try to connect
this.lgTvCtrl = new LgTvController(this.ip, this.mac, this.name, this.keyFile, this.broadcastAdr, this.alivePollingInterval, this.log);
this.lgTvCtrl.setVolumeLimit(this.volumeLimit);
this.lgTvCtrl.setDeepDebugLogEnabled(this.deepDebugLog);
this.lgTvCtrl.setSilentLogEnabled(this.silentLog);
this.lgTvCtrl.connect();
//register to listeners
this.lgTvCtrl.on(Events.SETUP_FINISHED, () => {
this.logInfo('TV setup finished, ready to control tv');
// add external inputs
this.initInputSources();
// remove the information service here and add the new one after setup is complete, this way i do not have to save anything?
this.updateInformationService();
});
this.lgTvCtrl.on(Events.TV_TURNED_ON, () => {
this.updateTvStatusFull();
});
this.lgTvCtrl.on(Events.TV_TURNED_OFF, () => {
this.updateTvStatusFull();
});
this.lgTvCtrl.on(Events.PIXEL_REFRESHER_STARTED, () => {
this.updateTvStatusFull();
});
this.lgTvCtrl.on(Events.SCREEN_SAVER_TURNED_ON, () => {
this.updateScreenSaverStatus();
});
this.lgTvCtrl.on(Events.SCREEN_STATE_CHANGED, () => {
this.updateScreenStatus();
});
this.lgTvCtrl.on(Events.POWER_STATE_CHANGED, () => {
this.updatePowerStatus();
this.updateScreenStatus();
this.updateScreenSaverStatus();
});
this.lgTvCtrl.on(Events.FOREGROUND_APP_CHANGED, (res) => {
this.updateActiveInputSource();
this.updateAppButtons();
this.updateChannelButtons();
});
this.lgTvCtrl.on(Events.AUDIO_STATUS_CHANGED, () => {
this.updateTvAudioStatus();
this.updateOccupancyTriggers();
});
this.lgTvCtrl.on(Events.LIVE_TV_CHANNEL_CHANGED, () => {
this.updateChannelButtons();
});
this.lgTvCtrl.on(Events.SOUND_OUTPUT_CHANGED, () => {
this.updateSoundOutputButtons();
});
this.lgTvCtrl.on(Events.NEW_APP_ADDED, (res) => {
if (res) {
this.newAppInstalledOnTv(res);
}
});
this.lgTvCtrl.on(Events.APP_REMOVED, (res) => {
if (res) {
this.appRemovedFromTv(res.appId);
}
});
this.lgTvCtrl.on(Events.VOLUME_UP, () => {
this.triggerVolumeUpAutomations();
});
this.lgTvCtrl.on(Events.VOLUME_DOWN, (res) => {
this.triggerVolumeDownAutomations();
});
this.lgTvCtrl.on(Events.PICTURE_SETTINGS_CHANGED, (res) => {
this.updatePictureSettingsServices();
this.updateOccupancyTriggers();
if (this.lgTvCtrl.getCurrentPictureMode()) {
this.updatePictureModeButtons();
}
});
this.lgTvCtrl.on(Events.SOUND_SETTINGS_CHANGED, (res) => {
if (this.lgTvCtrl.getCurrentSoundMode()) {
this.updateSoundModeButtons();
}
});
Events.SOUND_SETTINGS_CHANGED
}
/*----------========== SETUP SERVICES ==========----------*/
initTvAccessory() {
// generate uuid
this.UUID = Homebridge.hap.uuid.generate(this.mac + this.ip);
// prepare the tv accessory
this.tvAccesory = new Accessory(this.name, this.UUID, Homebridge.hap.Categories.TELEVISION);
// prepare accessory services
this.setupAccessoryServices();
this.api.publishExternalAccessories(PLUGIN_NAME, [this.tvAccesory]);
}
setupAccessoryServices() {
// update the services
this.updateInformationService();
// prepare the tv service
if (this.isHideTvService === false) {
this.prepareTvService();
}
// additional services
this.prepareVolumeService();
this.prepareChannelControlService();
this.prepareMediaControlService();
this.prepareScreenControlService();
this.prepareScreenSaverControlService();
this.preparServiceMenuButtonService();
this.prepareEzAdjustButtonService();
this.preparePictureSettingsControlServices();
this.prepareAppButtonService();
this.prepareChannelButtonService();
this.prepareNotificationButtonService();
this.prepareRemoteControlButtonService();
this.prepareSoundOutputButtonService();
this.prepareRemoteSequenceButtonsService();
this.preparePictureModeButtonService();
this.prepareSoundModeButtonService();
this.prepareSystemSettingsButtonService();
this.prepareTriggersService();
}
//
// tv information service ----------------------------------------------------------------
updateInformationService() {
let modelName = this.lgTvCtrl.getTvSystemInfo() ? this.lgTvCtrl.getTvSystemInfo().modelName : 'Unknown';
let productName = this.lgTvCtrl.getTvSwInfo() ? `${this.lgTvCtrl.getTvSwInfo().product_name} (${PLUGIN_VERSION})` : PLUGIN_VERSION;
let tvFirmwareVer = this.lgTvCtrl.getTvSwInfo() ? this.lgTvCtrl.getTvSwInfo().major_ver + '.' + this.lgTvCtrl.getTvSwInfo().minor_ver : 'Unknown';
// remove the preconstructed information service, since i will be adding my own
this.tvAccesory.removeService(this.tvAccesory.getService(Service.AccessoryInformation));
// add my own information service
let informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Name, this.name)
.setCharacteristic(Characteristic.Manufacturer, 'LG Electronics')
.setCharacteristic(Characteristic.Model, modelName)
.setCharacteristic(Characteristic.SerialNumber, productName)
.setCharacteristic(Characteristic.FirmwareRevision, tvFirmwareVer);
this.tvAccesory.addService(informationService);
}
// native tv services ----------------------------------------------------------------
prepareTvService() {
this.tvService = new Service.Television(this.name, 'tvService');
this.tvService
.setCharacteristic(Characteristic.SleepDiscoveryMode, Characteristic.SleepDiscoveryMode.ALWAYS_DISCOVERABLE);
this.tvService
.getCharacteristic(Characteristic.Active)
.onGet(this.getPowerState.bind(this))
.onSet(this.setPowerState.bind(this));
this.setServiceConfiguredName(this.tvService, this.name);
this.tvService
.setCharacteristic(Characteristic.ActiveIdentifier, NOT_EXISTING_INPUT); // do not preselect any inputs since there are no default inputs
this.tvService
.getCharacteristic(Characteristic.ActiveIdentifier)
.onGet(this.getActiveIdentifier.bind(this))
.onSet(this.setActiveIdentifier.bind(this));
this.tvService
.getCharacteristic(Characteristic.RemoteKey)
.onSet(this.remoteKeyPress.bind(this));
this.tvService
.getCharacteristic(Characteristic.PowerModeSelection)
.onSet(this.setPowerModeSelection.bind(this));
// not supported yet??
/*
this.tvService
.getCharacteristic(Characteristic.PictureMode)
.onSet((newValue) => {
console.log('set PictureMode => setNewValue: ' + newValue);
});
*/
this.tvAccesory.addService(this.tvService);
// prepare the additional native services - control center tv speaker and inputs
this.prepareTvSpeakerService();
this.prepareInputSourcesService();
}
prepareTvSpeakerService() {
const serviceName = this.name + ' Volume';
this.tvSpeakerService = new Service.TelevisionSpeaker(serviceName, 'tvSpeakerService');
this.tvSpeakerService
.setCharacteristic(Characteristic.Active, Characteristic.Active.ACTIVE)
.setCharacteristic(Characteristic.VolumeControlType, Characteristic.VolumeControlType.ABSOLUTE);
this.tvSpeakerService
.getCharacteristic(Characteristic.VolumeSelector)
.onSet(this.setVolumeSelectorState.bind(this));
this.tvSpeakerService
.getCharacteristic(Characteristic.Mute)
.onGet(this.getMuteState.bind(this))
.onSet(this.setMuteState.bind(this));
this.tvSpeakerService
.addCharacteristic(Characteristic.Volume)
.onGet(this.getVolume.bind(this))
.onSet(this.setVolume.bind(this));
this.setServiceConfiguredName(this.tvSpeakerService, serviceName);
this.tvService.addLinkedService(this.tvSpeakerService);
this.tvAccesory.addService(this.tvSpeakerService);
}
prepareInputSourcesService() {
// create dummy inputs
for (var i = 0; i < this.inputSourcesLimit; i++) {
let inputId = i;
let dummyInputSource = new Service.InputSource('dummy', `input_${inputId}`);
dummyInputSource
.setCharacteristic(Characteristic.Identifier, inputId)
.setCharacteristic(Characteristic.IsConfigured, Characteristic.IsConfigured.NOT_CONFIGURED)
.setCharacteristic(Characteristic.TargetVisibilityState, Characteristic.TargetVisibilityState.HIDDEN)
.setCharacteristic(Characteristic.CurrentVisibilityState, Characteristic.CurrentVisibilityState.HIDDEN);
this.setServiceConfiguredName(dummyInputSource, 'dummy');
// add the new dummy input source service to the tv accessory
this.tvService.addLinkedService(dummyInputSource);
this.tvAccesory.addService(dummyInputSource);
// keep references to all free dummy input services
this.dummyInputSourceServices.push(dummyInputSource);
}
// read out the saved tv inputs
let availableInputs = [];
try {
availableInputs = JSON.parse(fs.readFileSync(this.tvAvailableInputsFile));
} catch (err) {
this.logDebug('The TV has no configured inputs yet!');
}
// read out the tv input sources config
try {
this.tvInputsConfig = JSON.parse(fs.readFileSync(this.tvInputConfigFile));
} catch (err) {
this.logDebug('No TV inputs config file found!');
}
// add the saved inputs
//Note to myself, i am saving the inputs in a file as a cache in order when the user starts homebridge and the tv is off that the cached inputs got added already.
this.addInputSources(availableInputs);
}
addInputSources(inputSourcesList) {
// if the tv service is hidden then we cannot add any input sources so just skip
if (this.isHideTvService) {
return;
}
// make sure we always have an array here
if (!inputSourcesList || Array.isArray(inputSourcesList) === false) {
inputSourcesList = [];
}
this.logDebug(`Adding ${inputSourcesList.length} new input sources!`);
for (let value of inputSourcesList) {
if (this.dummyInputSourceServices.length === 0) {
this.logWarn(`Inputs limit (${this.inputSourcesLimit}) reached. Cannot add any more new inputs!`);
break;
}
var inputSourceService = this.dummyInputSourceServices.shift(); // get the first free input source service
// create a new input definition
let newInputDef = {};
// get appId
newInputDef.appId = value.appId;
// if appId null or empty then skip this input, appId is required to open an app
if (!newInputDef.appId || newInputDef.appId === '' || typeof newInputDef.appId !== 'string') {
this.logWarn(`Missing appId or appId is not of type string. Cannot add input source!`);
return;
}
// remove all white spaces from the appId string
newInputDef.appId = newInputDef.appId.replace(/\s/g, '');
//appId
newInputDef.appId = newInputDef.appId;
// name (name - input config, label - auto generated inputs)
newInputDef.name = value.name || value.label || newInputDef.appId;
// if we have a saved name in the input sources config then use that
if (this.tvInputsConfig[newInputDef.appId] && this.tvInputsConfig[newInputDef.appId].name) {
newInputDef.name = this.tvInputsConfig[newInputDef.appId].name;
}
// params
newInputDef.params = value.params || {};
//input Identifier
newInputDef.id = inputSourceService.getCharacteristic(Characteristic.Identifier).value;
let visible = false;
if (this.tvInputsConfig[newInputDef.appId] && this.tvInputsConfig[newInputDef.appId].visible === true) {
visible = true;
}
inputSourceService
.setCharacteristic(Characteristic.Name, newInputDef.name)
.setCharacteristic(Characteristic.IsConfigured, Characteristic.IsConfigured.CONFIGURED)
.setCharacteristic(Characteristic.InputSourceType, Characteristic.InputSourceType.APPLICATION)
.setCharacteristic(Characteristic.TargetVisibilityState, visible ? Characteristic.TargetVisibilityState.SHOWN : Characteristic.TargetVisibilityState.HIDDEN)
.setCharacteristic(Characteristic.CurrentVisibilityState, visible ? Characteristic.CurrentVisibilityState.SHOWN : Characteristic.CurrentVisibilityState.HIDDEN);
this.setServiceConfiguredName(inputSourceService, newInputDef.name);
// set visibility state
inputSourceService.getCharacteristic(Characteristic.TargetVisibilityState)
.onSet((state) => {
this.setInputTargetVisibility(state, newInputDef);
});
// set input name
inputSourceService.getCharacteristic(Characteristic.ConfiguredName)
.onSet((value) => {
this.setInputConfiguredName(value, newInputDef);
});
// add a reference to the input source to the new input and add it to the configured inputs list
newInputDef.inputService = inputSourceService;
this.configuredInputs[newInputDef.id] = newInputDef;
this.logDebug(`Created new input source: appId: ${newInputDef.appId}, name: ${newInputDef.name}`);
}
}
removeInputSource(inputDef) {
// removed it from configured inptuts
delete this.configuredInputs[inputDef.id];
// reset dummy info
let inputService = inputDef.inputService;
inputService
.setCharacteristic(Characteristic.Name, 'dummy')
.setCharacteristic(Characteristic.IsConfigured, Characteristic.IsConfigured.NOT_CONFIGURED)
.setCharacteristic(Characteristic.TargetVisibilityState, Characteristic.TargetVisibilityState.HIDDEN)
.setCharacteristic(Characteristic.CurrentVisibilityState, Characteristic.CurrentVisibilityState.HIDDEN);
this.setServiceConfiguredName(inputService, 'dummy');
// readd to the dummy list as free
this.dummyInputSourceServices.push(inputService);
}
// additional services ----------------------------------------------------------------
prepareVolumeService() {
if (!this.volumeControl || this.volumeControl === "none") {
return;
}
// slider - lightbulb or fan
if (this.volumeControl === true || this.volumeControl === "both" || this.volumeControl === 'slider' || this.volumeControl === 'lightbulb') {
const serviceName = this.name + ' Volume';
this.volumeAsLightbulbService = new Service.Lightbulb(serviceName, 'volumeService');
this.volumeAsLightbulbService
.getCharacteristic(Characteristic.On)
.onGet(this.getLightbulbMuteState.bind(this))
.onSet(this.setLightbulbMuteState.bind(this));
this.volumeAsLightbulbService
.addCharacteristic(new Characteristic.Brightness())
.onGet(this.getLightbulbVolume.bind(this))
.onSet(this.setLightbulbVolume.bind(this));
this.setServiceConfiguredName(this.volumeAsLightbulbService, serviceName);
this.tvAccesory.addService(this.volumeAsLightbulbService);
} else if (this.volumeControl === "fan") {
const serviceName = this.name + ' Volume';
this.volumeAsFanService = new Service.Fanv2(serviceName, 'volumeService');
this.volumeAsFanService
.getCharacteristic(Characteristic.Active)
.onGet(this.getFanMuteState.bind(this))
.onSet(this.setFanMuteState.bind(this));
this.volumeAsFanService.addCharacteristic(Characteristic.RotationSpeed)
.onGet(this.getRotationSpeedVolume.bind(this))
.onSet(this.setRotationSpeedVolume.bind(this));
this.setServiceConfiguredName(this.volumeAsFanService, serviceName);
this.tvAccesory.addService(this.volumeAsFanService);
}
// volume up/down buttons
if (this.volumeControl === true || this.volumeControl === "both" || this.volumeControl === 'buttons') {
this.volumeUpService = this.createStatlessSwitchService('Volume Up', 'volumeUpService', this.setVolumeUp.bind(this));
this.tvAccesory.addService(this.volumeUpService);
this.volumeDownService = this.createStatlessSwitchService('Volume Down', 'volumeDownService', this.setVolumeDown.bind(this));
this.tvAccesory.addService(this.volumeDownService);
}
}
prepareChannelControlService() {
if (!this.channelControl) {
return;
}
this.channelUpService = this.createStatlessSwitchService('Channel Up', 'channelUpService', this.setChannelUp.bind(this));
this.tvAccesory.addService(this.channelUpService);
this.channelDownService = this.createStatlessSwitchService('Channel Down', 'channelDownService', this.setChannelDown.bind(this));
this.tvAccesory.addService(this.channelDownService);
}
prepareMediaControlService() {
if (!this.mediaControl) {
return;
}
this.mediaPlayService = this.createStatlessSwitchService('Play', 'mediaPlayService', this.setPlay.bind(this));
this.tvAccesory.addService(this.mediaPlayService);
this.mediaPauseService = this.createStatlessSwitchService('Pause', 'mediaPauseService', this.setPause.bind(this));
this.tvAccesory.addService(this.mediaPauseService);
this.mediaStopService = this.createStatlessSwitchService('Stop', 'mediaStopService', this.setStop.bind(this));
this.tvAccesory.addService(this.mediaStopService);
this.mediaRewindService = this.createStatlessSwitchService('Rewind', 'mediaRewindService', this.setRewind.bind(this));
this.tvAccesory.addService(this.mediaRewindService);
this.mediaFastForwardService = this.createStatlessSwitchService('Fast Forward', 'mediaFastForwardService', this.setFastForward.bind(this));
this.tvAccesory.addService(this.mediaFastForwardService);
}
prepareScreenControlService() {
if (!this.screenControl) {
return;
}
// create the service
this.screenControlService = new Service.Switch('Screen', 'screenControlService');
this.screenControlService
.getCharacteristic(Characteristic.On)
.onGet(this.getTvScreenState.bind(this))
.onSet(this.setTvScreenState.bind(this));
this.setServiceConfiguredName(this.screenControlService, 'Screen');
this.tvAccesory.addService(this.screenControlService);
}
prepareScreenSaverControlService() {
if (!this.screenSaverControl) {
return;
}
// create the service
this.screenSaverControlService = new Service.Switch('Screen Saver', 'screenSaverControlService');
this.screenSaverControlService
.getCharacteristic(Characteristic.On)
.onGet(this.getScreenSaverState.bind(this))
.onSet(this.setScreenSaverState.bind(this));
this.setServiceConfiguredName(this.screenSaverControlService, 'Screen Saver');
this.tvAccesory.addService(this.screenSaverControlService);
}
preparServiceMenuButtonService() {
if (!this.serviceMenuButton) {
return;
}
this.serviceMenuButtonService = this.createStatlessSwitchService('Service Menu', 'serviceMenuButtonService', this.setServiceMenu.bind(this));
this.tvAccesory.addService(this.serviceMenuButtonService);
}
prepareEzAdjustButtonService() {
if (!this.ezAdjustButton) {
return;
}
this.ezAdjustButtonService = this.createStatlessSwitchService('ezAdjust', 'ezAdjustButtonService', this.setEzAdjust.bind(this));
this.tvAccesory.addService(this.ezAdjustButtonService);
}
preparePictureSettingsControlServices() {
if (this.backlightControl) {
this.backlightControlService = this.createPictureSettingsLightbulbService('Backlight', 'backlightControlService', this.setLightbulbBacklightOnState, this.setLightbulbBacklight, this.getLightbulbBacklight, );
this.tvAccesory.addService(this.backlightControlService);
}
if (this.brightnessControl) {
this.brightnessControlService = this.createPictureSettingsLightbulbService('Brightness', 'brightnessControlService', this.setLightbulbBrightnessOnState, this.setLightbulbBrightness, this.getLightbulbBrightness, );
this.tvAccesory.addService(this.brightnessControlService);
}
if (this.colorControl) {
this.colorControlService = this.createPictureSettingsLightbulbService('Color', 'colorControlService', this.setLightbulbColorOnState, this.setLightbulbColor, this.getLightbulbColor, );
this.tvAccesory.addService(this.colorControlService);
}
if (this.contrastControl) {
this.contrastControlService = this.createPictureSettingsLightbulbService('Contrast', 'contrastControlService', this.setLightbulbContrastOnState, this.setLightbulbContrast, this.getLightbulbContrast, );
this.tvAccesory.addService(this.contrastControlService);
}
}
prepareAppButtonService() {
if (this.checkArrayConfigProperty(this.appButtons, "appButtons") === false) {
return;
}
this.configuredAppButtons = {};
this.appButtons.forEach((value, i) => {
// create a new app button definition
let newAppButtonDef = {};
// get appid
newAppButtonDef.appId = value.appId || value;
// if appId null or empty then skip this app button, appId is required to open an app
if (!newAppButtonDef.appId || newAppButtonDef.appId === '' || typeof newAppButtonDef.appId !== 'string') {
this.logWarn(`Missing appId or appId in not of type string. Cannot add app button!`);
return;
}
// remove all white spaces from the appId string
newAppButtonDef.appId = newAppButtonDef.appId.replace(/\s/g, '');
// get name
newAppButtonDef.name = value.name || 'App ' + newAppButtonDef.appId;
// params
newAppButtonDef.params = value.params || {};
// create the service
let newAppButtonService = this.createStatefulSwitchService(newAppButtonDef.name, 'appButtonService' + i,
() => {
return this.getAppButtonState(newAppButtonDef.appId);
}, (state) => {
this.setAppButtonState(state, newAppButtonDef);
});
// add to the tv service
this.tvAccesory.addService(newAppButtonService);
// save the configured channel button service
newAppButtonDef.switchService = newAppButtonService;
this.configuredAppButtons[newAppButtonDef.appId + i] = newAppButtonDef; // need to add i here to the appid since a user can configure multiple appbuttons with the same appid
});
}
prepareChannelButtonService() {
if (this.checkArrayConfigProperty(this.channelButtons, "channelButtons") === false) {
return;
}
this.configuredChannelButtons = {};
this.channelButtons.forEach((value, i) => {
// create a new channel button definition
let newChannelButtonDef = {};
// get the channelNumber
newChannelButtonDef.channelNumber = value.channelNumber || value;
// if channelNumber null or is not a number then skip this channel button, channelNumber is required
if (Number.isInteger(parseInt(newChannelButtonDef.channelNumber)) === false) {
this.logWarn(`Missing channelNumber or channelNumber is not a number. Cannot add channel button!`);
return;
}
// convert to string if the channel number was not a string
newChannelButtonDef.channelNumber = newChannelButtonDef.channelNumber.toString();
// get channelId
newChannelButtonDef.channelId = value.channelId;
// get name
newChannelButtonDef.name = value.name || 'Channel ' + newChannelButtonDef.channelNumber;
// create the service
let newChannelButtonService = this.createStatefulSwitchService(newChannelButtonDef.name, 'channelButtonService' + i,
() => {
return this.getChannelButtonState(newChannelButtonDef.channelNumber);
}, (state) => {
this.setChannelButtonState(state, newChannelButtonDef);
});
// add to the tv service
this.tvAccesory.addService(newChannelButtonService);
// save the configured channel button service
newChannelButtonDef.switchService = newChannelButtonService;
this.configuredChannelButtons[newChannelButtonDef.channelNumber] = newChannelButtonDef;
});
}
prepareNotificationButtonService() {
if (this.checkArrayConfigProperty(this.notificationButtons, "notificationButtons") === false) {
return;
}
this.configuredNotificationButtons = [];
this.notificationButtons.forEach((value, i) => {
// create a new notification button definition
let newNotificationButtonDef = {};
// get the notification message
newNotificationButtonDef.message = value.message || value;
// if message null or empty then skip this notification button, message is required to display a notification
if (!newNotificationButtonDef.message || typeof newNotificationButtonDef.message !== 'string' || newNotificationButtonDef.message === '') {
this.logWarn(`Missing message or message is not of type string. Cannot add notification button!`);
return;
}
// get name
newNotificationButtonDef.name = value.name || 'Notification ' + newNotificationButtonDef.message;
// get the appId if specified
newNotificationButtonDef.appId = value.appId;
// params
newNotificationButtonDef.params = value.params || {};
// get the optional notification content file, if that is specified then the content of this file is read and displayed in the notification
if (value.file && typeof value.file === 'string' && value.file.length > 0) {
newNotificationButtonDef.file = value.file;
// if only file name specified then look for the file in the prefsdir
if (newNotificationButtonDef.file.includes('/') === false) {
newNotificationButtonDef.file = this.prefsDir + newNotificationButtonDef.file;
}
}
// create the stateless button service
let newNotificationButtonService = this.createStatlessSwitchService(newNotificationButtonDef.name, 'notificationButtonService' + i, (state) => {
this.setNotificationButtonState(state, newNotificationButtonDef);
});
this.tvAccesory.addService(newNotificationButtonService);
// save the configured notification button service
newNotificationButtonDef.switchService = newNotificationButtonService;
this.configuredNotificationButtons.push(newNotificationButtonDef);
});
}
prepareRemoteControlButtonService() {
if (this.checkArrayConfigProperty(this.remoteControlButtons, "remoteControlButtons") === false) {
return;
}
this.configuredRemoteControlButtons = [];
this.remoteControlButtons.forEach((value, i) => {
// create a new remote control button definition
let newRemoteControlButtonDef = {};
// get the remote control action
newRemoteControlButtonDef.action = value.action || value;
// if action null or empty then skip this remote control button, action is required for a remote control button
if (!newRemoteControlButtonDef.action || newRemoteControlButtonDef.action === '' || typeof newRemoteControlButtonDef.action !== 'string') {
this.logWarn(`Missing action or action is not of type string. Cannot add remote control button!`);
return;
}
// make sure the action is string and uppercase
newRemoteControlButtonDef.action = newRemoteControlButtonDef.action.toString().toUpperCase();
// get name
newRemoteControlButtonDef.name = value.name || 'Remote ' + newRemoteControlButtonDef.action;
// create the stateless button service
let newRemoteControlButtonService = this.createStatlessSwitchService(newRemoteControlButtonDef.name, 'remoteControlButtonService' + i, (state) => {
this.setRemoteControlButtonState(state, newRemoteControlButtonDef.action);
});
this.tvAccesory.addService(newRemoteControlButtonService);
// save the configured remote control button service
newRemoteControlButtonDef.switchService = newRemoteControlButtonService;
this.configuredRemoteControlButtons.push(newRemoteControlButtonDef);
});
}
prepareSoundOutputButtonService() {
if (this.checkArrayConfigProperty(this.soundOutputButtons, "soundOutputButtons") === false) {
return;
}
this.configuredSoundOutputButtons = {};
this.soundOutputButtons.forEach((value, i) => {
// create a new sound output button definition
let newSoundOutputButtonDef = {};
// get the sound output id
newSoundOutputButtonDef.soundOutput = value.soundOutput || value;
// if soundOutput null or empty then skip this sound output button, soundOutput is required for a sound output button
if (!newSoundOutputButtonDef.soundOutput || newSoundOutputButtonDef.soundOutput === '' || typeof newSoundOutputButtonDef.soundOutput !== 'string') {
this.logWarn(`Missing soundOutput or soundOutput is not of type string. Cannot add sound output button!`);
return;
}
// make sure the soundOutput is string
newSoundOutputButtonDef.soundOutput = newSoundOutputButtonDef.soundOutput.toString();
// get name
newSoundOutputButtonDef.name = value.name || 'Sound Output ' + newSoundOutputButtonDef.soundOutput;
// create the service
let newSoundOutputButtonService = this.createStatefulSwitchService(newSoundOutputButtonDef.name, 'soundOutputButtonService' + i,
() => {
return this.getSoundOutputButtonState(newSoundOutputButtonDef.soundOutput);
}, (state) => {
this.setSoundOutputButtonState(state, newSoundOutputButtonDef.soundOutput);
});
// add to the tv service
this.tvAccesory.addService(newSoundOutputButtonService);
// save the configured sound output button service
newSoundOutputButtonDef.switchService = newSoundOutputButtonService;
this.configuredSoundOutputButtons[newSoundOutputButtonDef.soundOutput] = newSoundOutputButtonDef;
});
}
preparePictureModeButtonService() {
if (this.checkArrayConfigProperty(this.pictureModeButtons, "pictureModeButtons") === false) {
return;
}
this.configuredPictureModeButtons = [];
this.pictureModeButtons.forEach((value, i) => {
// create a new picture mode button definition
let newPictureModeButtonDef = {};
// get the picture mode name
newPictureModeButtonDef.pictureMode = value.pictureMode || value;
// if pictureMode null or empty then skip this picture mode button, pictureMode is required for a picture mode button
if (!newPictureModeButtonDef.pictureMode || newPictureModeButtonDef.pictureMode === '' || typeof newPictureModeButtonDef.pictureMode !== 'string') {
this.logWarn(`Missing pictureMode or pictureMode is not of type string. Cannot add picture mode button!`);
return;
}
// make sure the pictureMode is string
newPictureModeButtonDef.pictureMode = newPictureModeButtonDef.pictureMode.toString();