File

src/app/services/affichage.service.ts

Index

Properties
Methods

Constructor

constructor(printService: PrintService)
Parameters :
Name Type Optional
printService PrintService No

Methods

detectUsbPrinters
detectUsbPrinters()
Returns : void
envoyerMessage
envoyerMessage(ticket: any)
Parameters :
Name Type Optional
ticket any No
Returns : void
getData
getData(param1: string, param2: string)
Parameters :
Name Type Optional
param1 string No
param2 string No
Returns : void
requestUsb
requestUsb()
Returns : void

Properties

ImprimStatut
Type : boolean
Default value : false
status
Type : boolean
Default value : false
usbPrintDrivers
Type : UsbDriver[]
Default value : []
usbPrintersToDetect
Type : string[]
Default value : []
import { Injectable } from '@angular/core';
import { PrintService, UsbDriver, WebPrintDriver } from 'ng-thermal-print';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root',
})
export class AffichageService {
  usbPrintDrivers: UsbDriver[] = []; // Tableau pour stocker les pilotes USB disponibles
  status: boolean = false;
  usbPrintersToDetect: string[] = [];
  ImprimStatut: boolean = false;

  constructor(
    private printService: PrintService
    // private http: HttpClient
  ) {}

  detectUsbPrinters() {
    const usbPrintersToDetect = [
      { vendorId: 0x04b8, productId: 0x0202 }, // Epson TM-U220B M188B
      { vendorId: 0x1208, productId: 0x0780 }, // Exemple de second périphérique
      { vendorId: 0x1208, productId: 0x0514 }, // Exemple de second périphérique

      // { vendorId: 0x4616, productId: 0x1920 }, // Exemple de second périphérique
    ];
    let printerFound = false; // Variable pour garder une trace de l'état de la connexion
    console.log('Imprimante non trouvée');
    console.log(printerFound);
    this.ImprimStatut = false;

    for (const printerInfo of usbPrintersToDetect) {
      console.log(this.usbPrintersToDetect);
      console.log(printerInfo);
      const usbDriver = new UsbDriver(
        printerInfo.vendorId,
        printerInfo.productId
      );
      usbDriver.isConnected.subscribe(result => {
        if (result && !printerFound) {
          // Vérifiez que l'imprimante n'a pas encore été trouvée
          console.log(
            `Connected to USB printer - Vendor ID: ${printerInfo.vendorId}, Product ID: ${printerInfo.productId}`
          );
          this.status = true;
          this.printService.setDriver(usbDriver);
          printerFound = true; // Définir la variable pour indiquer que l'imprimante a été trouvée
          this.ImprimStatut = true;
          console.log('Imprimante trouvée');
          console.log(printerFound);
        }
      });

      // Ajouter le pilote USB à la liste
      this.usbPrintDrivers.push(usbDriver);

      // Déclencher la connexion pour chaque pilote USB
      usbDriver.connect();

      // Sortir de la boucle si l'imprimante a été trouvée
      if (printerFound) {
        break;
      }
    }
  }

  requestUsb() {
    const usbDriver = new UsbDriver();
    usbDriver.requestUsb().subscribe(
      result => {
        this.printService.setDriver(usbDriver, 'ESC/POS');
        this.detectUsbPrinters();
        // this.reloadComponent();
      },
      error => {
        console.log(error);
      }
    );
  }

  envoyerMessage(ticket: any) {
    const panier = ticket;
    const payload = { user_id: 101293 };
    const apiUrl = `http://127.0.0.1:8000/api/ticket/${panier}`;
    // return this.http.post<boolean>(apiUrl, payload);

    this.printService
      .init()
      .writeLine(
        '<epos-display xmlns="http://www.epson-pos.com/schemas/2012/09/epos-display"><text>Hello world!!</text></epos-display>'
      )
      .flush();
  }

  getData(param1: string, param2: string) {
    const requestData = {
      user_id: param1,
    };
    // console.log(param3)
    // this.apiUrl =`http://127.0.0.1:8000/api/ticket/${param2}`
    // return this.http.post<boolean>(this.apiUrl, requestData);
  }
}

results matching ""

    No results matching ""