-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
187 lines (136 loc) · 3.7 KB
/
app.js
File metadata and controls
187 lines (136 loc) · 3.7 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/////////////
//VARIABLES//
/////////////
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');
var servos = require('./servos.js');
var lights = require('./lights.js');
//var wakeword = require('./wakeword.js');
var kitchenSocket = require('socket.io-client')('http://light-switch.local:8000');
//////////////////
//EXPRESS SERVER//
//////////////////
//Define Public Dir
app.use(express.static(__dirname + '/public'));
//Server Set Up
var port = 8000
http.listen(port,function(){
console.log('Server Initiated' );
});
///////////
//SOCKETS//
///////////
var clients = [];
io.on('connection', function(socket){
/////////////////
//ON CONNECTION//
/////////////////
clients.push(socket.id);
console.log('A user has connected');
if(clients.length == null)
console.log("Current Users: 0");
else
console.log("Current Users: " + clients.length);
io.emit('clients connected', clients.length);//update client's user count
/////////////////
//ON DISCONNECT//
/////////////////
socket.on('disconnect', function(){
for(var i = 0; i < clients.length; i++){
if(clients[i] == socket.id){
clients.splice(i,1);
}
}
console.log('A user has disconnected');
if(clients.length == null)
console.log("Current Users: 0");
else
console.log("Current Users: " + clients.length);
io.emit('clients connected', clients.length);//update client's user count
});
////////////
//CONTROLS//
////////////
//Set CLI variable
var exec = require('child_process').exec;
//Event Listener
socket.on('remoteCommand', function(cmd){
console.log(cmd);//log button pressed
//////////////////////
//PROJECTOR CONTROLS//
//////////////////////
if (cmd === 'PROJECTOR_ON'){
servos.projectorOn();
}
if (cmd === 'PROJECTOR_OFF'){
servos.projectorOff();
}
if (cmd === 'PROJECTOR_CALIBRATE'){
servos.projectorCalibrate();
}
if (cmd === 'PROJECTOR_UP'){
servos.projectorUp();
}
if (cmd === 'PROJECTOR_DOWN'){
servos.projectorDown();
}
if (cmd === 'PROJECTOR_IN'){
servos.projectorIn();
}
if (cmd === 'PROJECTOR_OUT'){
servos.projectorOut();
}
////////////////////////////
//PROJECTOR LIGHT CONTROLS//
////////////////////////////
if (cmd === 'LIGHTS_ON'){
lights.lightsOn();
}
if (cmd === 'LIGHTS_OFF'){
lights.lightsOff();
}
//////////////////////////
//KITCHEN LIGHT CONTROLS//
//////////////////////////
if (cmd === 'KITCHEN_LIGHTS_ON'){
io.emit('remoteCommand','KITCHEN_LIGHTS_ON');
console.log('Kitchen Lights On')
}
if (cmd === 'KITCHEN_LIGHTS_OFF'){
io.emit('remoteCommand','KITCHEN_LIGHTS_OFF');
console.log('Kitchen Lights Off')
}
////////////////////
//SPEAKER CONTROLS//
////////////////////
if (cmd === 'SPEAKER_ON'){
io.emit('SPEAKER_ON','SPEAKER_ON');
console.log('Speaker On')
}
});
/////////////////////////
//LIGHT SLIDER CONTROLS//
/////////////////////////
socket.on('sliderCommand', function(cmd){
console.log(cmd.id);//log slider change
if (cmd.id === 'SLIDER_WHITE'){
console.log(cmd.value);
lights.lightsSliderWhite(cmd.value);
}
if (cmd.id === 'SLIDER_RED'){
console.log(cmd.value);
lights.lightsSliderRed(cmd.value);
}
if (cmd.id === 'SLIDER_GREEN'){
console.log(cmd.value);
lights.lightsSliderGreen(cmd.value);
}
if (cmd.id === 'SLIDER_BLUE'){
console.log(cmd.value);
lights.lightsSliderBlue(cmd.value);
}
});
});