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:
Alexander Borovsky
2016-07-05 07:06:18 +04:00
committed by bladerunner2020
parent 667c8be203
commit df36c01603
3 changed files with 27 additions and 29 deletions

View File

@@ -53,4 +53,5 @@
<script type="text/x-red" data-help-name="mikrotik"> <script type="text/x-red" data-help-name="mikrotik">
<p>A node that work with Mikrotik WiFi routers.</p> <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> </script>

View File

@@ -1,7 +1,7 @@
/** /**
* Created by Bladerunner on 11/03/16. * Created by Bladerunner on 11/03/16.
*/ */
var mikrotik = require('mikronode'); var mikrotik = require('mikronode-ng');
module.exports = function(RED) { module.exports = function(RED) {
function NodeMikrotik(config) { function NodeMikrotik(config) {
@@ -30,40 +30,37 @@ module.exports = function(RED) {
action = '/system/reboot'; action = '/system/reboot';
break; break;
case 9: case 9:
// action = msg.payload;
action = ''; action = '';
break; break;
} }
var connection = null;
this.on('input', function(msg) { this.on('input', function(msg) {
if (action == '') action = msg.payload; if (action == '') action = msg.payload;
var connection = mikrotik(ip, login, pass, {debug: 0}); if(action == '') return false;
connection.on('error', function(err) { connection = mikrotik.getConnection(ip, login, pass, {closeOnDone : true});
node.error("Connection error: " + err); connection.getConnectPromise().then(function(conn) {
}); conn.getCommandPromise(action).then(function resolved(values) {
var parsed = mikrotik.parseItems(values);
connection.connect(function(conn) {
var chan = conn.openChannel();
chan.write(action, function() {
chan.on('done',function(data) {
var parsed = mikrotik.parseItems(data);
var pl = []; var pl = [];
parsed.forEach(function(item) { parsed.forEach(function(item) {
pl.push(item); pl.push(item);
}); });
msg.payload = values;
msg.payload = pl;
node.send(msg); node.send(msg);
}, function rejected(reason) {
chan.closeOnDone = true; node.error('Error executing cmd['+action+']: ' + JSON.stringify(reason));
conn.closeOnDone = true;
});
}); });
},
function(err) {
node.error("Connection error: " + err);
}
);
}); });
this.on('close', function() {
connection && connection.connected && connection.close(true);
}); });
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "node-red-contrib-mikrotik", "name": "node-red-contrib-mikrotik",
"version": "0.0.4", "version": "0.1.0",
"description": "Node to work with Mikrotik WiFi router", "description": "Node to work with Mikrotik WiFi router",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
@@ -16,7 +16,7 @@
], ],
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"mikronode": "^0.3.3" "mikronode-ng": "^1.0.5"
}, },
"main": "mikrotik.js", "main": "mikrotik.js",
"devDependencies": {}, "devDependencies": {},