Files
hlaeja-device-api/src/main/kotlin/ltd/hlaeja/service/DeviceDataService.kt
2025-07-29 14:17:01 +02:00

49 lines
1.7 KiB
Kotlin

package ltd.hlaeja.service
import io.github.oshai.kotlinlogging.KotlinLogging
import java.util.UUID
import ltd.hlaeja.library.deviceData.MeasurementData
import ltd.hlaeja.property.DeviceDataProperty
import ltd.hlaeja.util.deviceDataAddMeasurement
import ltd.hlaeja.util.deviceDataGetMeasurement
import org.springframework.http.HttpStatus.SERVICE_UNAVAILABLE
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Service
import org.springframework.web.ErrorResponseException
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.reactive.function.client.WebClientRequestException
import org.springframework.web.server.ResponseStatusException
private val log = KotlinLogging.logger {}
@Service
class DeviceDataService(
private val webClient: WebClient,
private val deviceDataProperty: DeviceDataProperty,
) {
suspend fun getMeasurement(
client: UUID,
node: UUID,
): MeasurementData.Response = try {
webClient.deviceDataGetMeasurement(client, node, deviceDataProperty)
} catch (e: ErrorResponseException) {
throw e
} catch (e: WebClientRequestException) {
log.error(e) { "Error device registry" }
throw ResponseStatusException(SERVICE_UNAVAILABLE)
}
suspend fun addMeasurement(
client: UUID,
request: MeasurementData.Request,
): ResponseEntity<Void> = try {
webClient.deviceDataAddMeasurement(client, request, deviceDataProperty)
} catch (e: ErrorResponseException) {
throw e
} catch (e: WebClientRequestException) {
log.error(e) { "Error device registry" }
throw ResponseStatusException(SERVICE_UNAVAILABLE)
}
}