-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
97 lines (78 loc) · 2.48 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
//Initiliase varaibles for server use
const express = require('express');
const app = express();
const admin = require("firebase-admin");
var serviceAccount = require("./serviceAccountKey.json");
var http = require('http').Server(app);
var router = express.Router();
//initialise board
var five = require('johnny-five');
var board = new five.Board();
//Initialise database connection
admin.initializeApp({
databaseURL: "https://project1-1d4a2.firebaseio.com/",
credential: admin.credential.cert(serviceAccount)
});
//initialise database variables
var db = admin.database();
var alldata = db.ref("serverData");
var vca = db.ref("serverData").child("variables").child("VCA")
var cca = db.ref("serverData").child("variables").child("CCA")
//allow errors to be printed to console
alldata.on("value", function(snapshot) {
console.log(snapshot.val())
}, function(errorObject){
console.log("Failed" + errorObject.code)
});
//get board ready, and observing
board.on("ready", function(){
var pin = 7;
var motion = new five.Motion(pin);
// "calibrated" occurs once, at the beginning of a session,
motion.on("calibrated", function() {
console.log("calibrated");
});
//initialise start time
motion.on("motionstart", function() {
startDate = new Date();
startTime = startDate.getTime();
console.log("Motion has started at" + startTime)
});
//initialise end time
motion.on("motionend", function() {
endDate = new Date();
endTime = endDate.getTime();
console.log("Motion has ended at" + endTime);
//create variable to hold motion time
motionTime = endTime - startTime
//now check to see whether value was short or long motion, using milliseconds
if((motionTime > 5000)) {
console.log("A long motion has been detected.");
vca.transaction(function(VCA){
return(VCA || 0) + 1
});
}
else if((motionTime < 5000)&&(motionTime > 0)) {
console.log("A short motion has been detected.");
vca.transaction(function(VCA){
return(VCA || 0) - 1
});
};
});
});
router.use(function(req, res, next){
next()
});
router.get("/", function(req, res){
res.sendFile("index.html", {root: __dirname});
});
router.get("/error", function(req, res){
res.sendFile("error.html", {root: __dirname});
});
router.get("/voting", function(req, res){
res.sendFile("voting.html", {root: __dirname});
});
app.use("/", router)
http.listen(8080, function(){
console.log("Live at port 8080")
});