-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
82 lines (76 loc) · 2.34 KB
/
index.ts
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
import { SinricPro, startSinricPro } from "sinricpro";
import {
CHROMECAST_IP,
SINRIC_APP_KEY,
SINRIC_APP_SECRET,
SINRIC_DEVICE_ID,
} from "./config";
import { Adb, KeyCodes } from "@devicefarmer/adbkit";
//https://github.com/sinricpro/sample_messages
const sinricpro = new SinricPro(
SINRIC_APP_KEY,
[SINRIC_DEVICE_ID],
SINRIC_APP_SECRET,
false
);
const adbClient = Adb.createClient({});
const deviceClient = adbClient
.connect(CHROMECAST_IP, 5555)
.then((deviceId) => adbClient.getDevice(deviceId));
const pressKeys = async (keys: KeyCodes[]) => {
const device = await deviceClient;
return device.shell(
keys.map((keyCode) => `input keyevent ${keyCode}`).join(" && ")
);
};
const shell = (command: string) =>
deviceClient
.then((device) => device.shell(command))
.then(Adb.util.readAll)
.then((buffer) => buffer.toString().trim());
startSinricPro(sinricpro, {
setPowerState: async (deviceid, data) => {
console.log("setPowerState", deviceid, data);
const isAwake =
(await shell(`dumpsys activity | grep -c "mWakefulness=Asleep"`)) === "0"; //0 if the tv is awake and a 1 if it's asleep
const requestedState = data === "On";
console.log("requestedState", requestedState, "isAwake", isAwake);
if (requestedState !== isAwake) {
await pressKeys([KeyCodes["KEYCODE_POWER"]]);
}
return true;
},
setVolume: async (deviceid, data) => {
console.log("setVolume", deviceid, data);
await pressKeys([
...Array.from({ length: 45 }).map(() => KeyCodes["KEYCODE_VOLUME_DOWN"]),
...Array.from({ length: data }).map(() => KeyCodes["KEYCODE_VOLUME_UP"]),
]);
return true;
},
adjustVolume: (deviceid, data) => {
console.log("adjustVolume", deviceid, data);
return true;
},
setMute: async (deviceid, data) => {
console.log("setMute", deviceid, data);
await pressKeys([KeyCodes["KEYCODE_VOLUME_MUTE"]]);
return true;
},
mediaControl: async (deviceid, data) => {
console.log("mediaControl", deviceid, data);
switch (data.toLowerCase()) {
case "play":
case "pause":
await pressKeys([KeyCodes["KEYCODE_MEDIA_PLAY_PAUSE"]]);
break;
case "stop":
await pressKeys([KeyCodes["KEYCODE_MEDIA_STOP"]]);
break;
}
return true;
},
onDisconnect: () => {
console.log("Connection closed");
},
});