Rename message fields

This commit is contained in:
Alexander Nozik 2020-07-24 23:13:58 +03:00
parent 02ce13b784
commit ea8ebcf38f
6 changed files with 41 additions and 50 deletions

View File

@ -1,5 +1,4 @@
val dataforgeVersion by extra("0.1.8")
val plotlyVersion by extra("0.2.0-dev-12")
allprojects {

View File

@ -1,35 +1,33 @@
package hep.dataforge.control.controllers
import hep.dataforge.control.controllers.DeviceMessage.Companion.PAYLOAD_VALUE_KEY
import hep.dataforge.control.controllers.DeviceMessage.Companion.DATA_VALUE_KEY
import hep.dataforge.meta.*
import hep.dataforge.names.asName
import kotlinx.serialization.*
@Serializable
class DeviceMessage : Scheme() {
var id by string()
var parent by string()
var origin by string()
var source by string()
var target by string()
var action by string(default = MessageController.GET_PROPERTY_ACTION, key = MESSAGE_ACTION_KEY)
var type by string(default = MessageController.GET_PROPERTY_ACTION, key = MESSAGE_TYPE_KEY)
var comment by string()
var status by string(RESPONSE_OK_STATUS)
var payload: List<MessagePayload>
get() = config.getIndexed(MESSAGE_PAYLOAD_KEY).values.map { MessagePayload.wrap(it.node!!) }
var data: List<MessageData>
get() = config.getIndexed(MESSAGE_DATA_KEY).values.map { MessageData.wrap(it.node!!) }
set(value) {
config[MESSAGE_PAYLOAD_KEY] = value.map { it.config }
config[MESSAGE_DATA_KEY] = value.map { it.config }
}
/**
* Append a payload to this message according to the given scheme
*/
fun <T : Configurable> append(spec: Specification<T>, block: T.() -> Unit): T =
spec.invoke(block).also { config.append(MESSAGE_PAYLOAD_KEY, it) }
spec.invoke(block).also { config.append(MESSAGE_DATA_KEY, it) }
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()
val MESSAGE_TYPE_KEY = "action".asName()
val MESSAGE_DATA_KEY = "data".asName()
val DATA_VALUE_KEY = "value".asName()
const val RESPONSE_OK_STATUS = "response.OK"
const val RESPONSE_FAIL_STATUS = "response.FAIL"
const val PROPERTY_CHANGED_ACTION = "event.propertyChange"
@ -38,14 +36,14 @@ class DeviceMessage : Scheme() {
request: DeviceMessage? = null,
block: DeviceMessage.() -> Unit = {}
): DeviceMessage = DeviceMessage {
parent = request?.id
target = request?.source
}.apply(block)
inline fun fail(
request: DeviceMessage? = null,
block: DeviceMessage.() -> Unit = {}
): DeviceMessage = DeviceMessage {
parent = request?.id
target = request?.source
status = RESPONSE_FAIL_STATUS
}.apply(block)
@ -62,12 +60,12 @@ class DeviceMessage : Scheme() {
}
}
class MessagePayload : Scheme() {
class MessageData : Scheme() {
var name by string { error("Property name could not be empty") }
var value by item(key = PAYLOAD_VALUE_KEY)
var value by item(key = DATA_VALUE_KEY)
companion object : SchemeSpec<MessagePayload>(::MessagePayload)
companion object : SchemeSpec<MessageData>(::MessageData)
}
@DFBuilder
fun DeviceMessage.property(block: MessagePayload.() -> Unit): MessagePayload = append(MessagePayload, block)
fun DeviceMessage.property(block: MessageData.() -> Unit): MessageData = append(MessageData, block)

View File

@ -40,17 +40,17 @@ class MessageController(
comment = "Wrong target name $deviceTarget expected but ${request.target} found"
}
} else try {
val result: List<MessagePayload> = when (val action = request.action) {
val result: List<MessageData> = when (val action = request.type) {
GET_PROPERTY_ACTION -> {
request.payload.map { property ->
MessagePayload {
request.data.map { property ->
MessageData {
name = property.name
value = device.getProperty(name)
}
}
}
SET_PROPERTY_ACTION -> {
request.payload.map { property ->
request.data.map { property ->
val propertyName: String = property.name
val propertyValue = property.value
if (propertyValue == null) {
@ -58,15 +58,15 @@ class MessageController(
} else {
device.setProperty(propertyName, propertyValue)
}
MessagePayload {
MessageData {
name = propertyName
value = device.getProperty(propertyName)
}
}
}
EXECUTE_ACTION -> {
request.payload.map { payload ->
MessagePayload {
request.data.map { payload ->
MessageData {
name = payload.name
value = device.exec(payload.name, payload.value)
}
@ -74,7 +74,7 @@ class MessageController(
}
PROPERTY_LIST_ACTION -> {
device.propertyDescriptors.map { descriptor ->
MessagePayload {
MessageData {
name = descriptor.name
value = MetaItem.NodeItem(descriptor.config)
}
@ -83,7 +83,7 @@ class MessageController(
ACTION_LIST_ACTION -> {
device.actionDescriptors.map { descriptor ->
MessagePayload {
MessageData {
name = descriptor.name
value = MetaItem.NodeItem(descriptor.config)
}
@ -95,10 +95,9 @@ class MessageController(
}
}
DeviceMessage.ok {
this.parent = request.id
this.origin = deviceTarget
this.target = request.origin
this.payload = result
source = deviceTarget
target = request.source
data = result
}
} catch (ex: Exception) {
DeviceMessage.fail {
@ -123,8 +122,8 @@ class MessageController(
if (value == null) return
scope.launch {
val change = DeviceMessage.ok {
this.origin = deviceTarget
action = PROPERTY_CHANGED_ACTION
this.source = deviceTarget
type = PROPERTY_CHANGED_ACTION
property {
name = propertyName
this.value = value

View File

@ -100,8 +100,8 @@ private suspend fun ApplicationCall.message(target: MessageController) {
private suspend fun ApplicationCall.getProperty(target: MessageController) {
val property: String by parameters
val request = DeviceMessage {
action = GET_PROPERTY_ACTION
origin = WEB_SERVER_TARGET
type = GET_PROPERTY_ACTION
source = WEB_SERVER_TARGET
this.target = target.deviceTarget
property {
name = property
@ -118,8 +118,8 @@ private suspend fun ApplicationCall.setProperty(target: MessageController) {
val json = Json.parseJson(body)
val request = DeviceMessage {
action = SET_PROPERTY_ACTION
origin = WEB_SERVER_TARGET
type = SET_PROPERTY_ACTION
source = WEB_SERVER_TARGET
this.target = target.deviceTarget
property {
name = property

View File

@ -1,10 +1,10 @@
plugins {
kotlin("jvm") version "1.3.72"
id("org.openjfx.javafxplugin") version "0.0.8"
id("org.openjfx.javafxplugin") version "0.0.9"
application
}
val plotlyVersion: String by rootProject.extra
val plotlyVersion by extra("0.2.0-dev-13")
repositories{
jcenter()

View File

@ -3,18 +3,15 @@ package hep.dataforge.control.demo
import hep.dataforge.control.server.startDeviceServer
import hep.dataforge.control.server.whenStarted
import hep.dataforge.meta.double
import io.ktor.application.uninstall
import hep.dataforge.meta.invoke
import io.ktor.server.engine.ApplicationEngine
import io.ktor.websocket.WebSockets
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.html.div
import kotlinx.html.link
import scientifik.plotly.layout
import scientifik.plotly.models.Trace
import scientifik.plotly.plot
import scientifik.plotly.server.PlotlyServerConfig
import scientifik.plotly.server.PlotlyUpdateMode
import scientifik.plotly.server.plotlyModule
import scientifik.plotly.trace
@ -50,15 +47,13 @@ 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 {
uninstall(WebSockets)
plotlyModule(
"plots",
PlotlyServerConfig { updateMode = PlotlyUpdateMode.PUSH; updateInterval = 50 }
) { container ->
plotlyModule("plots").apply {
updateMode = PlotlyUpdateMode.PUSH
updateInterval = 50
}.page { container ->
val sinFlow = device.sin.flow()
val cosFlow = device.cos.flow()
val sinCosFlow = sinFlow.zip(cosFlow) { sin, cos ->