2019-09-20 15:46:05 -04:00
|
|
|
import * as fs from 'fs';
|
|
|
|
|
2019-09-20 17:48:40 -04:00
|
|
|
import * as express from 'express';
|
|
|
|
import * as httpProxy from 'http-proxy';
|
2019-09-20 15:46:05 -04:00
|
|
|
import * as yaml from 'js-yaml';
|
2019-09-24 10:53:51 -04:00
|
|
|
import * as expressWs from 'express-ws';
|
2021-10-28 02:53:12 -04:00
|
|
|
import * as WebSocket from 'ws';
|
2019-09-20 17:48:40 -04:00
|
|
|
|
2021-10-28 02:53:12 -04:00
|
|
|
import { ExtendedMessage } from '../types/messages';
|
|
|
|
import OctoPrintConnection from './OctoPrintConnection';
|
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: {
|
2021-10-28 01:06:08 -04:00
|
|
|
[key: string]: { address: string; apikey: string };
|
|
|
|
};
|
2021-10-13 23:25:28 -04:00
|
|
|
};
|
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);
|
|
|
|
});
|
2021-10-28 02:53:12 -04:00
|
|
|
let octoprintConnections: OctoPrintConnection[] = [];
|
2019-09-20 15:46:05 -04:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-28 02:53:12 -04:00
|
|
|
function broadcastPayload(payload: ExtendedMessage) {
|
2019-09-20 15:46:05 -04:00
|
|
|
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) {
|
2021-10-28 02:53:12 -04:00
|
|
|
octoprintConnections.forEach((op: OctoPrintConnection) => op.send_init(ws));
|
2019-09-24 10:53:51 -04:00
|
|
|
});
|
2019-09-20 17:48:40 -04:00
|
|
|
|
|
|
|
app.get('/webcam/:printer', (req, res) => {
|
2021-10-28 02:53:12 -04:00
|
|
|
let printer: OctoPrintConnection | undefined = octoprintConnections.find(
|
|
|
|
(op) => op.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-24 10:53:51 -04:00
|
|
|
function initPrinters() {
|
2021-10-28 02:53:12 -04:00
|
|
|
octoprintConnections = Object.entries(config.printers).map(
|
2021-10-28 01:06:08 -04:00
|
|
|
([slug, printer]) =>
|
2021-10-28 02:53:12 -04:00
|
|
|
new OctoPrintConnection(slug, printer.address, printer.apikey, broadcastPayload)
|
2019-09-20 15:46:05 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-24 10:53:51 -04:00
|
|
|
initPrinters();
|