forked from acvigue/SmartMatrixServer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtidbyt-post-process.js
69 lines (55 loc) · 1.97 KB
/
tidbyt-post-process.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
const { debuglog } = require('util');
const { spawn } = require("child_process");
const debug = debuglog("smart-matrix-server:post-process:tidbyt");
const { APPLET_FOLDER = '/applets' } = process.env;
module.exports = async function onAppletRender(applet) {
const { name, tidbyts = [] } = applet;
for (let i = 0; i < tidbyts.length; i++) {
const deviceId = tidbyts[i].deviceId;
const apiToken = tidbyts[i].apiToken;
const installationId = tidbyts[i].installationId;
const background = !!tidbyts[i].background;
const pushArgs = [
'push',
deviceId,
`${APPLET_FOLDER}/${name}/${name}.webp`
];
let outputError = '';
if (apiToken) {
pushArgs.push('--api-token');
pushArgs.push(apiToken);
}
if (installationId) {
pushArgs.push('--installation-id');
pushArgs.push(installationId);
}
if (background) {
pushArgs.push('--background');
}
debug(`pushing ${name} to ${tidbyts[i].deviceId}${background ? ' in the background' : ''}...`);
const pushCommand = spawn('pixlet', pushArgs);
const timeout = setTimeout(() => {
console.log(`Rendering timed out for ${name}`);
try {
process.kill(pushCommand.pid, 'SIGKILL');
} catch (e) {
console.log('Could not kill process ^', e);
}
}, 20000);
pushCommand.stdout.on('data', (data) => {
outputError += data
})
pushCommand.stderr.on('data', (data) => {
outputError += data
})
pushCommand.on('close', (code) => {
clearTimeout(timeout);
if(code === 0) {
debug(`pushed ${name} to ${deviceId} successfully!`);
} else {
console.error(outputError);
debug("Applet failed to push.");
}
});
}
};