funziona
This commit is contained in:
@@ -24,6 +24,11 @@ module.exports = function(RED) {
|
||||
var password = this.device.password;
|
||||
var command;
|
||||
|
||||
var connection = mikrotik.getConnection(host, username, password, {closeOnDone : false, port: port});
|
||||
var connpromise = null;
|
||||
var connected = false;
|
||||
node.status({fill:"grey",shape:"dot",text:"unconfigured"});
|
||||
|
||||
switch (parseInt(node.action)) {
|
||||
case 0:
|
||||
command = '/log/print';
|
||||
@@ -42,32 +47,46 @@ module.exports = function(RED) {
|
||||
break;
|
||||
}
|
||||
|
||||
var connection = mikrotik.getConnection(host, username, password, {closeOnDone : false, port: port});
|
||||
var conn = null;
|
||||
try {
|
||||
conn = connection.connect();
|
||||
} catch (error) {
|
||||
node.error(error);
|
||||
function on_connect(){
|
||||
node.status({fill:"green",shape:"dot",text:"connected"});
|
||||
connected = true;
|
||||
}
|
||||
|
||||
connpromise = connection.connect(on_connect);
|
||||
connpromise.on('close', function(err){
|
||||
node.status({fill:"yellow",shape:"dot",text:"disconnected:"+err});
|
||||
connected = false;
|
||||
});
|
||||
connpromise.on('trap', function(err){
|
||||
node.status({fill:"red",shape:"ring",text:"FAILED:"+err});
|
||||
connected = false;
|
||||
});
|
||||
connpromise.on('error', function(err){
|
||||
node.status({fill:"red",shape:"dop",text:"ERROR:"+err});
|
||||
connected = false;
|
||||
});
|
||||
|
||||
this.on('input', function(msg) {
|
||||
if (command == '') command = msg.payload;
|
||||
if (command == '') return false;
|
||||
|
||||
conn.getCommandPromise(command).then(
|
||||
function resolved(values) { //promise resolved
|
||||
var parsed = mikrotik.parseItems(values);
|
||||
var pl = [];
|
||||
parsed.forEach(function(item) {
|
||||
pl.push(item);
|
||||
});
|
||||
msg.payload = values;
|
||||
node.send(msg);
|
||||
},
|
||||
function notresolved(reason) { //promise rejected
|
||||
node.error('Error executing cmd['+command+']: ' + JSON.stringify(reason));
|
||||
}
|
||||
);
|
||||
if (!connected) {
|
||||
connection.connect(on_connect);
|
||||
} else {
|
||||
connpromise.getCommandPromise(command).then(
|
||||
function resolved(values) { //promise resolved
|
||||
var parsed = mikrotik.parseItems(values);
|
||||
var pl = [];
|
||||
parsed.forEach(function(item) {
|
||||
pl.push(item);
|
||||
});
|
||||
msg.payload = values;
|
||||
node.send(msg);
|
||||
}, function notresolved(reason) { //promise rejected
|
||||
node.error('Error executing cmd['+command+']: ' + JSON.stringify(reason));
|
||||
}
|
||||
);
|
||||
}
|
||||
command = ''; //reset command string
|
||||
});
|
||||
|
||||
this.on('close', function() {
|
||||
|
||||
Reference in New Issue
Block a user