Martin Dimitrov 9f5a9be29e
Some checks failed
Build and push image for doorman / docker (push) Failing after 24s
exit redis client after exit
2024-05-03 16:08:53 -07:00

56 lines
1.4 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);
});
};