forked from timwasson/standdesk-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
287 lines (234 loc) · 7.29 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
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
283
284
285
286
287
var express = require('express');
var usonic = require('mmm-usonic-fixed');
var app = express();
var gpio = require('gpio');
var path = require('path');
var fs = require('fs');
var bodyParser = require('body-parser');
// Establish variables for desk distance, status, configuration, and timer
var distance, sitStand, timer, appConfig, standDuration, sitDuration, activeHourStart, activeMinuteStart, activeHourStop, activeMinuteStop, startTime, stopTime;
var activeDays = [];
var pausedVal = false;
// Get desk config
function getConfig() {
appConfig = JSON.parse(fs.readFileSync('config.json', 'utf8'));
standDuration = parseInt(appConfig.standDuration);
sitDuration = parseInt(appConfig.sitDuration);
activeHourStart = parseInt(appConfig.activeHourStart);
activeMinuteStart = parseInt(appConfig.activeMinuteStart);
activeHourStop = parseInt(appConfig.activeHourStop);
activeMinuteStop = parseInt(appConfig.activeMinuteStop);
startTime = parseInt(activeHourStart + "" + activeMinuteStart);
stopTime = parseInt(activeHourStop + "" + activeMinuteStop);
//Darn special characters
activeDays = JSON.stringify(appConfig["activeDays[]"]);
console.log("grabbed configuration | " + standDuration + " | " + sitDuration + " | " + activeDays);
}
getConfig();
var SecondsTohhmmss = function(totalSeconds) {
var hours = Math.floor(totalSeconds / 3600);
var minutes = Math.floor((totalSeconds - (hours * 3600)) / 60);
var seconds = totalSeconds - (hours * 3600) - (minutes * 60);
// round seconds
seconds = Math.round(seconds * 100) / 100
var result = (hours < 10 ? "0" + hours : hours);
result += ":" + (minutes < 10 ? "0" + minutes : minutes);
result += ":" + (seconds < 10 ? "0" + seconds : seconds);
return result;
}
// Initialize the distance sensor
// createSensor (#A, #B, xxxx)
// A = Echo Pin
// B = Trigger Pin
// xxxx = Measurement Timeout
usonic.init(function (error) {
if (error) {
console.log(error);
} else {
var sensor = usonic.createSensor(21, 20, 1000);
distance = sensor();
setInterval(function() {
distance = sensor();
}, 500);
if(distance < 80) {
timer = sitDuration * 60;
} else {
timer = standDuration * 60;
}
}
});
// Function for outputting month
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
// Set up jade templating engine
app.set('view engine', 'jade');
app.set('views', 'views');
// Set up express and the bodyParser to save desk configuration
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({ extended: false }));
// Exporting GPIO pins
//var power may not be needed - no connection
// var power = gpio.export(2, {
// direction: 'in',
// interval: 200
// });
var up = gpio.export(6, {
direction: 'out',
interval: 200,
ready: function(){
}
});
var down = gpio.export(5, {
direction: 'out',
interval: 200,
ready: function(){
}
});
// Hardware button presses
// Unsure of use. Possibly for other button control inputs through Pi
// Still exports at this point, so this possibly should also include distance sensor pins.
// Pins are GPIO# pins, not board pin count. IE pin 4 is GPIO 4 which is physical 7
// Unsure - but believed to be incomming signals of button presses, IE from the actual desk switch, to send info through pi to relays instead of directly to controller.
var upButtonPress = gpio.export(22, {
direction: 'in',
interval: 200
});
var downButtonPress = gpio.export(17, {
direction: 'in',
interval: 200
});
// End pin export
setInterval(function(){
// Ensure sitting/standing is set properly by checking distance
if(distance < 78) {
sitStand = 'sit';
} else if (distance > 88) {
sitStand = 'stand';
} else {
sitStand = 'moving';
}
// Only run this on certain days and certain times
var today = new Date();
var minutesPrefix = today.getMinutes() < 10 ? '0' : '';
var rightNow = parseInt(today.getHours() + minutesPrefix + today.getMinutes())
console.log(today.getDay() + " | " + today.getHours() + " | " + activeDays.indexOf(today.getDay()) + " | " + startTime + " | " + stopTime + " | " + rightNow);
if(activeDays.indexOf(today.getDay()) != -1 && rightNow >= startTime && rightNow <= stopTime && pausedVal == false) {
timer--;
// Brute force
if (timer <= 0) { timer = 0; }
console.log("Time Remaining: " + SecondsTohhmmss(timer) + " | Distance: " + distance + " | Currently: " + sitStand + " | Paused: " + pausedVal);
if(sitStand == 'stand' && timer == 0) {
downPress();
timer = sitDuration * 60;
} else if(sitStand == 'sit' && timer == 0) {
upPress();
timer = standDuration * 60;
}
}
},
// 1 second
1000
);
// downPress and upPress listed below triggers the up and down relays to be triggered.
//The timeout number listed below is the movement time, to simulate the button being pressed.
function downPress() {
down.set(1);
up.set(0);
sitStand = 'moving';
setTimeout(function() {
down.set(0);
}, 6900);
}
function upPress() {
down.set(0);
up.set(1);
sitStand = 'moving';
setTimeout(function() {
up.set(0);
}, 6900);
}
downButtonPress.on("change", function() {
if(downButtonPress.value == 1) {
downPress();
}
});
upButtonPress.on("change", function() {
if(upButtonPress.value == 1) {
upPress();
}
})
app.get('/', function (req, res) {
res.render('index', {
title: 'Billy\'s StandDesk Manager',
message: 'Billy\'s StandDesk Settings',
sitDuration: sitDuration,
standDuration: standDuration,
activeDays: activeDays,
activeHourStart: activeHourStart,
activeMinuteStart: activeMinuteStart,
activeHourStop: activeHourStop,
activeMinuteStop: activeMinuteStop
});
});
app.get('/up', function (req, res) {
// Send back JSON
var foo = {sitStand:'stand'};
var jsonString = JSON.stringify(foo);
res.send(jsonString);
upPress();
});
app.get('/down', function (req, res) {
// Send back JSON
var foo = {sitStand:'sit'};
var jsonString = JSON.stringify(foo);
res.send(jsonString);
downPress();
});
app.get('/pause', function (req, res) {
// Send back JSON
var foo = {paused:'yes'};
var jsonString = JSON.stringify(foo);
pausedVal = true;
res.send(jsonString);
});
app.get('/unpause', function (req, res) {
// Send back JSON
var foo = {paused:'no'};
var jsonString = JSON.stringify(foo);
pausedVal = false;
res.send(jsonString);
});
app.get('/status', function (req, res) {
var foo = {status:sitStand, timeRemain:timer,distance:distance,paused:pausedVal};
var jsonString = JSON.stringify(foo);
res.send(jsonString);
});
app.post('/save',function(req,res){
var data=req.body.data;
console.log(JSON.stringify(req.body));
fs.writeFile("config.json", JSON.stringify(req.body), function(err) {
if(err) {
return console.log(err);
} else {
getConfig();
console.log("The file was saved!");
}
});
res.end("yes");
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});