More work on #20. Fix typos in display code. Should re-work script into transpiled ts file.

This commit is contained in:
Matt McWilliams 2023-02-25 02:08:56 -05:00
parent c9bcb74a9f
commit e64277e438
12 changed files with 372 additions and 30 deletions

View File

@ -1,8 +0,0 @@
/**
* Delay in an async/await function
*
* @param {integer} ms Milliseconds to delay for
*
* @returns {Promise} Promise to resolve after timeout
**/
declare function delay(ms: number): Promise<unknown>;

View File

@ -3,8 +3,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const ws_1 = require("ws");
const express_1 = __importDefault(require("express")); const express_1 = __importDefault(require("express"));
const promises_1 = require("fs/promises"); const promises_1 = require("fs/promises");
const mime_1 = __importDefault(require("mime"));
const Log = require("log"); const Log = require("log");
class Server { class Server {
constructor() { constructor() {
@ -22,6 +24,7 @@ class Server {
]; ];
this.port = 9900; this.port = 9900;
this.wsPort = 9901; this.wsPort = 9901;
this.proxy = {};
this.init(); this.init();
} }
async init() { async init() {
@ -36,6 +39,7 @@ class Server {
} }
this.http.get('/', this.index.bind(this)); this.http.get('/', this.index.bind(this));
this.http.get('/client.js', this.script.bind(this)); this.http.get('/client.js', this.script.bind(this));
this.http.get('/image/:key', this.image.bind(this));
this.log.info("Server assets loaded"); this.log.info("Server assets loaded");
} }
template(name, data) { template(name, data) {
@ -45,7 +49,22 @@ class Server {
} }
return html; return html;
} }
async start() { async startWss() {
try {
this.wss = new ws_1.WebSocketServer({ port: this.wsPort });
}
catch (err) {
this.log.error(err);
return;
}
this.wss.on('connection', (ws) => {
this.log.info(`Client connected to WebSocketServer`);
console.dir(ws);
ws.send('mcopy');
});
this.log.info(`WSS [ ws://localhost:${this.wsPort} ]`);
}
async startHttp() {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
this.httpd = this.http.listen(this.port, function () { this.httpd = this.http.listen(this.port, function () {
this.isActive = true; this.isActive = true;
@ -55,11 +74,16 @@ class Server {
}.bind(this)); }.bind(this));
}.bind(this)); }.bind(this));
} }
stop() { async start() {
await this.startHttp();
await this.startWss();
}
async stop() {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
return this.httpd.close(function () { return this.httpd.close(function () {
this.isActive = false; this.isActive = false;
this.log.info(`Server stopped :(`); this.log.info(`Server stopped :(`);
return resolve(false);
}.bind(this)); }.bind(this));
}.bind(this)); }.bind(this));
} }
@ -74,6 +98,36 @@ class Server {
this.log.info('GET /script.js'); this.log.info('GET /script.js');
return res.send(js); return res.send(js);
} }
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) {
this.proxy[key] = {
path: filePath,
mime: mime_1.default.getType(filePath)
};
this.log.info(`Added proxy image [${key}]`);
}
} }
module.exports = function () { module.exports = function () {
return new Server(); return new Server();

View File

@ -1 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":";;;;;AACA,sDAA0E;AAC1E,0CAAsC;AACtC,2BAA4B;AAa5B,MAAM,MAAM;IAmBX;QAlBQ,OAAE,GAAY,QAAQ,CAAA;QACvB,aAAQ,GAAa,KAAK,CAAA;QAEzB,cAAS,GAAsB;YACtC;gBACC,IAAI,EAAE,OAAO;gBACb,IAAI,EAAG,aAAa;aACpB;YACD;gBACC,IAAI,EAAG,QAAQ;gBACf,IAAI,EAAG,qBAAqB;aAC5B;SACD,CAAA;QAGO,SAAI,GAAY,IAAI,CAAA;QACpB,WAAM,GAAY,IAAI,CAAA;QAG7B,IAAI,CAAC,IAAI,EAAE,CAAA;IAEZ,CAAC;IAED,KAAK,CA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,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAA;oBAC5B,OAAO,MAAM,CAAC,GAAG,CAAC,CAAA;iBACrB;gBACD,OAAO,OAAO,CAAC,IAAI,CAAC,CAAA;YACxB,CAAC,CAAC,CAAA;QACN,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;IACd,CAAC;IAEM,QAAQ,CAAE,GAAY,EAAE,QAAiB;QAC/C,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG;YACjB,IAAI,EAAG,QAAQ;YACf,IAAI,EAAG,cAAI,CAAC,OAAO,CAAC,QAAQ,CAAC;SAC7B,CAAA;QACD,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,sBAAsB,GAAG,GAAG,CAAC,CAAA;IAC5C,CAAC;CACD;AAED,MAAM,CAAC,OAAO,GAAG;IAChB,OAAO,IAAI,MAAM,EAAE,CAAA;AACpB,CAAC,CAAA"}

2
app/package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "mcopy-app", "name": "mcopy-app",
"version": "1.7.8", "version": "1.7.9",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {

View File

@ -1,6 +1,6 @@
{ {
"name": "mcopy-app", "name": "mcopy-app",
"version": "1.7.8", "version": "1.7.9",
"description": "GUI for the mcopy small gauge film optical printer platform", "description": "GUI for the mcopy small gauge film optical printer platform",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {

View File

@ -1,10 +1,37 @@
<html> <html>
<head> <head>
<style> <style>
html, body{ html, body{ background: #000; padding: 0; margin: 0; width: 100vw; height: 100vh; overflow: hidden; }
background: rgb(0, 0, 0); body.meter { background: rgb(117, 117, 117); }
#nosleep { color: #fff; margin: 50px auto }
#img {
position: absolute;
background-repeat:no-repeat;
background-size: contain;
background-position: center;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
#can{
margin: 0;
background: #fff;
display: none;
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
#can.show{
display: block;
} }
</style> </style>
<div id="nosleep">Click to prevent sleep</div>
<div id="img">
</div>
<canvas id="can">
</canvas>
<script src="http://localhost:{{PORT}}/client.js"></script> <script src="http://localhost:{{PORT}}/client.js"></script>
</body> </body>
</html> </html>

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,5 @@
{ {
"version": "1.7.8", "version": "1.7.9",
"ext_port": 1111, "ext_port": 1111,
"profiles": { "profiles": {
"mcopy": { "mcopy": {

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "mcopy", "name": "mcopy",
"version": "1.7.8", "version": "1.7.9",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "mcopy", "name": "mcopy",
"version": "1.7.8", "version": "1.7.9",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"arduino": "file:app/lib/arduino", "arduino": "file:app/lib/arduino",

View File

@ -1,6 +1,6 @@
{ {
"name": "mcopy", "name": "mcopy",
"version": "1.7.8", "version": "1.7.9",
"description": "Small gauge film optical printer platform", "description": "Small gauge film optical printer platform",
"main": "build.js", "main": "build.js",
"directories": { "directories": {

View File

@ -1,5 +1,5 @@
{ {
"version": "1.7.8", "version": "1.7.9",
"ext_port": 1111, "ext_port": 1111,
"profiles": { "profiles": {
"mcopy": { "mcopy": {

View File

@ -1,7 +1,8 @@
import * as WebSocket from 'ws' import WebSocket, { WebSocketServer } from 'ws'
import express, { Express, Request, Response, Application } from 'express' import express, { Express, Request, Response, Application } from 'express'
import { readFile } from 'fs/promises' import { readFile } from 'fs/promises'
import Log = require('log'); import mime from 'mime'
import Log = require('log')
interface ServerData { interface ServerData {
[key: string]: string; [key: string]: string;
@ -11,7 +12,16 @@ interface ServerData {
interface ServerTemplate { interface ServerTemplate {
name : string; name : string;
path : string; path : string;
data? : string data? : string;
}
interface ServerProxy {
path : string;
mime : string;
}
interface ServerProxyList {
[key: string]: ServerProxy;
} }
class Server { class Server {
@ -30,8 +40,10 @@ class Server {
] ]
private http : Application private http : Application
private httpd : Server private httpd : Server
private wss : WebSocketServer
private port : number = 9900 private port : number = 9900
private wsPort : number = 9901 private wsPort : number = 9901
private proxy : ServerProxyList = {}
constructor () { constructor () {
this.init() this.init()
@ -52,6 +64,7 @@ class Server {
this.http.get('/', this.index.bind(this)) this.http.get('/', this.index.bind(this))
this.http.get('/client.js', this.script.bind(this)) this.http.get('/client.js', this.script.bind(this))
this.http.get('/image/:key', this.image.bind(this))
this.log.info("Server assets loaded") this.log.info("Server assets loaded")
} }
@ -64,10 +77,23 @@ class Server {
return html return html
} }
async start () { async startWss () {
try {
this.wss = new WebSocketServer({ port: this.wsPort })
} catch (err) {
this.log.error(err)
return
}
this.wss.on('connection', (ws) => {
this.log.info(`Client connected to WebSocketServer`)
ws.send(JSON.stringify({ action : 'mcopy' });
})
this.log.info(`WSS [ ws://localhost:${this.wsPort} ]`)
}
async startHttp () {
return new Promise(function (resolve : Function, reject : Function) { return new Promise(function (resolve : Function, reject : Function) {
this.httpd = this.http.listen(this.port, function () { this.httpd = this.http.listen(this.port, function () {
this.isActive = true
this.log.info(`Server started!`) this.log.info(`Server started!`)
this.log.info(`URL [ http://localhost:${this.port} ]`) this.log.info(`URL [ http://localhost:${this.port} ]`)
return resolve(true) return resolve(true)
@ -75,14 +101,20 @@ class Server {
}.bind(this)) }.bind(this))
} }
stop() { async start () {
await this.startHttp()
await this.startWss()
this.isActive = true
}
async stop() {
return new Promise(function (resolve : Function, reject : Function) { return new Promise(function (resolve : Function, reject : Function) {
return this.httpd.close(function () { return this.httpd.close(function () {
this.isActive = false this.isActive = false
this.log.info(`Server stopped :(`) this.log.info(`Server stopped :(`)
return resolve(false)
}.bind(this)) }.bind(this))
}.bind(this)) }.bind(this))
} }
index (req : Request, res : Response, next : Function) { index (req : Request, res : Response, next : Function) {
@ -96,6 +128,36 @@ class Server {
this.log.info('GET /script.js') this.log.info('GET /script.js')
return res.send(js) return res.send(js)
} }
async image (req : Request, res : Response, next : Function) {
let filePath : string
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 : Function, reject: Function) {
return res.sendFile(filePath, function (err : any) {
if (err) {
res.status(err.status).end()
return reject(err)
}
return resolve(true)
})
}.bind(this))
}
public addProxy (key : string, filePath : string) {
this.proxy[key] = {
path : filePath,
mime : mime.getType(filePath)
}
this.log.info(`Added proxy image [${key}]`)
}
} }
module.exports = function () { module.exports = function () {