-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
249 lines (226 loc) · 8.94 KB
/
index.js
File metadata and controls
249 lines (226 loc) · 8.94 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
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
'user strict';
const config = require('./config.js');
const deskaction = require('./desk/action');
const desktrigger = require('./desk/trigger');
const firebase = require('firebase');
const SerialPort = require('serialport');
// const rpio = require('rpio');
const colors = require('colors');
/* Get terminal arguments
* --env : 'dev', 'prod'
* --action : 'up', 'down' // Only used when --env = dev
* --val : {numeric value, discrete value}
*/
const grab = flag => {
var index = process.argv.indexOf(flag);
return (index === -1) ? null : process.argv[index+1];
}
/* Convert hex value to decimal */
const hexToDec = hexVal => { return parseInt(hexVal, 16); }
const env = grab('--env'); // Environment: 'dev' 'prod'
/*
* GPIO Configuration
* GPIO 14 TX // PIN 08 Transmit Serial Message
* GPIO 14 TX // PIN 08 Transmit Serial Message
* GPIO 15 RX // PIN 10 Read Serial Message
* GPIO 18 Read // PIN 12 Read Status by polling - There are not Interrupts in Raspberry PI
*/
const pirPin = 12;
var pirBuffer = Buffer.alloc(10);
//rpio.open(pirPin, rpio.INPUT); // Read-Only Input
//rpio.poll(pirPin, function() {
// console.log('TEST:::');
//});
/* RXTX Serial Communication Configuration */
const ByteLength = SerialPort.parsers.ByteLength;
const port = new SerialPort('/dev/ttyS0', { baudRate: 57600 });
const parser = port.pipe(new ByteLength({length: 8}));
port.open(function(err) {
if(err) {
return console.log('PORT: Error when opening: '.red, err.message);
} else {
console.log('PORT: Has been openend'.magenta);
port.flush(null);
}
});
port.on('open', function() { console.log('PORT: "Open" Event'.bold.white); });
port.on('error', function(err) {
console.log('PORT: ERROR: '.red, err.message);
});
port.on('close', function(err) {
if(err) {
console.log('PORT: ERROR Closed Port: '.red, err.message);
}
console.log('PORT: "Close" Event'.bold.white);
});
var deskHeight = [];
deskHeight[0] = 0;
deskHeight[1] = 0;
previousHeight = -1;
var hexBuf = new Buffer(13);
var pointer = 0;
var heartbit = 1;
var desk = {
action: {
type: 'NAN',
value: -1,
status: 'NAN',
command: 'NAN'
},
currentHeight: -1,
state: 'OFF'
};
const executeAction = (command) => {
port.write(desktrigger[command], function(err, results) {
if(err) {
return console.log('PORT: ERROR: On Write: ', err.message);
}
console.log('LOG: Action Command: '.cyan + command.white + ' Desk'.white);
});
};
var raiseFun;
var lowerFun;
const wait = 10000; //10 secs
var waitUntil = new Date(new Date().getTime() + wait);
var currTime = new Date();
port.on('data', function(data) {
for(var i = 0; i < data.length; i++) {
if ( pointer == 0) {
if ( data[i] == 0xbd) {
hexBuf[pointer] = data[i];
pointer = 1;
}
} else if ((pointer > 0) && (pointer < 13)) {
hexBuf[pointer] = data[i];
pointer ++;
} else {
if (pointer == 13) {
console.log(`< ${hexBuf[0]}, ${hexBuf[1]}, ${hexBuf[2]}, ${hexBuf[3]}` +
`, ${hexBuf[4]}, ${hexBuf[5]}, ${hexBuf[6]}, ${hexBuf[7]}` +
`, ${hexBuf[8]}, ${hexBuf[9]}, ${hexBuf[10]}, ${hexBuf[11]}, ${hexBuf[12]} >`);
deskHeight[0] = hexBuf[11];
deskHeight[1] = hexBuf[12]
pointer = 0;
//heartbit = heartbit^1; // flip bit
//deskRef.update({heartbit: heartbit, currentHeight: deskHeight[0]});
//desk.currentHeight = deskHeight[0];
if(deskHeight[0] != 0 && deskHeight[0] != previousHeight) {
desk.currentHeight = deskHeight[0];
deskRef.update({currentHeight: deskHeight[0]});
previousHeight = deskHeight[0]
}
prevTime = new Date(new Date().getTime() + wait);
if(desk.state == 'ON') {
if ( (desk.action.command == deskaction.command.RAISE && (desk.action.value - desk.currentHeight) <= 0 )
|| (desk.action.command == deskaction.command.LOWER && (desk.action.value - desk.currentHeight) >= 0 )) {
desk.state = 'OFF';
deskRef.update({ state: 'OFF' });
desk.action.status = deskaction.status.COMPLETED;
deskRef.child('action').update({ status: deskaction.status.COMPLETED });
port.flush(null);
port.close(function(err) {
if(err) {
console.log('PORT: ERROR: When closing port after action: '.red, err.message)
}
});
console.log('LOG: Action: Completed'.bold.green);
}
}
}
}
}
currTime = new Date();
if(currTime > waitUntil) {
//idleDesk();
executeAction(deskaction.command.IDLE);
port.flush(function(err) {
if(err) {
console.log('PORT: ERROR: When flushing: '.red, err.message)
}
});
deskRef.update({heartbit: currTime});
waitUntil = new Date(new Date().getTime() + wait);
}
if(desk.action.status === deskaction.status.EXECUTING) {
if(desk.state == 'OFF') {
desk.state = 'ON';
deskRef.update({state: 'ON'});
}
console.log(`LOG: Status: ${desk.action.status}; `
+`Command: ${desk.action.command}; `
+`Value: ${desk.action.value}; CurrHeight: ${desk.currentHeight}`)
switch(desk.action.type) {
case deskaction.type.NUMERIC:
switch(desk.action.command) {
case deskaction.command.RAISE:
executeAction(deskaction.command.RAISE);
executeAction(deskaction.command.RAISE);
executeAction(deskaction.command.RAISE);
break;
case deskaction.command.LOWER:
executeAction(deskaction.command.LOWER);
executeAction(deskaction.command.LOWER);
break;
default:
console.log('EXECUTING DEFAULT INNER');
break;
}
break;
case deskaction.type.QUALITATIVE:
break;
default:
console.log('EXECUTING DEFAULT');
//break;
}
} else { // We are done, no need to raise or lower the desk
//desk.action.status = deskaction.status.COMPLETED;
}
currTime = new Date();
});
/* Firebase Configuration */
firebase.initializeApp(config.firebase.default);
const database = firebase.database();
const smartdeskStateRef = database.ref('smartdeskState');
const deskRef = database.ref('desk');
const officeRef = database.ref('office');
/* Listen for changes that take place when USER TALKS TO VOICE INTERFACE */
deskRef.child('action').on('value', function(snapshot) {
if(snapshot.val()['status'] === deskaction.status.EXECUTE) { // We receive a command from voice interface
port.open();
desk.action.type = snapshot.val()['type']; // LOWER, RAISE
console.log('FIREBASE: ', snapshot.val());
if(desk.action.type == deskaction.type.NUMERIC) {
desk.action.command = snapshot.val()['command'];
desk.action.value = snapshot.val()['value']; // A quantitative value
desk.action.status = deskaction.status.EXECUTING;
} else { // QUALITATIVE
desk.action.command = snapshot.val()['command'];
desk.action.value = snapshot.val()['value'];
var deltaHeight = 0;
switch(desk.action.value) {
case deskaction.command.value.SMALL:
deltaHeight = 5;
break
case deskaction.command.value.LARGE:
deltaHeight = 15;
break;
case deskaction.command.value.TOP:
break;
case deskaction.command.value.MIDDLE:
break;
case deskaction.command.value.BOTTOM:
break;
default:
break;
}
desk.action.value = desk.action.value + deltaHeight;
desk.action.status = deskaction.status.EXECUTING;
}
console.log(`desk->action->status: ${desk.action.status} ${desk.action.command} `
+ `of type: ${desk.action.type} ${desk.action.value}`);
} else if(snapshot.val()['status'] === deskaction.status.COMPLETED) {
// console.log('desk->action->status: DONE')
} else { // else 'DONE'
//
}
});