startPolling method

void startPolling()

Initiate repeated polling of the server to fetch the score.

Implementation

void startPolling() {
  DataUsageTracking.sharedInstance().processTroubleShootingEAIWorkFlow(
      CriticalPoints.EMOTIONS_AI_START_SCORING.name,
      Flagship.sharedInstance().currentVisitor);
  // Create a repeating timer with a 0.5 second interval (similar to FSRepeatingTimer in Swift).
  _pollingTimer =
      Timer.periodic(const Duration(milliseconds: 500), (timer) async {
    _retryCount++;
    Flagship.logger(Level.INFO,
        "GET THE SCORE FROM THE SERVER - RETRY COUNT: $_retryCount");

    // Fetch the score from the server (placeholder).
    final scoreResult = await EmotionAITools().fetchScore(visitorId);
    final statusCode = scoreResult.statusCode;
    final score = scoreResult.score;

    if (statusCode == 204) {
      // Score not ready; let the timer keep going
      Flagship.logger(Level.INFO,
          'Score not ready (statusCode=204). Continuing to poll...');
    } else if (statusCode == 200) {
      // We got a valid score
      Flagship.logger(
          Level.INFO, 'Score successfully received (statusCode=200)');
      delegate?.emotionAiCaptureCompleted(score);
      // Invalidate timers since we have our score
      _stopTimer?.cancel();
      _pollingTimer?.cancel();

      DataUsageTracking.sharedInstance().processTroubleShootingEAIWorkFlow(
          CriticalPoints.EMOTIONS_AI_SCORING_SUCCESS.name,
          Flagship.sharedInstance().currentVisitor,
          score: score);
    } else {
      // Some other status code (error, etc.)
      Flagship.logger(
          Level.INFO, 'Score not received - status code: $statusCode');
      DataUsageTracking.sharedInstance().processTroubleShootingEAIWorkFlow(
          CriticalPoints.EMOTIONS_AI_SCORING_FAILED.name,
          Flagship.sharedInstance().currentVisitor);
    }
  });
}