Newer
Older
tracker-web-ui / src / main / java / ru / mcs / tracker / ui / service / LocationApiService.java
package ru.mcs.tracker.ui.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import ru.mcs.tracker.ui.dto.LocationDto;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class LocationApiService {

    private static final Logger logger = LoggerFactory.getLogger(LocationApiService.class);

    private final RestClient restClient;
    private final String baseUrl;

    public LocationApiService(RestClient restClient, @Value("${api.base-url}") String baseUrl) {
        this.restClient = restClient;
        this.baseUrl = baseUrl;
        logger.info("LocationApiService initialized with base URL: {}", baseUrl);
    }

    public List<LocationDto> getLocations(String deviceGuid, LocalDateTime start, LocalDateTime end) {
        try {
            logger.debug("Requesting locations for device: {}, from: {} to: {}", deviceGuid, start, end);

            ResponseEntity<List<LocationDto>> response = restClient.get()
                    .uri(uriBuilder -> uriBuilder
                            .path("/api/locations/{deviceGuid}")
                            .queryParam("start", start)
                            .queryParam("end", end)
                            .build(deviceGuid))
                    .retrieve()
                    .toEntity(new ParameterizedTypeReference<List<LocationDto>>() {});

            if (response.getStatusCode() == HttpStatus.OK && response.getBody() != null) {
                logger.debug("Received {} locations", response.getBody().size());
                return response.getBody();
            } else {
                logger.warn("No locations found for device: {}", deviceGuid);
                return Collections.emptyList();
            }

        } catch (Exception e) {
            logger.error("Error retrieving locations: {}", e.getMessage(), e);
            return Collections.emptyList();
        }
    }

    public List<String> getDeviceGuids() {
        try {
            logger.debug("Requesting device GUIDs from API");

            ResponseEntity<List<String>> response = restClient.get()
                    .uri("/api/locations/devices")
                    .retrieve()
                    .toEntity(new ParameterizedTypeReference<List<String>>() {});

            if (response.getStatusCode() == HttpStatus.OK && response.getBody() != null) {
                logger.debug("Received {} device GUIDs", response.getBody().size());
                return response.getBody();
            } else {
                logger.warn("No device GUIDs found");
                return Collections.emptyList();
            }

        } catch (Exception e) {
            logger.error("Error retrieving device GUIDs: {}", e.getMessage(), e);
            return Collections.emptyList();
        }
    }
}