setConnectionChecker method

void setConnectionChecker({
  1. Duration timeout = const Duration(seconds: 10),
  2. Duration interval = const Duration(seconds: 30),
  3. required Function retryFunction,
  4. required String deviceQueryDeviceName,
  5. required Function useRetryResponse,
  6. required bool shouldCheck(),
})

Implementation

void setConnectionChecker(
    {Duration timeout = const Duration(seconds: 10),
    Duration interval = const Duration(seconds: 30),
    required Function retryFunction,
    required String deviceQueryDeviceName,
    required Function useRetryResponse,
    required bool Function() shouldCheck}) {
  // log("RETRY :$retry");
  if(!Platform.isWindows) {
    try {
      Future.delayed(interval, () {
        if (retry) {
          if (logIdentifier == "Platform") {
            log("$logIdentifier Connection Checker ${DateTime.now()}");
            DeviceQueryRequest req = DeviceQueryRequest(messageID: messageID, deviceName: deviceQueryDeviceName);
            if (isConnected) {
              if (shouldCheck()) {
                sendSocketMessage(req.toXML(), shouldLog: true).timeout(timeout, onTimeout: () {
                  log("$logIdentifier Connection Checker Timeout **Reconnect** ${DateTime.now()}");
                  retryFunction();
                  return "";
                }).then((value) {
                  if (value.isNotEmpty && !value.contains("Socket Closed")) {
                    log("$logIdentifier Connection Checker Connection OK ${DateTime.now()}");
                    setConnectionChecker(
                        timeout: timeout,
                        interval: interval,
                        retryFunction: retryFunction,
                        useRetryResponse: useRetryResponse,
                        deviceQueryDeviceName: deviceQueryDeviceName,
                        shouldCheck: shouldCheck);
                  }
                });
              } else {
                log("Retry and Connection checker called but shouldCheck ${shouldCheck()}");
              }
            } else {
              retryFunction();
            }
          } else {
            log("$logIdentifier Connection Checker ${DateTime.now()}");
            DeviceStatusRequest req = DeviceStatusRequest(messageID: messageID);
            if (isConnected) {
              if (shouldCheck()) {
                sendSocketMessage(req.toXML(), shouldLog: true).timeout(timeout, onTimeout: () {
                  log("$logIdentifier Connection Checker Timeout **Reconnect** ${DateTime.now()}");
                  retryFunction();
                  return "";
                }).then((value) {
                  if (value.isNotEmpty && !value.contains("Socket Closed")) {
                    DeviceStatusResponse statusResponse = DeviceStatusResponse.fromXML(value);
                    useRetryResponse(statusResponse);
                    log("$logIdentifier Connection Checker Connection OK ${DateTime.now()}");
                    setConnectionChecker(
                        timeout: timeout,
                        interval: interval,
                        retryFunction: retryFunction,
                        deviceQueryDeviceName: deviceQueryDeviceName,
                        useRetryResponse: useRetryResponse,
                        shouldCheck: shouldCheck);
                  }
                });
              } else {
                log("Retry and Connection checker called but shouldCheck ${shouldCheck()}");
              }
            } else {
              log("$logIdentifier Connection Checker called but socked is CLOSED && retry $retry");
              if (retry) {
                retryFunction();
              }
              // retryFunction();
            }
          }
        }
      });
    } catch (e) {
      log("$logIdentifier Connection Checker Error **Reconnect** ${DateTime.now()}");
      onConnectionLost!();
    }
  }
}