Skip to content

Instantly share code, notes, and snippets.

@fortruce
Created April 22, 2015 19:45
Show Gist options
  • Save fortruce/677eec08b1becfcd5289 to your computer and use it in GitHub Desktop.
Save fortruce/677eec08b1becfcd5289 to your computer and use it in GitHub Desktop.
var Promise = require('bluebird')
var Api = require('mikronode');
var colors = require('colors')
var con, api
var BluebirdMikrotik = function(a,debug){
console.log(a)
this.api = new Api(a.host,a.user,a.password);
}
BluebirdMikrotik.prototype.connect = function () {
var p = new Promise.defer();
//console.log('intentando conexion')
this.api.connect(function(c) {
console.log(c)
this.con=c
console.log('.')
console.log(this.con)
p.resolve()
})
this.api.on('error',function (e) {
//console.log(e)
p.reject(e)
})
return p.promise
}
BluebirdMikrotik.prototype.send = function(data){
data = data || 'data\n'
var p = new Promise.defer();
console.log(this.con)
var chan=this.con.openChannel();
chan.write(data,function() {
chan.on('trap',function(data) {
// console.log('t');
p.reject(data)
});
chan.on('done',function(data) {
var parsed = Api.parseItems(data);
console.log(parsed)
chan.close();
this.con.close();
p.resolve(parsed)
});
});
return p.promise;
}
module.exports = BluebirdMikrotik;
// var MK = new BluebirdMikrotik({},true)
// console.log(MK)
// MK.connect()
here ends the module for mikrotik api I have a main app file in which I call this code inside of a function:
// console.log(config.mikrotik)
mk = new bbmk(config.mikrotik)
//console.log(mk)
return mk.connect().bind(mk) //this was trying (has no effect)
.then(function () {
// body...
console.log('EEEEEEEEEEEEEEEE conectado')
console.log(mk.con) //this logs to undefined
return mk.send(['/ip/hotspot/user/profile/getall']) //throws an error because con is undefined.
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment