doUpdate method

Future<ScpStatus> doUpdate(
  1. String subnet,
  2. String mask,
  3. String jsonPath
)

Implementation

Future<ScpStatus> doUpdate(
    String subnet, String mask, String jsonPath) async {
  newDevices = List<ScpDevice>.empty(growable: true);
  // Get a list with all relevant IP addresses
  IPRange range = IPRange(subnet, int.parse(mask));
  List<String> allIPs = range.getAllIpAddressesInRange();

  List<Future> requests = List<Future>.empty(growable: true);

  allIPs.forEach((ip) {
    requests.add(ScpMessageSender.sendDiscoverHello(ip));
  });

  Future.wait(requests).then(
    (List responses) => responses.forEach((response) async {
      if (response != null && response.bodyBytes != null) {
        if (response.statusCode == 200) {
          ScpResponseDiscover parsedResponse =
              await ScpResponseParser.parseDiscoverResponse(
                  response, knownDevices);
          if (parsedResponse.isValid()) {
            // Update knowDevice from discover response
            knownDevices
                .firstWhere((element) =>
                    element.deviceId == parsedResponse.getDeviceId())
                .updateFromDiscoverResponse(parsedResponse);
            // Update IP of knownDevices from requested IPs
            knownDevices
                    .firstWhere((element) =>
                        element.deviceId == parsedResponse.getDeviceId())
                    .ipAddress =
                allIPs.firstWhere((ip) => response.request.url.host == ip);
            //Store this device in JsonStorage
            JsonStorage.storeDevice(
                knownDevices.firstWhere((element) =>
                    element.deviceId == parsedResponse.getDeviceId()),
                jsonPath);
            log('Updated device ${parsedResponse.getDeviceId()}.');
          }
        }
      }
    }),
  );
  return ScpStatus(status: ScpStatus.RESULT_SUCCESS);
}