wavey-wind/server.js

129 lines
2.8 KiB
JavaScript
Raw Normal View History

const express = require('express')
const http = require('http')
const WebSocket = require('ws')
2021-08-11 14:39:34 +02:00
const osc = require('osc')
2021-08-11 14:39:34 +02:00
const port = 6676
// Vzemi iz argumenta
const tty = process.argv[2]
const include_files = [
2021-08-14 15:15:16 +02:00
'/anim.js',
'/control.js',
'/osctl.js',
'/test.js',
'/node_modules/three/build/three.min.js',
'/node_modules/nouislider/distribute/nouislider.min.js',
'/node_modules/nouislider/distribute/nouislider.min.css',
'/node_modules/osc/dist/osc-browser.js'
];
const app = express();
const server = http.Server(app);
2021-08-11 14:39:34 +02:00
// Odprti serijski OSC link
2021-08-14 15:15:16 +02:00
let scon = null
function openSerial() {
console.log('opening ', tty)
scon = new osc.SerialPort({
devicePath: tty,
bitrate: 115200,
autoOpen: true
})
scon.open()
scon.on('open', e => {
console.log('scon opened', e)
console.log(scon)
})
scon.on('error', e => {
console.error('tty error', e)
/*
if (e.match(/cannot open/)) {
scon = null
setTimeout(openSerial, 1000)
}
*/
})
scon.on('close', e => {
console.warn('serial connection closed, restarting in 1 second')
setTimeout(openSerial, 1000)
scon = null
})
// Arduino OSC gre v web
scon.on('osc', msg => {
// Debug incoming osc
//console.log('tty osc', msg.address, ...msg.args.map(val => Math.round(val * 1000) / 1000))
sendAll(msg, null, null, osclients)
})
if (scon._closeCode) {
scon = null
setTimeout(openSerial, 1000)
}
}
openSerial()
2021-08-11 14:39:34 +02:00
app.get('/', (req, res) => {
2017-10-06 23:20:52 +02:00
res.sendFile(__dirname + '/index.html');
});
2021-08-11 14:39:34 +02:00
app.get('/ctl', (req, res) => {
2017-10-06 23:20:52 +02:00
res.sendFile(__dirname + '/control.html');
});
2021-08-12 18:10:19 +02:00
app.get('/test', (req, res) => {
res.sendFile(__dirname + '/test.html');
});
let settings = {};
app.get('/settings', function(req, res) {
res.send(settings);
2017-10-06 23:20:52 +02:00
});
include_files.map(function(file) {
app.get(file, function(req, res){
res.sendFile(__dirname + file);
});
2017-10-06 23:20:52 +02:00
});
server.listen(port, () => console.log('listening on *:' + port))
2017-10-06 23:20:52 +02:00
// Websocket init
const wss = new WebSocket.Server({ server })
2017-10-06 23:20:52 +02:00
2021-08-14 15:15:16 +02:00
const sendAll = (msg, info, oscWS, osclients) => {
osclients.forEach( client => {
if (client && oscWS != client) {
//console.log("sending", msg, info)
2021-08-11 14:39:34 +02:00
client.send(msg)
}
})
}
2021-08-14 15:15:16 +02:00
const osclients = []
2017-10-06 23:20:52 +02:00
2021-08-11 14:39:34 +02:00
wss.on('connection', function (ws) {
2021-08-14 15:15:16 +02:00
console.log('client connection', ws)
2021-08-11 14:39:34 +02:00
const oscWS = new osc.WebSocketPort({
2021-08-14 15:15:16 +02:00
socket: ws,
metadata: false
2021-08-11 14:39:34 +02:00
});
2021-08-14 15:15:16 +02:00
// Vsi OSC sem grejo naprej na kliente OSC
oscWS.on('packet', (packet, info) => {
//console.log('got msg', msg)
// Broadcast adjust msg
2021-08-14 15:15:16 +02:00
const [address, args] = packet
sendAll({ address, args}, info, oscWS, osclients)
})
oscWS.on('error', error => {
console.warn('Ignoring invalid OSC')
console.warn(error)
})
2021-08-14 15:15:16 +02:00
osclients.push(oscWS)
})