-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (92 loc) · 2.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server, {
'log': false
}),
ioClient = require('socket.io-client'),
fs = require('fs'),
midi = require('midi'),
LightArray = require('./lib/lightarray'),
mdns = require('mdns');
var version = 1,
midiInput,
sockets = {};
var lightArray = new LightArray({
'serialPort': '/dev/tty.usbmodem1421',
'debug': false,
'sockets': sockets,
'device': process.env.DEVICE
});
var remote,
remoteBrowser,
remoteAddress,
remotePort;
app.use("/js", express.static(__dirname + '/web/js'));
app.use("/css", express.static(__dirname + '/web/css'));
app.use(express.bodyParser());
// Web app routes
app.get('/', function(req, res){
fs.readFile(__dirname + "/web/index.html", "UTF-8", function(err, data){
res.send(data);
});
});
app.get('/client', function(req, res){
fs.readFile(__dirname + "/web/client.html", "UTF-8", function(err, data){
res.send(data);
});
});
// API routes
app.get('/api/v' + version + '/array', function(req, res){
res.send({
'values': lightArray.getValues()
});
});
app.post('/api/v' + version + '/array', function(req, res){
lightArray.updateAll(req.body.values);
});
app.get('/api/v' + version + '/array/:element', function(req, res){
res.send(lightArray.getValues()[req.params.element]);
});
app.post('/api/v' + version + '/array/:element', function(req, res){
lightArray.update(req.params.element, req.body.value);
});
io.sockets.on('connection', function (socket) {
sockets[socket.id] = socket;
socket.on('update', function(data){
lightArray.update(data.element, data.value);
});
socket.on('updateAll', function(data){
lightArray.updateAll(data.values);
});
socket.on('disconnect', function(){
delete sockets[socket.id];
});
});
lightArray.on('ready', function(){
// initialize midi stuff
midiInput = new midi.input();
midiInput.openVirtualPort("LightArray");
lightArray.start();
midiInput.on('message', function(time, message){console.log(message);
if (message[0] === 176 && 41 < message[1] < 46){
lightArray.update(message[1] - 42, message[2]);
}
});
// initialize remote
remoteBrowser = mdns.createBrowser(new mdns.ServiceType('lightarray', 'tcp'));
remoteBrowser.on('serviceUp', function(service) {
remoteAddress = service.addresses[0];
remotePort = service.port;
console.log('attempting connection to remote at ' + remoteAddress + ':' + remotePort)
remote = ioClient.connect("http://" + remoteAddress + ":" + remotePort);
if (remote){
console.log('successfully connected to remote');
lightArray.setRemote(remote);
}
});
remoteBrowser.start();
// initialize web server stuff
server.listen(8000);
console.log('listening');
});