forked from dwaan/homebridge-adb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
910 lines (776 loc) · 29.1 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
let exec = require('child_process').exec;
let Service, Characteristic, Homebridge, Accessory;
const PLUGIN_NAME = 'homebridge-adb';
const PLATFORM_NAME = 'HomebridgeADB';
const LIMIT_RETRY = 5;
const OTHER_APP_ID = "other";
const HOME_APP_ID = "home";
module.exports = (homebridge) => {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
Homebridge = homebridge;
Accessory = homebridge.platformAccessory;
homebridge.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, ADBPluginPlatform, true);
};
class ADBPlugin {
constructor(log, config, api) {
if(!config) return;
this.log = log;
this.config = config;
this.api = api;
// Configuration
this.debug = this.config.debug || false;
this.skipSpeaker = this.config.skipSpeaker || false;
// Name
this.name = this.config.name || 'Android Device';
// IP
this.ip = this.config.ip;
if(!this.ip) {
this.log.error(`\n\nPlease provide IP for this accessory: ${this.name}\n`);
return;
}
// Interval
this.interval = this.config.interval || 5000;
// Can't be lower than 300 miliseconds, it will flood your network
if(this.interval < 300) this.interval = 300;
// Inputs
this.inputs = this.config.inputs;
this.hidenumber = this.config.hidenumber;
this.hidehome = this.config.hidehome;
this.hideother = this.config.hideother;
if(!this.inputs) this.inputs = [];
if(!this.hidehome) this.inputs.unshift({ "name": "Home", "id": HOME_APP_ID });
if(!this.hideother) this.inputs.push({ "name": "Other", "id": OTHER_APP_ID });
// Sensor
this.playbacksensor = this.config.playbacksensor;
this.playbacksensorexclude = this.config.playbacksensorexclude;
if(!this.playbacksensorexclude) this.playbacksensorexclude = "";
// Power ON/OFF
this.poweron = this.config.poweron;
this.poweroff = this.config.poweroff;
if(!this.poweron) this.poweron = "KEYCODE_POWER";
if(!this.poweroff) this.poweroff = this.poweron;
// Category
this.category = "TELEVISION";
if(this.config.category) this.category = this.config.category.toUpperCase();
this.isSpeaker = function() {
// if(this.category == "SPEAKER" || this.category == "AUDIO_RECEIVER") return true;
// else return false;
return false;
}
// Variable
this.awake = false;
this.playing = false;
this.currentInputIndex = 0;
this.currentApp = false;
this.currentAppOnProgress = false;
this.checkPowerOnProgress = false;
this.checkPlaybackProgress = false;
this.prevStdout = "";
this.limit_retry = LIMIT_RETRY;
// PLayback
this.noPlaybackSensor = false;
this.useTail = undefined;
this.useHead = undefined;
/**
* Create the accessory
*/
// generate a UUID
const uuid = this.api.hap.uuid.generate('homebridge:adb-plugin' + this.ip + this.name);
const uuidos = this.api.hap.uuid.generate('homebridge:adb-plugin' + this.ip + this.name + "OccupancySensor");
// create the external accessory
this.device = new this.api.platformAccessory(this.name, uuid);
// set the external accessory category
if(this.category == "SPEAKER")
this.device.category = this.api.hap.Categories.SPEAKER;
else if(this.category == "TV_STREAMING_STICK")
this.device.category = this.api.hap.Categories.TV_STREAMING_STICK;
else if(this.category == "TV_SET_TOP_BOX")
this.device.category = this.api.hap.Categories.TV_SET_TOP_BOX;
else if(this.category == "AUDIO_RECEIVER")
this.device.category = this.api.hap.Categories.AUDIO_RECEIVER;
else if(this.category == "APPLE_TV")
this.device.category = this.api.hap.Categories.APPLE_TV;
else
this.device.category = this.api.hap.Categories.TELEVISION;
// add the device service
if(this.isSpeaker())
this.deviceService = this.device.addService(Service.SmartSpeaker);
else
this.deviceService = this.device.addService(Service.Television);
// get device information
this.deviceInfo = this.device.getService(Service.AccessoryInformation);
// set device service name
this.deviceService.setCharacteristic(Characteristic.ConfiguredName, this.name);
if(!this.isSpeaker()) this.deviceService.setCharacteristic(Characteristic.SleepDiscoveryMode, Characteristic.SleepDiscoveryMode.ALWAYS_DISCOVERABLE);
// Handle volume and media
// this.handleVolume();
// this.handleMediaStatus();
// this.handleMediaStates();
if(!this.isSpeaker()) {
// Handle On Off
this.handleOnOff();
// Handle input
if(this.inputs.length > 0) {
this.createInputs();
this.handleInputs();
}
// Show Control Center Remote if needed
this.handleRemoteControl();
// Create additional services
if(!this.skipSpeaker) {
this.createTelevisionSpeakers();
}
}
// add playback sensor
if(this.playbacksensor) {
this.devicePlaybackSensor = new this.api.platformAccessory(this.name + " Playback Sensor", uuidos);
this.devicePlaybackSensor.category = this.api.hap.Categories.SENSOR;
this.devicePlaybackSensorInfo = this.devicePlaybackSensor.getService(Service.AccessoryInformation);
this.devicePlaybackSensorService = this.devicePlaybackSensor.addService(Service.MotionSensor);
this.handleMediaAsSensor();
}
/**
* Publish as external accessory
* Check ADB connection before publishing the accesory
*/
this.connect(() => {
var adbCommand = `adb -s ${this.ip} shell "getprop ro.product.model && getprop ro.product.manufacturer && getprop ro.serialno"`;
// Check if device have tail and head command
if(this.useTail === undefined) {
exec(`adb -s ${this.ip} shell "tail --help"`, (err, stdout, stderr) => {
if(err) {
this.displayDebug(`constructor - can't use tail command, adb will send larger output`);
this.useTail = false;
} else {
this.useTail = true;
}
});
}
if(this.useHead === undefined) {
exec(`adb -s ${this.ip} shell "head --help"`, (err, stdout, stderr) => {
if(err) {
this.displayDebug(`constructor - can't use head command, adb will send larger output`);
this.useHead = false;
} else {
this.useHead = true;
}
});
}
// get the accesory information and send it to HB
exec(adbCommand, (err, stdout, stderr) => {
if(err) {
var message = "";
if(stderr.includes('device still authorizing')) message = 'Device still authorizing.';
else if(stderr.includes('device unauthorized.')) message = 'Device unauthorized.';
else message = 'Can\'t get device information.';
this.log.info(this.name, ` - ${message} You can ignore this message. But if problem occurred with your device, please restart your homebridge server.`);
}
stdout = stdout.split("\n");
// Publish the accessories
this.deviceInfo
.setCharacteristic(Characteristic.Model, stdout[0] || "Android")
.setCharacteristic(Characteristic.Manufacturer, stdout[1] || "Google")
.setCharacteristic(Characteristic.SerialNumber, stdout[2] || this.ip);
this.api.publishExternalAccessories(PLUGIN_NAME, [this.device]);
this.log.info(this.name, `- Created`);
// Publish additional sensor accesories
if(this.playbacksensor) {
this.devicePlaybackSensorInfo
.setCharacteristic(Characteristic.Model, stdout[0] || "Android")
.setCharacteristic(Characteristic.Manufacturer, stdout[1] || "Google")
.setCharacteristic(Characteristic.SerialNumber, stdout[2] || this.ip);
this.api.publishExternalAccessories(PLUGIN_NAME, [this.devicePlaybackSensor]);
this.log.info(this.name, `- Sensor created`);
}
});
// Loop the power status
this.update();
});
}
createInputs() {
/**
* Create Device Input Source Services
* These are the inputs the user can select from.
* When a user selected an input the corresponding Identifier Characteristic
* is sent to the device Service ActiveIdentifier Characteristic handler.
* This plugins will create 50 inputs (- current inputs) unconfigured
* and hidden input for future modification. Home app seems have problem
* to add new input after initial add of the accessory. The newly
* created inputs will shown up as related accesorries instead of
* input accessories
**/
for (let i = 0; i < 50; i++) {
let
input = this.inputs[i],
type = Characteristic.InputSourceType.APPLICATION,
configured = Characteristic.IsConfigured.CONFIGURED,
targetVisibility = Characteristic.TargetVisibilityState.SHOWN,
currentVisibility = Characteristic.CurrentVisibilityState.SHOWN,
name = "";
if(i == 0 && !this.hidehome) type = Characteristic.InputSourceType.HOME_SCREEN;
else if(i == this.inputs.length - 1 && !this.hideother) type = Characteristic.InputSourceType.OTHER;
let humanNumber = i + 1;
if(humanNumber < 10) humanNumber = "0" + (i + 1);
if(i >= this.inputs.length || !input.name || !input.id) {
// Create hidden input when name and id is empty and for future modification
configured = Characteristic.IsConfigured.NOT_CONFIGURED;
targetVisibility = Characteristic.TargetVisibilityState.HIDDEN;
currentVisibility = Characteristic.CurrentVisibilityState.HIDDEN;
name = `${humanNumber}. Hidden Input`;
} else {
name = `${input.name}`;
if (!this.hidenumber) name = `${humanNumber}. ${name}`;
}
// this.log.info(this.name, name, targetVisibility, currentVisibility);
let service = this.device.addService(Service.InputSource, `Input - ${name}`, i);
service
.setCharacteristic(Characteristic.Identifier, i)
.setCharacteristic(Characteristic.ConfiguredName, name)
.setCharacteristic(Characteristic.InputSourceType, type)
.setCharacteristic(Characteristic.TargetVisibilityState, targetVisibility)
.setCharacteristic(Characteristic.CurrentVisibilityState, currentVisibility)
.setCharacteristic(Characteristic.IsConfigured, configured);
this.deviceService.addLinkedService(service);
if(configured == Characteristic.IsConfigured.CONFIGURED) {
this.inputs[i].service = service;
}
};
}
createTelevisionSpeakers() {
/**
* Create a speaker service to allow volume control
*/
this.deviceTelevisionSpeakerService = this.device.addService(Service.TelevisionSpeaker);
this.deviceTelevisionSpeakerService
.setCharacteristic(Characteristic.Active, Characteristic.Active.ACTIVE)
// .setCharacteristic(Characteristic.VolumeControlType, Characteristic.VolumeControlType.ABSOLUTE);
.setCharacteristic(Characteristic.VolumeControlType, Characteristic.VolumeControlType.RELATIVE);
// handle [volume control]
this.deviceTelevisionSpeakerService.getCharacteristic(Characteristic.VolumeSelector)
.on('set', (state, callback) => {
var key = "";
if(state) key = "KEYCODE_VOLUME_DOWN";
else key = "KEYCODE_VOLUME_UP";
exec(`adb -s ${this.ip} shell "input keyevent ${key}"`, (err, stdout, stderr) => {
if(err) this.log.info(this.name, '- Can\'t set volume');
});
callback(null);
});
// handle [mute control] - not implemented yet
this.deviceTelevisionSpeakerService.getCharacteristic(Characteristic.Mute)
.on('get', (callback) => {
callback(null, 0);
})
.on('set', (state, callback) => {
callback(null, state);
});
this.deviceService.addLinkedService(this.deviceTelevisionSpeakerService);
}
handleOnOff() {
// handle [on / off]
this.deviceService.getCharacteristic(Characteristic.Active)
.on('set', (state, callback) => {
exec(`adb connect ${this.ip}`, (err, stdout, stderr) => {
if(!err && state != this.awake) {
if(state) {
exec(`adb -s ${this.ip} shell "input keyevent ${this.poweron}"`, (err, stdout, stderr) => {
if(err) {
this.log.info(this.name, "- Can't make device wakeup");
this.displayDebug("handleOnOff - Error - " + stderr.trim());
} else this.displayDebug("On");
this.deviceService.updateCharacteristic(Characteristic.Active, state);
});
} else {
exec(`adb -s ${this.ip} shell "input keyevent ${this.poweroff}"`, (err, stdout, stderr) => {
if(err) {
this.log.info(this.name, "- Can't make device sleep");
this.displayDebug("handleOnOff - Error - " + stderr.trim());
} else this.displayDebug("Off");
this.deviceService.updateCharacteristic(Characteristic.Active, state);
});
}
} else if(err) {
this.log.info(this.name, "- Device not responding");
this.displayDebug("handleOnOff - Error - " + stderr.trim());
} else {
this.deviceService.updateCharacteristic(Characteristic.Active, state);
}
});
callback(null);
}).on('get', (callback) => {
this.checkPowerOnProgress = false;
this.checkPower(() => {
this.deviceService.setCharacteristic(Characteristic.Active, this.awake);
});
callback(null, this.awake);
});
}
handleInputs() {
// handle [input source]
this.deviceService.getCharacteristic(Characteristic.ActiveIdentifier)
.on('set', (state, callback) => {
if(!this.currentAppOnProgress) {
this.currentAppOnProgress = true;
exec(`adb connect ${this.ip}`, (err, stdout, stderr) => {
if(!err) {
let adb = `adb -s ${this.ip} shell "input keyevent KEYCODE_HOME"`;
this.currentInputIndex = state;
if(this.currentInputIndex != 0 && this.inputs[this.currentInputIndex].id != OTHER_APP_ID) {
let type = this.inputs[this.currentInputIndex].id.trim();
if(!type.includes(" ") && type.includes("."))
adb = `adb -s ${this.ip} shell "monkey -p ${this.inputs[this.currentInputIndex].id} 1"`;
else if (this.inputs[this.currentInputIndex].adb)
adb = `adb -s ${this.ip} shell "${this.inputs[this.currentInputIndex].adb}"`;
else
adb = `adb -s ${this.ip} shell "${this.inputs[this.currentInputIndex].id}"`;
}
exec(adb, (err, stdout, stderr) => {
if(!err) {
this.currentApp = this.inputs[this.currentInputIndex].id;
this.log.info(this.name, "- handleInputs -", this.inputs[this.currentInputIndex].name);
} else this.log.info(this.name, "- handleInputs - Can't open -", this.inputs[this.currentInputIndex].name);
});
} else {
this.log.info(this.name, "- handleInputs - Device not responding");
}
this.currentAppOnProgress = false;
});
}
callback(null);
});
}
handleVolume() {
// handle [volume]
this.deviceService.getCharacteristic(Characteristic.Volume)
.on('get', (callback, state) => {
callback(null, 100);
})
.on('set', (state, callback) => {
this.log.info(this.name, '- Volume: ' + state);
callback(null);
});
}
handleMediaStatus() {
// unused - handle [media status]
this.deviceService.getCharacteristic(Characteristic.CurrentMediaState)
.on('get', (callback) => {
var state = Characteristic.CurrentMediaState.LOADING;
var tail = "";
var head = ""
if(this.useHead === true) head = " | head -1";
if(this.useTail === true) tail = " | tail -1";
if(!this.noPlaybackSensor) {
exec(`adb -s ${this.ip} shell "dumpsys media_session | grep state=PlaybackState ${head}"`, (err, stdout, stderr) => {
if(err) {
this.displayDebug(`handleMediaStatus - error - using media session`);
} else {
stdout = stdout.split("\n");
stdout = stdout[0].trim();
if(stdout.includes("state=2, position=0")) state = Characteristic.CurrentMediaState.STOP;
else if(stdout.includes("state=3")) state = Characteristic.CurrentMediaState.PLAY;
else state = Characteristic.CurrentMediaState.PAUSE;
}
this.deviceService.setCharacteristic(Characteristic.CurrentMediaState, state);
});
} else {
state = Characteristic.CurrentMediaState.STOP;
this.deviceService.setCharacteristic(Characteristic.CurrentMediaState, state);
}
callback(null, state);
});
}
handleMediaStates() {
// handle [media state]
this.deviceService.getCharacteristic(Characteristic.TargetMediaState)
.on('get', (callback) => {
var state = Characteristic.TargetMediaState.STOP;
exec(`adb -s ${this.ip} shell "dumpsys media_session | grep state=PlaybackState"`, (err, stdout, stderr) => {
if(err) this.log.error(this.name, '- Can\'t get media status');
else {
stdout = stdout.split("\n");
stdout = stdout[0].trim();
if(stdout.includes("state=2, position=0")) state = Characteristic.TargetMediaState.STOP;
else if(stdout.includes("state=3")) state = Characteristic.TargetMediaState.PLAY;
else state = Characteristic.TargetMediaState.PAUSE;
}
this.deviceService.setCharacteristic(Characteristic.TargetMediaState, state);
});
callback(null, state);
})
.on('set', (state, callback) => {
var key = "";
switch(state) {
case Characteristic.TargetMediaState.PLAY: {
key = 'play';
break;
}
case Characteristic.TargetMediaState.PAUSE: {
key = 'pause';
break;
}
case Characteristic.TargetMediaState.STOP:
default: {
key = 'stop';
break;
}
}
exec(`adb -s ${this.ip} shell "media dispatch ${key}"`, (err, stdout, stderr) => {
if(err) this.log.error(this.name, '- Can\'t send: ' + key.toUpperCase());
else this.log.info(this.name, '- Sending: ' + key.toUpperCase());
});
callback(null);
});
}
handleMediaAsSensor() {
var that = this;
// handle [media state]
this.devicePlaybackSensorService.getCharacteristic(Characteristic.MotionDetected)
.on('get', (callback) => {
var state = Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED;
if(that.playing) state = Characteristic.OccupancyDetected.OCCUPANCY_DETECTED;
that.checkPlayback();
callback(null, state);
});
}
handleRemoteControl() {
// handle [remote control]
this.deviceService.getCharacteristic(Characteristic.RemoteKey)
.on('set', (state, callback) => {
var key = "";
switch(state) {
case Characteristic.RemoteKey.REWIND: {
key = 'KEYCODE_MEDIA_REWIND';
break;
}
case Characteristic.RemoteKey.FAST_FORWARD: {
key = 'KEYCODE_MEDIA_FAST_FORWARD';
break;
}
case Characteristic.RemoteKey.NEXT_TRACK: {
key = 'KEYCODE_MEDIA_NEXT';
break;
}
case Characteristic.RemoteKey.PREVIOUS_TRACK: {
key = 'KEYCODE_MEDIA_PREVIOUS';
break;
}
case Characteristic.RemoteKey.ARROW_UP: {
key = 'KEYCODE_DPAD_UP';
break;
}
case Characteristic.RemoteKey.ARROW_DOWN: {
key = 'KEYCODE_DPAD_DOWN';
break;
}
case Characteristic.RemoteKey.ARROW_LEFT: {
key = 'KEYCODE_DPAD_LEFT';
break;
}
case Characteristic.RemoteKey.ARROW_RIGHT: {
key = 'KEYCODE_DPAD_RIGHT';
break;
}
case Characteristic.RemoteKey.SELECT: {
key = 'KEYCODE_ENTER';
break;
}
case Characteristic.RemoteKey.BACK: {
if(this.config.backbutton) key = this.config.backbutton;
else key = 'KEYCODE_BACK';
break;
}
case Characteristic.RemoteKey.EXIT: {
key = 'KEYCODE_HOME';
break;
}
case Characteristic.RemoteKey.PLAY_PAUSE: {
if(this.config.playpausebutton) key = this.config.playpausebutton;
else key = 'KEYCODE_MEDIA_PLAY_PAUSE';
break;
}
case Characteristic.RemoteKey.INFORMATION: {
if(this.config.infobutton) key = this.config.infobutton;
else key = 'KEYCODE_INFO';
break;
}
}
exec(`adb -s ${this.ip} shell "input keyevent ${key}"`, (err, stdout, stderr) => {
if(err) this.displayDebug(`handleRemoteControl - Can't send: ${key}`);
});
callback(null);
});
}
checkPlayback() {
var that = this;
var changed = false;
var state = Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED;
var tail = "";
var head = ""
if(this.useHead === true) head = " | head -1";
if(this.useTail === true) tail = " | tail -1";
if(!this.awake) {
// When device is asleep, set the sensor off
if(this.playing) {
this.displayDebug(`checkPlayback - device is asleep`);
this.playing = false;
this.devicePlaybackSensorService.setCharacteristic(Characteristic.MotionDetected, state);
}
} else if(!this.checkPlaybackProgress && this.currentApp && !this.playbacksensorexclude.includes(this.currentApp)) {
if(!this.noPlaybackSensor) {
var changeState = function(state, stdout) {
if(changed) {
if(stdout) that.displayDebug(`checkPlayback - ${stdout}`);
that.displayDebug(`checkPlayback - Current app - ${that.currentApp}`);
that.displayDebug(`checkPlayback - ${that.playing}`);
that.log.info(`Media playing - ${that.playing}`);
that.devicePlaybackSensorService.setCharacteristic(Characteristic.MotionDetected, state);
}
that.checkPlaybackProgress = false;
}
var errorState = function(using) {
that.displayDebug(`checkPlayback - error - using ${using}`);
if(that.playing) {
that.playing = false;
changed = true;
}
}
this.checkPlaybackProgress = true;
exec(`adb -s ${this.ip} shell "dumpsys media_session | grep -e 'Media button session is' -e 'AlexaMediaPlayerRuntime'"`, (err, stdout, stderr) => {
stdout = stdout.trim();
if(err) {
this.displayDebug(`checkPlayback - error - checking current media app`);
} else if(this.currentApp == HOME_APP_ID || this.currentApp != OTHER_APP_ID || stdout.includes(this.currentApp) || stdout.includes('AlexaMediaPlayerRuntime')) {
exec(`adb -s ${this.ip} shell "dumpsys media_session | grep 'state=PlaybackState'"`, (err, stdout, stderr) => {
if(err) errorState('media_session');
else {
stdout = stdout.trim();
if(stdout != "") {
if(stdout.includes("state=3")) {
if(!this.playing) {
state = Characteristic.OccupancyDetected.OCCUPANCY_DETECTED;
this.playing = true;
changed = true;
}
} else if(this.playing) {
this.playing = false;
changed = true;
}
} else {
this.displayDebug(`checkPlayback - media_session - no audio playing?`);
}
}
changeState(state, 'media_session - ' + stdout);
});
} else {
exec(`adb -s ${this.ip} shell "dumpsys audio | grep 'player piid:' | grep ' state:' ${tail}"`, (err, stdout, stderr) => {
// After restart, android device will display error when running this command
if(err) errorState('audio');
else {
stdout = stdout.trim();
if(stdout != "") {
stdout = stdout.trim().split("\n");
stdout = stdout[stdout.length - 1].trim();
if(stdout.includes("state:started")) {
if(!this.playing) {
state = Characteristic.OccupancyDetected.OCCUPANCY_DETECTED;
this.playing = true;
changed = true;
}
} else if(this.playing) {
this.playing = false;
changed = true;
}
} else {
this.displayDebug(`checkPlayback - audio - no audio playing?`);
}
}
changeState(state, 'audio - ' + stdout);
});
}
});
}
}
}
checkPower(callback) {
if(!this.checkPowerOnProgress) {
this.checkPowerOnProgress = true;
exec(`adb -s ${this.ip} shell "dumpsys power | grep mHoldingDisplay"`, (err, stdout, stderr) => {
if(err) {
// When device can't be found, set it sleep
if(this.awake) {
this.awake = false;
if(!this.isSpeaker()) this.deviceService.getCharacteristic(Characteristic.Active).updateValue(this.awake);
this.displayDebug(`checkPower - Error`);
}
if(callback) callback('error');
} else {
var output = stdout.trim().split('=')[1];
if((output == 'true' && !this.awake) || (output == 'false' && this.awake)) {
this.awake = !this.awake;
if(!this.isSpeaker()) this.deviceService.getCharacteristic(Characteristic.Active).updateValue(this.awake);
this.displayDebug(`checkPower - ${this.awake}`);
}
if(callback) callback(this.awake);
}
this.checkPowerOnProgress = false;
});
}
}
checkInput(callback) {
if(this.awake && !this.currentAppOnProgress) {
this.currentAppOnProgress = true;
exec(`adb -s ${this.ip} shell "dumpsys window windows | grep -E mFocusedApp"`, (err, stdout, stderr) => {
if(err) {
this.displayDebug(`checkInput - error`)
} else {
if(!stdout) stdout = "";
stdout = stdout.trim();
if(stdout != this.prevStdout) {
let otherApp = true;
// Identified current focused app
this.prevStdout = stdout;
if(stdout) {
stdout = stdout.split("/");
stdout[0] = stdout[0].split(" ");
stdout[0] = stdout[0][4];
if(stdout[0] == undefined) stdout[0] = HOME_APP_ID;
if(stdout[1] == undefined) stdout[1] = "";
if(!this.hidehome && (stdout[1].includes("Launcher") || stdout[1].substr(0, 13) == ".MainActivity" || stdout[1].includes("RecentsTvActivity"))) stdout = this.inputs[0].id;
else stdout = stdout[0];
} else stdout = OTHER_APP_ID;
if(this.inputs.length > 0) {
if(this.inputs[this.currentInputIndex].id != stdout && (stdout === HOME_APP_ID || this.inputs[this.currentInputIndex].type !== 'command')) {
this.inputs.forEach((input, i) => {
// Home or registered app
if(stdout == input.id) {
this.currentInputIndex = i;
otherApp = false;
}
});
// Other app
if(otherApp && !this.hideother) {
let name = stdout.split("."),
humanName = "",
i = 0;
// Extract human readable name from app package name
while(name[i]) {
name[i] = name[i].charAt(0).toUpperCase() + name[i].slice(1);
if(i > 0)
if(name[i] != "Com" && name[i] != "Android")
if(name[i] == "Vending") humanName += "Play Store";
else if(name[i] == "Gm") humanName += "GMail";
else humanName += (" " + name[i]);
i++;
}
humanName = humanName.trim();
if(humanName != "Other") humanName = `Other (${humanName.trim()})`;
this.currentInputIndex = this.inputs.length - 1;
if(this.inputs[this.currentInputIndex]) this.inputs[this.currentInputIndex].id = stdout;
if(this.inputs[this.currentInputIndex].service) {
if (!this.hidenumber) humanName = `${this.currentInputIndex + 1}. ${humanName}`;
this.inputs[this.currentInputIndex].service.setCharacteristic(Characteristic.ConfiguredName, `${humanName}`);
}
}
this.deviceService.updateCharacteristic(Characteristic.ActiveIdentifier, this.currentInputIndex);
}
}
if(this.currentApp != stdout) {
this.log.info(this.name, "- Current app -", "\x1b[4m" + stdout + "\x1b[0m");
this.currentApp = stdout;
}
}
}
this.currentAppOnProgress = false;
});
}
}
connect(callback, done) {
var that = this;
var error = function() {
that.log.info(`${that.name} - Device disconnected? Trying to reconnect.`);
}
exec(`adb disconnect ${this.ip}`, (err, stdout, stderr) => {
if(err) {
error();
that.displayDebug('connect - disconnect - ' + stderr.trim());
}
exec(`adb connect ${this.ip}`, (err, stdout, stderr) => {
if(err) {
error();
that.displayDebug('connect - connect -' + stderr.trim());
}
else if(callback) callback();
});
if(done) done();
});
}
update() {
var that = this;
// Update device status every second -> or based on configuration
this.intervalHandler = setInterval(() => {
that.checkPower(function(result) {
if(result == 'error') {
// Can't check the device power status, try reconnect
that.connect();
} else {
// Check for current input
if(!that.isSpeaker()) that.checkInput();
// Check playback status
if(that.playbacksensor) that.checkPlayback();
}
});
}, this.interval);
}
// Will be removed
dumpsys(callback) {
var that = this;
var adbCommand = `adb -s ${this.ip} shell "dumpsys power | grep mHoldingDisplay"`;
exec(adbCommand, (err, stdout, stderr) => {
if(err) {
if(that.limit_retry > 0) {
that.limit_retry--;
callback(true, 'false');
} else if (that.limit_retry == 0){
that.log.info(that.name, "- Device disconnected. Please turn on your device manually.");
that.limit_retry--;
callback(false, 'false');
}
} else {
if(that.limit_retry < 0) {
that.limit_retry = LIMIT_RETRY;
that.log.info(that.name, "- Connected.");
}
stdout = stdout.trim().split('=')[1];
callback(false, stdout);
}
});
}
displayDebug(text){
if(this.debug) this.log.info(`\x1b[2m${this.name} - ${text}\x1b[0m`);
}
}
class ADBPluginPlatform {
constructor(log, config, api) {
if(!config) return;
this.log = log;
this.api = api;
this.config = config;
if(this.api) this.api.on('didFinishLaunching', this.initAccessory.bind(this));
}
initAccessory() {
// read from config.accessories
if(this.config.accessories && Array.isArray(this.config.accessories)) {
for (let accessory of this.config.accessories) {
if(accessory) new ADBPlugin(this.log, accessory, this.api);
}
} else if(this.config.accessories) {
this.log.info('Cannot initialize. Type: %s', typeof this.config.accessories);
}
if(!this.config.accessories) {
this.log.info('-------------------------------------------------');
this.log.info('Please add one or more accessories in your config');
this.log.info('-------------------------------------------------');
}
}
}