UNPKG

domotz-remote-pawn

Version:

Domotz Agent

81 lines (75 loc) 2.73 kB
/** * This file is part of Domotz Agent. * * @license * 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/>. * * @copyright Copyright (C) Domotz Inc */ /** * The TCP library Options * @typedef {Object} TCPOptions * @property {string} command - The tcp command to execute against the device * @property {int} [port=23] - The tcp port to connect to * @property {int} [timeout=500] - The time the tcp command will execute for at most (in milliseconds) * @property {boolean} [keepAlive=false] - Boolean flag to use if the TCP connection should persist until the timeout value runs out. Disabled by default * @property {str} [encoding=utf8] - The tcp command response encoding. Default 'utf8' */ /** * The TCP library callback * @typedef {callback} TCPCallback * @property {string} output - The Result from the tcp command execution * @property {ErrorResult} [error] - Will be present if the execution resulted in an error */ var tcpClient = require('net'); var commandUtils = require('../../../src/utils/command'); var sandboxConsole; /** * TCP Library Resource Locator object * @private * @readonly */ var sandboxResourceLocator = { log: { decorateLogs: function () { return sandboxConsole; }, }, net: tcpClient, utils: { command: commandUtils, }, }; /** * Sends a command via TCP * @function * @private * @readonly * @param {Object} myConsole - The Domotz Sandbox console * @param {TCPOptions} options - The TCP Command execution options */ function sendTCPCommand(myConsole, options, callback) { sandboxConsole = myConsole; var message = { payload: options, }; function outputTransformer(result) { var error = result.error; var output = result.response; callback(output, error); } var tcpSend = require('../../commands/tcpRequest').command(message, sandboxResourceLocator); return tcpSend.execute(outputTransformer); } module.exports.sendTCPCommand = sendTCPCommand;