UNPKG

domotz-remote-pawn

Version:

Domotz Agent

75 lines (62 loc) 2.38 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 Iacopo Papalini <ipapalini@domotz.com> on 08/04/15. * */ /** * */ var udpSendPacket = function (message, resourceLocator) { var scheduler = resourceLocator.utils.scheduler; var udp = resourceLocator.udp.utils; var payload = message.payload; var port = payload.port; var host = payload.host; var filters = payload.datagram_filters; function deferredMessage(socket, buffer, host, port) { return function () { var len = buffer.length; for (var i = 0; i < payload.burst; i++) { socket.send(buffer, 0, len, port, host); } console.silly(len + ' bytes sent ' + i + ' times'); }; } return { execute: function (callback) { var socket = udp.createSocket(); var interval = payload.interval || 200; var schedule = []; for (var i = 0; i < filters.length; i++) { var buffer = udp.decode(filters[i]); var action = deferredMessage(socket, buffer, host, port); schedule.push([action, interval, "Send packet #" + i]); } schedule.push([function () { socket.close(); }, 0, 'Closing socket']); schedule.push([callback, 0, 'Calling callback']); scheduler.execute(schedule); }, describe: function () { return message.correlationId + " - udpSendPackets - " + host + ':' + port + ' - ' + filters.length + ' packets'; } }; }; module.exports.command = udpSendPacket;