diff --git a/http/measurement.http b/http/measurement.http new file mode 100644 index 0000000..ae903a5 --- /dev/null +++ b/http/measurement.http @@ -0,0 +1,24 @@ +### get measurement +GET {{hostname}}/measurement +Content-Type: application/json +Identity: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ + +### add measurement for all +POST {{hostname}}/measurement +Content-Type: application/json +Identity: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ + +{ + "button0": 0, + "button1": 1, + "button2": 0 +} + +### add measurement for one +POST {{hostname}}/measurement +Content-Type: application/json +Identity: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ + +{ + "button0": 0 +} diff --git a/src/main/kotlin/ltd/hlaeja/controller/MeasurementController.kt b/src/main/kotlin/ltd/hlaeja/controller/MeasurementController.kt new file mode 100644 index 0000000..26ade7a --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/controller/MeasurementController.kt @@ -0,0 +1,56 @@ +package ltd.hlaeja.controller + +import ltd.hlaeja.library.deviceData.MeasurementData +import ltd.hlaeja.library.deviceRegistry.Identity +import ltd.hlaeja.service.DeviceDataService +import ltd.hlaeja.service.DeviceRegistryService +import ltd.hlaeja.service.JwtService +import org.springframework.http.HttpStatus.CREATED +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RequestHeader +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.ResponseStatus +import org.springframework.web.bind.annotation.RestController + +@RestController +@RequestMapping("/measurement") +class MeasurementController( + private val deviceDataService: DeviceDataService, + private val deviceRegistry: DeviceRegistryService, + private val jwtService: JwtService, +) { + + @GetMapping + suspend fun getNodeMeasurement( + @RequestHeader("Identity") identityToken: String, + ): Map = extracted(identityToken) + .let { deviceDataService.getMeasurement(it.client, it.node).fields } + + @PostMapping + @ResponseStatus(CREATED) + suspend fun addUnitMeasurement( + @RequestHeader("Identity") identityToken: String, + @RequestBody measurement: Map, + ) { + return extracted(identityToken) + .let { + deviceDataService.addMeasurement( + it.client, + MeasurementData.Request( + mutableMapOf( + "node" to it.node.toString(), + "device" to it.device.toString(), + ), + measurement, + ), + ) + } + } + + private suspend fun extracted( + identityToken: String, + ): Identity.Response = jwtService.readIdentity(identityToken) + .let { deviceRegistry.getIdentityFromDevice(it) } +}