UNPKG

domotz-remote-pawn

Version:

Domotz Agent

62 lines (49 loc) 2.15 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/>. **/ /** * * Created by Tommaso Latini <tommaso@domotz.com> on 22/07/15. */ const MAX_NETWORK_SWITCH_CYCLE = 3; const MAX_ZIGBEE_CONTROLLER_CYCLE = 15; const MAX_COUNTER_VALUE = MAX_NETWORK_SWITCH_CYCLE * MAX_ZIGBEE_CONTROLLER_CYCLE; const LOG_EVENT = 'HANDLER - (deviceStatusUpdate)'; var compute_query_parameters = function(call_counter) { var ret = 'macro_category=power_plug'; if (call_counter % MAX_ZIGBEE_CONTROLLER_CYCLE === 0) { ret += '&macro_category=zigbee_controller'; } if (call_counter % MAX_NETWORK_SWITCH_CYCLE === 0) { ret += '&macro_category=network_switch'; } return ret; }; var deviceStatusUpdate = function (resourceLocator) { arguments.callee.counter = (++arguments.callee.counter) || 0; var status = resourceLocator.status; if (status.get() === status.RESTRICTED || status.get() === status.UPDATING) { console.warn(LOG_EVENT + ': Pawn in restricted or updating mode. Cannot perform Discovery'); return; } var query_string = compute_query_parameters(arguments.callee.counter); var engineRequest = resourceLocator.engineRequest; var engineUrlForPlugs = '/device-status?' + query_string; console.warn(LOG_EVENT + ': ' + query_string); engineRequest.send(engineUrlForPlugs, 'PUT', {}); arguments.callee.counter %= MAX_COUNTER_VALUE; }; module.exports.handler = deviceStatusUpdate;