migrate ns-printer
parent
f29d9edb22
commit
988d6a6c6b
|
|
@ -1,6 +1,7 @@
|
|||
|
||||
|
||||
import 'package:aman_kassa_flutter/core/services/DataService.dart';
|
||||
import 'package:aman_kassa_flutter/core/services/blue_print_service.dart';
|
||||
|
||||
import '../core/services/DbService.dart';
|
||||
|
||||
|
|
@ -25,6 +26,8 @@ class LocatorInjector {
|
|||
locator.registerLazySingleton<DialogService>(() => DialogService());
|
||||
_log.d('Initializing DbService Service');
|
||||
locator.registerLazySingleton<DbService>(() => DbService.instance);
|
||||
_log.d('Initializing BluePrintService Service');
|
||||
locator.registerLazySingleton<BluePrintService>(() => BluePrintService());
|
||||
|
||||
|
||||
// depencies
|
||||
|
|
|
|||
|
|
@ -0,0 +1,62 @@
|
|||
import 'dart:typed_data';
|
||||
|
||||
import 'package:aman_kassa_flutter/core/base/base_service.dart';
|
||||
import 'package:bluetooth_print/bluetooth_print.dart';
|
||||
import 'package:bluetooth_print/bluetooth_print_model.dart';
|
||||
|
||||
|
||||
|
||||
class BluePrintService extends BaseService {
|
||||
BluetoothPrint _bluetooth = BluetoothPrint.instance;
|
||||
BluetoothDevice? _device;
|
||||
|
||||
Future<void> scan() async {
|
||||
await _bluetooth.startScan(timeout: Duration(seconds: 4));
|
||||
}
|
||||
|
||||
Future<void> stopScan() async {
|
||||
await _bluetooth.stopScan();
|
||||
}
|
||||
|
||||
Stream<List<BluetoothDevice>> get scanResult => _bluetooth.scanResults;
|
||||
|
||||
|
||||
Stream<bool> get isScanning => _bluetooth.isScanning;
|
||||
Stream<int> get state => _bluetooth.state;
|
||||
set device(BluetoothDevice device) => _device = device;
|
||||
|
||||
|
||||
Future<bool> connect() async {
|
||||
bool response = false;
|
||||
if (_device == null) {
|
||||
response = false;
|
||||
} else {
|
||||
try {
|
||||
await _bluetooth.connect(_device!);
|
||||
await Future.delayed(Duration(seconds: 2));
|
||||
response = true;
|
||||
} catch (e) {
|
||||
print('Error connect $e');
|
||||
response = false;
|
||||
}
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
Future<bool> disconnect() async {
|
||||
bool response = false;
|
||||
try {
|
||||
await _bluetooth.disconnect();
|
||||
response = true;
|
||||
} catch (e) {
|
||||
print('Error $e');
|
||||
response = false;
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
Future<bool> printBytes(Uint8List bytes) async {
|
||||
Map<String, dynamic> config = Map();
|
||||
return await _bluetooth.rawBytes(config, bytes);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
import 'package:aman_kassa_flutter/redux/constants/setting_const.dart';
|
||||
import 'package:aman_kassa_flutter/redux/state/setting_state.dart';
|
||||
import 'package:bluetooth_print/bluetooth_print_model.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:redux/redux.dart';
|
||||
import 'package:redux_thunk/redux_thunk.dart';
|
||||
import 'package:flutter_bluetooth_basic/src/bluetooth_device.dart';
|
||||
import '../store.dart';
|
||||
|
||||
@immutable
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import 'package:aman_kassa_flutter/redux/constants/setting_const.dart';
|
||||
import 'package:bluetooth_print/bluetooth_print_model.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:flutter_bluetooth_basic/src/bluetooth_device.dart';
|
||||
|
||||
@immutable
|
||||
class SettingState {
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
import 'package:aman_kassa_flutter/core/locator.dart';
|
||||
import 'package:aman_kassa_flutter/core/models/check_image_modal.dart';
|
||||
import 'package:aman_kassa_flutter/core/models/dialog_models.dart';
|
||||
import 'package:aman_kassa_flutter/core/route_names.dart';
|
||||
import 'package:aman_kassa_flutter/core/services/blue_print_service.dart';
|
||||
import 'package:aman_kassa_flutter/core/services/dialog_service.dart';
|
||||
import 'package:aman_kassa_flutter/core/services/navigator_service.dart';
|
||||
import 'package:aman_kassa_flutter/redux/constants/setting_const.dart';
|
||||
|
|
@ -13,11 +15,10 @@ import 'package:aman_kassa_flutter/shared/app_colors.dart';
|
|||
import 'package:aman_kassa_flutter/shared/ui_helpers.dart';
|
||||
import 'package:aman_kassa_flutter/views/settings/printer/PrinterTest.dart';
|
||||
import 'package:aman_kassa_flutter/widgets/fields/busy_button_icon.dart';
|
||||
import 'package:esc_pos_bluetooth/esc_pos_bluetooth.dart';
|
||||
import 'package:bluetooth_print/bluetooth_print_model.dart';
|
||||
import 'package:esc_pos_utils/esc_pos_utils.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart';
|
||||
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
|
||||
import 'package:vocsy_esys_flutter_share/vocsy_esys_flutter_share.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
|
@ -32,11 +33,10 @@ class ImageShowContainer extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _ImageShowContainerState extends State<ImageShowContainer> {
|
||||
final PrinterBluetoothManager printerManager = PrinterBluetoothManager();
|
||||
final BluePrintService printerManager = locator<BluePrintService>();
|
||||
final DialogService _dialogService = locator<DialogService>();
|
||||
final BluetoothDevice? printerBtDevice =
|
||||
Redux.store!.state.settingState!.printerBT;
|
||||
final BluetoothManager bluetoothManager = BluetoothManager.instance;
|
||||
|
||||
bool _printing = false;
|
||||
|
||||
|
|
@ -44,7 +44,7 @@ class _ImageShowContainerState extends State<ImageShowContainer> {
|
|||
if (Platform.isIOS) {
|
||||
_print();
|
||||
} else {
|
||||
bluetoothManager.state.listen((val) {
|
||||
printerManager.state.listen((val) {
|
||||
print("state = $val");
|
||||
if (!mounted) return;
|
||||
if (val == 12) {
|
||||
|
|
@ -82,32 +82,32 @@ class _ImageShowContainerState extends State<ImageShowContainer> {
|
|||
_printing = true;
|
||||
});
|
||||
try {
|
||||
printerManager.selectPrinter(PrinterBluetooth(state.printerBT!));
|
||||
printerManager.device = state.printerBT!;
|
||||
await printerManager.connect();
|
||||
PaperSize paper = state.printerPaperSize == SettingPrinterPaperM80
|
||||
? PaperSize.mm80
|
||||
: PaperSize.mm58;
|
||||
if (SettingPrinterEncodingImage == state.printerEncoding) {
|
||||
final PosPrintResult res = await printerManager.printTicket(
|
||||
await printImageCheck(paper, widget.showModel.data!.base64Data!),
|
||||
chunkSizeBytes: chunkSizeBytes,
|
||||
queueSleepTimeMs: queueSleepTimeMs);
|
||||
if (res.value != 1) {
|
||||
_dialogService.showDialog(description: res.msg);
|
||||
final bool res = await printerManager.printBytes(
|
||||
Uint8List.fromList(await printImageCheck(paper, widget.showModel.data!.base64Data!)));
|
||||
if (!res) {
|
||||
_dialogService.showDialog(description: 'Ошибка при печати');
|
||||
}
|
||||
} else {
|
||||
final PosPrintResult res = await printerManager.printTicket(
|
||||
await printTextCheck(paper, state.printerEncoding!,
|
||||
jsonDecode(widget.showModel.data!.textData!)),
|
||||
chunkSizeBytes: chunkSizeBytes,
|
||||
queueSleepTimeMs: queueSleepTimeMs);
|
||||
if (res.value != 1) {
|
||||
_dialogService.showDialog(description: res.msg);
|
||||
final bool res = await printerManager.printBytes(
|
||||
Uint8List.fromList(await printTextCheck(paper, state.printerEncoding!,
|
||||
jsonDecode(widget.showModel.data!.textData!)))
|
||||
);
|
||||
if (!res) {
|
||||
_dialogService.showDialog(description: 'Ошибка при печати');
|
||||
}
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
await Future.delayed(Duration(seconds: 7));
|
||||
await Future.delayed(Duration(seconds: 15));
|
||||
await printerManager.disconnect();
|
||||
setState(() {
|
||||
_printing = false;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,22 +1,13 @@
|
|||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:aman_kassa_flutter/core/logger.dart';
|
||||
import 'package:aman_kassa_flutter/redux/actions/setting_actions.dart';
|
||||
import 'package:aman_kassa_flutter/redux/store.dart';
|
||||
|
||||
import 'package:esc_pos_bluetooth/esc_pos_bluetooth.dart';
|
||||
import 'package:esc_pos_utils/esc_pos_utils.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart' hide Image;
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
|
||||
import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
|
||||
import '../data/settings_envi.dart';
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,20 +1,11 @@
|
|||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:aman_kassa_flutter/core/logger.dart';
|
||||
import 'package:aman_kassa_flutter/redux/actions/setting_actions.dart';
|
||||
import 'package:aman_kassa_flutter/redux/store.dart';
|
||||
|
||||
import 'package:esc_pos_bluetooth/esc_pos_bluetooth.dart';
|
||||
import 'package:esc_pos_utils/esc_pos_utils.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart' hide Image;
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
|
||||
import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
|
||||
import '../data/settings_envi.dart';
|
||||
|
|
|
|||
|
|
@ -1,25 +1,16 @@
|
|||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:aman_kassa_flutter/core/locator.dart';
|
||||
import 'package:aman_kassa_flutter/core/logger.dart';
|
||||
import 'package:aman_kassa_flutter/core/services/blue_print_service.dart';
|
||||
import 'package:aman_kassa_flutter/redux/actions/setting_actions.dart';
|
||||
import 'package:aman_kassa_flutter/redux/store.dart';
|
||||
import 'package:bluetooth_print/bluetooth_print_model.dart';
|
||||
|
||||
import 'package:esc_pos_bluetooth/esc_pos_bluetooth.dart';
|
||||
import 'package:esc_pos_utils/esc_pos_utils.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart' hide Image;
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
|
||||
import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
|
||||
import '../PrinterTest.dart';
|
||||
|
||||
|
||||
class PrinterSelectView extends StatefulWidget {
|
||||
PrinterSelectView({Key? key, this.title}) : super(key: key);
|
||||
final String? title;
|
||||
|
|
@ -30,16 +21,14 @@ class PrinterSelectView extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _PrinterSelectViewState extends State<PrinterSelectView> {
|
||||
PrinterBluetoothManager printerManager = PrinterBluetoothManager();
|
||||
List<PrinterBluetooth> _devices = [];
|
||||
BluePrintService printerManager = locator<BluePrintService>();
|
||||
List<BluetoothDevice> _devices = [];
|
||||
Logger _logger = getLogger('PrinterSelectView');
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
printerManager.scanResults.listen((devices) async {
|
||||
// print('UI: Devices found ${devices.length}');
|
||||
printerManager.scanResult.listen((devices) async {
|
||||
setState(() {
|
||||
_devices = devices;
|
||||
});
|
||||
|
|
@ -51,15 +40,15 @@ class _PrinterSelectViewState extends State<PrinterSelectView> {
|
|||
setState(() {
|
||||
_devices = [];
|
||||
});
|
||||
printerManager.startScan(Duration(seconds: 4));
|
||||
printerManager.scan();
|
||||
}
|
||||
|
||||
void _stopScanDevices() {
|
||||
printerManager.stopScan();
|
||||
}
|
||||
|
||||
void _selectPrinter(PrinterBluetooth printer, BuildContext context, ) async {
|
||||
printerManager.selectPrinter(printer);
|
||||
void _selectPrinter(BluetoothDevice printer, BuildContext context, ) async {
|
||||
printerManager.device = printer;
|
||||
_logger.i(printer.name);
|
||||
_logger.i(printer.address);
|
||||
|
||||
|
|
@ -71,27 +60,6 @@ class _PrinterSelectViewState extends State<PrinterSelectView> {
|
|||
await Redux.store!.dispatch(selectPrinterFromSetting(device));
|
||||
Navigator.of(context).pop(false);
|
||||
}
|
||||
|
||||
void _testPrint(PrinterBluetooth printer) async {
|
||||
printerManager.selectPrinter(printer);
|
||||
|
||||
// TODO Don't forget to choose printer's paper
|
||||
const PaperSize paper = PaperSize.mm58;
|
||||
|
||||
// TEST PRINT
|
||||
// final PosPrintResult res =
|
||||
// await printerManager.printTicket(await testTicket(paper), queueSleepTimeMs: 50);
|
||||
|
||||
final PosPrintResult res =
|
||||
await printerManager.printTicket(
|
||||
await testTicketImage(paper),
|
||||
chunkSizeBytes: 1024,
|
||||
queueSleepTimeMs: 50
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
final key = GlobalKey();
|
||||
|
||||
@override
|
||||
|
|
@ -140,7 +108,7 @@ class _PrinterSelectViewState extends State<PrinterSelectView> {
|
|||
);
|
||||
}),
|
||||
floatingActionButton: StreamBuilder<bool>(
|
||||
stream: printerManager.isScanningStream,
|
||||
stream: printerManager.isScanning,
|
||||
initialData: false,
|
||||
builder: (c, snapshot) {
|
||||
if (snapshot.data != null) {
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@ import 'dart:io';
|
|||
import 'dart:typed_data';
|
||||
|
||||
import 'package:aman_kassa_flutter/core/models/dialog_models.dart';
|
||||
import 'package:aman_kassa_flutter/core/services/blue_print_service.dart';
|
||||
import 'package:aman_kassa_flutter/widgets/fields/busy_button_icon.dart';
|
||||
import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart';
|
||||
import 'package:aman_kassa_flutter/core/locator.dart';
|
||||
import 'package:aman_kassa_flutter/core/logger.dart';
|
||||
import 'package:aman_kassa_flutter/core/route_names.dart';
|
||||
|
|
@ -12,9 +12,6 @@ import 'package:aman_kassa_flutter/core/services/navigator_service.dart';
|
|||
import 'package:aman_kassa_flutter/redux/constants/setting_const.dart';
|
||||
import 'package:aman_kassa_flutter/redux/state/setting_state.dart';
|
||||
import 'package:aman_kassa_flutter/redux/store.dart';
|
||||
import 'package:aman_kassa_flutter/shared/app_colors.dart';
|
||||
import 'package:aman_kassa_flutter/widgets/fields/aman_icon_button_horizontal.dart';
|
||||
import 'package:esc_pos_bluetooth/esc_pos_bluetooth.dart';
|
||||
import 'package:esc_pos_utils/esc_pos_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
|
|
@ -34,8 +31,7 @@ class SettingPrinterView extends StatefulWidget {
|
|||
class _SettingPrinterViewState extends State<SettingPrinterView> {
|
||||
NavigatorService _navigatorService = locator<NavigatorService>();
|
||||
final DialogService _dialogService = locator<DialogService>();
|
||||
final PrinterBluetoothManager printerManager = PrinterBluetoothManager();
|
||||
final BluetoothManager bluetoothManager = BluetoothManager.instance;
|
||||
final BluePrintService printerManager = locator<BluePrintService>();
|
||||
final Logger log = getLogger('SettingPrinterView');
|
||||
|
||||
bool _printing = false;
|
||||
|
|
@ -54,7 +50,8 @@ class _SettingPrinterViewState extends State<SettingPrinterView> {
|
|||
});
|
||||
try {
|
||||
final SettingState state = Redux.store!.state.settingState!;
|
||||
printerManager.selectPrinter(PrinterBluetooth(state.printerBT!));
|
||||
printerManager.device = state.printerBT!;
|
||||
await printerManager.connect();
|
||||
bool isIos = Platform.isIOS;
|
||||
int chunkSizeBytes = 3096;
|
||||
int queueSleepTimeMs = 100;
|
||||
|
|
@ -67,31 +64,29 @@ 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;
|
||||
if (SettingPrinterEncodingImage == state.printerEncoding) {
|
||||
final PosPrintResult res = await printerManager.printTicket(
|
||||
await testTicketImage(paper),
|
||||
chunkSizeBytes: chunkSizeBytes,
|
||||
queueSleepTimeMs: queueSleepTimeMs
|
||||
);
|
||||
_dialogService.showDialog(description: res.msg);
|
||||
final bool res = await printerManager.printBytes(
|
||||
Uint8List.fromList(await testTicketImage(paper)
|
||||
));
|
||||
_dialogService.showDialog(description: 'result is $res');
|
||||
} else {
|
||||
final PosPrintResult res = await printerManager.printTicket(
|
||||
await printTextCheck(paper, state.printerEncoding!, exampleJson['check_text']),
|
||||
chunkSizeBytes: chunkSizeBytes,
|
||||
queueSleepTimeMs: queueSleepTimeMs
|
||||
final bool res = await printerManager.printBytes(
|
||||
Uint8List.fromList(await printTextCheck(paper, state.printerEncoding!, exampleJson['check_text']))
|
||||
);
|
||||
_dialogService.showDialog(description: res.msg);
|
||||
_dialogService.showDialog(description: 'result is $res');
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
print('ERROR');
|
||||
print(e);
|
||||
}
|
||||
|
||||
//7 sec safe disconnect
|
||||
await Future.delayed(Duration(seconds: 7));
|
||||
|
||||
await Future.delayed(Duration(seconds: 15));
|
||||
await printerManager.disconnect();
|
||||
setState(() {
|
||||
_printing = false;
|
||||
});
|
||||
|
|
@ -194,7 +189,7 @@ class _SettingPrinterViewState extends State<SettingPrinterView> {
|
|||
if (Platform.isIOS) {
|
||||
_testPrint();
|
||||
} else {
|
||||
bluetoothManager.state.listen((val) {
|
||||
printerManager.state.listen((val) {
|
||||
print("state = $val");
|
||||
if (!mounted) return;
|
||||
if (val == 12) {
|
||||
|
|
|
|||
23
pubspec.lock
23
pubspec.lock
|
|
@ -29,6 +29,13 @@ packages:
|
|||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "4.2.0"
|
||||
bluetooth_print:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
path: "../bluetooth_print"
|
||||
relative: true
|
||||
source: path
|
||||
version: "3.0.1"
|
||||
boolean_selector:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
|
@ -113,13 +120,6 @@ packages:
|
|||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.0.3"
|
||||
esc_pos_bluetooth:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: esc_pos_bluetooth
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.4.1"
|
||||
esc_pos_utils:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
|
|
@ -160,13 +160,6 @@ packages:
|
|||
description: flutter
|
||||
source: sdk
|
||||
version: "0.0.0"
|
||||
flutter_bluetooth_basic:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: flutter_bluetooth_basic
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.1.7"
|
||||
flutter_lock_screen:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
|
|
@ -498,7 +491,7 @@ packages:
|
|||
name: rxdart
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.26.0"
|
||||
version: "0.27.3"
|
||||
shared_preferences:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
|
|
|
|||
|
|
@ -34,10 +34,12 @@ dependencies:
|
|||
shared_preferences: ^2.0.12
|
||||
flutter_lock_screen: ^2.0.1
|
||||
local_auth: ^1.1.10
|
||||
esc_pos_bluetooth: ^0.4.1
|
||||
esc_pos_utils: ^1.1.0 # no edit for esc_pos_bluetooth: ^0.2.8
|
||||
#esc_pos_bluetooth: ^0.4.1
|
||||
esc_pos_utils: ^1.1.0
|
||||
charset_converter: ^2.0.0
|
||||
permission_handler: ^8.3.0
|
||||
bluetooth_print:
|
||||
path: ../bluetooth_print
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
|
|
|
|||
Loading…
Reference in New Issue