PrintEpcUtil.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * node-red EPC 发卡程序
  3. */
  4. var Uuid = require('../../common/Uuid.js').default;
  5. export default {
  6. webSocket: '', // websocket对象
  7. socketUrl: 'ws://127.0.0.1:18800/api/ws/print', // websocket客户端地址
  8. /**
  9. * 打印单张EPC
  10. */
  11. printSingleEpc: function (printerName, epc) {
  12. if (epc == null || epc.length == 0) {
  13. let promise = new Promise((resolve, reject) => {
  14. reject({
  15. success: false,
  16. message: '打印数据中没有EPC的定义。',
  17. });
  18. });
  19. return promise;
  20. }
  21. var _self = this;
  22. var commandData = {
  23. id: Uuid.createUUID(),
  24. printerName: printerName,
  25. command: 'writeEpc',
  26. epc: epc,
  27. };
  28. var commandStr = JSON.stringify(commandData);
  29. let promise = new Promise((resolve, reject) => {
  30. _self.connectNodeRed(resolve, reject, commandStr);
  31. });
  32. return promise;
  33. },
  34. readEpc: function (printerName) {
  35. var _self = this;
  36. var commandData = {
  37. id: Uuid.createUUID(),
  38. printerName: printerName,
  39. command: 'readEpc',
  40. };
  41. var commandStr = JSON.stringify(commandData);
  42. var promise = new Promise((resolve, reject) => {
  43. _self.connectNodeRed(resolve, reject, commandStr, null);
  44. });
  45. return promise;
  46. },
  47. /**
  48. * 恢复标签,清除密码区域
  49. */
  50. resetTag: function(printerName){
  51. var _self = this;
  52. var commandData = {
  53. id: Uuid.createUUID(),
  54. printerName: printerName,
  55. command: 'resetTag',
  56. };
  57. var commandStr = JSON.stringify(commandData);
  58. var promise = new Promise((resolve, reject) => {
  59. _self.connectNodeRed(resolve, reject, commandStr, null);
  60. });
  61. return promise;
  62. },
  63. // 连接node red的websocket
  64. connectNodeRed: function (resolve, reject, commandStr) {
  65. var _self = this;
  66. _self.webSocket = new WebSocket(this.socketUrl);
  67. _self.webSocket.onopen = function (event) {
  68. console.log('打印 Websocket 连接成功。');
  69. if (commandStr != null && commandStr.length > 0) {
  70. _self.sendCommand(commandStr);
  71. } else {
  72. _self.close();
  73. }
  74. };
  75. _self.webSocket.onclose = function (event) {
  76. console.log('打印 Websocket 断开连接。');
  77. };
  78. _self.webSocket.onerror = function (event) {
  79. console.log('打印 Websocket 出错。');
  80. _self.close();
  81. reject({
  82. success: false,
  83. message: '打印 Websocket 出错。' + event.data,
  84. });
  85. };
  86. _self.webSocket.onmessage = function (event) {
  87. var data = JSON.parse(event.data);
  88. console.log(data);
  89. resolve(data);
  90. _self.close();
  91. };
  92. },
  93. /**
  94. * WebSocket 发送指令
  95. * @param {指令} command
  96. */
  97. sendCommand: function (command) {
  98. if (this.webSocket) {
  99. this.webSocket.send(command);
  100. }
  101. },
  102. /**
  103. * 关闭Websocket
  104. */
  105. close: function () {
  106. if (this.webSocket) {
  107. this.webSocket.close();
  108. this.webSocket = null;
  109. };
  110. },
  111. };