add MeasurementController

This commit is contained in:
2024-11-15 16:36:19 +01:00
parent c99c8dc748
commit d994595bcf
2 changed files with 80 additions and 0 deletions

24
http/measurement.http Normal file
View File

@@ -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
}

View File

@@ -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<String, Number> = extracted(identityToken)
.let { deviceDataService.getMeasurement(it.client, it.node).fields }
@PostMapping
@ResponseStatus(CREATED)
suspend fun addUnitMeasurement(
@RequestHeader("Identity") identityToken: String,
@RequestBody measurement: Map<String, Number>,
) {
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) }
}