read method

void read(
  1. DeviceInfo deviceInfo,
  2. String manufacturesDictUrl
)

Implementation

void read(DeviceInfo deviceInfo, String manufacturesDictUrl) {
  _state = RemoteInfoState(
    status: WorkStatus.working,
    info: RemoteInfo(),
  );
  notifyState(state);

  () async {
    try {
      final manufacturesResponse =
          await http.get(Uri.parse(manufacturesDictUrl));
      if (manufacturesResponse.statusCode != 200) {
        _raiseError(
          Error.unexpectedNetworkResponse,
          errorCode: manufacturesResponse.statusCode,
        );
        return;
      }
      final manufacturesBody =
          _loadDict(manufacturesDictUrl, manufacturesResponse.body);

      final hardwaresUrl = manufacturesBody[deviceInfo.manufactureName];
      if (hardwaresUrl == null) {
        state.status = WorkStatus.success;
        notifyState(state);
        return;
      }

      final hardwaresResponse = await http.get(Uri.parse(hardwaresUrl));
      if (hardwaresResponse.statusCode != 200) {
        _raiseError(
          Error.unexpectedNetworkResponse,
          errorCode: hardwaresResponse.statusCode,
        );
        return;
      }
      final hardwaresBody = _loadDict(hardwaresUrl, hardwaresResponse.body);

      final hardwareUrl = hardwaresBody[deviceInfo.hardwareName];
      if (hardwareUrl == null) {
        state.status = WorkStatus.success;
        notifyState(state);
        return;
      }

      await _readSoftwares(deviceInfo, hardwareUrl);
    } catch (_) {
      _raiseError(Error.networkError);
    }
  }.call();
}