doDiscover method

Future<ScpStatus> doDiscover(
  1. String subnet,
  2. String mask
)

Implementation

Future<ScpStatus> doDiscover(String subnet, String mask) 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));
  });

  await Future.wait(requests).then(
      (List responses) => responses.forEach((response) async {
            if (response != null && response.bodyBytes != null) {
              if (response.statusCode == 200) {
                log('Received discover response from ${response.request.url}.');
                ScpResponseDiscover parsedResponse =
                    await ScpResponseParser.parseDiscoverResponse(
                        response, knownDevices);
                if (parsedResponse.isValid()) {
                  ScpDevice dev = ScpDevice(
                    deviceId: parsedResponse.getDeviceId(),
                    deviceType: parsedResponse.getDeviceType(),
                    deviceName: parsedResponse.getDeviceName(),
                    currentPasswordNumber:
                        parsedResponse.getCurrentPasswordNumber(),
                    ipAddress: allIPs
                        .firstWhere((ip) => response.request.url.host == ip),
                    isDefaultPasswordSet:
                        parsedResponse.getCurrentPasswordNumber() == 0
                            ? true
                            : false,
                    knownPassword:
                        parsedResponse.getCurrentPasswordNumber() == 0
                            ? '01234567890123456789012345678901'
                            : '',
                    controlActions: parsedResponse.getControlActions(),
                    measureActions: parsedResponse.getMeasureActions(),
                  );
                  log('Found device: ${dev.toJson()}');
                  if (dev.isDefaultPasswordSet) {
                    log('default password set, adding to new devices.');
                    newDevices.add(dev);
                  } else {
                    log('default password not set.');
                    bool deviceKnown = false;
                    for (ScpDevice knownDevice in knownDevices) {
                      if (knownDevice.deviceId == dev.deviceId) {
                        log('Device ${dev.deviceId} already known.');
                        deviceKnown = true;
                      }
                    }
                    if (deviceKnown == false) {
                      log('Device ${dev.deviceId} not known, adding to known devices.');
                      knownDevices.add(dev);
                    }
                  }
                } else {
                  log('Failed parsing response.');
                }
              }
            }
          }), onError: (e) {
    log('Could not reach device.');
  });
  return ScpStatus(status: ScpStatus.RESULT_SUCCESS);
}