From 625daaa056689a2a1400d36a2357f3bc7e8e6001 Mon Sep 17 00:00:00 2001 From: Ty Eggen Date: Sun, 29 Dec 2024 21:40:05 +0000 Subject: [PATCH] performance(server): optimized automatic unarming hooks --- server/lib/stores/devices/index.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/server/lib/stores/devices/index.js b/server/lib/stores/devices/index.js index e1cd54c..ce9acea 100644 --- a/server/lib/stores/devices/index.js +++ b/server/lib/stores/devices/index.js @@ -4,16 +4,14 @@ const debug = require('debug')('tacos:lib:stores:devices') const { config } = require('../../config/') const { prisma } = require('../../db/prisma') +const { coerceMilliseconds } = require('../../utils') const DeviceStore = function () { this.devices = prisma.devices if (this.devices == null) throw new Error('Missing devices') - setInterval(() => { - debug('Autodisarming') - this.automaticDisarm() - }, 5000) + this.automaticDisarm() } DeviceStore.prototype.automaticDisarm = async function () { @@ -118,10 +116,19 @@ DeviceStore.prototype.armDevice = async function (deviceId) { where: { id: deviceId }, data: { armed: 1, - activation_expiry: Date.now() + Number(config.activation_timeout) + activation_expiry: + Date.now() + + coerceMilliseconds(Number(config.activation_timeout)) } }) + setTimeout( + () => { + void this.unarmDevice(deviceId) + }, + coerceMilliseconds(Number(config.activation_timeout)) + ) + return this.getDeviceDetails(deviceId) }