2020-09-23 00:23:59 +02:00
|
|
|
const express = require('express')
|
|
|
|
const http = require('http')
|
|
|
|
const WebSocket = require('ws')
|
|
|
|
|
2021-08-11 14:39:34 +02:00
|
|
|
const osc = require('osc')
|
2017-12-02 21:25:04 +01:00
|
|
|
|
2021-08-11 14:39:34 +02:00
|
|
|
const port = 6676
|
|
|
|
// Vzemi iz argumenta
|
|
|
|
const tty = process.argv[2]
|
2017-12-02 21:25:04 +01:00
|
|
|
|
|
|
|
const include_files = [
|
|
|
|
'/anim.js',
|
|
|
|
'/control.js',
|
2020-09-22 22:40:05 +02:00
|
|
|
'/node_modules/three/build/three.min.js',
|
|
|
|
'/node_modules/nouislider/distribute/nouislider.min.js',
|
|
|
|
'/node_modules/nouislider/distribute/nouislider.min.css',
|
2017-12-02 21:25:04 +01:00
|
|
|
];
|
|
|
|
|
2020-09-23 00:23:59 +02:00
|
|
|
const app = express();
|
|
|
|
const server = http.Server(app);
|
|
|
|
|
2021-08-11 14:39:34 +02:00
|
|
|
// Odprti serijski OSC link
|
|
|
|
const scon = new osc.SerialPort({
|
|
|
|
devicePath: tty
|
|
|
|
})
|
|
|
|
scon.on('error', e => {
|
|
|
|
console.log('tty error', e)
|
|
|
|
})
|
|
|
|
scon.open()
|
|
|
|
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
|
2017-12-02 21:25:04 +01:00
|
|
|
let settings = {};
|
|
|
|
app.get('/settings', function(req, res) {
|
|
|
|
res.send(settings);
|
2017-10-06 23:20:52 +02:00
|
|
|
});
|
|
|
|
|
2017-12-02 21:25:04 +01:00
|
|
|
include_files.map(function(file) {
|
|
|
|
app.get(file, function(req, res){
|
|
|
|
res.sendFile(__dirname + file);
|
|
|
|
});
|
2017-10-06 23:20:52 +02:00
|
|
|
});
|
|
|
|
|
2020-09-23 00:23:59 +02:00
|
|
|
server.listen(port, () => console.log('listening on *:' + port))
|
2017-10-06 23:20:52 +02:00
|
|
|
|
2020-09-23 00:23:59 +02:00
|
|
|
// Websocket init
|
|
|
|
const wss = new WebSocket.Server({ server })
|
2017-10-06 23:20:52 +02:00
|
|
|
|
2021-08-11 14:39:34 +02:00
|
|
|
const sendAll = (msg, wss, ws = null) => {
|
|
|
|
wss.clients.forEach( client => {
|
|
|
|
if (client != ws) {
|
|
|
|
client.send(msg)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debug osc
|
|
|
|
scon.on('osc', msg => {
|
|
|
|
console.log('tty osc', msg.address, ...msg.args)
|
|
|
|
})
|
2017-10-06 23:20:52 +02:00
|
|
|
|
2021-08-11 14:39:34 +02:00
|
|
|
wss.on('connection', function (ws) {
|
|
|
|
const oscWS = new osc.WebSocketPort({
|
|
|
|
socket: ws,
|
|
|
|
metadata: true
|
|
|
|
});
|
|
|
|
|
|
|
|
// Arduino OSC gre v web
|
|
|
|
scon.on('osc', msg => {
|
|
|
|
switch(msg.address) {
|
|
|
|
case '/gyro/':
|
|
|
|
const [gx, gy, gz] = msg.args
|
|
|
|
sendAll('adjust:rotx:' + gx, wss)
|
|
|
|
sendAll('adjust:roty:' + gy, wss)
|
|
|
|
sendAll('adjust:rotz:' + gz, wss)
|
|
|
|
break;
|
|
|
|
case '/accel/':
|
|
|
|
const [ax, ay, az] = msg.args
|
|
|
|
sendAll('adjust:rotx:' + ax, wss)
|
|
|
|
sendAll('adjust:roty:' + ay, wss)
|
|
|
|
sendAll('adjust:rotz:' + az, wss)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
})
|
2020-09-23 00:23:59 +02:00
|
|
|
|
|
|
|
ws.on('message', (msg) => {
|
|
|
|
//console.log('got msg', msg)
|
|
|
|
const parts = msg.split(":")
|
|
|
|
const cmd = parts[0]
|
|
|
|
|
|
|
|
// Broadcast adjust msg
|
|
|
|
switch (cmd) {
|
|
|
|
case 'adjust':
|
2021-08-11 14:39:34 +02:00
|
|
|
sendAll(msg, wss, ws)
|
2020-09-23 00:23:59 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|