forked from marcass/node_door
-
Notifications
You must be signed in to change notification settings - Fork 0
/
garagedoor.js
126 lines (106 loc) · 3.4 KB
/
garagedoor.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
//check out https://www.npmjs.com/package/chip-gpio and https://www.npmjs.com/package/router
// import our modules
var http = require('http')
var finalhandler = require('finalhandler')
var Gpio = require('chip-gpio').Gpio;
var Router = require('router')
//store pin for toggling door - check if it needs to be pulled down (high) or pulled up (low) and declare here in case it
// drops each time function is run
var pin = new Gpio(5, 'low');
var open_reed = new Gpio(6, 'in');
var closed_reed = new Gpio(7, 'in');
var state;
//Set variables for strings
//State of door
var state;
//What are we doing with door?
var action;
//functions for operating door
//reset pin that actuates opener to high state after required time has elapsed
function resetOpener() {
pin.write(0)
}
//pull pin down for 100 milliseconds to kick door
function operateDoor() {
pin.write(1)
setTimeout(resetOpener, 1000)
}
//function for assessing door status
function getDoorStatus() {
if (open_reed.read() == 1 && closed_reed.read() == 0) {
state = "doorOpen"
}
else if (open_reed.read() == 0 && closed_reed.read() == 1) {
state = "doorClosed"
}
else {
state = "unknown"
}
}
// store our test messages to display (if needed)
//var open = "Open sesame"
//var close = "Bang crash!"
// initialize the router & server and add a final callback.
var router = Router()
var server = http.createServer(function onRequest(req, res) {
router(req, res, finalhandler(req, res))
})
// handle `GET` requests to `/status` (eg, if door open, closed or something else?)
router.get('/status', function (req, res) {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
getDoorStatus()
res.end(state + '\n')
})
//make another router with our options for open
var closing = Router()
//mount our new router to a route
router.use('/action/close', closing)
//handle `GET` requests to `/action/close` (eg, toggle door state by manipulating pin state)
closing.get('/', function (req, res) {
//call function to assess status and store value
getDoorStatus()
if (state == "doorOpen") {
//populate action varialbe for diags
action = "closing"
}
else if (state == "doorClosed") {
operateDoor()
//populate action varialbe for diags
action = "Door already closed, not closing"
}
else {
//Status must be unknown or somthing has fucked up
action ="check_door"
}
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
res.end(action + '\n')
})
//make another router with our options for opening
var opening = Router()
//mount our new router to a route
router.use('/action/open', opening)
//handle `GET` requests to `/action/close` (eg, toggle door state by manipulating pin state)
opening.get('/', function (req, res) {
//call function to assess status and store value
getDoorStatus()
if (state == "doorClosed") {
operateDoor()
//populate action varialbe for diags
action = "opening"
}
else if (state == "doorOpen") {
//populate action varialbe for diags
action = "Door already open, not opening"
}
else {
//Status must be unknown or somthing has fucked up
action ="check_door"
}
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
res.end(action + '\n')
})
// make our http server listen to connections
server.listen(8080)