getDeviceInfo static method

Future<DeviceInfo> getDeviceInfo()

Returns the newPOS's device information.

This function returns a DeviceInfo object, which contains information about the device's firmware version, device type, KSN, and current ele per.

If the device information cannot be obtained, the function throws a FlutterPosException.

Example:

final deviceInfo = await FlutterNewposSdk.getDeviceInfo();

Parameters:

  • None.

Return:

A DeviceInfo object containing information about the device.

Throws:

  • FlutterPosException with the code GET_DEVICE_INFO_TIMEOUT if the device information cannot be obtained within the timeout period.
  • FlutterPosException with the code GET_DEVICE_INFO_FAILED if an unknown error occurs while getting the device information.

Example:

final deviceInfo = await FlutterNewposSdk.getDeviceInfo();
print(deviceInfo.firmwareVersion);
print(deviceInfo.deviceType);
print(deviceInfo.ksn);
print(deviceInfo.currentElePer);

Implementation

static Future<DeviceInfo> getDeviceInfo() async {
  /// Stream method
  final stream = FlutterNewposSdk._methodStream.stream
      .where((m) => m.method == 'OnGetDeviceInfo')
      .map((m) {
    final arguments = m.arguments as Map<Object?, Object?>;
    final convertedMap = <dynamic, dynamic>{};
    arguments.forEach((key, value) {
      if (key is String) {
        convertedMap[key] = value;
      }
    });
    return convertedMap;
  }).map(
    (event) {
      try {
        return DeviceInfo.fromMap(event);
      } catch (e) {
        throw const FlutterPosException(
          code: 'GET_DEVICE_INFO_FAILED',
          message: 'Unknown error getting device info',
        );
      }
    },
  );
  try {
    await _invokeMethod(
      'getDeviceInfo',
    );
    final result = await getFirstResultInStream(
      stream,
      const Duration(seconds: 5),
    );

    final success = result != null;

    if (success) {
      return result;
    } else {
      throw const FlutterPosException(
        code: 'GET_DEVICE_INFO_FAILED',
        message: 'Unknown error getting device info',
      );
    }
  } on TimeoutException {
    throw const FlutterPosException(
      code: 'GET_DEVICE_INFO_TIMEOUT',
      message: 'Timeout getting device info',
    );
  } catch (e) {
    throw const FlutterPosException(
      code: 'GET_DEVICE_INFO_FAILED',
      message: 'Unknown error getting device info',
    );
  }
}