| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- var http = require('http');
- var exec = require('child_process').exec;
- var fs = require('fs');
- var WebSocketServer = require('websocket').server;
- var HID = require('node-hid');
- var devices = HID.devices();
- var myBarCode= {};
- var lastBarCode = "";
- var lastLabel="";
- var request = require('request');
- try{
- var tempObj = fs.readFileSync('whoami');
- tempRead = JSON.parse(tempObj);
- whoami = tempRead.Name;
- console.log("I am '" + whoami + "'");
- } catch (e) {
- console.log("I could not open whoami, I'm going to quit");
- process.exit();
- }
- for(var i in devices){
- console.log(devices[i]);
- if(devices[i].product){
- var myString = devices[i].product;
- if(myString.indexOf('Bar Code')>-1){
- console.log("found barcode reader: " + myString);
- myBarCode=devices[i]
- };
- }
- }
- if(myBarCode.path){
- var device = new HID.HID(myBarCode.path);
- } else {
- console.log("I couldn't get barcode readers path, I'm going to exit.");
- process.exit();
- }
- device.on("data", function(data) {
- var myRead = data.toString();
- var myArray = myRead.split("");
- var startLog=false;
- var myRes="";
- for (var i = 0, len = myArray.length; i < len; i++) {
- if(startLog){
- if(myArray[i]=='\u0000'){
- break;
- }
- myRes=myRes + myArray[i];
- }
- if(myArray[i]=='\u0000'){startLog=true;}
- }
- console.log ("I think you want '" + myRes + "'");
- if (myRes == "REBOOT"){exec('shutdown -r now '); return;}
- if (myRes == "CLOSE"){exec('pkill -f firefox '); return;}
-
- if (myRes.substring(0,3) == "MOD"){
- console.log("Sending Modal Selection to browsers = " + myRes);
- for(var i in clients){
- var sendMe = JSON.stringify({'type':'Modal', 'MOD':myRes});
- clients[i].sendUTF(sendMe);
- }
- return;
- }
- if (myRes.substring(0,9) == "WEBREBOOT"){
- request.get('https://icg.islandceramics.com:8081/?command=webReboot', function (error, response, body) {
- if (!error && response.statusCode == 200) {
- console.log(body);
- } else {
- console.log(error);
- }
- });
- return;
- }
- if (myRes.substring(0,4) == "MANT"){
- console.log("Sending MAINT signoff to browsers = " + myRes);
- for(var i in clients){
- var sendMe = JSON.stringify({'type':'MANT', 'MANT':myRes});
- clients[i].sendUTF(sendMe);
- }
- return;
- }
- if (myRes.substring(0,4) == "ROLL"){
- console.log("Sending Modal Selection to browsers = " + myRes);
- for(var i in clients){
- var sendMe = JSON.stringify({'type':'ROLL', 'ROLL':myRes});
- clients[i].sendUTF(sendMe);
- }
- return;
- }
- if (myRes.substring(0,6) == "CONFIG"){
- console.log("Configuration Request");
- exec("/sbin/ifconfig", function(error,stdout,stderr){
- var myMessage = error + stdout + stderr;
- console.log (myMessage);
- for(var i in clients){
- var sendMe = JSON.stringify({'type':'Message', 'Message':myMessage});
- clients[i].sendUTF(sendMe);
- }
- })
- return;
- }
- if (myRes.substring(0,3) == "F11"){
- console.log("full screen me browsers = " + myRes);
- exec('/usr/bin/xdotool search --name Mozilla key F11', {env: {'DISPLAY': ':0.0', 'XAUTHORITY':'/home/bhitch/.Xauthority'}}, function(error,stdout,stderr){
- console.log(error + "~" + stdout + "~" + stderr);
- });
- return;
- }
- if (myRes.substring(0,3) == "REL"){
- console.log("Shift CTL Reload = " + myRes);
- exec('/usr/bin/xdotool search --name Mozilla key shift+ctrl+r', {env: {'DISPLAY': ':0.0', 'XAUTHORITY':'/home/bhitch/.Xauthority'}}, function(error,stdout,stderr){
- console.log(error + "~" + stdout + "~" + stderr);
- });
- return;
- }
- if (myRes.substring(0,3) == "UPG"){
- console.log("Copy hidLabel.js from server: " + myRes);
- exec('/home/pi/updateAndReboot.sh');
- return;
- }
- if (myRes.substring(0,3) == "DOC"){
- console.log("Sending DocRevCheck to browsers = " + myRes);
- for(var i in clients){
- var sendMe = JSON.stringify({'type':'DocRevCheck', 'Doc':myRes});
- clients[i].sendUTF(sendMe);
- }
- return;
- }
- if (myRes.substring(0,3) == "LAB"){
- console.log("Sending Label Request to browsers = " + myRes.substring(3));
- for(var i in clients){
- var sendMe = JSON.stringify({'type':'LabelRequest', 'labelNum':myRes.substring(3)});
- console.log(sendMe);
- clients[i].sendUTF(sendMe);
- }
- return;
- }
- if (myRes.substring(0,3) == "PIC"){
- console.log("Sending Picture Request to browsers = " + myRes.substring(3));
- for(var i in clients){
- var sendMe = JSON.stringify({'type':'PICrequest', 'ID':myRes.substring(3)});
- console.log(sendMe);
- clients[i].sendUTF(sendMe);
- }
- return;
- }
- if (myRes.substring(0,6) == "MCLOSE"){
- //console.log("Sending ModalCloseRequest");
- for(var i in clients){
- var sendMe = JSON.stringify({'type':'ModalClose'});
- console.log(sendMe);
- clients[i].sendUTF(sendMe);
- }
- return;
- }
- // if we get this far it turns on whether numeric. Numeric = job, non-numeric = scanDictionary lookup
- if(isNumeric(myRes)){
- if(myRes==lastBarCode){
- // Print a label
- console.log("print a label");
- labelPrint(myRes);
- return;
- }
- lastBarCode=myRes;
- console.log("sending barcode to browser");
- for(var i in clients){
- var sendMe = JSON.stringify({'type':'barcode', 'barcode':myRes});
- clients[i].sendUTF(sendMe);
- }
- return;
- }
- console.log("Only thing left is scan dictionary");
- for(var i in clients){
- var sendMe = JSON.stringify({'type':'scanDictionary', 'lookMeUp':myRes});
- clients[i].sendUTF(sendMe);
- }
- return;
- });
- device.on("error", function(err) {
- console.log(err);
- });
- var server = http.createServer(function(request, response){
- response.writeHead(200, {'Content-Type': 'text/plain'});
- response.end("Hello There");
- })
- server.listen(8080, '127.0.0.1', function(){ console.log("Node Web Server is listening\n\n");});
- var clients={};
- var count=0;
- wss = new WebSocketServer({httpServer: server});
- wss.on('request', function(r){
- var connection=r.accept('barcode', r.origin);
- var id = count++;
- clients[id]= connection;
- connection.on('close', function(reasonCode, description){
- console.log("Connection Closed");
- delete clients[id];
- })
- connection.on('message', function(message){
- var myLabel2 = JSON.parse(message.utf8Data);
- if(myLabel2.type=="labelPrint"){
- fs.writeFileSync('labelOut2.zpl', myLabel2.zCode);
- console.log(myLabel2);
- printLabel('labelOut2.zpl');
- }
- })
- });
- function labelPrint(lotid){
- if(lotid == lastLabel) {
- //just print, dont request new label string
- printLabel('shipLabel.zpl');
- return;
- }
-
- requestOptions.path="/ShopLabel.php?MLOT="+lotid+"&who="+whoami+"&ship=1";
- console.log(requestOptions);
- request.get("http://192.168.10.26/ShopLabel.php?MLOT="+lotid+"&who="+whoami+"&ship=1", function (error, response, body) {
- if (!error && response.statusCode == 200) {
- console.log("Here comes the web response:");
- console.log(body);
- } else {
- console.log("Here comes the error:");
- console.log(error);
- }
- });
- }
- function printLabel(filename){
- console.log("trying to print label");
- exec('cat ' + filename + ' > /dev/usb/lp0', function(error, stdout, stderr) {
- });
- }
- function isNumeric(n) {
- return !isNaN(parseFloat(n)) && isFinite(n);
- }
|