diff --git a/cf.js b/cf.js index 50e9c1bf75f4101f1e5d9bf56fc8c53a21112ada..176b4e062c902e871b24a6c9ff43de509ce836fd 100644 --- a/cf.js +++ b/cf.js @@ -77,13 +77,13 @@ app.post('/save/systems/:file', (req, res) => { // we also want to institute some pipes: this is a holdover for a better system // more akin to nautilus, where server-side graphs are manipulated // for now, we just want to dive down to a usb port, probably, so this shallow link is OK -let workers = [] app.get('/spawnProcess/:file', (req, res) => { // launches another node instance at this file w/ these args, let args = '' if(req.query){ args = req.query.args.split(',') } + console.log(`forking ${req.params.file} with ${args}`) const process = child_process.fork(`processes/${req.params.file}`) process.on('message', (data) => { console.log('process msg', data) @@ -98,28 +98,6 @@ app.get('/spawnProcess/:file', (req, res) => { process.on('close', (code) => { console.log('process closes', code) }) - /* - const piper = new Worker(`${__dirname}/pipes/${req.params.file}`, {workerData: req.query}) - piper.on('message', (msg) => { - if(msg.startup){ - console.log('worker for /pipeHookup/' + req.params.file + ' resolves') - res.send({startup: true, ip: ownIp, port: msg.port}) - } - }) - piper.on('error', (err) => { - console.log('worker err', err) - }) - piper.on('exit', (code) => { - if(code !== 0){ - console.log('worker exist with nonzero code', code) - } else { - console.log('worker exits OK') - } - }) - */ - // then this (or similar) should be all we really need to add here, and we can do local-dev of the pipes in hunks/pipes/pipename.js and pipes/pipename.js - // so, do we spawn, or do we use workers ? - // awh yis it's workers, messages easy, errors also OK to catch... nextup: draw the sys, what wraps what doesn't? mostly: want to be able to refresh / reload / restart remotely }) // finally, we tell the thing to listen here: diff --git a/hunks/pipes/vfptc.js b/hunks/pipes/vfptc.js index bccb54a8fed0e93cb7a1965a764917fc01fd7b7c..e129826e5489bec2ffe73f85136688da55ba6b4e 100644 --- a/hunks/pipes/vfptc.js +++ b/hunks/pipes/vfptc.js @@ -4,6 +4,10 @@ very fast ~~picket ship~~ pipe transport (client) */ +// OK: this is nice and should serve for a demo, +// BUT: before other things, really want this to successfully reset +// *reliably* - killing the whole downstream process + import { Hunkify, Input, @@ -110,8 +114,10 @@ export default function VFPTC() { let data = JSON.parse(msg.data) if(data.type === 'echo'){ console.log('echo returns') + } else if (data.type === 'serial status'){ + serialStatusMessage.set(data.status) } else { - console.log('how to handle:', data) + console.error('vfptc unhandled:', data) } } else { if(msg.data instanceof Blob){ @@ -146,7 +152,7 @@ export default function VFPTC() { this.onDelete = () => { // important to also shutdown remote process, // do: have reply come with pid, - // on delete, send req to kill that pid ... + // on delete, send req to kill that pid ... if (ws && ws.readyState === 1) ws.close() } } diff --git a/processes/vfpts.js b/processes/vfpts.js index aecb2fc9863d87cc90857a47ad957c524a7c4ba0..0c0d839a29ed91975dfd72dff5da0bc6195ee918 100644 --- a/processes/vfpts.js +++ b/processes/vfpts.js @@ -8,6 +8,12 @@ const WebSocketServer = require('ws').Server const SerialPort = require('serialport') const Delimiter = require('@serialport/parser-delimiter') +const STATUS_UNKNOWN = 'unknown...' +const STATUS_OPENING = 'opening...' +const STATUS_OPEN = 'open' +const STATUS_CLOSED = 'closed' +const STATUS_ERROR = 'error' + // port to issue on, let port = '2042' let pid = '8022' @@ -19,21 +25,28 @@ const WSS = new WebSocketServer({port: port}, () => { }) }) +let WS = null + WSS.on('connection', (ws) => { console.log('ws connects') + // handles, + WS = ws + // send current status + sendSerialStatus() + // handlers, ws.onmessage = (msg) => { if(typeof msg.data === 'string'){ // probably a control object, do let data = JSON.parse(msg.data) if(data.type === 'echo'){ - ws.send(msg.data) + sendToBrowser(msg.data) } else { console.log("how to handle:", data) } } else { if(Buffer.isBuffer(msg.data)){ // echo also for now, - ws.send(msg.data) + sendToBrowser(msg.data) // assume it's to-be-put-to-port } } @@ -45,10 +58,46 @@ WSS.on('connection', (ws) => { } }) +// send wrapper +let sendToBrowser = (msg) => { + if (WS) { + WS.send(msg) + return true + } else { + return false + } +} + // now the usb, let serport = null let comname = '' +let sendSerialStatus = () => { + if(serport){ + if(serport.opening){ + sendToBrowser(JSON.stringify({ + type: 'serial status', + status: STATUS_OPENING + })) + } else if (serport.readable){ + sendToBrowser(JSON.stringify({ + type: 'serial status', + status: STATUS_OPEN + })) + } else { + sendToBrowser(JSON.stringify({ + type: 'serial status', + status: STATUS_CLOSED + })) + } + } else { + sendToBrowser(JSON.stringify({ + type: 'serial status', + status: STATUS_CLOSED + })) + } +} + let findSerialPort = () => { let found = false SerialPort.list((err, ports) => { @@ -63,12 +112,13 @@ let findSerialPort = () => { } let openPort = () => { - /* serport = new SerialPort(comname, { baudRate: 3000000 }) serport.on('open', () => { + sendSerialStatus() serport.on('error', (err) => { + sendSerialStatus() console.log('port error', err) }) const parser = serport.pipe(new Delimiter({delimiter: [0]})) @@ -86,7 +136,6 @@ let openPort = () => { } }) }) - */ } findSerialPort()