96 lines
3.5 KiB
Dart
96 lines
3.5 KiB
Dart
import 'package:json_annotation/json_annotation.dart';
|
|
|
|
part 'printer_device_models.g.dart';
|
|
|
|
/// Android USB Host 实际枚举到的一台标准 Printer Class 设备。
|
|
class PrinterUsbDeviceInfo {
|
|
/// 创建一次只读的 USB 打印机发现快照。
|
|
const PrinterUsbDeviceInfo({
|
|
required this.manufacturerName,
|
|
required this.productName,
|
|
required this.deviceId,
|
|
required this.vendorId,
|
|
required this.productId,
|
|
});
|
|
|
|
/// USB 描述符中的厂商名称;读取不到时为空。
|
|
final String? manufacturerName;
|
|
|
|
/// USB 描述符中的产品名称;读取不到时为空。
|
|
final String? productName;
|
|
|
|
/// Android 在本次连接期间分配的 USB 设备编号;重新插拔后可能变化。
|
|
final int? deviceId;
|
|
|
|
/// USB Vendor ID 十进制值,范围为 0 到 65535。
|
|
final int vendorId;
|
|
|
|
/// USB Product ID 十进制值,范围为 0 到 65535。
|
|
final int productId;
|
|
|
|
/// 页面列表选择使用的进程内稳定键,不作为服务端或数据库主键。
|
|
String get selectionKey => '$vendorId:$productId:${deviceId ?? -1}';
|
|
|
|
/// 优先组合厂商和产品描述,缺少描述符时回退为通用打印机名称。
|
|
String get displayName {
|
|
final parts = <String>[
|
|
if (manufacturerName?.trim().isNotEmpty == true) manufacturerName!.trim(),
|
|
if (productName?.trim().isNotEmpty == true) productName!.trim(),
|
|
];
|
|
return parts.isEmpty ? 'USB Printer' : parts.toSet().join(' / ');
|
|
}
|
|
|
|
/// 将 USB 编号格式化为四位大写十六进制,便于与设备文档交叉核对。
|
|
String get hexadecimalIdentity =>
|
|
'0x${vendorId.toRadixString(16).padLeft(4, '0').toUpperCase()} / '
|
|
'0x${productId.toRadixString(16).padLeft(4, '0').toUpperCase()}';
|
|
|
|
/// 生成只包含服务端允许字段的打印机信息上报请求。
|
|
///
|
|
/// 终端固定图片打印,每次上报都携带打印方式声明,让服务端跳过指令流生成。
|
|
PrinterInfoReportRequest toReportRequest() => PrinterInfoReportRequest(
|
|
printerModel: displayName,
|
|
usbVendorId: vendorId,
|
|
usbProductId: productId,
|
|
printMode: PrinterOutputMode.image,
|
|
);
|
|
}
|
|
|
|
/// 终端打印方式常量:当前终端固定图片打印,指令打印已停用。
|
|
abstract final class PrinterOutputMode {
|
|
/// 图片打印:把服务端下发的凭证 PNG 渲染成 ESC/POS 位图出纸。
|
|
static const String image = 'IMAGE';
|
|
|
|
/// 指令打印:直接把服务端拼装的指令流写入 USB;真机兼容性不达标,已停用。
|
|
static const String command = 'CMD';
|
|
}
|
|
|
|
/// 终端向服务端上报实际连接打印机型号与 USB 标识的请求。
|
|
///
|
|
/// 设备字段与打印方式均可缺省,服务端按非空字段部分更新。
|
|
@JsonSerializable(createFactory: false)
|
|
class PrinterInfoReportRequest {
|
|
/// 创建一份不包含设备路径、序列号等敏感信息的上报请求。
|
|
const PrinterInfoReportRequest({
|
|
this.printerModel,
|
|
this.usbVendorId,
|
|
this.usbProductId,
|
|
this.printMode,
|
|
});
|
|
|
|
/// 由 USB 厂商和产品描述组合的打印机型号,服务端最长保存 128 个字符。
|
|
final String? printerModel;
|
|
|
|
/// USB Vendor ID 十进制值。
|
|
final int? usbVendorId;
|
|
|
|
/// USB Product ID 十进制值。
|
|
final int? usbProductId;
|
|
|
|
/// 终端打印方式声明;图片打印终端上报 IMAGE 让服务端跳过指令流生成。
|
|
final String? printMode;
|
|
|
|
/// 转换为终端打印机信息上报接口使用的 JSON 对象。
|
|
Map<String, dynamic> toJson() => _$PrinterInfoReportRequestToJson(this);
|
|
}
|