wavey-wind/server.js

210 lines
4.3 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')
2022-03-03 11:11:38 +01:00
const readline = require('readline')
const fs = require('fs')
2021-08-11 14:39:34 +02:00
const port = 6676
// Vzemi iz argumenta
const tty = process.argv[2]
2022-03-03 21:51:02 +01:00
const baudrate = parseInt(process.argv[3])
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
2022-03-03 21:51:02 +01:00
function getArgs(parts) {
console.log('split to parts', parts)
switch (parts[1]) {
case 'quaternion':
return parts.splice(2).map((num) => ({
type: 'f',
value: parseFloat(num)
}))
}
// Default
return []
}
function getMsg(line) {
return null
}
function openSerial(bitrate) {
2021-08-14 15:15:16 +02:00
console.log('opening ', tty)
2022-03-03 21:51:02 +01:00
if (!bitrate) {
bitrate = 115200
}
2021-08-14 15:15:16 +02:00
scon = new osc.SerialPort({
devicePath: tty,
2022-03-03 21:51:02 +01:00
bitrate: bitrate,
autoOpen: true,
useSLIP: true
2021-08-14 15:15:16 +02:00
})
scon.open()
scon.on('open', e => {
console.log('serial connection opened')
//console.log(scon)
2021-08-14 15:15:16 +02:00
})
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
2022-03-03 21:51:02 +01:00
scon.on('message', msg => {
console.log('osc msg', msg)
2021-08-14 15:15:16 +02:00
// Debug incoming osc
2022-01-01 22:10:59 +01:00
if (msg.address.indexOf('quaternion') > -1) {
console.log('tty osc', msg.address, ...msg.args.map(val => Math.round(val * 1000) / 1000))
}
2021-08-14 15:15:16 +02:00
sendAll(msg, null, null, osclients)
})
if (scon._closeCode) {
scon = null
setTimeout(openSerial, 1000)
}
}
2022-03-03 11:11:38 +01:00
function openBT() {
console.log('opening bluetooth', tty)
const rl = readline.createInterface({
input: fs.createReadStream(tty)
})
rl.on('line', (line) => {
2022-03-03 21:51:02 +01:00
// Convert line to byte array
var enc = new TextEncoder(); // always utf-8
const arr = enc.encode(line);
try {
const msg = osc.readMessage(arr)
console.log(msg)
sendAll(msg, null, null, osclients)
} catch (e) {
console.warn("msg error", e)
}
2022-03-03 11:11:38 +01:00
})
}
if (tty) {
if (baudrate) {
openSerial(baudrate)
} else {
openBT()
}
2022-03-03 21:51:02 +01:00
}
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
const scudp = new osc.UDPPort({
multicastMembership: ['224.0.1.9'],
remotePort: 6696,
multicastTTL: 10,
metadata: true
})
scudp.on('open', () => {
console.log("UDP to OSC open")
})
scudp.on('message', (msg) => {
console.log('got UDP msg', msg);
osclients.forEach( client => {
if (client) {
//console.log("sending", msg, info)
client.send(msg)
}
})
})
scudp.open()
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)
}
})
if (scudp) {
scudp.send(msg)
}
2021-08-11 14:39:34 +02:00
}
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)
})
2022-03-03 21:51:02 +01:00