ICGLabelClient.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const WebSocket = require('ws');
  2. var exec = require('child_process').exec;
  3. const fs = require("fs");
  4. var whoami;
  5. try{
  6. var tempObj = fs.readFileSync('whoami');
  7. tempRead = JSON.parse(tempObj);
  8. whoami = tempRead.Name;
  9. console.log("I am '" + whoami + "'");
  10. } catch (e) {
  11. console.log("I could not open whoami, I'm going to exit");
  12. process.exit();
  13. }
  14. var ws;
  15. var wsConnect = function(){
  16. ws = new WebSocket('ws://10.10.10.2:8080/');
  17. ws.on('open', function open() {
  18. console.log("connected");
  19. })
  20. ws.on('message', function incoming(data, flags) {
  21. console.log("Received : " + data);
  22. try{
  23. myObj = JSON.parse(data);
  24. console.log(myObj);
  25. console.log(myObj.labelSource);
  26. if(whoami == myObj.who){
  27. console.log("I am " + whoami + ", this message was for me");
  28. if(myObj.label){
  29. fs.writeFileSync('internetShipLabel.zpl', myObj.label);
  30. printLabel('internetShipLabel.zpl');
  31. }
  32. }else{
  33. console.log("this Message was not for me");
  34. }
  35. } catch(e){console.log(e);}
  36. })
  37. ws.on('error', function(error){
  38. console.log('Connection Error: ' + error.toString());
  39. //process.exit();
  40. });
  41. ws.on('close', function() {
  42. console.log('socket close');
  43. setTimeout(wsConnect, 30*1000);
  44. });
  45. }
  46. function printLabel(filename){
  47. console.log('trying to print');
  48. exec('cat ' + filename + ' > /dev/usb/lp0', function(error, stdout, stderr) {
  49. });
  50. }
  51. wsConnect();