-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsesame.js
290 lines (255 loc) · 8.22 KB
/
sesame.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
288
289
290
// INCLUDE DOT ENV TO OBSCURE SENSATIVE DATA
require('dotenv').config({path: '/home/pi/Code/sesame-hw/.env'});
// INCLUDE BLYNK AND ONOFF TO CONTROL THE PINS
var Blynk = require('blynk-library');
var Gpio = require('onoff').Gpio;
// INCLUDE CMD LINE INTERFACE
var cmd = require('node-cmd');
// INCLUDE EVENT WATCHER ON THE MOUSE AT EVENT0
var ie = require('input-event');
var input = new ie('/dev/input/event0');
var mouse = new ie.Mouse(input);
// INCLUDE LOGGER FOR DEBUG AND DATA GATHERING
const log = require('simple-node-logger').createSimpleFileLogger('/home/pi/Code/sesame-hw/sesame.log');
// INIT THE BLYNK CLIENT
var AUTH = process.env.AUTH;
var blynk = new Blynk.Blynk(AUTH, options = {
connector : new Blynk.TcpClient( options = { addr:process.env.IP, port:8442 } )
});
// SET TO TRUE TO ENABLE DOOR ACTIVATION
var ARMED = false;
//***********************************
// INIT A FEW PINS.
//***********************************
// VIRTUAL 0: TIED TO THE "LOCK" BUTTON ON THE APP
// VIRTUAL 1: TIED TO THE "ACTIVATOR" BUTTON ON THE APP
// VIRTUAL 10: TIED TO THE CAMERA MODE SELETION ON THE APP
// VIRTUAL 11: TIED TO THE "TAKE PICTURE" BUTTON ON THE APP
// LEDLOCK: ACTUAL GPIO CONTROL OF THE LED PIN ON THE HARDWARE
// GARAGEMOTOR: ACTUAL GPIO CONTROL OF THE 4066 IC THAT SHORTS THE GARAGE OPENER
//***********************************/
var v0 = new blynk.VirtualPin(0);
var v1 = new blynk.VirtualPin(1);
var v10 = new blynk.VirtualPin(10);
var v11 = new blynk.VirtualPin(11);
var v13 = new blynk.VirtualPin(13);
var v12 = new blynk.VirtualPin(12);
var v20 = new blynk.VirtualPin(20);
var v21 = new blynk.VirtualPin(21);
var ledLock = new Gpio(18, 'out');
var garageMotor = new Gpio(17, 'out');
var locked = true;
var blinkProgress = 0;
const blinkCount = 58;
var date = new Date();
var garageState = {
CLOSED: 0,
OPENED: 1,
OPENING: 2,
CLOSING: 3,
};
/**
* BUILD THE GARAGE OBJECT
*/
var Garage = function(){
var self = this;
this.status = garageState.CLOSED;
this.inMotion = false;
this.position = 0;
this.ceiling = 0;
this.lastMovement = 0;
this.positionWatcher = function(lastMovement, timeout) {
var timeout = timeout || 500;
if(this.lastMovement == lastMovement) {
v12.write(this.position);
}
setTimeout(function(){
self.positionWatcher(this.lastMovement, timeout);
}, timeout);
};
this.activateIndicators = function() {
locked = true;
ledLock.writeSync(0);
v0.write(1);
v20.write(0);
v21.write(0);
this.inMotion = true;
if(this.status == garageState.CLOSED) {
this.status = garageState.OPENING;
} else {
this.status = garageState.CLOSING;
}
v13.write(centerText(garage.statusToString()));
// THIS SHOULD BE ADDED TO THE VIRTUAL AND PHYSICAL PINS
blink(blinkCount);
};
this.activateOpener = function(holdTime) {
var holdTime = holdTime || 200;
// this.status += 2;
if (ARMED === true) {
garageMotor.writeSync(1);
setTimeout(function(){
garageMotor.writeSync(0);
}, holdTime);
}
}
this.statusToString = function(state) {
var state = state || this.status;
var keyNames = Object.keys(garageState);
for (var i in keyNames) {
if (state == i) {
return keyNames[i];
}
}
}
};
var garage = new Garage();
// THIS OPENS AND CLOSE THE GARAGE. BE CAREFUL!
v1.on('write', function(param) {
if(locked == false) {
if (param[0] == 1) {
garage.activateIndicators();
garage.activateOpener(500);
}
}
});
// THIS IS THE LOCK FOR THE GARAGE BUTTON
v0.on('write', function(param) {
if (garage.inMotion == true) {
// blynk.virtualWrite(0, 1);
v0.write(1);
return;
}
if(param[0] == 0) {
locked = false;
ledLock.writeSync(1);
}
if(param[0] == 1) {
locked = true;
ledLock.writeSync(0);
}
});
// Control Camera Exposure
v10.on('write', function(param){
if (garage.inMotion == true) {
return;
}
var index = param[0];
switch (index) {
case "1":
log.info("Exposure: Auto");
cmd.run('echo "em auto" > /var/www/html/camera/FIFO');
break;
case "2":
log.info("Exposure: Night");
cmd.run('echo "em night" > /var/www/html/camera/FIFO');
break;
case "3":
log.info("Exposure: Spotlight");
cmd.run('echo "em spotlight" > /var/www/html/camera/FIFO');
break;
default:
log.info("What happened?");
log.info(index);
break;
}
});
// SNAP A PIC
v11.on('write', function(param){
if (param[0] == "1") {
cmd.run('echo "im 1" > /var/www/html/camera/FIFO');
}
});
function centerText(text, targetLength) {
var targetLength = targetLength || 16;
var difference = targetLength - text.length;
var returnText = "";
if (difference <= 0) {
return text;
}
for (i=0; i<(difference/2); i++) {
returnText += " ";
}
returnText += text;
for (i=0; i<(difference/2); i++) {
returnText += " ";
}
return returnText;
}
function blink(count) {
if (garage.status == garageState.CLOSING) {
blinkProgress = Math.round(100*(count/blinkCount));
} else if (garage.status == garageState.OPENING) {
blinkProgress = Math.round(100-(100*(count/blinkCount)));
}
v13.write(centerText(garage.statusToString()));
// v13.write("0123456789ABCDEF012345");
v12.write(blinkProgress);
if (count <= 0) {
ledLock.write(0);
garage.inMotion = false;
log.info("Blinker stopped.");
if(garage.status == garageState.OPENING) {
garage.ceiling = Math.max(garage.ceiling, garage.position);
// mouse.ceiling = mouse.y;
log.info("Garage is open with a ceiling of: " + garage.ceiling);
v20.write(255);
v21.write(0);
garage.status = garageState.OPENED;
}
if(garage.status == garageState.CLOSING) {
garage.position = 0;
// mouse.y = 0;
log.info("Garage has closed.");
v20.write(0);
v21.write(255);
garage.status = garageState.CLOSED;
}
v13.write(centerText(garage.statusToString()));
// blynk.virtualWrite(13, garageStatus);
return;// garageMotor.unexport();
}
ledLock.read(function (err, value) { // Asynchronous read.
if (err) {
throw err;
}
ledLock.write(value ^ 1, function (err) { // Asynchronous write.
if (err) {
throw err;
}
});
});
setTimeout(function () {
blink(count - 1);
}, 200);
}
mouse.on('move', function(data) {
if (data.code == 1) {
// WATCH FOR LOCAL GARAGE ACTIVATION
if (garage.inMotion == false) {
blynk.notify("Somebody activated the garage!");
log.info("Somebody activated the garage!");
garage.activateIndicators();
if ((data.value + garage.position) < garage.position) {
// LESS THAN MEANS THE GARAGE IS CLOSING
if(garage.status != garageState.CLOSING) {
// THIS INDICATES THAT THE GARAGE IS ACTUALLY CLOSING,
// THOUGH THE SYSTEM THINKS IT IS OPENING
garage.status = garageState.CLOSING;
}
} else {
// GREATER THAN MEANS THE GARAGE IS OPENING
if(garage.status != garageState.OPENING) {
// THIS INDICATES THAT THE GARAGE IS ACTUALLY OPENING,
// THOUGH THE SYSTEM THINKS IT IS CLOSING
garage.status = garageState.CLOSING;
}
}
}
garage.position += data.value;
log.info("garage position = " + garage.position);
garage.lastMovement = date.getMilliseconds();
}
// log.info("mouse y=" + mouse.y);
// cmd.run('echo "X:' + mouse.x + ' :: Y: ' + mouse.y + '" >> sesame.log');
});