-
Notifications
You must be signed in to change notification settings - Fork 3
/
communication.js
133 lines (117 loc) · 3.54 KB
/
communication.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const _ = require('lodash');
const express = require('express');
const http = require('http');
const app = express();
const server = http.createServer(app);
const io = require('socket.io').listen(server);
const now = require('performance-now');
const path = require('path');
const Device = require('./device')
const Multiplexer = require('./multiplexer')
var device1, device2, device3, device4
var multiplexer
device1 = new Device(300, 'COM4')
device2 = new Device(300, 'COM5')
setTimeout(() => {
multiplexer = new Multiplexer(600, [device1, device2], (index) => {
if(index < 300) {
return [0, index]
} else {
if(index < 450)
return [1, index - 300 + 150]
else
return [1, index - 300 - 150]
}
})
}, 2000)
let state = "off"
io.on('connection', (socket) => {
function setState(newState){
if(state !== newState) {
state = newState;
console.log("Broadcasteando state", state)
io.sockets.emit('data', {state: state})
}
}
function sendLightsToLeds(colorArray){
if (multiplexer) {
multiplexer.setState(colorArray)
}
}
let djActionTotalTime = 0;
let djActionInterval = 0;
let djActionStartTime = 0;
let colorFunc = null;
function temporalEffectAction(getColorsFunc, timeInc){
const emitTimeRemaining = _.throttle(t => io.sockets.emit('data', {stateTimeRemaining: t}), 200)
return () => {
setState("dj-action");
colorFunc = getColorsFunc;
djActionTotalTime += timeInc;
if(!djActionInterval){
djActionStartTime = new Date();
// 60 fps
djActionInterval = setInterval(()=>{
if(state === "dj-action") {
let elapsedTime = new Date() - djActionStartTime;
if (elapsedTime < djActionTotalTime) {
sendLightsToLeds(colorFunc())
emitTimeRemaining(djActionTotalTime - elapsedTime)
} else {
djActionTotalTime = 0
setState("off");
clearTimeout(djActionInterval)
djActionInterval = 0
}
}
else {
djActionTotalTime = 0
clearTimeout(djActionInterval)
djActionInterval = 0
}
}, 1000/60)
}
}
}
const djActions = {
"off": {
description: "APAGAR +5s",
run: temporalEffectAction(t => _.range(0,600).map(i => '#000000'), 5000)
},
"white": {
description: "WHITE +5s",
run: temporalEffectAction(t => _.range(0,600).map(i => '#777777'), 5000)
},
"warro": {
description: "Logo Pampa Warro +2s",
run: temporalEffectAction(t => {
let todoNaranja = _.range(0,600).map(i => '#FF9933')
_.each(_.range(0,30).concat(_.range(450,480)), i => todoNaranja[i] = "#000000")
return todoNaranja
}, 2000)
},
"resume": {
description: "Retomar programa",
run: () => setState("off")
}
}
socket.emit('data', {state: state, actions: _.mapValues(djActions, action => action.description)})
socket.on('message', (data) => {
if (data.action === 'leds') {
if (state !== "dj-action") {
setState("running-program")
sendLightsToLeds(data.payload)
}
} else if (data.action === "dj-action"){
let action = djActions[data.payload];
if(action) {
console.log(`DJ ACTION ${action.description}`)
action.run()
}
} else if (data.action === "remoteCmd"){
console.log("Remote command:", data.payload)
socket.broadcast.emit('cmd', data.payload)
}
})
});
server.listen(3000, '0.0.0.0')