Some checks failed
Build and push image for doorman / docker (push) Failing after 22s
62 lines
1.5 KiB
JavaScript
62 lines
1.5 KiB
JavaScript
/**
|
|
* Try to unlock the door with auth mode
|
|
*/
|
|
|
|
const redis = require('redis');
|
|
|
|
function doorStatusKey(id) {
|
|
return concatKeys("doors", id, 'open');
|
|
}
|
|
|
|
function concatKeys(...keys) {
|
|
return keys.join(':');
|
|
}
|
|
|
|
exports.handler = function(context, event, callback) {
|
|
const response = new Twilio.Response();
|
|
|
|
let door = event.door;
|
|
let pin = event.key;
|
|
|
|
if (!door || !pin) {
|
|
response.setStatusCode(400);
|
|
return callback(null, response);
|
|
}
|
|
|
|
if (!context['FIXED_PIN_' + door.toUpperCase()]) {
|
|
response.setStatusCode(404);
|
|
return callback(null, response);
|
|
}
|
|
|
|
let correctPin = context['FIXED_PIN_' + door.toUpperCase()];
|
|
|
|
if (correctPin !== pin) {
|
|
response.setStatusCode(401);
|
|
return callback(null, response);
|
|
}
|
|
|
|
let client = redis.createClient({ url: context.REDIS_CONNECT_URL });
|
|
client.connect()
|
|
.then(async () => {
|
|
const statusKey = doorStatusKey(door);
|
|
const fingerprint = { method: "PIN" };
|
|
const timeout = context['OPEN_TIMEOUT_' + door.toUpperCase()] || 60;
|
|
|
|
await client.set(statusKey, JSON.stringify(fingerprint));
|
|
await client.expire(statusKey, timeout);
|
|
await client.quit();
|
|
|
|
response
|
|
.setStatusCode(200)
|
|
.appendHeader('Content-Type', 'application/json')
|
|
.setBody({ msg: `Opened the door "${door}" for ${timeout}s` });
|
|
|
|
return callback(null, response);
|
|
}).catch((e) => {
|
|
console.log(e);
|
|
response
|
|
.setStatusCode(500)
|
|
.appendHeader('Content-Type', 'application/json')
|
|
.setBody({ err: e });
|
|
});
|
|
}; |