96 lines
2.3 KiB
JavaScript
96 lines
2.3 KiB
JavaScript
exports.id = 'tcpipsend';
|
|
exports.title = 'TCP/IP Send';
|
|
exports.group = 'TCP/IP';
|
|
exports.color = '#888600';
|
|
exports.version = '1.0.2';
|
|
exports.icon = 'sign-out';
|
|
exports.input = true;
|
|
exports.output = 1;
|
|
exports.author = 'Lukas Muransky';
|
|
exports.options = {};
|
|
|
|
exports.html = `<div class="padding">
|
|
<div data---="dropdown__tcpclient__datasource:tcpipconfig;required:true" class="m">@(Clients)</div>
|
|
</div>
|
|
<script>
|
|
var tcpipconfig = [];
|
|
var clients = {};
|
|
ON('open.tcpipsend', function(component, options) {
|
|
|
|
/*TRIGGER('tcpip.clients', function(data) {
|
|
clients = data;
|
|
tcpipconfig = [];
|
|
data.forEach(function(client){
|
|
tcpipconfig.push(client.id);
|
|
});
|
|
console.log(tcpipconfig);
|
|
});*/
|
|
|
|
TRIGGER('tcpip.clients', 'tcpipconfig');
|
|
|
|
});
|
|
</script>`;
|
|
|
|
exports.readme = `# TCPIP Connect`;
|
|
|
|
|
|
exports.install = function(instance) {
|
|
var net = require('net');
|
|
|
|
instance.on('data', function(flowdata) {
|
|
connect(flowdata);
|
|
});
|
|
|
|
instance.reconfigure = function() {
|
|
if (!instance.options.tcpclient)
|
|
return instance.status('Not configured', 'red');
|
|
else
|
|
return instance.status('Configured');
|
|
};
|
|
|
|
instance.on('options', instance.reconfigure);
|
|
instance.reconfigure();
|
|
|
|
function connect(response) {
|
|
//instance.debug('Input data: ' + data);
|
|
//let Summary = [];
|
|
var client = new net.Socket();
|
|
|
|
var tcpip = TCPIP_CLIENTS.find(obj => obj.id == instance.options.tcpclient);
|
|
|
|
var opt = {};
|
|
opt.ip = tcpip.options.ip;
|
|
opt.port = tcpip.options.port;
|
|
opt.helvar = tcpip.options.helvar;
|
|
|
|
client.connect(opt.port, opt.ip, function() {
|
|
instance.status('Connected');
|
|
});
|
|
//instance.debug('Response: ' + response.data);
|
|
|
|
client.on('close', function() {
|
|
//instance.debug('Connection closed (inside)');
|
|
instance.status('Closed');
|
|
});
|
|
|
|
client.on('data', function(answer) {
|
|
response.data = answer.toString();
|
|
instance.send2(response);
|
|
});
|
|
|
|
if(opt.helvar && response.data.slice(-1) == '*'){
|
|
response.data = response.data.slice(0, -1); //odstrani *
|
|
client.write(response.data);
|
|
response.data = response.data.slice(0, -1); //odstrani # lebo je prida ma koniec
|
|
response.data = '?'+response.data.slice(1) + "=Command sent#";
|
|
instance.send2(response);
|
|
//client.destroy();
|
|
} else {
|
|
client.write(response.data);
|
|
}
|
|
|
|
client.end();
|
|
}
|
|
};
|
|
|
|
///FLOW.find() najde komponentu instance.connect(data)
|