Now it depends on mikronode-ng. (#1)
* Now it depends on mikronode-ng. Connection forcefully closed when node-red stops. * Link to API added in info of mikrotik node
This commit is contained in:
committed by
bladerunner2020
parent
667c8be203
commit
df36c01603
@@ -53,4 +53,5 @@
|
||||
|
||||
<script type="text/x-red" data-help-name="mikrotik">
|
||||
<p>A node that work with Mikrotik WiFi routers.</p>
|
||||
<p>Read more about RouterOs API here: <a href="http://wiki.mikrotik.com/wiki/API_command_notes" target="_blank">http://wiki.mikrotik.com/wiki/API_command_notes</a>.</p>
|
||||
</script>
|
||||
51
mikrotik.js
51
mikrotik.js
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Created by Bladerunner on 11/03/16.
|
||||
*/
|
||||
var mikrotik = require('mikronode');
|
||||
var mikrotik = require('mikronode-ng');
|
||||
|
||||
module.exports = function(RED) {
|
||||
function NodeMikrotik(config) {
|
||||
@@ -30,40 +30,37 @@ module.exports = function(RED) {
|
||||
action = '/system/reboot';
|
||||
break;
|
||||
case 9:
|
||||
// action = msg.payload;
|
||||
action = '';
|
||||
break;
|
||||
}
|
||||
|
||||
var connection = null;
|
||||
|
||||
this.on('input', function(msg) {
|
||||
if (action == '') action = msg.payload;
|
||||
var connection = mikrotik(ip, login, pass, {debug: 0});
|
||||
connection.on('error', function(err) {
|
||||
node.error("Connection error: " + err);
|
||||
});
|
||||
|
||||
|
||||
connection.connect(function(conn) {
|
||||
var chan = conn.openChannel();
|
||||
chan.write(action, function() {
|
||||
chan.on('done',function(data) {
|
||||
var parsed = mikrotik.parseItems(data);
|
||||
|
||||
var pl = [];
|
||||
parsed.forEach(function(item) {
|
||||
pl.push(item);
|
||||
});
|
||||
|
||||
msg.payload = pl;
|
||||
node.send(msg);
|
||||
|
||||
chan.closeOnDone = true;
|
||||
conn.closeOnDone = true;
|
||||
|
||||
if(action == '') return false;
|
||||
connection = mikrotik.getConnection(ip, login, pass, {closeOnDone : true});
|
||||
connection.getConnectPromise().then(function(conn) {
|
||||
conn.getCommandPromise(action).then(function resolved(values) {
|
||||
var parsed = mikrotik.parseItems(values);
|
||||
var pl = [];
|
||||
parsed.forEach(function(item) {
|
||||
pl.push(item);
|
||||
});
|
||||
msg.payload = values;
|
||||
node.send(msg);
|
||||
}, function rejected(reason) {
|
||||
node.error('Error executing cmd['+action+']: ' + JSON.stringify(reason));
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
function(err) {
|
||||
node.error("Connection error: " + err);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
this.on('close', function() {
|
||||
connection && connection.connected && connection.close(true);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "node-red-contrib-mikrotik",
|
||||
"version": "0.0.4",
|
||||
"version": "0.1.0",
|
||||
"description": "Node to work with Mikrotik WiFi router",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
@@ -16,7 +16,7 @@
|
||||
],
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"mikronode": "^0.3.3"
|
||||
"mikronode-ng": "^1.0.5"
|
||||
},
|
||||
"main": "mikrotik.js",
|
||||
"devDependencies": {},
|
||||
|
||||
Reference in New Issue
Block a user