UNPKG

domotz-remote-pawn

Version:

Domotz Agent

85 lines (75 loc) 2.93 kB
/** This file is part of Domotz Agent. * Copyright (C) 2016 Domotz Ltd * * Domotz Agent is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Domotz Agent is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>. * * Maintains a cache with the devices list, as the engine knows it * * Created by Tommaso Latini <tommaso@domotz.com> on 23/09/15. */ const MODULE = 'DEVICE_CACHE'; var devicesInfo = function (q, async, time, engineRequest, configuration) { var toMs = time.toMs; var servicesRunInterval = configuration.cache_expiration_time || toMs(6); var devicesCache = null; var servicesExecutions = {}; function getList() { throw Error('USING DEPRECATED METHOD'); } function assureServiceExecutionExists(mac, service) { 'use strict'; servicesExecutions[mac] = servicesExecutions[mac] || {}; servicesExecutions[mac][service] = servicesExecutions[mac][service] || { last_run: null }; } function isToBeUpdated(mac, service) { assureServiceExecutionExists(mac, service); var oldOrMissing = false; if (servicesExecutions[mac][service].last_run === null) { oldOrMissing = true; console.debug('%s - %s for device with mac %s has not executed yet', MODULE, service, mac); } else { var elapsed = new Date() - servicesExecutions[mac][service].last_run; oldOrMissing = elapsed > servicesRunInterval; if (oldOrMissing) { console.info('%s - %s last run too old for device with mac %s (%s seconds)', MODULE, service, mac, elapsed / 1000); } } return oldOrMissing; } function setUpdateTimeToNow(mac, service) { 'use strict'; assureServiceExecutionExists(mac, service); var now = new Date(); servicesExecutions[mac][service].last_run = now; console.info('%s - %s last run set now [%s] for mac %s', MODULE, service, now, mac); } function get() { return getList(); } function getDevicesCache() { if (devicesCache) { return devicesCache; } else { return {}; } } return { isToBeUpdated: isToBeUpdated, get: get, getList: getList, setUpdateTimeToNow: setUpdateTimeToNow, getDevicesCache: getDevicesCache, }; }; module.exports.factory = devicesInfo;