UNPKG

domotz-remote-pawn

Version:

Domotz Agent

116 lines (104 loc) 4.14 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/>. * * Automatically sets the wake on lan command to all the devices that have it enabled and are offline * * Created by Iacopo Papalini <iacopo@domotz.com> on 17/09/15. */ const CACHE_DURATION_IN_MS = 7200000; // 2 hours var cache = { readTime: 0, macList: null, }; var handler = function (resourceLocator) { var q = require('q'); var myConsole = resourceLocator.log.decorateLogs(); var bindingStorage = resourceLocator.interfacesBindingStorage; var executeCommand = resourceLocator.utils.command.execute; var join = resourceLocator.path.join; var engineRequest = resourceLocator.engineRequest; var commandPath = join(__dirname, '..', '..', 'commands', 'network', 'wakeOnLan.js'); var status = resourceLocator.status; var lodash = resourceLocator.lodash; if (!status.canExecuteEventHandler()) { myConsole.info('Pawn in restricted or updating mode. Cannot perform auto wake on lan'); return q.resolve(); } myConsole.info('Starting'); function createReporter(mac) { return function (data, err) { if (err) { myConsole.warn('Error for mac %s', mac); myConsole.warn(JSON.stringify(err)); } else { myConsole.info('Success for mac %s', mac); } }; } var temp = bindingStorage.listFresh(); var activeDevices = {}; temp.forEach(function (binding) { activeDevices[binding.mac] = true; }); function updateMacListToWakeUp() { 'use strict'; var dateNow = new Date(); if (cache.macList === null || dateNow - cache.readTime > CACHE_DURATION_IN_MS) { myConsole.info('Updating mac addresses list - last update was at ' + cache.readTime); cache.readTime = dateNow; return engineRequest .sendWithPromise('/device/wake-on-lan', 'GET') .then(function (data) { myConsole.info('Updated mac addresses list at ' + cache.readTime); cache.macList = data; return data; }) .catch(function (error) { myConsole.error('Cannot update list, assuming no device to wake on lan, %s', JSON.stringify(error)); return []; }); } else { myConsole.info('Keeping mac addresses list - last update was at ' + cache.readTime); return q(cache.macList); } } return updateMacListToWakeUp().then(function () { 'use strict'; lodash.forEach(cache.macList, function (mac) { myConsole.debug('Checking mac %s', mac); var activeDevice = activeDevices[mac]; if (activeDevice === undefined) { myConsole.info('Triggering wake on lan for MAC: %s', mac); executeCommand( commandPath, { correlationId: 'EVENT', payload: { mac: mac }, }, resourceLocator, createReporter(mac) ); } else { myConsole.debug('Device %s is online', mac); } }); }); }; module.exports.handler = handler; module.exports.invalidate = function () { 'use strict'; cache.macList = null; };