domotz-remote-pawn
Version:
Domotz Agent
62 lines (58 loc) • 2.43 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/>.
* Created by Tommaso Latini <tommaso@domotz.com> on 31/03/16.
*/
var factory = function (dgram, resourceLocator) {
var myConsole = resourceLocator.log.decorateLogs();
return {
createSocket: function () {
return dgram.createSocket('udp4');
},
send: function (socket, buffer, host, port, burst) {
var callback = function () {
myConsole.silly('Message %s sent to %s:%d', buffer.toString(), host, port);
};
var i;
for (i = 0; i < burst; i++) {
socket.send(buffer, 0, buffer.length, port, host, callback);
}
myConsole.silly(buffer.length + ' bytes sent ' + i + ' times');
},
decode: function (filter) {
var hexString = filter.replace(/\s+/g, '');
myConsole.silly('Filtering by ' + hexString);
return Buffer(hexString, 'hex');
},
logListening: function (socket) {
var address = socket.address();
var ip = address.address,
port = address.port;
myConsole.verbose('UDP Socket bound on ' + ip + ':' + port);
},
logClose: function (type) {
myConsole.verbose('UDP %s closed', type);
},
logMessage: function (msg, rinfo) {
myConsole.debug('Received %d bytes from %s:%d', msg.length, rinfo.address, rinfo.port);
myConsole.verbose('Message: %s', msg.toString());
},
logError: function (error) {
myConsole.error('UDP error - Error: {}' + error.message);
myConsole.error(error.stack);
},
};
};
module.exports.factory = factory;