-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
163 lines (146 loc) · 4.26 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
import WebSocket from 'ws';
import got from 'got';
const wsUrl = process.env.MOONRAKER_WS || 'ws://127.0.0.1:80/websocket';
const spectodaUrl = process.env.SPECTODA_URL || 'http://127.0.0.1:8888/';
const devMode = !!process.env.DEV;
let serverInfoId = 0;
let lastProgress = undefined;
const buildRpc = (method, params, id) => {
if (id === undefined) {
id = Math.ceil(Math.random() * 1000000);
}
return {
"id": id,
"method": method,
"jsonrpc": "2.0",
"params": params
}
}
const sendEvent = async (label, value, type = 'percentage') => {
const payload = {
label: label,
id: 255,
}
if (value) {
payload.value = value;
payload.type = type;
} else {
payload.type = "empty"
}
if (devMode) {
console.log('POST', `${spectodaUrl}event`, payload);
return;
}
try {
return await got.post(`${spectodaUrl}event`, { json: payload }).json();
} catch (e) {
console.error('error: ', payload, e.code);
}
}
const requestInfo = (ws) => {
const id = Math.ceil(Math.random() * 1000000);
ws.send(JSON.stringify(buildRpc("server.info", undefined, serverInfoId)));
return id;
}
const parseServerInfo = (ws, data) => {
// console.log("parseServerInfo:", data);
if (data.result.klippy_state === 'ready') {
sendEvent('kread');
}
if (data.result.klippy_state === 'disconnected') {
sendEvent('kdisc');
}
if (data.result.klippy_state === 'error') {
sendEvent('kerro');
}
if (data.result.klippy_state === 'startup') {
sendEvent('kstup');
setTimeout(() => {
serverInfoId = requestInfo(ws);
}, 2000);
}
}
const subscribeRpc = buildRpc("printer.objects.subscribe", {
"objects":
{
"webhooks": null,
"print_stats": null,
"virtual_sdcard": ["progress"],
}
}, 1);
const stateEvents = {
"standby": "stand",
"printing": "print",
"paused": "pause",
"complete": "done",
"error": "error",
}
const handleStatusUpdate = (data) => {
// console.log("handleStatusUpdate:", data);
const stats = data.params[0].print_stats;
if (stats && stats.state) {
const label = stateEvents[stats.state];
sendEvent(label);
}
const vsc = data.params[0].virtual_sdcard;
if (vsc && vsc.progress && lastProgress !== Math.floor(vsc.progress * 100.0)) {
lastProgress = Math.floor(vsc.progress * 100.0);
sendEvent('progr', lastProgress, 'percentage')
}
}
const parseSubscribe = (data) => {
// console.log("parseSubscribe:", data);
const stats = data.result?.status?.print_stats
if (stats && Object.keys(stateEvents).includes(stats.state)) {
const event = stateEvents[stats.state];
sendEvent(event);
}
}
const connectWs = () => {
console.log(`Connecting to ${wsUrl}.`);
const ws = new WebSocket(wsUrl);
ws.on('error', console.error);
ws.on('open', function open() {
ws.send(JSON.stringify(subscribeRpc));
serverInfoId = requestInfo(ws);
sendEvent('init');
});
ws.on('message', function message(data) {
const d = JSON.parse(data);
// console.log("message:", d);
// Info about server. Not interesting
if (d.method === 'notify_proc_stat_update') {
return;
}
// Requested server.info
if (d.id === serverInfoId) {
parseServerInfo(ws, d);
}
if (d.method === 'notify_klippy_disconnected') {
sendEvent('kdisc');
}
if (d.method === 'notify_klippy_ready') {
sendEvent('kread');
// resubscribe to events when klippy is ready
ws.send(JSON.stringify(subscribeRpc));
}
if (d.method === 'notify_klippy_shutdown') {
sendEvent('kshut');
}
if (d.id === 1) {
return parseSubscribe(d);
}
if (d.method === 'notify_status_update') {
return handleStatusUpdate(d);
}
});
ws.on('close', function close() {
console.log('disconnected');
sendEvent('disc');
setTimeout(() => {
console.log('reconnecting...');
connectWs();
}, 2000);
});
}
connectWs();