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.

137 lines
3.3 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
StreamSubscription addDiscoveryListen(
3 years ago
{required void Function(dynamic data) onReceive,
void Function()? onStart,
void Function()? onFinish}) {
3 years ago
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() {
_discoveryStream?.cancel();
_discoveryStream = null;
print('结束搜索');
disposeDiscovery();
3 years ago
}
static const EventChannel _connectChannel = EventChannel("com.connect");
3 years ago
StreamSubscription? _connectStream;
3 years ago
StreamSubscription addConnectListen({required void Function() onConnect,
required void Function() onDisconnect}) {
3 years ago
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;
}
}
void get startSearch {
final res = _channel.invokeMethod('startSearch');
3 years ago
}
bool _hasInit = false;
bool get hasInit => _hasInit;
3 years ago
Future<bool?> connect(String address) async {
3 years ago
final res = await _channel.invokeMethod('connect', {'address': address});
return res;
}
void disposeDiscovery() {
print('disposeDiscovery');
final res = _channel.invokeMethod('disposeDiscovery');
}
Future<bool?> disposeConnect() async {
final res = await _channel.invokeMethod('disposeConnect');
return res;
}
3 years ago
Future<bool?> init() async {
3 years ago
final res = await _channel.invokeMethod('init');
_hasInit = true;
3 years ago
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
Future<bool?> printAScode({required String code,
required String channel,
required String country,
required String countStr,
required int offset,
required bool hasPlan}) async {
3 years ago
final res = await _channel.invokeMethod('print', {
'code': code,
'channel': channel,
'country': country,
'countStr': countStr,
'offset': offset,
'hasPlan': hasPlan,
});
return res;
3 years ago
}
}