initialize method

Implementation

Future<CuppsCommandResponse> initialize() async {
  try {
    initializing = true;
    statusMsg = "Initializing...";
    CUPPS().notifier!();

    InterfaceLevelsAvailableRequest req =
        InterfaceLevelsAvailableRequest(messageID: currentMessageID, hsXsdVersion: '01.01.0128');
    String interfaceLevelsAvailableResXML = await send(xml: req.toXML());
    InterfaceLevelsAvailableResponse interfaceLevelsAvailableRes =
        InterfaceLevelsAvailableResponse.fromXML(interfaceLevelsAvailableResXML);
    if (interfaceLevelsAvailableRes.result.toLowerCase() == "ok") {
      InterfaceLevelRequest req = InterfaceLevelRequest(
          level: interfaceLevelsAvailableRes.interfaceLevels.firstWhere((element) => element.level == "01.03").level,
          messageID: currentMessageID);
      String interfaceLevelResXML = await send(xml: req.toXML());
      InterfaceLevelResponse interfaceLevelRes = InterfaceLevelResponse.fromXML(interfaceLevelResXML);
      if (interfaceLevelRes.result.toLowerCase() == "ok") {
        CuppsCommandResponse acquireRes = await acquire();
        if (acquireRes.status) {
          CuppsCommandResponse modeRes = await mode();
          // return CuppsCommandResponse(
          //     status: modeRes.status,
          //     msg: "Device: $deviceName Initialized Successfully");
          if (modeRes.status) {
            initializing = false;
            modded = true;
            initialized = true;
            statusMsg = "Initialized";
            CUPPS().notifier!();
            if(!_hasConnectionChecker){
              setConnectionChecker(retry: restart);
              _hasConnectionChecker = true;
            }
            socketService.enableRetry();
            // CuppsCommandResponse refreshStatusRes = await refreshStatus();
            // CUPPS().notifier!();
            return CuppsCommandResponse(status: modeRes.status, msg: "Device: $deviceName Initialized Successfully");
          } else {
            initializing = false;
            modded = false;
            statusMsg = modeRes.msg;
            CUPPS().notifier!();
            return modeRes;
          }
        } else {
          initializing = false;
          statusMsg = acquireRes.msg;
          CUPPS().notifier!();
          return acquireRes;
        }
      } else {
        initializing = false;
        statusMsg = interfaceLevelRes.result;
        CUPPS().notifier!();
        return CuppsCommandResponse(msg: interfaceLevelRes.result, status: false);
      }
    } else {
      initializing = false;
      statusMsg = interfaceLevelsAvailableRes.result;
      CUPPS().notifier!();
      return CuppsCommandResponse(msg: interfaceLevelsAvailableRes.result, status: false);
    }
  } catch (e) {
    initializing = false;
    statusMsg = "Initialize Exception";
    CUPPS().notifier!();
    if("$e".contains("StreamSink is closed")){
      socketService.nullIt();
      return restart();
    }else{
      return CuppsCommandResponse(msg: "Device $deviceName initialize Exception: $e", status: false);
    }


  }
}