2023-03-18 20:59:26 +00:00
|
|
|
'use strict';
|
2023-02-25 07:08:05 +00:00
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
2023-02-25 05:24:07 +00:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2024-05-23 22:49:18 +00:00
|
|
|
exports.Server = void 0;
|
2023-02-25 07:08:56 +00:00
|
|
|
const ws_1 = require("ws");
|
2023-02-25 07:08:05 +00:00
|
|
|
const express_1 = __importDefault(require("express"));
|
2023-02-25 05:24:07 +00:00
|
|
|
const promises_1 = require("fs/promises");
|
2023-02-25 18:22:20 +00:00
|
|
|
const path_1 = require("path");
|
2023-02-25 16:59:56 +00:00
|
|
|
const uuid_1 = require("uuid");
|
2024-05-19 22:14:33 +00:00
|
|
|
const log_1 = require("log");
|
2018-01-05 04:43:33 +00:00
|
|
|
class Server {
|
2024-05-23 23:54:42 +00:00
|
|
|
constructor(ui) {
|
2023-02-25 05:24:07 +00:00
|
|
|
this.id = 'server';
|
|
|
|
this.isActive = false;
|
|
|
|
this.templates = [
|
|
|
|
{
|
|
|
|
name: 'index',
|
|
|
|
path: 'server.html'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'script',
|
|
|
|
path: 'lib/client/index.js'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
this.port = 9900;
|
|
|
|
this.wsPort = 9901;
|
2023-02-25 07:08:56 +00:00
|
|
|
this.proxy = {};
|
2023-02-25 16:59:56 +00:00
|
|
|
this.queue = {};
|
|
|
|
this.intervalPeriod = 10000; //10 sec
|
2023-02-25 05:24:07 +00:00
|
|
|
this.init();
|
2024-05-23 23:54:42 +00:00
|
|
|
this.ui = ui;
|
2023-02-25 05:24:07 +00:00
|
|
|
}
|
|
|
|
async init() {
|
2024-05-19 22:14:33 +00:00
|
|
|
this.log = await (0, log_1.Log)({ label: this.id });
|
2023-02-25 07:08:05 +00:00
|
|
|
await this.load();
|
|
|
|
await this.start();
|
2023-02-25 05:24:07 +00:00
|
|
|
}
|
|
|
|
async load() {
|
2024-04-06 23:09:50 +00:00
|
|
|
this.http = (0, express_1.default)();
|
2023-02-25 05:24:07 +00:00
|
|
|
for (let tmpl of this.templates) {
|
2024-04-06 23:09:50 +00:00
|
|
|
tmpl.data = await (0, promises_1.readFile)(tmpl.path, 'utf8');
|
2023-02-25 05:24:07 +00:00
|
|
|
}
|
2023-02-25 07:08:05 +00:00
|
|
|
this.http.get('/', this.index.bind(this));
|
2023-02-25 07:08:56 +00:00
|
|
|
this.http.get('/image/:key', this.image.bind(this));
|
2023-02-25 05:24:07 +00:00
|
|
|
this.log.info("Server assets loaded");
|
|
|
|
}
|
|
|
|
template(name, data) {
|
|
|
|
let html = this.templates.find(el => el.name === name).data;
|
|
|
|
for (let key of Object.keys(data)) {
|
|
|
|
html = html.replace(`{{${key}}}`, data[key]);
|
|
|
|
}
|
|
|
|
return html;
|
|
|
|
}
|
2023-02-25 07:08:56 +00:00
|
|
|
async startWss() {
|
|
|
|
try {
|
|
|
|
this.wss = new ws_1.WebSocketServer({ port: this.wsPort });
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
this.log.error(err);
|
|
|
|
return;
|
|
|
|
}
|
2023-03-18 20:59:26 +00:00
|
|
|
this.wss.on('connection', async function (ws, req) {
|
|
|
|
const address = req.socket.remoteAddress;
|
|
|
|
ws.on('message', function (data) {
|
2023-02-25 16:59:56 +00:00
|
|
|
let obj = JSON.parse(data);
|
2023-02-25 17:40:35 +00:00
|
|
|
//this.log.info(data)
|
2023-02-25 16:59:56 +00:00
|
|
|
if (obj.id && this.queue[obj.id]) {
|
|
|
|
this.queue[obj.id](obj);
|
|
|
|
delete this.queue[obj.id];
|
2023-02-25 21:33:55 +00:00
|
|
|
if (obj.action !== 'ping') {
|
|
|
|
this.log.info(`${obj.action} ACK`);
|
|
|
|
}
|
2023-02-25 16:59:56 +00:00
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
ws.on('close', function () {
|
|
|
|
this.log.info('Client disconnected');
|
2023-03-18 20:59:26 +00:00
|
|
|
this.notify('Client disconnected', `No longer forwarding digital display to client ${address}`);
|
2023-02-25 16:59:56 +00:00
|
|
|
}.bind(this));
|
|
|
|
await this.cmd(ws, 'mcopy');
|
|
|
|
this.log.info('Client connected');
|
2023-03-18 20:59:26 +00:00
|
|
|
this.notify('Client connected', `Forwarding digital display to client: ${address}`);
|
2023-02-25 16:59:56 +00:00
|
|
|
}.bind(this));
|
|
|
|
this.log.info(`Websocket server started!`);
|
2023-02-25 07:08:56 +00:00
|
|
|
this.log.info(`WSS [ ws://localhost:${this.wsPort} ]`);
|
|
|
|
}
|
|
|
|
async startHttp() {
|
2023-02-25 07:08:05 +00:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
this.httpd = this.http.listen(this.port, function () {
|
2023-02-25 16:59:56 +00:00
|
|
|
this.log.info(`HTTP server started!`);
|
2023-02-25 07:08:05 +00:00
|
|
|
this.log.info(`URL [ http://localhost:${this.port} ]`);
|
|
|
|
return resolve(true);
|
|
|
|
}.bind(this));
|
2023-02-25 05:24:07 +00:00
|
|
|
}.bind(this));
|
|
|
|
}
|
2023-02-25 07:08:56 +00:00
|
|
|
async start() {
|
|
|
|
await this.startHttp();
|
|
|
|
await this.startWss();
|
2023-02-25 16:59:56 +00:00
|
|
|
this.interval = setInterval(async function () {
|
|
|
|
await this.cmdAll('ping');
|
|
|
|
}.bind(this), this.intervalPeriod);
|
|
|
|
this.isActive = true;
|
2023-02-25 07:08:56 +00:00
|
|
|
}
|
2023-02-25 16:59:56 +00:00
|
|
|
async stopHttp() {
|
2023-02-25 07:08:05 +00:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
return this.httpd.close(function () {
|
2023-02-25 16:59:56 +00:00
|
|
|
this.log.info(`HTTP server stopped :(`);
|
2023-02-25 07:08:56 +00:00
|
|
|
return resolve(false);
|
2023-02-25 07:08:05 +00:00
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
2023-02-25 05:24:07 +00:00
|
|
|
}
|
2023-02-25 16:59:56 +00:00
|
|
|
async stop() {
|
|
|
|
await this.stopHttp();
|
|
|
|
clearInterval(this.interval);
|
|
|
|
this.isActive = false;
|
|
|
|
}
|
2023-02-25 05:24:07 +00:00
|
|
|
index(req, res, next) {
|
2023-02-25 18:11:40 +00:00
|
|
|
const SCRIPT = this.template('script', { PORT: `${this.wsPort}` });
|
|
|
|
const html = this.template('index', { SCRIPT });
|
2023-02-25 07:08:05 +00:00
|
|
|
this.log.info('GET /');
|
2023-02-25 05:24:07 +00:00
|
|
|
return res.send(html);
|
|
|
|
}
|
2023-02-25 07:08:56 +00:00
|
|
|
async image(req, res, next) {
|
|
|
|
let filePath;
|
|
|
|
if (req.params && req.params.key) {
|
|
|
|
if (this.proxy[req.params.key]) {
|
|
|
|
filePath = this.proxy[req.params.key].path;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
return res.sendFile(filePath, function (err) {
|
|
|
|
if (err) {
|
|
|
|
res.status(err.status).end();
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
return resolve(true);
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
addProxy(key, filePath) {
|
2023-02-25 16:59:56 +00:00
|
|
|
//wipe every time
|
|
|
|
this.proxy = {};
|
2023-02-25 07:08:56 +00:00
|
|
|
this.proxy[key] = {
|
2023-02-25 18:22:20 +00:00
|
|
|
path: filePath
|
2023-02-25 07:08:56 +00:00
|
|
|
};
|
|
|
|
this.log.info(`Added proxy image [${key}]`);
|
|
|
|
}
|
2023-02-25 16:59:56 +00:00
|
|
|
async cmdAll(action, options = {}) {
|
|
|
|
const cmds = [];
|
2023-02-25 22:00:11 +00:00
|
|
|
if (this.useServer()) {
|
2023-02-25 16:59:56 +00:00
|
|
|
this.wss.clients.forEach(function (ws) {
|
|
|
|
cmds.push(this.cmd(ws, action, options));
|
|
|
|
}.bind(this));
|
2023-02-25 18:11:40 +00:00
|
|
|
await Promise.all(cmds);
|
|
|
|
return true;
|
2023-02-25 16:59:56 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-25 18:22:20 +00:00
|
|
|
async displayImage(src) {
|
|
|
|
let key;
|
2023-02-25 22:00:11 +00:00
|
|
|
if (this.useServer()) {
|
2024-04-06 23:09:50 +00:00
|
|
|
key = (0, path_1.basename)(src);
|
2023-02-25 18:22:20 +00:00
|
|
|
this.addProxy(key, src);
|
|
|
|
await this.cmdAll('image', { image: `/image/${key}` });
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-25 22:00:11 +00:00
|
|
|
useServer() {
|
|
|
|
return this.isActive && this.wss.clients.size > 0;
|
|
|
|
}
|
2023-02-25 16:59:56 +00:00
|
|
|
/**
|
|
|
|
* WSS
|
|
|
|
**/
|
|
|
|
async cmd(ws, action, options = {}) {
|
2024-04-06 23:09:50 +00:00
|
|
|
const id = (0, uuid_1.v4)();
|
2023-02-25 16:59:56 +00:00
|
|
|
let obj = {
|
|
|
|
id, action
|
|
|
|
};
|
|
|
|
let str;
|
|
|
|
obj = Object.assign(obj, options);
|
|
|
|
str = JSON.stringify(obj);
|
|
|
|
ws.send(str);
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
this.queue[id] = function (obj) {
|
|
|
|
return resolve(obj);
|
|
|
|
};
|
|
|
|
//setTimeout() ?
|
|
|
|
}.bind(this));
|
|
|
|
}
|
2023-03-18 20:59:26 +00:00
|
|
|
notify(title, message) {
|
|
|
|
this.ui.send('gui', { notify: { title, message } });
|
|
|
|
}
|
2018-01-05 04:43:33 +00:00
|
|
|
}
|
2024-05-23 22:49:18 +00:00
|
|
|
exports.Server = Server;
|
2024-05-24 00:51:35 +00:00
|
|
|
module.exports = { Server };
|
2023-02-25 05:24:07 +00:00
|
|
|
//# sourceMappingURL=index.js.map
|