-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
173 lines (154 loc) · 4.46 KB
/
server.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const argv = require('yargs/yargs')(process.argv.slice(2)).argv;
const express = require('express');
const app = express();
const http = require('http').Server(app);
const { LedHandler } = require("./ledhandler");
var port = 3000;
if (argv.port){
port = argv.port;
}
http.listen(port, () => {
console.log('listening on *:' + port);
});
app.use(express.static('public'));
var brightness = 0;
var maxbrightness = 0;
var pressure = 0;
var maxbrushsize = 0;
var brush = 1;
var hexcolor = '#000000';
var stampbrush = 'false';
var ledHandlerInstance = new LedHandler(argv.led);
ledHandlerInstance.run();
const io = require('socket.io')(http);
io.sockets.on('connection', (socket) => {
console.log('Client has connected...');
io.sockets.emit('brush', {value: brush});
io.sockets.emit('brightness', {value: brightness});
io.sockets.emit('maxbrightness', {value: maxbrightness});
io.sockets.emit('pressure', {value: pressure});
io.sockets.emit('maxbrushsize', {value: maxbrushsize});
io.sockets.emit('hexcolor', {value: hexcolor});
socket.on('brightness', (data) => {
//Expected value range: 0-100
var obj = getJsonObject(data);
if(checkValue(obj.value)!=null){
brightness = obj.value;
ledHandlerInstance.setBrightness(brightness);
io.sockets.emit('brightness', {value: brightness});
}
});
socket.on('brush', (data) => {
//Expected values: 1,2,...
var obj = getJsonObject(data);
if(checkValue(obj.value)!=null){
brush = obj.value
ledHandlerInstance.setBrush(brush);
io.sockets.emit('brush', {value: brush});
}
});
socket.on('errorhandling', () => {
ledHandlerInstance.showError();
});
socket.on('hexcolor', (data) => {
var obj = getJsonObject(data);
if(checkValue(obj.value)!=null){
hexcolor = obj.value;
ledHandlerInstance.setHexColor(hexcolor);
io.sockets.emit('hexcolor', {value: hexcolor});
}
});
socket.on('maxbrightness', (data) => {
//Expected value range: 0-100
var obj = getJsonObject(data);
if(checkValue(obj.value)!=null){
maxbrightness = obj.value;
ledHandlerInstance.setMaxBrightness(maxbrightness);
io.sockets.emit('maxbrightness', {value: maxbrightness});
}
});
socket.on('maxbrushsize', (data) => {
//Expected value range: 0-1 (float)
var obj = getJsonObject(data);
if(checkValue(obj.value)!=null){
maxbrushsize = obj.value;
maxbrushsize = maxbrushsize.replace(/,/g, '.');
ledHandlerInstance.setMaxBrushSize(maxbrushsize);
io.sockets.emit('maxbrushsize', {value: maxbrushsize});
}
});
socket.on('pressure', (data) => {
//Expected value range: 0-100
var obj = getJsonObject(data);
if(checkValue(obj.value)!=null){
pressure = obj.value;
ledHandlerInstance.setPressure(pressure);
io.sockets.emit('pressure', {value: pressure});
}
});
socket.on('stampbrush', (data) => {
//Expected values: true, false
var obj = getJsonObject(data);
if(checkValue(obj.value)!=null){
var stampbrush = obj.value;
if (stampbrush == 'true'){
stampbrush = true;
} else {
stampbrush = false;
}
ledHandlerInstance.setStampBrush(stampbrush);
io.sockets.emit('stampbrush', {value: stampbrush});
}
});
socket.on('gradient', (data) => {
//Expected values: true, false
var obj = getJsonObject(data);
if(checkValue(obj.value)!=null){
var gradient = obj.value;
if (gradient == 'true'){
gradient = true;
} else {
gradient = false;
}
ledHandlerInstance.setGradient(gradient);
io.sockets.emit('gradient', {value: gradient});
}
});
socket.on('glow', (data) => {
//Expected values: true, false
var obj = getJsonObject(data);
if(checkValue(obj.value)!=null){
var glow = obj.value;
if (glow == 'true'){
glow = true;
} else {
glow = false;
}
ledHandlerInstance.setGlow(glow);
io.sockets.emit('glow', {value: glow});
}
});
});
function checkValue(value){
if (value!=null){
return value;
} else {
io.sockets.emit('errorhandling');
return null;
}
}
function blink(seconds){
//ledHandlerInstance.setPressure(50);
for(i=0; i<= 100;i++){
ledHandlerInstance.setPressure(i);
sleep(seconds*50);
}
}
function getJsonObject(data){
if (typeof data == 'string'){
var obj = JSON.parse(data);
} else {
obj = data;
}
return obj;
}