Browse Source

add hidLabel.js

bernn 2 years ago
parent
commit
947991a4c0
1 changed files with 245 additions and 0 deletions
  1. 245 0
      hidLabel.js

+ 245 - 0
hidLabel.js

@@ -0,0 +1,245 @@
+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);
+}