Skip to content

Commit

Permalink
bump 5.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
matejkramny committed Jul 21, 2021
1 parent 881fe5f commit d0404ad
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [5.0.2]

- Add NetworkPrinterManager
## [5.0.0]

- Merge esc_pos_utils into this package
Expand Down
1 change: 1 addition & 0 deletions lib/escpos.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export './src/pos_column.dart';
export './src/pos_styles.dart';
export './src/qrcode.dart';
export './src/ticket.dart';
export './src/network_printer.dart';
77 changes: 77 additions & 0 deletions lib/src/network_printer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* esc_pos_printer
* Created by Andrey Ushakov
*
* Copyright (c) 2019-2020. All rights reserved.
* See LICENSE for distribution and usage details.
*/

import 'dart:io';
import 'ticket.dart';

class PosPrintResult {
const PosPrintResult._internal(this.value);
final int value;
static const success = PosPrintResult._internal(1);
static const timeout = PosPrintResult._internal(2);
static const printerNotSelected = PosPrintResult._internal(3);
static const ticketEmpty = PosPrintResult._internal(4);
static const printInProgress = PosPrintResult._internal(5);
static const scanInProgress = PosPrintResult._internal(6);

String get msg {
if (value == PosPrintResult.success.value) {
return 'Success';
} else if (value == PosPrintResult.timeout.value) {
return 'Error. Printer connection timeout';
} else if (value == PosPrintResult.printerNotSelected.value) {
return 'Error. Printer not selected';
} else if (value == PosPrintResult.ticketEmpty.value) {
return 'Error. Ticket is empty';
} else if (value == PosPrintResult.printInProgress.value) {
return 'Error. Another print in progress';
} else if (value == PosPrintResult.scanInProgress.value) {
return 'Error. Printer scanning in progress';
} else {
return 'Unknown error';
}
}
}

/// Printer Network Manager
class PrinterNetworkManager {
String _host = '';
int _port = 9100;
Duration _timeout = Duration(seconds: 5);

/// Select a network printer
///
/// [timeout] is used to specify the maximum allowed time to wait
/// for a connection to be established.
void selectPrinter(
String host, {
int port = 9100,
Duration timeout = const Duration(seconds: 5),
}) {
_host = host;
_port = port;
_timeout = timeout;
}

Future<PosPrintResult> printTicket(Ticket ticket) {
if (_host.isEmpty) {
return Future<PosPrintResult>.value(PosPrintResult.printerNotSelected);
} else if (ticket.bytes.isEmpty) {
return Future<PosPrintResult>.value(PosPrintResult.ticketEmpty);
}

return Socket.connect(_host, _port, timeout: _timeout)
.then((Socket socket) {
socket.add(ticket.bytes);
socket.destroy();
return Future<PosPrintResult>.value(PosPrintResult.success);
}).catchError((dynamic e) {
return Future<PosPrintResult>.value(PosPrintResult.timeout);
});
}
}
1 change: 0 additions & 1 deletion lib/src/qrcode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import 'dart:convert';

import 'commands.dart';

class QRSize {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: escpos
description: Dart library for ESC/POS with networking capability
version: 5.0.1
version: 5.0.2
homepage: https://github.com/CastawayLabs/dart-escpos
environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down

0 comments on commit d0404ad

Please sign in to comment.