read method Null safety
- PrinterDevice device,
- {Duration? timeout}
override
监听打印机返回数据
device
打印机设备
Implementation
@override
Stream<Uint8List> read(PrinterDevice device, {Duration? timeout}) {
StreamSubscription? subscription;
StreamController<List<int>>? controller;
bool cancelSubscription = false;
controller = StreamController(onListen: () {
if (timeout != null) {
Future.delayed(timeout, () {
if (!cancelSubscription) {
cancelSubscription = true;
controller?.close();
subscription?.cancel();
}
});
}
}, onCancel: () {
if (!cancelSubscription) {
cancelSubscription = true;
subscription?.cancel();
}
});
if (device is BluetoothDevice) {
subscription = bluetoothReadStream.listen((e) {
var returnDevice = BluetoothDevice.fromProto(e.device);
if (returnDevice.identify() == device.identify()) {
controller?.add(e.data);
}
},
onError: controller.addError,
onDone: controller.close,
cancelOnError: true);
}
if (device is UsbDevice) {
subscription = usbReadStream.listen((e) {
var returnDevice = UsbDevice.fromProto(e.device);
if (returnDevice.identify() == device.identify()) {
controller?.add(e.data);
}
},
onError: controller.addError,
onDone: controller.close,
cancelOnError: true);
}
if (device is UsbAccessoryDevice) {
subscription = usbAccessoryReadStream.listen((e) {
var returnDevice = UsbAccessoryDevice.fromProto(e.device);
if (returnDevice.identify() == device.identify()) {
controller?.add(e.data);
}
},
onError: controller.addError,
onDone: controller.close,
cancelOnError: true);
}
if (device is TcpDevice) {
subscription = tcpReadStream.listen((e) {
var returnDevice = TcpDevice.fromProto(e.device);
if (returnDevice.identify() == device.identify()) {
controller?.add(e.data);
}
},
onError: controller.addError,
onDone: controller.close,
cancelOnError: true);
}
if (device is TtyDevice) {
subscription = ttyReadStream.listen((e) {
var returnDevice = TtyDevice.fromProto(e.device);
if (returnDevice.identify() == device.identify()) {
controller?.add(e.data);
}
},
onError: controller.addError,
onDone: controller.close,
cancelOnError: true);
}
if (subscription != null) {
return controller.stream.map((e) => Uint8List.fromList(e));
}
throw UnimplementedError();
}