All checks were successful
Build and push image for doorman / docker (push) Successful in 1m39s
94 lines
1.7 KiB
JavaScript
94 lines
1.7 KiB
JavaScript
const { DynamoDBClient, GetItemCommand, DeleteItemCommand, PutItemCommand } = require("@aws-sdk/client-dynamodb");
|
|
|
|
exports.createDDBClient = (context) => {
|
|
return new DynamoDBClient({
|
|
region: "us-east-1" ,
|
|
credentials: {
|
|
accessKeyId: context.AWS_ACCESS_KEY,
|
|
secretAccessKey: context.AWS_SECRET_ACCESS_KEY,
|
|
},
|
|
});
|
|
};
|
|
|
|
exports.getLockStatusCommand = (door) => {
|
|
return new GetItemCommand({
|
|
TableName: "doorman",
|
|
Key: {
|
|
"PK": {
|
|
S: door,
|
|
},
|
|
"SK": {
|
|
S: "lock",
|
|
}
|
|
},
|
|
});
|
|
};
|
|
|
|
exports.getDoorAliasCommand = (buzzerNumber) => {
|
|
return new GetItemCommand({
|
|
TableName: "doorman",
|
|
Key: {
|
|
"PK": {
|
|
S: buzzerNumber,
|
|
},
|
|
"SK": {
|
|
S: "alias",
|
|
}
|
|
},
|
|
});
|
|
};
|
|
|
|
exports.getDoorConfigCommand = (door) => {
|
|
return new GetItemCommand({
|
|
TableName: "doorman",
|
|
Key: {
|
|
"PK": {
|
|
S: `door-${door}`,
|
|
},
|
|
"SK": {
|
|
S: "config",
|
|
},
|
|
},
|
|
});
|
|
};
|
|
|
|
exports.isLockOpen = (lock) => {
|
|
// ttl is a UTC ms time for how long it is unlocked
|
|
const ttl = lock.Item?.TTL?.N || 0;
|
|
return ttl > Date.now();
|
|
};
|
|
|
|
exports.clearLockStatusCommand = (lock) => {
|
|
return new DeleteItemCommand({
|
|
TableName: "doorman",
|
|
Key: {
|
|
"PK": {
|
|
S: lock.Item.PK.S,
|
|
},
|
|
"SK": {
|
|
S: "lock",
|
|
}
|
|
}
|
|
});
|
|
};
|
|
|
|
exports.setLockStatusCommand = (door, timeoutSeconds, fingerprintObj) => {
|
|
return new PutItemCommand({
|
|
TableName: "doorman",
|
|
Item: {
|
|
"PK": {
|
|
S: door,
|
|
},
|
|
"SK": {
|
|
S: "lock",
|
|
},
|
|
"TTL": {
|
|
N: `${Date.now() + timeoutSeconds * 1000}`,
|
|
},
|
|
"fingerprint": {
|
|
S: JSON.stringify(fingerprintObj),
|
|
}
|
|
}
|
|
});
|
|
};
|