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 dataforgeVersion by extra("0.1.8")
val plotlyVersion by extra("0.2.0-dev-12")
allprojects { allprojects {

View File

@ -1,35 +1,33 @@
package hep.dataforge.control.controllers 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.meta.*
import hep.dataforge.names.asName import hep.dataforge.names.asName
import kotlinx.serialization.* import kotlinx.serialization.*
@Serializable @Serializable
class DeviceMessage : Scheme() { class DeviceMessage : Scheme() {
var id by string() var source by string()
var parent by string()
var origin by string()
var target 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 comment by string()
var status by string(RESPONSE_OK_STATUS) var status by string(RESPONSE_OK_STATUS)
var payload: List<MessagePayload> var data: List<MessageData>
get() = config.getIndexed(MESSAGE_PAYLOAD_KEY).values.map { MessagePayload.wrap(it.node!!) } get() = config.getIndexed(MESSAGE_DATA_KEY).values.map { MessageData.wrap(it.node!!) }
set(value) { 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 * Append a payload to this message according to the given scheme
*/ */
fun <T : Configurable> append(spec: Specification<T>, block: T.() -> Unit): T = 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> { companion object : SchemeSpec<DeviceMessage>(::DeviceMessage), KSerializer<DeviceMessage> {
val MESSAGE_ACTION_KEY = "action".asName() val MESSAGE_TYPE_KEY = "action".asName()
val MESSAGE_PAYLOAD_KEY = "payload".asName() val MESSAGE_DATA_KEY = "data".asName()
val PAYLOAD_VALUE_KEY = "value".asName() val DATA_VALUE_KEY = "value".asName()
const val RESPONSE_OK_STATUS = "response.OK" const val RESPONSE_OK_STATUS = "response.OK"
const val RESPONSE_FAIL_STATUS = "response.FAIL" const val RESPONSE_FAIL_STATUS = "response.FAIL"
const val PROPERTY_CHANGED_ACTION = "event.propertyChange" const val PROPERTY_CHANGED_ACTION = "event.propertyChange"
@ -38,14 +36,14 @@ class DeviceMessage : Scheme() {
request: DeviceMessage? = null, request: DeviceMessage? = null,
block: DeviceMessage.() -> Unit = {} block: DeviceMessage.() -> Unit = {}
): DeviceMessage = DeviceMessage { ): DeviceMessage = DeviceMessage {
parent = request?.id target = request?.source
}.apply(block) }.apply(block)
inline fun fail( inline fun fail(
request: DeviceMessage? = null, request: DeviceMessage? = null,
block: DeviceMessage.() -> Unit = {} block: DeviceMessage.() -> Unit = {}
): DeviceMessage = DeviceMessage { ): DeviceMessage = DeviceMessage {
parent = request?.id target = request?.source
status = RESPONSE_FAIL_STATUS status = RESPONSE_FAIL_STATUS
}.apply(block) }.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 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 @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" comment = "Wrong target name $deviceTarget expected but ${request.target} found"
} }
} else try { } else try {
val result: List<MessagePayload> = when (val action = request.action) { val result: List<MessageData> = when (val action = request.type) {
GET_PROPERTY_ACTION -> { GET_PROPERTY_ACTION -> {
request.payload.map { property -> request.data.map { property ->
MessagePayload { MessageData {
name = property.name name = property.name
value = device.getProperty(name) value = device.getProperty(name)
} }
} }
} }
SET_PROPERTY_ACTION -> { SET_PROPERTY_ACTION -> {
request.payload.map { property -> request.data.map { property ->
val propertyName: String = property.name val propertyName: String = property.name
val propertyValue = property.value val propertyValue = property.value
if (propertyValue == null) { if (propertyValue == null) {
@ -58,15 +58,15 @@ class MessageController(
} else { } else {
device.setProperty(propertyName, propertyValue) device.setProperty(propertyName, propertyValue)
} }
MessagePayload { MessageData {
name = propertyName name = propertyName
value = device.getProperty(propertyName) value = device.getProperty(propertyName)
} }
} }
} }
EXECUTE_ACTION -> { EXECUTE_ACTION -> {
request.payload.map { payload -> request.data.map { payload ->
MessagePayload { MessageData {
name = payload.name name = payload.name
value = device.exec(payload.name, payload.value) value = device.exec(payload.name, payload.value)
} }
@ -74,7 +74,7 @@ class MessageController(
} }
PROPERTY_LIST_ACTION -> { PROPERTY_LIST_ACTION -> {
device.propertyDescriptors.map { descriptor -> device.propertyDescriptors.map { descriptor ->
MessagePayload { MessageData {
name = descriptor.name name = descriptor.name
value = MetaItem.NodeItem(descriptor.config) value = MetaItem.NodeItem(descriptor.config)
} }
@ -83,7 +83,7 @@ class MessageController(
ACTION_LIST_ACTION -> { ACTION_LIST_ACTION -> {
device.actionDescriptors.map { descriptor -> device.actionDescriptors.map { descriptor ->
MessagePayload { MessageData {
name = descriptor.name name = descriptor.name
value = MetaItem.NodeItem(descriptor.config) value = MetaItem.NodeItem(descriptor.config)
} }
@ -95,10 +95,9 @@ class MessageController(
} }
} }
DeviceMessage.ok { DeviceMessage.ok {
this.parent = request.id source = deviceTarget
this.origin = deviceTarget target = request.source
this.target = request.origin data = result
this.payload = result
} }
} catch (ex: Exception) { } catch (ex: Exception) {
DeviceMessage.fail { DeviceMessage.fail {
@ -123,8 +122,8 @@ class MessageController(
if (value == null) return if (value == null) return
scope.launch { scope.launch {
val change = DeviceMessage.ok { val change = DeviceMessage.ok {
this.origin = deviceTarget this.source = deviceTarget
action = PROPERTY_CHANGED_ACTION type = PROPERTY_CHANGED_ACTION
property { property {
name = propertyName name = propertyName
this.value = value this.value = value

View File

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

View File

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

View File

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