From df36c01603e395c13ea6fb9183f30fd2cab2e6a0 Mon Sep 17 00:00:00 2001 From: Alexander Borovsky Date: Tue, 5 Jul 2016 07:06:18 +0400 Subject: [PATCH] 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 --- mikrotik.html | 1 + mikrotik.js | 51 ++++++++++++++++++++++++--------------------------- package.json | 4 ++-- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/mikrotik.html b/mikrotik.html index cee499f..fc62ab4 100644 --- a/mikrotik.html +++ b/mikrotik.html @@ -53,4 +53,5 @@ \ No newline at end of file diff --git a/mikrotik.js b/mikrotik.js index 76653f2..bc088c5 100644 --- a/mikrotik.js +++ b/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); }); } diff --git a/package.json b/package.json index 1b55c03..c13a3e3 100644 --- a/package.json +++ b/package.json @@ -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": {},