hidLabel.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. var http = require('http');
  2. var exec = require('child_process').exec;
  3. var fs = require('fs');
  4. var WebSocketServer = require('websocket').server;
  5. var HID = require('node-hid');
  6. var devices = HID.devices();
  7. var myBarCode= {};
  8. var lastBarCode = "";
  9. var lastLabel="";
  10. var request = require('request');
  11. try{
  12. var tempObj = fs.readFileSync('whoami');
  13. tempRead = JSON.parse(tempObj);
  14. whoami = tempRead.Name;
  15. console.log("I am '" + whoami + "'");
  16. } catch (e) {
  17. console.log("I could not open whoami, I'm going to quit");
  18. process.exit();
  19. }
  20. for(var i in devices){
  21. console.log(devices[i]);
  22. if(devices[i].product){
  23. var myString = devices[i].product;
  24. if(myString.indexOf('Bar Code')>-1){
  25. console.log("found barcode reader: " + myString);
  26. myBarCode=devices[i]
  27. };
  28. }
  29. }
  30. if(myBarCode.path){
  31. var device = new HID.HID(myBarCode.path);
  32. } else {
  33. console.log("I couldn't get barcode readers path, I'm going to exit.");
  34. process.exit();
  35. }
  36. device.on("data", function(data) {
  37. var myRead = data.toString();
  38. var myArray = myRead.split("");
  39. var startLog=false;
  40. var myRes="";
  41. for (var i = 0, len = myArray.length; i < len; i++) {
  42. if(startLog){
  43. if(myArray[i]=='\u0000'){
  44. break;
  45. }
  46. myRes=myRes + myArray[i];
  47. }
  48. if(myArray[i]=='\u0000'){startLog=true;}
  49. }
  50. console.log ("I think you want '" + myRes + "'");
  51. if (myRes == "REBOOT"){exec('shutdown -r now '); return;}
  52. if (myRes == "CLOSE"){exec('pkill -f firefox '); return;}
  53. if (myRes.substring(0,3) == "MOD"){
  54. console.log("Sending Modal Selection to browsers = " + myRes);
  55. for(var i in clients){
  56. var sendMe = JSON.stringify({'type':'Modal', 'MOD':myRes});
  57. clients[i].sendUTF(sendMe);
  58. }
  59. return;
  60. }
  61. if (myRes.substring(0,9) == "WEBREBOOT"){
  62. request.get('https://icg.islandceramics.com:8081/?command=webReboot', function (error, response, body) {
  63. if (!error && response.statusCode == 200) {
  64. console.log(body);
  65. } else {
  66. console.log(error);
  67. }
  68. });
  69. return;
  70. }
  71. if (myRes.substring(0,4) == "MANT"){
  72. console.log("Sending MAINT signoff to browsers = " + myRes);
  73. for(var i in clients){
  74. var sendMe = JSON.stringify({'type':'MANT', 'MANT':myRes});
  75. clients[i].sendUTF(sendMe);
  76. }
  77. return;
  78. }
  79. if (myRes.substring(0,4) == "ROLL"){
  80. console.log("Sending Modal Selection to browsers = " + myRes);
  81. for(var i in clients){
  82. var sendMe = JSON.stringify({'type':'ROLL', 'ROLL':myRes});
  83. clients[i].sendUTF(sendMe);
  84. }
  85. return;
  86. }
  87. if (myRes.substring(0,6) == "CONFIG"){
  88. console.log("Configuration Request");
  89. exec("/sbin/ifconfig", function(error,stdout,stderr){
  90. var myMessage = error + stdout + stderr;
  91. console.log (myMessage);
  92. for(var i in clients){
  93. var sendMe = JSON.stringify({'type':'Message', 'Message':myMessage});
  94. clients[i].sendUTF(sendMe);
  95. }
  96. })
  97. return;
  98. }
  99. if (myRes.substring(0,3) == "F11"){
  100. console.log("full screen me browsers = " + myRes);
  101. exec('/usr/bin/xdotool search --name Mozilla key F11', {env: {'DISPLAY': ':0.0', 'XAUTHORITY':'/home/bhitch/.Xauthority'}}, function(error,stdout,stderr){
  102. console.log(error + "~" + stdout + "~" + stderr);
  103. });
  104. return;
  105. }
  106. if (myRes.substring(0,3) == "REL"){
  107. console.log("Shift CTL Reload = " + myRes);
  108. exec('/usr/bin/xdotool search --name Mozilla key shift+ctrl+r', {env: {'DISPLAY': ':0.0', 'XAUTHORITY':'/home/bhitch/.Xauthority'}}, function(error,stdout,stderr){
  109. console.log(error + "~" + stdout + "~" + stderr);
  110. });
  111. return;
  112. }
  113. if (myRes.substring(0,3) == "UPG"){
  114. console.log("Copy hidLabel.js from server: " + myRes);
  115. exec('/home/pi/updateAndReboot.sh');
  116. return;
  117. }
  118. if (myRes.substring(0,3) == "DOC"){
  119. console.log("Sending DocRevCheck to browsers = " + myRes);
  120. for(var i in clients){
  121. var sendMe = JSON.stringify({'type':'DocRevCheck', 'Doc':myRes});
  122. clients[i].sendUTF(sendMe);
  123. }
  124. return;
  125. }
  126. if (myRes.substring(0,3) == "LAB"){
  127. console.log("Sending Label Request to browsers = " + myRes.substring(3));
  128. for(var i in clients){
  129. var sendMe = JSON.stringify({'type':'LabelRequest', 'labelNum':myRes.substring(3)});
  130. console.log(sendMe);
  131. clients[i].sendUTF(sendMe);
  132. }
  133. return;
  134. }
  135. if (myRes.substring(0,3) == "PIC"){
  136. console.log("Sending Picture Request to browsers = " + myRes.substring(3));
  137. for(var i in clients){
  138. var sendMe = JSON.stringify({'type':'PICrequest', 'ID':myRes.substring(3)});
  139. console.log(sendMe);
  140. clients[i].sendUTF(sendMe);
  141. }
  142. return;
  143. }
  144. if (myRes.substring(0,6) == "MCLOSE"){
  145. //console.log("Sending ModalCloseRequest");
  146. for(var i in clients){
  147. var sendMe = JSON.stringify({'type':'ModalClose'});
  148. console.log(sendMe);
  149. clients[i].sendUTF(sendMe);
  150. }
  151. return;
  152. }
  153. // if we get this far it turns on whether numeric. Numeric = job, non-numeric = scanDictionary lookup
  154. if(isNumeric(myRes)){
  155. if(myRes==lastBarCode){
  156. // Print a label
  157. console.log("print a label");
  158. labelPrint(myRes);
  159. return;
  160. }
  161. lastBarCode=myRes;
  162. console.log("sending barcode to browser");
  163. for(var i in clients){
  164. var sendMe = JSON.stringify({'type':'barcode', 'barcode':myRes});
  165. clients[i].sendUTF(sendMe);
  166. }
  167. return;
  168. }
  169. console.log("Only thing left is scan dictionary");
  170. for(var i in clients){
  171. var sendMe = JSON.stringify({'type':'scanDictionary', 'lookMeUp':myRes});
  172. clients[i].sendUTF(sendMe);
  173. }
  174. return;
  175. });
  176. device.on("error", function(err) {
  177. console.log(err);
  178. });
  179. var server = http.createServer(function(request, response){
  180. response.writeHead(200, {'Content-Type': 'text/plain'});
  181. response.end("Hello There");
  182. })
  183. server.listen(8080, '127.0.0.1', function(){ console.log("Node Web Server is listening\n\n");});
  184. var clients={};
  185. var count=0;
  186. wss = new WebSocketServer({httpServer: server});
  187. wss.on('request', function(r){
  188. var connection=r.accept('barcode', r.origin);
  189. var id = count++;
  190. clients[id]= connection;
  191. connection.on('close', function(reasonCode, description){
  192. console.log("Connection Closed");
  193. delete clients[id];
  194. })
  195. connection.on('message', function(message){
  196. var myLabel2 = JSON.parse(message.utf8Data);
  197. if(myLabel2.type=="labelPrint"){
  198. fs.writeFileSync('labelOut2.zpl', myLabel2.zCode);
  199. console.log(myLabel2);
  200. printLabel('labelOut2.zpl');
  201. }
  202. })
  203. });
  204. function labelPrint(lotid){
  205. if(lotid == lastLabel) {
  206. //just print, dont request new label string
  207. printLabel('shipLabel.zpl');
  208. return;
  209. }
  210. request.get("http://192.168.10.26/ShopLabel.php?MLOT="+lotid+"&who="+whoami+"&ship=1", function (error, response, body) {
  211. if (!error && response.statusCode == 200) {
  212. console.log("Here comes the web response:");
  213. console.log(body);
  214. } else {
  215. console.log("Here comes the error:");
  216. console.log(error);
  217. }
  218. });
  219. }
  220. function printLabel(filename){
  221. console.log("trying to print label");
  222. exec('cat ' + filename + ' > /dev/usb/lp0', function(error, stdout, stderr) {
  223. });
  224. }
  225. function isNumeric(n) {
  226. return !isNaN(parseFloat(n)) && isFinite(n);
  227. }