domotz-remote-pawn
Version:
Domotz Agent
70 lines (65 loc) • 2.71 kB
JavaScript
/** 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 LOG_EVENT = 'HANDLER - (autoWakeOnLan)';
var handler = function (resourceLocator) {
var hostInfo = resourceLocator.devices.info;
var bindingStorage = resourceLocator.interfacesBindingStorage;
var executeCommand = resourceLocator.utils.command.execute;
var join = resourceLocator.path.join;
var commandPath = join(__dirname, '..', '..', 'commands', 'network', 'wakeOnLan.js');
console.info(LOG_EVENT);
function createReporter(device) {
return function (data, err) {
if (err) {
console.warn('%s: Error for device - MAC: %s, id: %s',
LOG_EVENT, device.mac, device.id);
console.warn(JSON.stringify(err));
} else {
console.info('%s: Success for device - MAC: %s, id: %s',
LOG_EVENT, device.mac, device.id);
}
};
}
var temp = bindingStorage.listFresh();
var activeDevices = {};
temp.forEach(function (binding) {
activeDevices[binding.mac] = true;
});
hostInfo.getList().then(function (devicesList) {
"use strict";
for (var mac in devicesList) {
if (!devicesList.hasOwnProperty(mac)) {
continue;
}
var device = devicesList[mac];
if (device.details.auto_wol && !activeDevices[device.mac]) {
console.info("%s: triggering wake on lan for device - MAC: %s, id: %s",
LOG_EVENT, device.mac, device.id);
executeCommand(commandPath, {
correlationId: "EVENT",
payload: {mac: device.mac}
}, resourceLocator,
createReporter(device));
}
}
});
};
module.exports.handler = handler;