UNPKG

domotz-remote-pawn

Version:

Domotz Agent

73 lines (64 loc) 2.43 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/>. * * * Sends the counters of DHCP requests to the cloud, * Created by Stefano Cicero <stefano.cicero@domotz.com> on 19/06/23. */ const ONE_DAY = 86400; module.exports.factory = function (resourceLocator) { const fileUtils = resourceLocator.utils.file; const lodash = resourceLocator.lodash; const os = resourceLocator.os; const path = resourceLocator.path; const time = resourceLocator.utils.time; const filename = path.join(os.tmpdir(), 'domotz_dhcp_requests.json'); var dhcpRequests = []; var discoveredMacAddresses = {}; function registerDHCPRequest(macAddress) { const ts = Math.floor(Date.now() / 1000); dhcpRequests.push({ mac: macAddress, ts: ts, }); discoveredMacAddresses[macAddress] = true; } function retrieveMacAddresses() { var macAddressesList = Object.keys(discoveredMacAddresses); discoveredMacAddresses = {}; return macAddressesList; } function getDHCPRequest(fromTs) { return lodash.filter(dhcpRequests, function (value) { return value.ts > fromTs; }); } function storeToFS() { const currentTs = time.getTimestamp(); const dhcpRequestsFilter = getDHCPRequest(currentTs - ONE_DAY); fileUtils.writeFileSafe(filename, JSON.stringify(dhcpRequestsFilter)); } function loadFromFS() { dhcpRequests = fileUtils.readFile(filename, JSON.parse) || []; } return { registerDHCPRequest: registerDHCPRequest, getDHCPRequest: getDHCPRequest, storeToFS: storeToFS, loadFromFS: loadFromFS, retrieveMacAddresses: retrieveMacAddresses, }; };