Skip to content

Commit

Permalink
printer changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian committed Dec 22, 2013
1 parent 24c63ae commit 838b9c2
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 142 deletions.
154 changes: 96 additions & 58 deletions lib/asteroid_client.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
var util = require('util'),
events = require('events'),
DDPClient = require("ddp"),
moment = require("moment"),
bundle = require('../package.json'),

http = require('http'),
url = require('url'),
_ = require("underscore");



var KEEPALIVE_INTERVAL = 30;
/* seconds */

Expand All @@ -24,27 +23,23 @@ var AsteroidClient = function (opts) {
self.port = opts.port || 80;
self.verbose = opts.verbose || false;
self.token = opts.token || undefined;
self.secret = opts.secret || undefined;

self._ddpclient = undefined;



self._plugins = [];

};


/**
* Inherits from EventEmitter
*/
util.inherits(AsteroidClient, events.EventEmitter);



AsteroidClient.prototype.addPlugin = function(plugin){
AsteroidClient.prototype.addPlugin = function (plugin) {
this._plugins.push(plugin);

}
};

/*
* Connects to a remote Asteroid server using the DDP protocol.
Expand All @@ -53,9 +48,9 @@ AsteroidClient.prototype.connect = function () {
var self = this;

self._ddpclient = new DDPClient({ 'host': self.host, 'port': self.port});
_.each(self._plugins,function(plugin){
plugin._ddpclient = self._ddpclient;
})
_.each(self._plugins, function (plugin) {
plugin._ddpclient = self._ddpclient;
});

// If we receive a message notifying us that the code property of our device
// description in database has changed, then let's re-run the code.
Expand All @@ -64,19 +59,16 @@ AsteroidClient.prototype.connect = function () {
console.log("DDP: %s", msg);
var msg = JSON.parse(msg);
// if (msg.msg=='changed' && msg.collection == 'devices' && 'code' in msg.fields) {
// Let the message be processed so that the local collection
// is updated and then fire the event.
// TODO: A better way to do that would be reactive collections.
setTimeout(function() {
_.each(self._plugins, function (plugin) {



if ((msg.msg=='added') &&( _.contains( plugin.collections, msg.collection)))
plugin.change(msg);
})
}, 1);

// Let the message be processed so that the local collection
// is updated and then fire the event.
// TODO: A better way to do that would be reactive collections.
setTimeout(function () {
_.each(self._plugins, function (plugin) {

if ((_.contains(['added', 'changed'], msg.msg) ) && ( _.contains(plugin.collections, msg.collection)))
plugin.change(msg);
});
}, 1);

});
self._ddpclient.on('socket-close', function (code, message) {
Expand Down Expand Up @@ -152,54 +144,100 @@ AsteroidClient.prototype._findRedirectTarget = function (cb) {
console.warn("Error fetching redirect target (%j): %s", options, e);
});
req.end();
}
};
AsteroidClient.prototype._runServices = function (result) {
var self = this;

self._ddpclient.subscribe('devices', function () {
/*
* Once subscribed, we can look at the local copy of our object and execute the code.
*/

// self._codeUpdated();
});

_.each(self._plugins, function (plugin) {
console.log('reg printers' + self.token);
plugin.registered(result, self.token);
});
/*
* When we have identified ourselves, we are now 'registered', the next step
* is to subscribe to updates to the object in database that describes this device.
*/

if (self.upInterval) {
clearInterval(self.upInterval);
}
self.upInterval = setInterval(function () {
self._keepAlive();
}, KEEPALIVE_INTERVAL * 1000);
};

AsteroidClient.prototype.getPass = function (secret) {
return secret;
};

hashCode = function (data) {
var hash = 0;
if (data.length == 0) return hash;
for (i = 0; i < data.length; i++) {
var achar = data.charCodeAt(i);
hash = ((hash << 5) - hash) + achar;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
};

createTimeHash = function (time, secret) {
return hashCode(time + secret);
};
/*
* Call the register method to identify ourselves and make sure
* the server knows who we are.
*/
AsteroidClient.prototype._registerToServer = function () {
var self = this;

infos = {};


infos.time = moment().unix();
infos.hash = createTimeHash(infos.time,self.secret.trim());
// Call register method with our bundle version, IP address, etc
self._ddpclient.call('registerDevice', [self.token.trim(), bundle.name, bundle.version, infos ],
function (err, result) {
if (!result) {
console.log('Register result: %j (err: %j)', result, err);
return;
}
else {
if (result.unregistered) {
infos.secret = self.secret.trim();
self._ddpclient.call('registerDevice', [self.token.trim(), bundle.name, bundle.version, infos ],
function (err, result) {
if (!result) {
console.log('Register result: %j (err: %j)', result, err);
return;
}
else {
{
self._runServices(result);
}
}
});


infos = {};

// Call register method with our bundle version, IP address, etc
self._ddpclient.call('registerDevice', [self.token.trim(), bundle.name, bundle.version, infos ],
function (err, result) {
if (!result) {
console.log('Register result: %j (err: %j)', result, err);
return;
}
else {
self._ddpclient.subscribe('devices', function() {
/*
* Once subscribed, we can look at the local copy of our object and execute the code.
*/

// self._codeUpdated();
});

_.each(self._plugins, function (plugin) {
console.log('reg printers'+self.token);
plugin.registered(result, self.token);
})
/*
* When we have identified ourselves, we are now 'registered', the next step
* is to subscribe to updates to the object in database that describes this device.
*/

if (self.upInterval) {
clearInterval(self.upInterval);
}
self.upInterval = setInterval(function () {
self._keepAlive();
}, KEEPALIVE_INTERVAL * 1000);
self._runServices(result);
}

}
);
}
);

}
};

/*
* Writes a console message to the server. Fire-and-forget.
Expand Down
62 changes: 62 additions & 0 deletions lib/networkPrinter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
*
* User: adrianjones
* Date: 8/9/13
* Time: 3:48 PM
*/


var net = require('net');

function d2h(d) {
return d.toString(16);
}
function asc2hex(tmp) {
var str = '',
i = 0,
tmp_len = tmp.length,
c;

for (; i < tmp_len; i += 1) {
c = tmp.charCodeAt(i);
str += d2h(c) + ' ';
}
return str;
}
var networkPrinter = function (opts) {
var self = this;
self.client = new net.Socket();
self.client.setEncoding('binary');
self.opt = opts;

};

networkPrinter.prototype.connect = function (opt, data) {
var self = this;

self.client.on('error', self.onError);
self.client.on('close', self.onClose);

console.log('connecting..');

self.client.connect(opt.port, opt.host,
function () { //'connect' listener
console.log('client connected and printing');
console.log(data);
console.log(asc2hex(data));
self.client.end(data, 'binary');

});

};


networkPrinter.prototype.unref = function(){
this.client.unref();
};
networkPrinter.prototype.setTimeout = function (timeout, func) {
var self = this;
self.client.setTimeout(5000, func);

};
module.exports = networkPrinter;
44 changes: 12 additions & 32 deletions lib/pos_printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,10 @@
* Time: 3:17 PM
*/

var net = require('net');
var networkPrinter = require('./networkPrinter');



function d2h(d) {
return d.toString(16);
}
function asc2hex(tmp) {
var str = '',
i = 0,
tmp_len = tmp.length,
c;

for (; i < tmp_len; i += 1) {
c = tmp.charCodeAt(i);
str += d2h(c) + ' ';
}
return str;
}

var printerQueue = {
printQue: [],
Expand Down Expand Up @@ -64,23 +49,25 @@ var printerQueue = {
if (job === undefined)
{ return; }
printerQueue.running = true;
// job.printer = { port: 9100, host: 'localhost'};
// job.printer = { port: 9100, host: 'localhost'};


console.log('printing on:');
console.log(job.printer);
opt.port = job.printer.port || 9100;
opt.host = job.printer.host || 'localhost';
job.error = false;
client = new net.Socket();
client.setEncoding('binary');
client.on('error', function () {
console.log(opt);
client = new networkPrinter(opt);

client.onError = function () {
job.callback('error', job);
job.error = true;
printerQueue.runPrintTimer();
console.log('connection Error');
});
client.on('close', function () {
};
client.onClose = function () {


console.log('closing');
printerQueue.runPrintTimer();
Expand All @@ -90,7 +77,7 @@ var printerQueue = {
client.unref();
// client.destroy();

});
};
client.setTimeout(5000, function () {
console.log('timeout');
job.callback('error', job);
Expand All @@ -100,15 +87,8 @@ var printerQueue = {
});

console.log('connnecting..');
console.log(opt);
client.connect(opt.port, opt.host,
function () { //'connect' listener
console.log('client connected and printing');
console.log(job.data);
console.log(asc2hex(job.data));
client.end(job.data, 'binary');

});

client.connect(opt, job.data);

}
};
Expand Down
Loading

0 comments on commit 838b9c2

Please sign in to comment.