Skip to content

Instantly share code, notes, and snippets.

@HERRKIN
Last active August 29, 2015 14:14
Show Gist options
  • Save HERRKIN/a311a62a079b23319dd4 to your computer and use it in GitHub Desktop.
Save HERRKIN/a311a62a079b23319dd4 to your computer and use it in GitHub Desktop.
module
var Promise=require('bluebird');
var net= require('net');
//var openedPort=false;
//Cas.config;
//Cas.client;
var Cas = function(args) {
this.config=args
console.log(this.config)
// console.log(config)
//this.connect()
// console.log(this)
};
Cas.prototype.connect = function (d){
this.client = new net.Socket();
console.log('intentando conexion')
this.client.connect(this.config.port, this.config.ip, function(err) {
if(err){
console.log(err)
}
console.log('Connected')
connectiontest(this)
});
this.client.on('close', function(e) {
console.log('Connection closed');
//console.log(client)
});
this.client.on('error', function(e) {
console.log(e)
})
this.client.on('data', function(data) {
h=data.toString('hex')
if(h.charAt(29)==='1'){
console.log('CAS'.green+': PING')
bf= new Buffer(caspackage('020000'),'hex')
client.write(bf);
console.log('interface'.green+': PONG')
}
});
return new Promise(function(resolve,reject){
//esperartrigger()
// resolve(connection);
})
}
Cas.prototype.send = function (bf){
client.write(bf)
var p = new Promise.defer();
client.once('data', function(data) {
console.log('Received: ') ;
h=data.toString('hex')
//console.log(h)
//console.log(data.length)
if (h.charAt(29)==='2')
console.log('CAS'.green+': PONG')
p.resolv
});
};
Cas.prototype.getPID = function (d){
};
connectiontest = function(t){
console.log(t.config)
console.log('interface'.green+': PING')
bf=new Buffer(caspackage('010000'),'hex')
//console.log(bf)
this.send(bf);
}
datehex = function (date) {
year=zeropad(date.getFullYear().toString(16),4)
month=zeropad((1+date.getMonth()).toString(16),2)
day=zeropad(date.getDate().toString(16),2)
hours = zeropad(date.getHours().toString(16),2)
minutes = zeropad(date.getMinutes().toString(16),2)
seconds = zeropad(date.getSeconds().toString(16),2)
return year+month+day+hours+minutes+seconds
}
zeropad= function(cad, len){
while (cad.length < len) {
cad = '0' + cad;
} // Zero pad.
//console.log(cad)
return cad;
}
caspackage = function(args){
console.log(config)
return this.config.protcolid+zeropad(config.smsid,4)+'00000000'+datehex(new Date())+args;
}
module.exports = Cas;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment