You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.1 KiB

3 years ago
import 'dart:async';
3 years ago
import 'package:dj_printer/src/status_enum.dart';
3 years ago
import 'package:flutter/services.dart';
class DjPrinter {
3 years ago
static late final DjPrinter _instance = DjPrinter._();
DjPrinter._();
factory DjPrinter() => _instance;
3 years ago
static const MethodChannel _channel = MethodChannel('dj_printer');
3 years ago
static const EventChannel _deviceChannel =
EventChannel("com.discovery.devices");
3 years ago
StreamSubscription? _discoveryStream;
3 years ago
3 years ago
Future<StreamSubscription> addDiscoveryListen(
3 years ago
{required void Function(dynamic data) onReceive,
void Function()? onStart,
void Function()? onFinish}) async {
if (_discoveryStream == null) {
return _deviceChannel.receiveBroadcastStream().listen((data) {
if (data == "start" && onStart != null) {
onStart();
} else if (data == "finish" && onFinish != null) {
onFinish();
} else {
onReceive(data);
}
});
} else {
return _discoveryStream!;
}
}
3 years ago
void cancelDiscovery() {
3 years ago
if (_discoveryStream != null) {
_discoveryStream!.cancel();
_discoveryStream = null;
}
}
static const EventChannel _connectChannel = EventChannel("com.connect");
3 years ago
StreamSubscription? _connectStream;
3 years ago
3 years ago
Future<StreamSubscription> addConnectListen(
3 years ago
{required void Function() onConnect,
required void Function() onDisconnect}) async {
if (_connectStream == null) {
return _connectChannel.receiveBroadcastStream().listen((data) {
if (data == 'connected') {
onConnect();
} else if (data == 'disconnected') {
onDisconnect();
}
});
} else {
return _connectStream!;
}
}
3 years ago
void cancelConnect() {
3 years ago
if (_connectStream != null) {
_connectStream!.cancel();
_connectStream = null;
}
}
3 years ago
Future<bool?> get startSearch async {
3 years ago
final res = await _channel.invokeMethod('startSearch');
return res;
}
3 years ago
Future<bool?> connect(String address) async {
3 years ago
final res = await _channel.invokeMethod('connect', {'address': address});
return res;
}
3 years ago
Future<bool?> init() async {
3 years ago
final res = await _channel.invokeMethod('init');
return res;
}
//0 normal
//1 busy
//2 paper empty
//4 cover open
//8 battery low
3 years ago
Future<PRINT_STATUS?> getStatus() async {
3 years ago
final res = await _channel.invokeMethod('getStatus');
switch (res) {
case 0:
return PRINT_STATUS.normal;
case 1:
return PRINT_STATUS.busy;
case 2:
return PRINT_STATUS.paperEmpty;
case 4:
return PRINT_STATUS.coverOpen;
case 8:
return PRINT_STATUS.batteryLow;
default:
return null;
}
}
3 years ago
3 years ago
Future<bool?> print(
3 years ago
{required String code,
required String channel,
required String country,
required String countStr,
required int offset,
required bool hasPlan}) async {
final res = await _channel.invokeMethod('print', {
'code': code,
'channel': channel,
'country': country,
'countStr': countStr,
'offset': offset,
'hasPlan': hasPlan,
});
return res;
3 years ago
}
}