-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
284 lines (241 loc) · 6.89 KB
/
app.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Required Libraries
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
url = require('url'),
fs = require('fs');
var sys = require('sys');
var exec = require('child_process').exec;
// Global Variables
var red_stat = 0; //Alarm on/off
var grn_stat = 0; // Radio on/off
var pressed = 0; // HW Button pressed/not pressed
var active = 1; // Watching for alarm-conditions on/off
var al_days = new Array(0,0,0,0,0,0,0); // Sunday = 0, ... , Sa = 6
var alarm_time = "12:00"; // Initial Alarm time
// Start Socket on port 8001
app.listen(8001);
// GPIO Magic
// --------------------------
// Pin 17: button
// Pin 22: Red LED
// Pin 27: Green LED
// Pin 23: Audio Amp ( High = 1 = On, Low = 0 = Off)
var GPIO = require('onoff').Gpio;
var red = new GPIO(22, 'out');
var grn = new GPIO(27, 'out');
var amp = new GPIO (23, 'out');
var button = new GPIO(17, 'in', 'both', {persistentWatch: true, debounceTimeout: 50});
//Init LEDs to off
red.writeSync(red_stat);
grn.writeSync(grn_stat);
amp.writeSync(0);
//HTTP Handler Function
function handler (req, res) {
// Parse requested URL
var path = url.parse(req.url).pathname
// Manage the root route
if (path == '/') {
index = fs.readFile('index.html',
function (error, data) {
if (error) {
res.writeHead(500);
return res.end("Error: unable to load index.html");
} // if
res.writeHead(200,{'Content-Type': 'text/html'});
res.end(data);
}); // function
// Manage the js route
} else if( /\.(js)$/.test(path) ) {
index = fs.readFile(__dirname+path,
function(error,data) {
if (error) {
res.writeHead(500);
return res.end("Error: unable to load " + path);
}
res.writeHead(200,{'Content-Type': 'text/plain'});
res.end(data);
}); // elseif
// Manage the css route
} else if( /\.(css)$/.test(path) ) {
index = fs.readFile(__dirname+path,
function(error,data) {
if (error) {
res.writeHead(500);
return res.end("Error: unable to load " + path);
}
res.writeHead(200,{'Content-Type': 'text/css'});
res.end(data);
}); // elseif
} else {
res.writeHead(404);
res.end("Error: 404 - File not found.");
} // if
} //HTTP handler
// Web Socket Connection
io.sockets.on ('connection', function (socket) {
// Respond to ping
socket.on('ping', function(data) {
console.log("ping");
delay = data ["duration"];
// Set a timer for when we should stop watering
setTimeout(function(){
socket.emit("pong");
}, delay*100);
});
// Set Radio
socket.on('green', function(data) {
// turn RED on
grn_stat = data.green;
set_green();
}); // green
// SetVolume
socket.on('vol', function(data) {
set_volume(data.dir);
}); // green
// Skip Title
socket.on('skip', function(data) {
skip(data.dir);
}); // green
// Set Alarm
socket.on('red', function(data) {
// turn Green on
red_stat = data.red;
set_red();
}); // red
// Set Alarm-days
socket.on('wd', function(data) {
al_days = data.wd;
send_status_all();
console.log(al_days);
}); // red
// Set Alarm-Time
socket.on('alarm-time', function(data) {
// turn Green on
alarm_time = data.al_time;
send_status_all();
console.log (alarm_time);
}); // alarm_time
// Respond to Status Request
socket.on('req_status', send_status );
// Update Time once a second
timewatch = setInterval( sendTime, 1000);
// Watch the Hardware button
button.watch(function(err, state) {
// Error Handling
if (err) {
throw err;
} else {
console.log('Button: ' + state);
// check the state of the button
// 1 == pressed, 0 == not pressed
if(state == 0 && pressed ==0) {
// toogle Radio
console.log("Button pressed");
if (grn_stat === 0){
grn_stat = 1
}else{
grn_stat = 0
};
set_green();
pressed = 1;
}; // if pressed
if (state == 1 && pressed == 1){
pressed = 0;
console.log("Button released");
}; // if released
} //err
}); //button watch
// Get time, send time and check for alarm
function sendTime (){
// get time
var jetzt = new Date();
// Format time
var tag =( (jetzt.getDate() < 10) ? "0" : "") + (jetzt.getDate());
var monat = ((jetzt.getMonth() +1) < 10 ? "0" : "") + (jetzt.getMonth() +1);
var jahr = jetzt.getFullYear();
var stunden = ( (jetzt.getHours() < 10) ? "0" : "") + (jetzt.getHours());
var minuten = ( (jetzt.getMinutes() < 10) ? "0" : "") + (jetzt.getMinutes());
var sekunden = ((jetzt.getSeconds() < 10) ? "0" : "") + (jetzt.getSeconds());
var wt = jetzt.getDay();
var wota = new Array("So","Mo","Di","Mi","Do","Fr","Sa");
// Put formatted time in var datum
var datum = wota[wt] +", " + tag + "." + monat + "." + jahr;
// send time
socket.emit("time", {
hours: jetzt.getHours(),
minutes: minuten,
seconds: sekunden,
date: datum
});
// prüfen, ob Weckzeit erreicht ist
var act_time = stunden + ":" + minuten;
//console.log(act_time + " vs. " + alarm_time + " Al: " + red_stat+ " Act: " + active);
var day_active = al_days[wt];
if(alarm_time == act_time && red_stat ===1 && active ===1 && day_active ==1){
if (grn_stat != 1){
grn_stat = 1;
set_green();
active =0;
};
}; //if alarm time
// Rest Active if no alarm-time
if(alarm_time != act_time){
active =1;
}//if no alarm time
};
// send status to ALL clients
function send_status_all(){
io.sockets.emit("status", {
red: red_stat ,
green: grn_stat,
al_time: alarm_time,
wd: al_days
});
};
// send status to requesting client
function send_status(){
socket.emit("status", {
red: red_stat ,
green: grn_stat,
al_time: alarm_time,
wd: al_days
});
};
// set red LED according to status and send status to all clients
function set_red(){
red.writeSync(red_stat);
send_status_all();
console.log("Red LED set to " + red_stat);
};
function puts(error, stdout, stderr){
sys.puts(stdout);
io.sockets.emit("radio", {stdout});
};
// set green LED and radio according to status and send status to all clients
function set_green(){
grn.writeSync(grn_stat);
if(grn_stat === 1){
exec("mpc play", puts);
amp.writeSync(1);
};
if(grn_stat === 0){
exec("mpc stop", puts);
amp.writeSync(0);
};
send_status_all();
console.log("Green LED set to " + grn_stat);
};
function set_volume(dir){
if(dir === "+"){exec("mpc volume +1", puts);};
if(dir === "-"){exec("mpc volume -1", puts);};
if(dir >=1 && dir <= 100 ){exec("mpc volume "+dir, puts);};
send_status_all();
console.log("Volume adjusted");
};
function skip(dir){
if(dir === "fwd"){exec("mpc next", puts);};
if(dir === "bck"){exec("mpc prev", puts);};
send_status_all();
console.log("Skipped");
};
}); //io.sockets