ns blue_print_service.dart

migrate_to_ns
error500 2022-01-27 09:35:05 +06:00
parent 988d6a6c6b
commit 7b06e4c390
2 changed files with 44 additions and 29 deletions

View File

@ -33,7 +33,7 @@ class BluePrintService extends BaseService {
} else {
try {
await _bluetooth.connect(_device!);
await Future.delayed(Duration(seconds: 2));
await Future.delayed(Duration(seconds: 5));
response = true;
} catch (e) {
print('Error connect $e');
@ -55,8 +55,21 @@ class BluePrintService extends BaseService {
return response;
}
Future<bool> printBytes(Uint8List bytes) async {
Future<bool> printBytes(Uint8List bytes, { int chunkSizeBytes = 20, int queueSleepTimeMs = 20 }) async {
Map<String, dynamic> config = Map();
return await _bluetooth.rawBytes(config, bytes);
final len = bytes.length;
List<List<int>> chunks = [];
for (var i = 0; i < len; i += chunkSizeBytes) {
var end = (i + chunkSizeBytes < len) ? i + chunkSizeBytes : len;
chunks.add(bytes.sublist(i, end));
}
for (var i = 0; i < chunks.length; i += 1) {
await _bluetooth.rawBytes(config, chunks[i]);
await Future.delayed(Duration(milliseconds: queueSleepTimeMs));
}
return true;
}
}

View File

@ -40,10 +40,8 @@ class _SettingPrinterViewState extends State<SettingPrinterView> {
void initState() {
super.initState();
_permission();
}
void _testPrint() async {
setState(() {
_printing = true;
@ -64,33 +62,35 @@ class _SettingPrinterViewState extends State<SettingPrinterView> {
log.i(chunkSizeBytes);
log.i(queueSleepTimeMs);
// TODO Don't forget to choose printer's paper
PaperSize paper = state.printerPaperSize == SettingPrinterPaperM80 ? PaperSize.mm80 : PaperSize.mm58;
PaperSize paper = state.printerPaperSize == SettingPrinterPaperM80
? PaperSize.mm80
: PaperSize.mm58;
if (SettingPrinterEncodingImage == state.printerEncoding) {
final bool res = await printerManager.printBytes(
Uint8List.fromList(await testTicketImage(paper)
));
Uint8List.fromList(await testTicketImage(paper)),
chunkSizeBytes: chunkSizeBytes,
queueSleepTimeMs: queueSleepTimeMs);
_dialogService.showDialog(description: 'result is $res');
} else {
final bool res = await printerManager.printBytes(
Uint8List.fromList(await printTextCheck(paper, state.printerEncoding!, exampleJson['check_text']))
);
Uint8List.fromList(await printTextCheck(
paper, state.printerEncoding!, exampleJson['check_text'])),
chunkSizeBytes: chunkSizeBytes,
queueSleepTimeMs: queueSleepTimeMs);
_dialogService.showDialog(description: 'result is $res');
}
} catch (e) {
print('ERROR');
print(e);
}
//7 sec safe disconnect
await Future.delayed(Duration(seconds: 15));
//10 sec safe disconnect
await Future.delayed(Duration(seconds: 14));
await printerManager.disconnect();
await Future.delayed(Duration(seconds: 3));
setState(() {
_printing = false;
});
}
@override
@ -117,13 +117,17 @@ class _SettingPrinterViewState extends State<SettingPrinterView> {
}),
SettingItem(
title: 'Кодировка',
name: vm.printerEncoding != null ? encoding[vm.printerEncoding] : '' ,
name: vm.printerEncoding != null
? encoding[vm.printerEncoding]
: '',
onTap: () {
_navigatorService.push(SettingsPrinterEncodingRoute);
}),
SettingItem(
title: 'Ширина ленты',
name: vm.printerPaperSize != null ? paperSize[vm.printerPaperSize] : null ,
name: vm.printerPaperSize != null
? paperSize[vm.printerPaperSize]
: null,
onTap: () {
_navigatorService.push(SettingsPrinterPaperRoute);
}),
@ -162,18 +166,18 @@ class _SettingPrinterViewState extends State<SettingPrinterView> {
if (status.isDenied || status.isPermanentlyDenied) {
DialogResponse response = await _dialogService.showConfirmationDialog(
title: 'Доступ',
description: 'Для поиска устройств Bluetooth необходимо предоставить доступ к отслеживанию геолокации.',
description:
'Для поиска устройств Bluetooth необходимо предоставить доступ к отслеживанию геолокации.',
cancelTitle: 'Нет',
confirmationTitle: 'Хорошо',
);
if (response.confirmed) {
if (await Permission.location
.request()
.isGranted) {
if (await Permission.location.request().isGranted) {
print('Granted');
} else {
_dialogService.showDialog(
description: 'Необходимо указать постоянный доступ к местоположении для поиска принтера');
description:
'Необходимо указать постоянный доступ к местоположении для поиска принтера');
_navigatorService.pop();
}
} else {
@ -184,8 +188,6 @@ class _SettingPrinterViewState extends State<SettingPrinterView> {
}
void _startInitialPrint() async {
if (Platform.isIOS) {
_testPrint();
} else {
@ -195,11 +197,11 @@ class _SettingPrinterViewState extends State<SettingPrinterView> {
if (val == 12) {
print('on');
_testPrint();
} else if (val == 10) {
print('off');
_dialogService.showDialog(
description: 'Отсутвует соеденение Bluetooth или он отключен' , title: 'Bluetooth');
description: 'Отсутвует соеденение Bluetooth или он отключен',
title: 'Bluetooth');
}
print('state is $val');
});