-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
168 lines (138 loc) · 4.59 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
const
nodecastor = require('nodecastor'),
JSON5 = require('json5'),
fs = require('fs');
function log(s) {
console.log(new Date().toISOString() + " " + s)
}
function isIdle(status) {
if (status.applications === undefined || status.applications === null) {
return false;
}
if (status.applications.length !== 1) {
return false;
}
if (status.applications[0].isIdleScreen !== true) {
return false;
}
return true;
}
function isKiosk(status) {
if (status.applications === undefined || status.applications === null) {
return false;
}
if (status.applications.length !== 1) {
return false;
}
if (status.applications[0].appId !== '10B2AF08') {
return false;
}
return true
}
function cast(host, device, url, reload, isReload) {
device.status((err, status) => {
if (err !== null && err !== undefined) {
log(`${host}: error on getting status: ${err}`)
return
}
if (isReload === true) {
if (!isIdle(status) && !isKiosk(status)) {
log(`${host}: unable to reload: is not idle nor in kiosk mode`);
return;
}
} else {
if (!isIdle(status)) {
log(`${host}: unable to cast: is not idle`);
return;
}
}
log(`${device.address}: casting...${url} (reload:${isReload})`);
// alternative to de.michaelkuerbis.kiosk (also change isKiosk function!)
// device.application('5C3F0A3C', function (err, app) {
// app.run('urn:x-cast:es.offd.dashcast', function (err, s) {
// s.send({ url: url });
// });
// });
if (reload !== undefined && reload !== null && reload >= 0) {
log(`${device.address}: reloading in ${reload}ms`)
setTimeout(()=>{
cast(host, device, url, reload, true)
}, reload)
}
device.application('10B2AF08', function (err, app) {
if (err !== null && err !== undefined) {
log(`${device.address}: unable to start application: ${err}`)
return
}
// log('Application', util.inspect(a));
app.run('urn:x-cast:de.michaelkuerbis.kiosk', function (err, s) {
if (err !== null && err !== undefined) {
log(`${device.address}: unable to start cast: ${err}`)
return
}
s.send({ url: url, type: "load", refresh: 0 });
});
});
});
}
function watch(name, host, url, timeout, reload) {
log(`connecting to ${host}`)
if (timeout === undefined || timeout === null || timeout <= 0) {
timeout = 60000;
}
const device = new nodecastor.CastDevice({
friendlyName: name,
address: host,
port: 8009,
reconnect: false,
});
device.on('connect', function () {
cast(host, device, url, reload, true)
});
device.on('status', (status) => {
if (!isIdle(status)) {
log(`${host}: unable to cast: is not idle`);
return;
}
log(`${host}: casting in ${timeout}ms`);
setTimeout(() => {
cast(host, device, url, reload, false);
}, timeout);
})
device.on('error', () => {
log(`${host}: error, retrying in ${timeout}ms`);
setTimeout(() => {
watch(name, host, url, timeout, reload);
}, timeout);
})
device.on('disconnect', () => {
log(`${host}: disconnect, retrying in ${timeout}ms`);
setTimeout(() => {
watch(name, host, url, timeout, reload);
}, timeout);
})
}
let config = JSON5.parse(fs.readFileSync('config.json'));
let discoverDevices = [];
for (let i = 0; i < config.length; i++) {
if (config[i].Host === undefined || config[i].Host === null) {
discoverDevices.push(config[i]);
continue;
}
watch('Chromecast', config[i].Host, config[i].URL, config[i].Timeout, config[i].Reload);
}
if (discoverDevices.length > 0) {
console.log("scanning")
nodecastor.scan()
.on('online', device => {
for (var i = discoverDevices.length - 1; i >= 0; i--) {
if (device.friendlyName !== discoverDevices[i].Name) {
continue;
}
watch(device.friendlyName, device.address, discoverDevices[i].URL, discoverDevices[i].Timeout, discoverDevices[i].Reload);
discoverDevices.splice(i, 1);
return;
}
})
.start();
}