-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoormon.js
55 lines (43 loc) · 1.41 KB
/
Doormon.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
/*
This is an extremely simple API which allows a client to track the
latest state of a door switch. It offers two actions: getState and setState.
The action is specified as part of the query string using the GET method.
This should probably be rewritten as a proper REST api.
BUGS
* Presently, the door ID is recorded but otherwise unused.
* The expected state is always a door being opened. This could be expanded
to include duration, and door shutting.
* Presently we only handle state for a single door
*/
module.exports =
function(ctx, dude) {
switch(ctx.data.action) {
/* Query string fields
* The 'action' field here is setState
* Field 'state' is the latest state, expected to be "opened"
* Field 'id' is an unique ID referencing a specific door */
case 'setState':
console.log("Received door state " + ctx.data.state + " from door " + ctx.data.id);
var data = { time: Date.now(), door: ctx.data.id, state: ctx.data.state }
ctx.storage.set(JSON.stringify(data), {force: 1}, function(error) {
if(error) {
return dude(error);
} else {
dude(null, 'OK!');
}
});
break;
/* Query string fields
* The 'action' field here is getState */
case 'getState':
ctx.storage.get(function(error,data) {
if(error) {
return dude(error);
} else {
console.log("Returning record: " + data)
dude(null, data);
}
});
break;
}
}