getPrinterStatus method Null safety

Future<PrinterStatus?> getPrinterStatus(
  1. PrinterDevice device,
  2. PrinterMode mode
)

获取打印机状态(正常、缺纸、开盖、过热等) device 打印机设备 mode 打印模式(指令类型)

Implementation

Future<PrinterStatus?> getPrinterStatus(
    PrinterDevice device, PrinterMode mode) async {
  Future<PrinterStatus?> status;
  switch (mode) {
    case PrinterMode.esc:
      status = writeAndRead(device,
              Uint8List.fromList([0x0d, 0x0a, 0x10, 0x04, 0x02, 0x0d, 0x0a]))
          .then((e) {
        var t = e?.first;
        int status = PrinterStatus.statusOk;
        if (t == null) {
          return null;
        }
        if ((t & 0x04) == 0x04) {
          status |= PrinterStatus.statusOpenCover;
        }
        if ((t & 0x20) == 0x20) {
          status |= PrinterStatus.statusNoPaper;
        }
        if ((t & 0x40) == 0x40) {
          status |= PrinterStatus.statusUnknownError;
        }
        return PrinterStatus(status);
      });
      break;
    case PrinterMode.cpcl:
      status = writeAndRead(device,
          Uint8List.fromList([0x0d, 0x0a, 0x1b, 0x68, 0x0d, 0x0a])).then((e) {
        var t = e?.first;
        int status = PrinterStatus.statusOk;
        if (t == null) {
          return null;
        }
        if ((t & 0x04) == 0x04) {
          status |= PrinterStatus.statusOpenCover;
        }
        if ((t & 0x20) == 0x20) {
          status |= PrinterStatus.statusNoPaper;
        }
        if ((t & 0x01) == 0x01) {
          status |= PrinterStatus.statusPrinting;
        }
        return PrinterStatus(status);
      });
      break;
    case PrinterMode.zpl:
      status = writeAndRead(device, Uint8List.fromList("^XA~HS^XZ".codeUnits))
          .then((e) {
        var t = e?.first;
        if (t == null) {
          return null;
        }
        //todo zpl状态返回解析
        return PrinterStatus(PrinterStatus.statusOk);
      });
      break;
    default:
      status = writeAndRead(
          device,
          Uint8List.fromList(
              [0x0d, 0x0a, 0x1b, ...'!?'.codeUnits, 0x0d, 0x0a])).then((e) {
        var t = e?.first;
        var status = PrinterStatus.statusOk;
        if (t == null) {
          return null;
        }
        if ((t & 0x01) == 0x01) {
          status |= PrinterStatus.statusOpenCover;
        }
        if ((t & 0x02) == 0x02) {
          status |= PrinterStatus.statusWillNoPaper;
        }
        if ((t & 0x04) == 0x04) {
          status |= PrinterStatus.statusNoPaper;
        }
        if ((t & 0x08) == 0x08) {
          status |= PrinterStatus.statusNoCarbon;
        }
        if ((t & 0x10) == 0x10) {
          status |= PrinterStatus.statusPrintPause;
        }
        if ((t & 0x20) == 0x20) {
          status |= PrinterStatus.statusPrinting;
        }
        if ((t & 0x80) == 0x80) {
          status |= PrinterStatus.statusUnknownError;
        }
        return PrinterStatus(status);
      });
      break;
  }
  return status;
}