2019-09-20 15:46:05 -04:00
|
|
|
import * as fs from 'fs';
|
2019-09-20 17:48:40 -04:00
|
|
|
import * as path from 'path';
|
2019-09-20 15:46:05 -04:00
|
|
|
|
2019-09-20 17:48:40 -04:00
|
|
|
import * as express from 'express';
|
2019-09-20 15:46:05 -04:00
|
|
|
import fetch from 'node-fetch';
|
2019-09-20 17:48:40 -04:00
|
|
|
import * as httpProxy from 'http-proxy';
|
2019-09-20 15:46:05 -04:00
|
|
|
import * as WebSocket from 'ws';
|
|
|
|
import * as yaml from 'js-yaml';
|
2019-09-24 10:53:51 -04:00
|
|
|
import * as expressWs from 'express-ws';
|
2019-09-20 17:48:40 -04:00
|
|
|
|
2021-10-11 21:28:12 -04:00
|
|
|
import * as messages from '../types/messages';
|
|
|
|
import * as octoprint from '../types/octoprint';
|
2019-09-20 15:46:05 -04:00
|
|
|
|
2020-02-19 20:43:27 -05:00
|
|
|
const PORT = process.env.PORT || 1234;
|
|
|
|
|
2021-10-13 23:25:28 -04:00
|
|
|
type configuration = {
|
|
|
|
printers: {
|
|
|
|
[key: string]: { address: string; apikey: string }
|
|
|
|
}
|
|
|
|
};
|
2020-09-09 16:47:04 -04:00
|
|
|
|
2019-09-20 15:46:05 -04:00
|
|
|
// Load config
|
2021-10-11 21:18:50 -04:00
|
|
|
const config: configuration = yaml.load(
|
2020-09-09 16:47:04 -04:00
|
|
|
fs.readFileSync('config.yaml', 'utf8')
|
|
|
|
) as configuration;
|
2019-09-20 15:46:05 -04:00
|
|
|
|
2019-09-20 17:48:40 -04:00
|
|
|
const proxy = httpProxy.createProxyServer({});
|
2020-03-22 00:38:49 -04:00
|
|
|
proxy.on('error', function (e) {
|
2020-02-19 18:37:32 -05:00
|
|
|
console.error('Proxy failed:');
|
|
|
|
console.error(e);
|
|
|
|
});
|
2019-09-20 15:46:05 -04:00
|
|
|
let printerStatuses: PrinterStatus[] = [];
|
|
|
|
|
|
|
|
function broadcast(data: WebSocket.Data) {
|
2019-09-24 10:53:51 -04:00
|
|
|
wsInstance.getWss().clients.forEach((client: WebSocket) => {
|
2019-09-20 15:46:05 -04:00
|
|
|
if (client.readyState === WebSocket.OPEN) {
|
|
|
|
client.send(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function broadcastPayload(payload: messages.ExtendedMessage) {
|
|
|
|
broadcast(JSON.stringify(payload));
|
|
|
|
}
|
|
|
|
|
2019-09-24 10:53:51 -04:00
|
|
|
const wsInstance = expressWs(express());
|
|
|
|
const app = wsInstance.app;
|
|
|
|
|
2020-03-22 00:38:49 -04:00
|
|
|
app.ws('/ws', function (ws, req) {
|
2019-09-24 10:53:51 -04:00
|
|
|
printerStatuses.forEach((ps: PrinterStatus) => ps.send_init(ws));
|
|
|
|
});
|
2019-09-20 17:48:40 -04:00
|
|
|
|
|
|
|
app.get('/webcam/:printer', (req, res) => {
|
|
|
|
let printer: PrinterStatus | undefined = printerStatuses.find(
|
2021-10-13 23:25:28 -04:00
|
|
|
(p) => p.slug === req.params.printer
|
2019-09-20 17:48:40 -04:00
|
|
|
);
|
2020-02-19 18:38:30 -05:00
|
|
|
if (printer?.webcamURL) {
|
2019-10-22 12:59:12 -04:00
|
|
|
req.url = ''; // truncate the url for passing to the proxy
|
2021-10-11 21:01:14 -04:00
|
|
|
proxy.web(req, res, { target: printer.webcamURL.toString() });
|
2019-10-22 12:59:12 -04:00
|
|
|
} else res.status(404).send('Not Found: Printer not known or has no webcam.');
|
2019-09-20 17:48:40 -04:00
|
|
|
});
|
|
|
|
|
2020-02-19 20:43:27 -05:00
|
|
|
app.listen(PORT);
|
2019-09-20 17:48:40 -04:00
|
|
|
|
2019-09-20 15:46:05 -04:00
|
|
|
class PrinterStatus {
|
2021-10-13 23:25:28 -04:00
|
|
|
slug: string;
|
2019-09-20 15:46:05 -04:00
|
|
|
address: string;
|
|
|
|
apikey: string;
|
|
|
|
|
2021-10-11 21:01:14 -04:00
|
|
|
webcamURL?: URL;
|
2019-09-20 15:46:05 -04:00
|
|
|
name?: string;
|
|
|
|
|
|
|
|
websocket?: WebSocket;
|
|
|
|
lastStatus?: messages.ExtendedMessage;
|
|
|
|
|
2021-10-13 23:25:28 -04:00
|
|
|
constructor(slug: string, address: string, apikey: string) {
|
|
|
|
this.slug = slug;
|
2019-09-20 15:46:05 -04:00
|
|
|
this.address = address;
|
|
|
|
this.apikey = apikey;
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.init(); // async init
|
|
|
|
} catch (e) {
|
|
|
|
throw 'Failed to Init' + e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
// TODO: error handling (try/catch)
|
|
|
|
const settings = await this.api_get('settings');
|
2021-10-11 21:01:14 -04:00
|
|
|
this.webcamURL = new URL(settings.webcam.streamUrl, this.address);
|
2019-09-20 15:46:05 -04:00
|
|
|
this.name = settings.appearance.name;
|
|
|
|
|
|
|
|
// do passive login to get a session key from the API key
|
|
|
|
const login: octoprint.LoginResponse = await this.api_post('login', {
|
|
|
|
passive: 'true',
|
|
|
|
});
|
|
|
|
|
2020-11-09 12:43:29 -05:00
|
|
|
this.connect_websocket(login.name + ':' + login.session);
|
|
|
|
}
|
|
|
|
|
|
|
|
connect_websocket(authToken: string): void {
|
2021-10-11 21:01:14 -04:00
|
|
|
const url = new URL('/sockjs/websocket', this.address)
|
|
|
|
url.protocol = 'ws';
|
|
|
|
this.websocket = new WebSocket(url.toString());
|
2019-09-20 15:46:05 -04:00
|
|
|
this.websocket
|
|
|
|
.on('open', () => {
|
2020-11-09 12:43:29 -05:00
|
|
|
this.websocket!.send(JSON.stringify({ auth: authToken }));
|
2019-09-20 15:46:05 -04:00
|
|
|
})
|
|
|
|
.on('message', (data: WebSocket.Data) => {
|
|
|
|
const event: octoprint.Message = JSON.parse(data as string);
|
|
|
|
|
|
|
|
let ext_event: messages.ExtendedMessage = {
|
|
|
|
...event,
|
2021-10-13 23:25:28 -04:00
|
|
|
printer: this.slug,
|
|
|
|
name: this.name,
|
2019-09-20 15:46:05 -04:00
|
|
|
};
|
|
|
|
broadcastPayload(ext_event);
|
|
|
|
|
|
|
|
if ('current' in event || 'history' in event) {
|
|
|
|
this.lastStatus = ext_event;
|
|
|
|
}
|
2020-11-09 12:43:29 -05:00
|
|
|
})
|
|
|
|
.on('close', () => {
|
2021-10-13 23:25:28 -04:00
|
|
|
console.log('Lost connection to ' + this.slug + ' reconnecting...');
|
2020-11-09 12:43:29 -05:00
|
|
|
setTimeout(() => this.connect_websocket(authToken), 5000);
|
2019-09-20 15:46:05 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async api_get(endpoint: string): Promise<any> {
|
2021-10-11 21:01:14 -04:00
|
|
|
const r = await fetch(new URL('/api/' + endpoint, this.address), {
|
2019-09-20 15:46:05 -04:00
|
|
|
headers: { 'X-Api-Key': this.apikey },
|
|
|
|
});
|
|
|
|
return await r.json();
|
|
|
|
}
|
|
|
|
|
|
|
|
async api_post(endpoint: string, data: any): Promise<any> {
|
2021-10-11 21:01:14 -04:00
|
|
|
const r = await fetch(new URL('/api/' + endpoint, this.address), {
|
2019-09-20 15:46:05 -04:00
|
|
|
headers: {
|
|
|
|
'X-Api-Key': this.apikey,
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
});
|
|
|
|
return await r.json();
|
|
|
|
}
|
|
|
|
|
|
|
|
send_init(ws: WebSocket) {
|
|
|
|
let payload: messages.ExtendedMessage;
|
|
|
|
if (this.lastStatus) {
|
|
|
|
payload = this.lastStatus;
|
|
|
|
} else {
|
2021-10-13 23:25:28 -04:00
|
|
|
payload = { init: null, printer: this.slug, name: this.name };
|
2019-09-20 15:46:05 -04:00
|
|
|
}
|
|
|
|
ws.send(JSON.stringify(payload));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 10:53:51 -04:00
|
|
|
function initPrinters() {
|
2021-10-13 23:25:28 -04:00
|
|
|
printerStatuses = Object.entries(config.printers).map(
|
|
|
|
([slug, printer]) => new PrinterStatus(slug, printer.address, printer.apikey)
|
2019-09-20 15:46:05 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-24 10:53:51 -04:00
|
|
|
initPrinters();
|