prima versione qausi funzionante, non riconnette
This commit is contained in:
85
mikrotik-custom.js
Normal file
85
mikrotik-custom.js
Normal file
@@ -0,0 +1,85 @@
|
||||
var mikrotik = require('mikronode-ng2');
|
||||
|
||||
module.exports = function(RED) {
|
||||
function NodeMikrotikDevice(n) {
|
||||
RED.nodes.createNode(this, n);
|
||||
this.host = n.host;
|
||||
this.port = n.port;
|
||||
this.username = n.username;
|
||||
this.password = n.password;
|
||||
}
|
||||
RED.nodes.registerType("mikrotik-device-custom", NodeMikrotikDevice);
|
||||
|
||||
|
||||
function NodeMikrotik(config) {
|
||||
RED.nodes.createNode(this,config);
|
||||
|
||||
this.device = RED.nodes.getNode(config.device);
|
||||
this.action = config.action;
|
||||
|
||||
var node = this;
|
||||
var host = this.device.host;
|
||||
var port = this.device.port;
|
||||
var username = this.device.username;
|
||||
var password = this.device.password;
|
||||
var command;
|
||||
|
||||
switch (parseInt(node.action)) {
|
||||
case 0:
|
||||
command = '/log/print';
|
||||
break;
|
||||
case 1:
|
||||
command = '/system/resource/print';
|
||||
break;
|
||||
case 2:
|
||||
command = '/interface/wireless/registration-table/print';
|
||||
break;
|
||||
case 3:
|
||||
command = '/system/reboot';
|
||||
break;
|
||||
case 9:
|
||||
command = '';
|
||||
break;
|
||||
}
|
||||
|
||||
var connection = mikrotik.getConnection(host, username, password, {closeOnDone : false, port: port});
|
||||
var conn = null;
|
||||
try {
|
||||
conn = connection.connect();
|
||||
} catch (error) {
|
||||
node.error(error);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
this.on('close', function() {
|
||||
connection && connection.connected && connection.close(true);
|
||||
});
|
||||
}
|
||||
|
||||
RED.nodes.registerType("mikrotik-custom", NodeMikrotik);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user