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(apiUrl, payload); this.printService .init() .writeLine( 'Hello world!!' ) .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(this.apiUrl, requestData); } }