-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
executable file
·1256 lines (1241 loc) · 45.9 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** setup node web server */
// require needed modules
var api = require('./routes/api'),
async = require('async'), //allow better formatting for asynchronous calls - https://github.com/caolan/async
bodyParser = require('body-parser'),
datastore = require('nedb'),
express = require('express'),
fs = require('fs'),
gpio = require('rpi-gpio'), // allow use of gpio - https://www.npmjs.com/package/rpi-gpio
http = require('http'),
log = require('npmlog'),
moment = require('moment-timezone'),
os = require('os'),
path = require('path'),
piblaster = require('pi-blaster.js'),
routes = require('./routes'),
serialport = require('serialport'),
url = require('url');
// get and setup ip and port
var ifaces, //set later in case ip is not yet setup
applicationPath = '/home/pi/roboFeeder',
ip, // set later
port = process.argv[3] || 8080;
var app = module.exports = express();
// sets port to default(8080) unless otherwise specified via script init argument
app.set('port', port);
// localize serialport object constructor
var SerialPort = serialport.SerialPort;
// object for each part of application
var Database = {
datastore: new datastore({ filename: applicationPath + '/db/roboFeeder.db', autoload: true }),
init: function(){
// do nothing currently
Log.log.info('Database', 'Database initialized', false);
}
};
var Rfid = {
// allowedTags uses strings to preserve leading zeros
allowedTags: [],
lastTrigger: null,
init: function(){
//read allowed tags from file
Rfid.getAllowedTags();
RoboFeeder.status.rfid = true;
Log.log.info('Rfid', 'RFID initialized', false);
},
setLastTrigger: function(){
var date = new Date();
var unix_secs = date.getTime();
Rfid.lastTrigger = unix_secs;
Log.log.info('Rfid', 'Rfid.lastTrigger set: ' + unix_secs, false);
},
getAllowedTags: function(){
async.forever(
function(next) {
// next is suitable for passing to things that need a callback(err [, whatever]);
// it will result in this function being called again.
Database.datastore.find(
{
type: 'tag',
allowed: true
},
function (err, docs) {
Rfid.allowedTags = [];
if(typeof docs[0] != "undefined"){
for(var i=0; i < docs.length; i++){
var tag = {
tag: docs[i]['tag'],
name: docs[i]['name']
};
Rfid.allowedTags.push(tag);
}
Log.log.info('Rfid', 'Allowed tags loaded', false);
}
else{
// if no allowed tags loaded
Log.log.info('Rfid', 'No allowed tags loaded', false);
}
});
},
function(err) {
// if next is called with a value in its first parameter, it will appear
// in here as 'err', and execution will stop.
}
);
},
saveAllowedTag: function(tagObj){
var tag = tagObj.tag;
var filterObj = Rfid.filterAllowedTags(tagObj.tag);
if(typeof filterObj.tagObj == "undefined"){
var allowedTagDoc = {
type: 'tag',
allowed: true,
tag: tagObj.tag,
name: tagObj.name
};
Rfid.allowedTags.push({tag: tagObj.tag, name: tagObj.name});
Database.datastore.update(
{tag: tagObj.tag},
allowedTagDoc,
{upsert: true},
function (err, newDoc) { // Callback is optional
Log.log.info('Rfid', 'The new allowed tag ' + tag + ' was saved.');
}
);
}
else{
Log.log.warn('Rfid', 'The tag "' + tag + '" was already in the allowed tag list.');
}
},
removeAllowedTag: function(tag){
var filterObj = Rfid.filterAllowedTags(tag);
if (typeof filterObj.tagObj != "undefined") {
Rfid.allowedTags.splice(filterObj.tagIndex, 1);
Database.datastore.remove(
{ tag: tag },
{},
function (err, numRemoved) {
Log.log.info('Rfid', 'The allowed tag ' + tag + ' was removed.');
}
);
}
else{
Log.log.warn('Rfid', 'The tag ' + tag + ' was not in the allowed tags list to be removed.');
}
},
loadTag: function(tag){
var filterObj = Rfid.filterAllowedTags(tag);
return filterObj.tagObj;
},
filterAllowedTags: function(tag){
var tagIndex = null;
var tagObj = Rfid.allowedTags.filter(function ( obj, index ) {
if(obj.tag === tag){
tagIndex = index;
}
return obj.tag === tag;
})[0];
return {tagIndex: tagIndex, tagObj: tagObj};
}
};
var Motor = {
reversePin: 16,
forwardPin: 18,
enablePin: 22,
runTime: 2800,
waitTime: 5000,
running: false,
init: function(){
async.parallel([
function(callback){
gpio.setup(Motor.forwardPin, gpio.DIR_OUT, callback)
},
function(callback){
gpio.setup(Motor.reversePin, gpio.DIR_OUT, callback)
},
function(callback){
gpio.setup(Motor.enablePin, gpio.DIR_OUT, callback)
},
], function(err, results){
Log.log.info('Motor', 'Motor Pins setup', false);
Log.log.info('Motor', 'Running initial open/close cycle without enabling PIR monitoring', false);
RoboFeeder.cycle(false);
RoboFeeder.status.motor = true;
Log.log.info('Motor', 'Motor initialized', false);
});
},
on: function(){
//turns on the motor drive pin
//needs to be called with Motor.forward or Motor.reverse to actually run motor
if(!Motor.running){
Motor.running = true;
gpio.write(Motor.enablePin, true, function(err) {
if (err) throw err;
});
}
},
off: function(){
//turns all the way off all three pins involved
Motor.running = false;
gpio.write(Motor.enablePin, false, function(err) {
if (err) throw err;
Log.log.info('Motor', 'Motor.enablePin ' + Motor.enablePin + ' set LOW.', false);
});
gpio.write(Motor.forwardPin, false, function(err) {
if (err) throw err;
Log.log.info('Motor', 'Motor.forwardPin ' + Motor.forwardPin + ' set LOW.', false);
});
gpio.write(Motor.reversePin, false, function(err) {
if (err) throw err;
Log.log.info('Motor', 'Motor.reversePin ' + Motor.reversePin + ' set LOW.', false);
});
},
forward: function(){
Motor.on();
gpio.write(Motor.forwardPin, true, function(err) {
if (err) throw err;
});
gpio.write(Motor.reversePin, false, function(err) {
if (err) throw err;
});
},
reverse: function(){
Motor.on();
gpio.write(Motor.reversePin, true, function(err) {
if (err) throw err;
});
gpio.write(Motor.forwardPin, false, function(err) {
if (err) throw err;
});
}
};
var Gpio = {
init: function(){
//bind change of GPIO pins
Gpio.bindChange();
Log.log.info('Gpio', 'GPIO initialized', false);
},
bindChange: function(){
gpio.on('change', function(channel, value){
//send monitoring data to server for monitor on site
Log.log.info('Gpio', 'Channel ' + channel + ' value is now ' + value, false);
});
},
closePins: function(){
gpio.destroy(function() {
Log.log.info('Gpio', '--- All pins un-exported, GPIO closed ---', false);
return process.exit(0);
});
}
};
var Toolbox = {
datetime: {
dt: null,
now: function(){
return new Date();
},
newDT: function(){
Toolbox.datetime.dt = Toolbox.datetime.now();
},
convert: function(d){ // Source: http://stackoverflow.com/questions/497790
// Converts the date in d to a date-object. The input can be:
// a date object: returned without modification
// an array : Interpreted as [year,month,day]. NOTE: month is 0-11.
// a number : Interpreted as number of milliseconds
// since 1 Jan 1970 (a timestamp)
// a string : Any format supported by the javascript engine, like
// "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc.
// an object : Interpreted as an object with year, month and date
// attributes. **NOTE** month is 0-11.
return (
d.constructor === Date ? d :
d.constructor === Array ? new Date(d[0],d[1],d[2]) :
d.constructor === Number ? new Date(d) :
d.constructor === String ? new Date(d) :
typeof d === "object" ? new Date(d.year,d.month,d.date) :
NaN
);
},
compare: function(a,b){ // Source: http://stackoverflow.com/questions/497790
// Compare two dates (could be of any type supported by the convert
// function above) and returns:
// -1 : if a < b
// 0 : if a = b
// 1 : if a > b
// NaN : if a or b is an illegal date
// NOTE: The code inside isFinite does an assignment (=).
return (
isFinite(a=this.convert(a).valueOf()) &&
isFinite(b=this.convert(b).valueOf()) ?
(a>b)-(a<b) :
NaN
);
},
inDateRange: function(d, start, end){ // Source: http://stackoverflow.com/questions/497790
// Checks if date in d is between dates in start and end.
// Returns a boolean or NaN:
// true : if d is between start and end (inclusive)
// false : if d is before start or after end
// NaN : if one or more of the dates is illegal.
// NOTE: The code inside isFinite does an assignment (=).
return (
isFinite(d=this.convert(d).valueOf()) &&
isFinite(start=this.convert(start).valueOf()) &&
isFinite(end=this.convert(end).valueOf()) ?
start <= d && d <= end :
NaN
);
},
inTimeRange: function(start, end, tz){
// Checks if current date's time (hours, minutes) is between dates in start and end time values
// ignores non-time portion of start and end datetimes.
// Returns a boolean
tz = tz || 'America/Los_Angeles';
// set all dates to the same timezone to allow calculations to not be insane
var d = moment(new Date()).tz(tz);
start = moment(this.convert(start)).tz(tz);
end = moment(this.convert(end)).tz(tz);
var d_hour = parseInt(d.format('H'));
var d_minute = parseInt(d.format('mm'));
var start_hour = parseInt(start.format('H'));
var start_minute = parseInt(start.format('mm'));
var end_hour = parseInt(end.format('H'));
var end_minute = parseInt(end.format('mm'));
if( (start_hour < d_hour) && (d_hour < end_hour) ){
return true;
}
else if(start_hour == d_hour){
if(start_minute <= d_minute){
return true;
}
}
else if(end_hour == d_hour){
if(d_minute <= end_minute){
return true;
}
}
return false;
}
},
ips: {},
init: function(){
Toolbox.datetime.newDT();
Toolbox.getIPs();
Log.log.info('Toolbox', 'Toolbox initialized and datetime created', false);
},
zeroFill: function(number, width){
width -= number.toString().length;
if ( width > 0 ) {
return new Array( width + (/\./.test( number ) ? 2 : 1) ).join( '0' ) + number;
}
return number + ""; // always return a string
},
getIPs: function(){ // from http://stackoverflow.com/questions/3653065/get-local-ip-address-in-node-js
ifaces = os.networkInterfaces();
Object.keys(ifaces).forEach(function (ifname) {
var alias = 0;
ifaces[ifname].forEach(function (iface) {
if ('IPv4' !== iface.family) {
// skip over non-ipv4 addresses
return;
}
if (alias >= 1) {
// this single interface has multiple ipv4 addresses
Toolbox.ips[ifname] = {
alias: alias,
address: iface.address
};
} else {
// this interface has only one ipv4 address
Toolbox.ips[ifname] = {
address: iface.address
};
}
});
});
}
};
var Serial = {
sp: new SerialPort("/dev/ttyAMA0", {
baudrate: 9600,
parser: serialport.parsers.raw
}),
init: function(){
Serial.sp.on('open', function() {
Log.log.info('Serial', 'Serial connection open.', false);
Serial.sp.on('data', function(data) {
var encoded_int = Serial.receiveData(data);
RoboFeeder.codeResponse(encoded_int);
});
});
RoboFeeder.status.serial = true;
},
receiveData: function(data){
var buff = new Buffer(data, 'utf8');
var encoded_hex = buff.toString('hex');
var encoded_int = parseInt(encoded_hex, 16);
return encoded_int;
}
};
var Pir = {
// Passive InfraRed Sensor
enablePin: 11,
sensorPin: 12,
lastTrigger: '',
checkFrequency: 50,
init: function(){
async.parallel([
function(callback){
gpio.setup(Pir.enablePin, gpio.DIR_OUT, callback)
},
function(callback){
gpio.setup(Pir.sensorPin, gpio.DIR_IN, callback)
},
], function(err, results){
Pir.enable();
Pir.read();
Pir.disable();
RoboFeeder.status.pir = true;
Log.log.info('Pir', 'PIR pins setup and sensor tested', false);
});
},
enable: function(){
gpio.write(Pir.enablePin, true, function(err) {
if (err) throw err;
Log.log.info('Pir', 'Pir.enablePin ' + Pir.enablePin + ' set HIGH', false);
});
},
disable: function(){
gpio.write(Pir.enablePin, false, function(err) {
if (err) throw err;
Log.log.info('Pir', 'Pir.enablePin ' + Pir.enablePin + ' set LOW', false);
});
},
read: function(){
gpio.read(Pir.sensorPin, function(err, value) {
Log.log.info('Pir', 'The Pir sensor pin value is ' + value, false);
return value;
});
},
monitor: function(){
Pir.enable();
var ee = new process.EventEmitter(),
pinState;
ee.on('stateChange', function(previousValue, value){
Log.log.info('Pir', 'PIR sensor pin state changed from ' + previousValue + ' to ' + value, false);
Pir.setLastTrigger();
});
Pir.intervalTimer = setInterval(function(){
gpio.read(Pir.sensorPin, function(err, value) {
if(err){
ee.emit('error', err);
}
else{
if(pinState !== value){
var previousState = pinState;
pinState = value;
ee.emit('stateChange', previousState, value);
}
}
});
}, Pir.checkFrequency);
},
monitorEnd: function(){
Pir.disable();
clearInterval(Pir.intervalTimer);
},
setLastTrigger: function(){
var date = new Date();
var unix_secs = date.getTime();
Pir.lastTrigger = unix_secs;
Log.log.info('Pir', 'Pir.lastTrigger set: ' + unix_secs, false);
}
};
var Output = {
// Status indication LEDs, etc.
init: function(){
// TODO
Log.log.info('Output', 'Output init run', false);
}
};
var Log = {
log: log,
items: [],
init: function(){
// setup event listener to save our npmlog events to the db log
Log.log.on('log', function(stream){
//if extra args not set or !== false then save log item to db log
if(typeof stream.messageRaw[1] == "undefined" || (typeof stream.messageRaw[1] != "undefined" && stream.messageRaw[1] !== false)){
Log.saveItem(stream);
}
});
Log.load();
Log.log.info('Log', 'Log initialized and log items loaded', false);
},
saveItem: function(stream){
var date = new Date();
var unix_secs = date.getTime();
var logItem = {
type: 'log',
level: stream.level,
category: stream.prefix,
message: stream.messageRaw[0],
timestamp: unix_secs
};
Database.datastore.insert(logItem, function (err, newDoc) {
Log.log.info('Log', 'Log.saveItem success', false);
});
},
load: function(){
Database.datastore.find({type: 'log'}, function (err, docs) {
if(typeof docs[0] != "undefined"){
Log.items = [];
for (var i=0; i < docs.length; i++) {
logItem = {
message: docs[i]['message'],
level: docs[i]['level'],
category: docs[i]['category'],
timestamp: docs[i]['timestamp']
};
Log.items.push(logItem);
}
}
});
},
reset: function(){
Database.datastore.remove({ type: 'log' }, { multi: true }, function (err, numRemoved) {
// newDoc is the newly inserted document, including its _id
Log.log.info('Log', 'Log.reset - number of log items removed: ' + numRemoved, true);
Log.items = [];
return Log.items;
});
}
};
var Rules = {
/** rules object definition
* {
type: string 'rule',
name: unique string *user input*,
weight: int,
active: boolean,
rule: {
tag: int 8,
start: int timestamp,
end: int timestamp,
activate: int timestamp,
expire: int timestamp
}
}
*/
rules: [],
init: function(){
Rules.load();
Log.log.info('Rules', 'Rules initialized and rule items loaded', false);
},
load: function(){
Database.datastore.find({type: 'rule'}, function (err, docs) {
if(typeof docs[0] != "undefined"){
Rules.rules = docs;
}
});
},
get: function(){
return Rules.rules;
},
save: function(ruleObj){
Rules.rules.push(ruleObj);
Database.datastore.update(
{
type: 'rule',
name: ruleObj.name
},
ruleObj,
{upsert: true},
function (err, newDoc) { // Callback is optional
Log.log.info('Rules', 'Rules.save - rule ' + ruleObj.name + ' saved.', true);
}
);
},
edit: function(ruleObj){
var filterObj = Rules.filterRules(ruleObj, 'name');
var oldName = null;
if(typeof ruleObj.newName != "undefined"){
var oldName = ruleObj.name;
ruleObj.name = ruleObj.newName;
delete ruleObj.newName;
}
if(typeof filterObj.ruleIndex != "undefined"){
Rules.rules[filterObj.ruleIndex] = ruleObj;
Database.datastore.update(
{
type: 'rule',
name: (oldName === null ? ruleObj.name : oldName)
},
ruleObj,
{},
function (err, newDoc) { // Callback is optional
Log.log.info('Rules', 'Rules.edit - rule ' + ruleObj.name + ' saved.', true);
}
);
}
},
remove: function(rule){
var filterObj = Rules.filterRules(rule, 'name');
if (typeof filterObj.ruleObj != "undefined") {
Rules.rules.splice(filterObj.ruleIndex, 1);
Database.datastore.remove(
{
type: 'rule',
name: rule.name
},
{},
function (err, numRemoved) {
Log.log.info('Rules', 'The rule "' + rule.name + '" was removed.');
}
);
}
else{
Log.log.warn('Rules', 'The rule "' + rule.name + '" was not in the rules list to be removed.');
}
},
reset: function(){
Database.datastore.remove({ type: 'rule' }, { multi: true }, function (err, numRemoved) {
// newDoc is the newly inserted document, including its _id
Log.log.info('Rules', 'Rules.reset - number of rule items removed: ' + numRemoved, true);
Rules.rules = [];
return Rules.rules;
});
},
filterRules: function(rule, field){
var ruleIndex = null;
var ruleObj = Rules.rules.filter(function ( obj, index ) {
if(obj[field] === rule[field]){
ruleIndex = index;
}
return obj[field] === rule[field];
})[0];
return {ruleIndex: ruleIndex, ruleObj: ruleObj};
},
isWithinActiveDatePeriod: function(ruleObj){
return Toolbox.datetime.inDateRange(Toolbox.datetime.now(), ruleObj.rule.activate, ruleObj.rule.expire);
},
isWithinActiveTimePeriod: function(ruleObj){
return Toolbox.datetime.inTimeRange(ruleObj.rule.start, ruleObj.rule.end, Toolbox.datetime.browserTZ);
},
status: function(ruleObj){
return ruleObj.active;
},
codeRuleIndexes: function(codeIndex){
var ruleIndexes = [];
for(var i=0; i<Rules.rules.length; i++){
if(Rules.rules[i].rule.tag == Rfid.allowedTags[codeIndex].tag){
ruleIndexes.push(i);
}
}
return ruleIndexes;
},
isActive: function(ruleObj){
if(Rules.status( ruleObj )) {
if (Rules.isWithinActiveDatePeriod(ruleObj)) {
if (Rules.isWithinActiveTimePeriod(ruleObj)) {
return true;
}
}
}
return false;
}/*,
isAnyRuleActive: function(){
for(var i=0; i<Rules.rules.length; i++){
if(Rules.isActive(Rules.rules[i])){
return true;
}
}
return false;
}*/
};
var Servo = {
// for status indicator flag
pin: 22,
position: { // percent of pwm to enable, servos run on pwm pulse length to determine position
start: 0.07,
end: 0.20
},
stopTimeout: 0,
cycleTimeout: 0,
intervalTimer: 0,
checkFrequency: 5000,
init: function(){
Servo.test();
Servo.monitor();
RoboFeeder.status.flagManualMode = false;
},
move: function(pwm_percent){
piblaster.setPwm(Servo.pin, pwm_percent, Servo.stop);
},
lowerFlag: function(){
if(RoboFeeder.status.flag){
Log.log.info('Servo', 'Flag lowered.', false);
// set to start position
Servo.move(Servo.position.end);
RoboFeeder.status.flag = false;
}
},
raiseFlag: function(){
if(!RoboFeeder.status.flag){
Log.log.info('Servo', 'Flag raised.', false);
// set to end position
Servo.move(Servo.position.start);
RoboFeeder.status.flag = true;
}
},
stop: function(){
clearTimeout(Servo.stopTimeout);
// uses timeout to make sure servo is finished moving before stopping
Servo.stopTimeout = setTimeout(
function(){
piblaster.setPwm(Servo.pin, 0);
}, 1000);
},
cycle: function(timeout){
clearTimeout(Servo.cycleTimeout);
Servo.raiseFlag();
Servo.cycleTimeout = setTimeout( function(){
Servo.lowerFlag();
}, timeout);
},
test: function(){
Log.log.info('Servo', 'Servo initialization test running.', false);
if(!RoboFeeder.status.servo){
Servo.stop();
setTimeout( function(){
Servo.cycle(5000);
RoboFeeder.status.servo = true;
Log.log.info('Servo', 'Servo initialization test finished.', false);
}, 1200);
}
},
monitor: function(){
Servo.intervalTimer = setInterval(
function(){
if(!RoboFeeder.status.flagManualMode){
if(RoboFeeder.isAnyCodeAllowed()){
Log.log.info('Servo', 'RoboFeeder.isAnyCodeAllowed == true', false);
Servo.raiseFlag();
}
else{
Log.log.info('Servo', 'RoboFeeder.isAnyCodeAllowed == false', false);
Servo.lowerFlag();
}
}
}, Servo.checkFrequency
);
},
monitorEnd: function(){ // currently unused
clearInterval(Servo.intervalTimer);
},
setManualMode: function(manualMode){
RoboFeeder.status.flagManualMode = manualMode;
}
};
var RoboFeeder = {
//for higher level functions and variables
settings: {
// set from db, configurable from ui
//time in milliseconds, default to 10 seconds
pirThreshold: '', // pir threshold for closing
rfidThreshold: '' // rfid threshold for closing
},
defaultSettings: {
rfidThreshold : 10000,
pirThreshold : 10000
},
status: {
open: false,
pir: false,
rfid: false,
motor: false,
serial: false,
flag: false,
servo: false,
flagManualMode: false
},
intervalTimer: '',
openTimer: '',
checkFrequency: 100,
init: function(){
async.parallel([
function(){ Log.init(); },
function(){ Gpio.init(); }
]);
async.series({
database_init: function (callback) {
Database.init();
callback(null, true);
},
roboFeeder_loadSetings: function(callback){
RoboFeeder.loadSettings();
callback(null, true);
},
rfid_init: function(callback){
Rfid.init();
callback(null, true);
},
serial_init: function(callback){
Serial.init();
callback(null, true);
},
pir_init: function(callback){
Pir.init();
callback(null, true);
},
motor_init: function(callback){
Motor.init();
callback(null, true);
},
rules_init: function(callback){
Rules.init();
callback(null, true);
},
output_init: function(callback){
Output.init();
callback(null, true);
},
toolbox_init: function(callback){
Toolbox.init();
callback(null, true);
},
servo_init: function(callback){
Servo.init();
callback(null, true);
}
}, function(err, results){
if(!err){
WebServer.init();
Log.log.info('RoboFeeder', 'All startup processes initialized', true);
}
});
},
open: function(enable){
//boolean `enable` is for enabling the pir sensor and log of the event being saved to the db log
// `enable` is false during boot
if(enable !== false){
enable = true;
}
// make sure to kill any previous timer
RoboFeeder.openTimerEnd();
Log.log.info('RoboFeeder', 'Opening Tray', enable);
Motor.reverse();
RoboFeeder.status.open = true;
RoboFeeder.openTimer = setTimeout(
function(){ RoboFeeder.openCallback(enable); },
Motor.runTime
);
},
openCallback: function(enable){
Motor.off();
if(enable){
Pir.monitor();
RoboFeeder.monitor();
}
},
close: function(enable){
//boolean `enable` is for enabling the log of the event being saved to the db log
// `enable` is false during boot, defaults to true after
if(enable !== false){
enable = true;
}
// make sure to kill any previous timer
RoboFeeder.openTimerEnd();
Log.log.info('RoboFeeder', 'Closing Tray', enable);
Motor.forward();
RoboFeeder.status.open = false;
RoboFeeder.openTimer = setTimeout(
RoboFeeder.closeCallback,
Motor.runTime
);
},
closeCallback: function(){
Motor.off();
},
cycle: function(enable){
if(enable !== false){
enable = true;
}
RoboFeeder.open(enable);
setTimeout(
function(){RoboFeeder.close(enable)},
Motor.waitTime
);
},
loadSettings: function(){
Database.datastore.find({type: 'setting'}, function (err, docs) {
if(typeof docs[0] != "undefined"){
var count = 0;
for (var setting in RoboFeeder.settings) {
RoboFeeder.settings[setting] = docs[count]['value'];
count++;
}
}
else{
// if cannot get settings from db use default settings
RoboFeeder.settings = RoboFeeder.defaultSettings;
}
Log.log.info('RoboFeeder', 'Settings loaded', false);
});
},
saveSettings: function(req){
RoboFeeder.settings = req.body.roboFeederSettings;
for(var roboFeederSetting in RoboFeeder.settings){
Database.datastore.update(
{ name: roboFeederSetting },
{ $set: { value: RoboFeeder.settings[roboFeederSetting] } },
{},
function (err, numReplaced) {
Log.log.info('WebServer', 'Setting ' + roboFeederSetting + ' saved', false);
}
);
}
Log.log.info('WebServer', 'Settings saved');
},
monitor: function(){
var ee = new process.EventEmitter(),
rfid = false,
pir = false;
ee.on('stateChange', function(event_name){
Log.log.info('RoboFeeder', 'roboFeeder monitor event emitter triggered: ' + event_name, false);
});
RoboFeeder.intervalTimer = setInterval(function(){
var date = new Date();
var unix_secs = date.getTime();
if(unix_secs - RoboFeeder.settings.pirThreshold >= Pir.lastTrigger){
if(!pir){
ee.emit('stateChange', 'PIR: past interval');
pir = true;
}
}
else{
if(pir){
ee.emit('stateChange', 'PIR: reset interval');
pir = false;
}
}
if(unix_secs - RoboFeeder.settings.rfidThreshold >= Rfid.lastTrigger){
if(!rfid){
ee.emit('stateChange', 'RFID: past interval');
rfid = true;
}
}
else{
if(rfid){
ee.emit('stateChange', 'RFID: reset interval');
rfid = false;
}
}
if(pir && rfid){
Pir.monitorEnd();
RoboFeeder.monitorEnd();
RoboFeeder.close();
}
}, RoboFeeder.checkFrequency);
},
monitorEnd: function(){
clearInterval(RoboFeeder.intervalTimer);
},
openTimerEnd: function(){
clearInterval(RoboFeeder.openTimer);
},
checkCode: function(code){
// returns null if not found instead of zero, since 0 could be an index
// make sure to use codeIndex !== null when checking returned value to see if a code matched
if(typeof code === 'object'){
var zerofilled_code = Toolbox.zeroFill(code.tag, 8);
}
else{
var zerofilled_code = Toolbox.zeroFill(code, 8);
}
var codeIndex = null;
for(var i=0; i < Rfid.allowedTags.length; i++){
if(Rfid.allowedTags[i].tag == zerofilled_code){
codeIndex = i;
break;
}
}
return codeIndex;
},
codeResponse: function(code){
var codeIndex = RoboFeeder.checkCode(code);
// has allowed code
if(codeIndex !== null){
// check if has rule(s) for allowed code
var ruleIndexes = Rules.codeRuleIndexes(codeIndex);
if(ruleIndexes.length > 0){
for(var j=0; j<ruleIndexes.length; j++){
// check if rule is in valid period between activation and expiration
if(Rules.isActive( Rules.rules[ruleIndexes[j]] )){
// if matching rule is fully active
Log.log.info('RoboFeeder', 'RFID allowed tag rule "' + Rules.rules[ruleIndexes[j]].name + '" found and fully active: tag "' + Rfid.allowedTags[codeIndex].tag + '" authorized', true);
RoboFeeder.tagMatch(codeIndex);
return true;
}
else{
// matching rule(s) not fully active
Log.log.info('RoboFeeder', 'RFID allowed tag rule "' + Rules.rules[ruleIndexes[j]].name + '" found but NOT active: tag "' + Rfid.allowedTags[codeIndex].tag + '" NOT authorized', true);
}
}
}
else{