fix serialization

This commit is contained in:
Alexander Nozik 2020-07-19 21:10:10 +03:00
parent d1ec920e1c
commit 02ce13b784
3 changed files with 21 additions and 10 deletions

View File

@ -3,10 +3,11 @@ package hep.dataforge.control.controllers
import hep.dataforge.control.controllers.DeviceMessage.Companion.PAYLOAD_VALUE_KEY
import hep.dataforge.meta.*
import hep.dataforge.names.asName
import kotlinx.serialization.*
@Serializable
class DeviceMessage : Scheme() {
var id by string { error("The message id must not be empty") }
var id by string()
var parent by string()
var origin by string()
var target by string()
@ -25,7 +26,7 @@ class DeviceMessage : Scheme() {
fun <T : Configurable> append(spec: Specification<T>, block: T.() -> Unit): T =
spec.invoke(block).also { config.append(MESSAGE_PAYLOAD_KEY, it) }
companion object : SchemeSpec<DeviceMessage>(::DeviceMessage) {
companion object : SchemeSpec<DeviceMessage>(::DeviceMessage), KSerializer<DeviceMessage> {
val MESSAGE_ACTION_KEY = "action".asName()
val MESSAGE_PAYLOAD_KEY = "payload".asName()
val PAYLOAD_VALUE_KEY = "value".asName()
@ -47,6 +48,17 @@ class DeviceMessage : Scheme() {
parent = request?.id
status = RESPONSE_FAIL_STATUS
}.apply(block)
override val descriptor: SerialDescriptor = MetaSerializer.descriptor
override fun deserialize(decoder: Decoder): DeviceMessage {
val meta = MetaSerializer.deserialize(decoder)
return wrap(meta)
}
override fun serialize(encoder: Encoder, value: DeviceMessage) {
MetaSerializer.serialize(encoder, value.toMeta())
}
}
}

View File

@ -14,7 +14,6 @@ import hep.dataforge.meta.toMetaItem
import hep.dataforge.meta.wrap
import io.ktor.application.*
import io.ktor.features.CORS
import io.ktor.features.ContentNegotiation
import io.ktor.features.StatusPages
import io.ktor.html.respondHtml
import io.ktor.http.HttpStatusCode
@ -22,7 +21,6 @@ import io.ktor.request.receiveText
import io.ktor.response.respond
import io.ktor.response.respondRedirect
import io.ktor.routing.*
import io.ktor.serialization.json
import io.ktor.server.cio.CIO
import io.ktor.server.engine.ApplicationEngine
import io.ktor.server.engine.embeddedServer
@ -63,9 +61,9 @@ fun CoroutineScope.startDeviceServer(
install(CORS) {
anyHost()
}
install(ContentNegotiation) {
json()
}
// install(ContentNegotiation) {
// json()
// }
install(StatusPages) {
exception<IllegalArgumentException> { cause ->
call.respond(HttpStatusCode.BadRequest, cause.message ?: "")
@ -96,7 +94,7 @@ private suspend fun ApplicationCall.message(target: MessageController) {
val request = DeviceMessage.wrap(meta)
val response = target.respondMessage(request)
respond(response.toMeta())
respondMessage(response)
}
private suspend fun ApplicationCall.getProperty(target: MessageController) {
@ -111,7 +109,7 @@ private suspend fun ApplicationCall.getProperty(target: MessageController) {
}
val response = target.respondMessage(request)
respond(response.toMeta())
respondMessage(response)
}
private suspend fun ApplicationCall.setProperty(target: MessageController) {

View File

@ -50,6 +50,7 @@ suspend fun Trace.updateXYFrom(flow: Flow<Iterable<Pair<Double, Double>>>) {
}
fun CoroutineScope.startDemoDeviceServer(device: DemoDevice): ApplicationEngine {
val server = startDeviceServer(mapOf("demo" to device))
server.whenStarted {