printer.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import Vue from "vue";
  2. import { ConnectedDevice, Lifecycle } from "@psdk/frame-father";
  3. import { CPCL, GenericCPCL } from "@psdk/cpcl";
  4. import { GenericTSPL, TSPL } from "@psdk/tspl";
  5. import { ESC, GenericESC } from "@psdk/esc";
  6. class Printer {
  7. private _connectedDevice?: ConnectedDevice;
  8. private _cpcl?: GenericCPCL;
  9. private _tspl?: GenericTSPL;
  10. private _esc?: GenericESC;
  11. init(connectedDevice: ConnectedDevice) {
  12. this._connectedDevice = connectedDevice;
  13. const lifecycle = new Lifecycle(connectedDevice);
  14. this._cpcl = CPCL.generic(lifecycle);
  15. this._tspl = TSPL.generic(lifecycle);
  16. this._esc = ESC.generic(lifecycle);
  17. }
  18. isConnected(): boolean {
  19. return this._connectedDevice != null;
  20. }
  21. connectedDevice(): ConnectedDevice | undefined {
  22. return this._connectedDevice;
  23. }
  24. cpcl(): GenericCPCL {
  25. if (!this._connectedDevice) throw Error("The device is not connected");
  26. return this._cpcl!;
  27. }
  28. tspl(): GenericTSPL {
  29. if (!this._connectedDevice) throw Error("The device is not connected");
  30. return this._tspl!;
  31. }
  32. esc(): GenericESC {
  33. if (!this._connectedDevice) throw Error("The device is not connected");
  34. return this._esc!;
  35. }
  36. }
  37. export default {
  38. install: function () {
  39. Vue.prototype.$printer = new Printer();
  40. },
  41. };