const express = require('express') const http = require('http') const WebSocket = require('ws') const osc = require('osc') const port = 6676 // Vzemi iz argumenta const tty = process.argv[2] const include_files = [ '/anim.js', '/control.js', '/node_modules/three/build/three.min.js', '/node_modules/nouislider/distribute/nouislider.min.js', '/node_modules/nouislider/distribute/nouislider.min.css', ]; const app = express(); const server = http.Server(app); // 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) => { res.sendFile(__dirname + '/index.html'); }); app.get('/ctl', (req, res) => { res.sendFile(__dirname + '/control.html'); }); let settings = {}; app.get('/settings', function(req, res) { res.send(settings); }); include_files.map(function(file) { app.get(file, function(req, res){ res.sendFile(__dirname + file); }); }); server.listen(port, () => console.log('listening on *:' + port)) // Websocket init const wss = new WebSocket.Server({ server }) 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) }) 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; } }) ws.on('message', (msg) => { //console.log('got msg', msg) const parts = msg.split(":") const cmd = parts[0] // Broadcast adjust msg switch (cmd) { case 'adjust': sendAll(msg, wss, ws) } }) })