fix print
This commit is contained in:
parent
0bccec8a1e
commit
f4cdfcb96f
@ -31,10 +31,10 @@ Future<void> onPrint(
|
||||
await PrinterLocalDatasource.instance.getPrinterByCode('checker');
|
||||
final kitchenPrinter =
|
||||
await PrinterLocalDatasource.instance.getPrinterByCode('kitchen');
|
||||
final barPrinter =
|
||||
await PrinterLocalDatasource.instance.getPrinterByCode('bar');
|
||||
final receiptPrinter =
|
||||
await PrinterLocalDatasource.instance.getPrinterByCode('receipt');
|
||||
final barPrinter =
|
||||
await PrinterLocalDatasource.instance.getPrinterByCode('bar');
|
||||
|
||||
if (receiptPrinter != null) {
|
||||
try {
|
||||
|
||||
@ -11,7 +11,7 @@ class PrinterService {
|
||||
factory PrinterService() => _instance;
|
||||
PrinterService._internal();
|
||||
|
||||
final Lock _lock = Lock();
|
||||
final Map<String, Lock> _locks = {};
|
||||
String? _connectedMac;
|
||||
|
||||
/// Connect to Bluetooth printer
|
||||
@ -60,13 +60,15 @@ class PrinterService {
|
||||
}
|
||||
}
|
||||
|
||||
/// Print with automatic printer type detection
|
||||
Lock _getLock(String address) {
|
||||
return _locks.putIfAbsent(address, () => Lock());
|
||||
}
|
||||
|
||||
Future<bool> printWithPrinter(
|
||||
PrintModel printer, List<int> printData, BuildContext context) async {
|
||||
if (printer.type == 'Bluetooth') {
|
||||
return await _lock.synchronized(() async {
|
||||
return await _getLock(printer.address).synchronized(() async {
|
||||
try {
|
||||
// Connect hanya kalau beda printer atau belum connected
|
||||
bool isConnected = await PrintBluetoothThermal.connectionStatus;
|
||||
if (!isConnected || _connectedMac != printer.address) {
|
||||
if (isConnected) {
|
||||
@ -84,25 +86,25 @@ class PrinterService {
|
||||
return false;
|
||||
}
|
||||
_connectedMac = printer.address;
|
||||
await Future.delayed(const Duration(milliseconds: 500));
|
||||
await Future.delayed(
|
||||
const Duration(milliseconds: 1000)); // naikkan dari 500ms
|
||||
}
|
||||
|
||||
// Print
|
||||
bool result = await PrintBluetoothThermal.writeBytes(printData);
|
||||
bool result = await _writeBytesChunked(printData);
|
||||
|
||||
// Beri waktu printer flush sebelum job berikutnya
|
||||
await Future.delayed(const Duration(milliseconds: 1000));
|
||||
|
||||
log("Print result ${printer.name}: $result");
|
||||
return result;
|
||||
} catch (e, stackTrace) {
|
||||
_connectedMac = null;
|
||||
FirebaseCrashlytics.instance.recordError(
|
||||
e,
|
||||
stackTrace,
|
||||
reason: 'Error printing ${printer.name}',
|
||||
information: [
|
||||
'Printer: ${printer.name}',
|
||||
'MAC: ${printer.address}'
|
||||
],
|
||||
);
|
||||
log("Error printing ${printer.name}: $e");
|
||||
FirebaseCrashlytics.instance.recordError(e, stackTrace,
|
||||
reason: 'Error printing ${printer.name}',
|
||||
information: [
|
||||
'Printer: ${printer.name}',
|
||||
'MAC: ${printer.address}'
|
||||
]);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
@ -111,6 +113,18 @@ class PrinterService {
|
||||
}
|
||||
}
|
||||
|
||||
// Kirim data per chunk untuk hindari buffer overflow Bluetooth
|
||||
Future<bool> _writeBytesChunked(List<int> data, {int chunkSize = 512}) async {
|
||||
for (int i = 0; i < data.length; i += chunkSize) {
|
||||
final end = (i + chunkSize > data.length) ? data.length : i + chunkSize;
|
||||
final chunk = data.sublist(i, end);
|
||||
bool result = await PrintBluetoothThermal.writeBytes(chunk);
|
||||
if (!result) return false;
|
||||
await Future.delayed(const Duration(milliseconds: 30));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Print using Bluetooth printer
|
||||
Future<bool> printBluetooth(List<int> printData) async {
|
||||
try {
|
||||
|
||||
@ -1831,7 +1831,7 @@ class PrintDataoutputs {
|
||||
: '--------------------------------',
|
||||
styles: const PosStyles(bold: false, align: PosAlign.center));
|
||||
|
||||
paper == 80 ? bytes += generator.feed(3) : bytes += generator.feed(1);
|
||||
paper == 80 ? bytes += generator.feed(3) : bytes += generator.feed(2);
|
||||
|
||||
if (paper == 80) {
|
||||
bytes += generator.cut();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user