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