-
Notifications
You must be signed in to change notification settings - Fork 26
/
august-server.js
170 lines (150 loc) · 4.88 KB
/
august-server.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
var Alexa = require('./alexaws');
var config = require('./config');
var p = require('child_process');
var sys = require('sys');
var https = require('https');
var http = require('http');
var url = require('url');
var fs = require('fs');
var augustBridgeCallbacks = [];
var augustBridge;
var augustBridgeMessage = function(m) {
if (augustBridgeCallbacks[m.callbackId])
augustBridgeCallbacks[m.callbackId](m.res);
delete augustBridgeCallbacks[m.callbackId];
}
function execCommand(command, callback, arg) {
if (!augustBridge || !augustBridge.connected) {
augustBridge = p.fork(__dirname+'/augustctl/augustbridge.js');
augustBridge.on('message', augustBridgeMessage);
}
var cbackId = augustBridgeCallbacks.push(callback)-1;
augustBridge.send({cmd:command, callbackId:cbackId, arg:arg});
return true;
}
var relockTime = 0;
var cachedLockStatus = "unknown"
var cachedEverlockTime = -1
function httpHandler( request, response ){
var get = url.parse(request.url, true);
if (get.pathname == config.alexaSkillGatewayURL) {
var alexa = new Alexa(request, response);
alexa.on('launch', function () {
alexa.setOutputSpeech("What would you like to ask to your August lock?");
alexa.setShouldEndSession(false);
});
alexa.on('intent', function (intent, args) {
if (intent == "LockDoor") {
execCommand('lock');
alexa.setOutputSpeech("Ok.");
}
else if (intent == "UnlockDoor") {
execCommand('unlock');
alexa.setOutputSpeech("Ok.");
}
else if (intent == "UnlockDoorFor") {
execCommand('everlockOffUnlock');
relockTime = new Date().getTime() + parseInt(args.Duration)*60000;
cachedLockStatus = "unlocked"
cachedEverlockTime = 0
alexa.setOutputSpeech("Ok. I will lock the door in "+args.Duration+" minutes");
}
else if (intent == "GetStatus") {
alexa.autoSend = false;
execCommand('getAllStatus', function(status){
var res = JSON.parse(status);
alexa.setOutputSpeech(res.lock=='locked'?"The door is locked.":"The door is unlocked.");
alexa.send();
});
}
else {
alexa.setOutputSpeech("What would you like to do?");
alexa.setShouldEndSession(false);
}
});
return;
}
response.writeHead( 200, {"content-type": "text/plain", 'Transfer-Encoding':'chunked'} );
if (get.pathname == config.baseURL+'/relocktime') {
var remainingTime = 0;
if (relockTime)
remainingTime = Math.max(0, Math.ceil((relockTime - new Date().getTime())/1000));
response.end('{"remaining":'+remainingTime+'}');
}
else
if (get.pathname == config.baseURL+'/cached') {
var remainingTime = 0;
if (relockTime)
remainingTime = Math.max(0, Math.ceil((relockTime - new Date().getTime())/1000));
response.end('{"remaining":'+remainingTime+', "lock":"'+cachedLockStatus+'", "everlocktime":'+cachedEverlockTime+'}');
}
else
if (get.pathname == config.baseURL+'/unlock') {
execCommand('unlock');
if (cachedEverlockTime<=0) cachedLockStatus = "unlocked"
response.end('{"system":"unlocking"}');
}
else
if (get.pathname == config.baseURL+'/lock') {
execCommand('lock');
if (cachedEverlockTime<=0) cachedLockStatus = "locked"
response.end('{"system":"locking"}');
}
else
if (get.pathname == config.baseURL+'/neverlock') {
execCommand('everlockOffUnlock');
if (get.query.relock)
relockTime = new Date().getTime() + parseInt(get.query.relock)*1000;
else
relockTime = 0;
cachedLockStatus = "unlocked"
cachedEverlockTime = 0
response.end('{"system":"neverlocking"}');
}
else
if (get.pathname == config.baseURL+'/everlock') {
execCommand('everlockOnLock', undefined, config.autolockTime);
relockTime = 0;
cachedLockStatus = "locked"
cachedEverlockTime = config.autolockTime
response.end('{"system":"everlocking"}');
}
else
if (get.pathname == config.baseURL+'/status') {
execCommand('getAllStatus', function(status){
var res = JSON.parse(status);
cachedLockStatus = res.lock;
cachedEverlockTime = res.everlocktime;
response.end(status);
});
}
else {
response.writeHead(404, {"Content-Type": "text/plain"});
response.end('not found');
}
}
if (config.httpsServerPort) {
var options = {
key: fs.readFileSync(config.sslKey),
cert: fs.readFileSync(config.sslCert)
};
var serverssl = https.createServer(options, httpHandler)
serverssl.listen( config.httpsServerPort );
}
if (config.httpServerPort) {
var server = http.createServer(httpHandler);
server.listen( config.httpServerPort );
}
setInterval(function(){
if (relockTime && relockTime < new Date().getTime()) {
execCommand('everlockOnLock', undefined, config.autolockTime);
cachedLockStatus = "locked";
cachedEverlockTime = config.autolockTime;
relockTime = 0;
}
}, 1000);
execCommand('getAllStatus', function(status){
var res = JSON.parse(status);
cachedLockStatus = res.lock;
cachedEverlockTime = res.everlocktime;
});