Guest User

Untitled

a guest
May 31st, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. var Promise = require('bluebird')
  2. var Api = require('mikronode');
  3. var colors = require('colors')
  4. var con, api
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12. var BluebirdMikrotik = function(a,debug){
  13. console.log(a)
  14. this.api = new Api(a.host,a.user,a.password);
  15. }
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26. BluebirdMikrotik.prototype.connect = function () {
  27. var p = new Promise.defer();
  28. //console.log('intentando conexion')
  29. this.api.connect(function(c) {
  30. console.log(c)
  31. this.con=c
  32. console.log('.')
  33. console.log(this.con)
  34. p.resolve()
  35. })
  36. this.api.on('error',function (e) {
  37. //console.log(e)
  38. p.reject(e)
  39. })
  40. return p.promise
  41. }
  42.  
  43.  
  44.  
  45.  
  46.  
  47. BluebirdMikrotik.prototype.send = function(data){
  48. data = data || 'data\n'
  49. var p = new Promise.defer();
  50. console.log(this.con)
  51. var chan=this.con.openChannel();
  52. chan.write(data,function() {
  53. chan.on('trap',function(data) {
  54. // console.log('t');
  55. p.reject(data)
  56. });
  57. chan.on('done',function(data) {
  58.  
  59. var parsed = Api.parseItems(data);
  60. console.log(parsed)
  61. chan.close();
  62. this.con.close();
  63. p.resolve(parsed)
  64. });
  65. });
  66.  
  67. return p.promise;
  68.  
  69. }
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76. module.exports = BluebirdMikrotik;
  77. // var MK = new BluebirdMikrotik({},true)
  78. // console.log(MK)
  79. // MK.connect()
  80.  
  81.  
  82.  
  83. here ends the module for mikrotik api I have a main app file in which I call this code inside of a function:
  84.  
  85.  
  86.  
  87. // console.log(config.mikrotik)
  88. mk = new bbmk(config.mikrotik)
  89. //console.log(mk)
  90. return mk.connect().bind(mk) //this was trying (has no effect)
  91. .then(function () {
  92. // body...
  93. console.log('EEEEEEEEEEEEEEEE conectado')
  94. console.log(mk.con) //this logs to undefined
  95. return mk.send(['/ip/hotspot/user/profile/getall']) //throws an error because con is undefined.
  96. })
Advertisement
Add Comment
Please, Sign In to add comment