From fa2414ef473feaa33402e13c68b6d89f949757c8 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 2 Feb 2024 16:04:41 +0300 Subject: [PATCH] Add demo for device message listening --- .../kscience/controls/api/DeviceMessage.kt | 2 +- .../controls/demo/DemoControllerView.kt | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/controls-core/src/commonMain/kotlin/space/kscience/controls/api/DeviceMessage.kt b/controls-core/src/commonMain/kotlin/space/kscience/controls/api/DeviceMessage.kt index 4cd91b0..406cfca 100644 --- a/controls-core/src/commonMain/kotlin/space/kscience/controls/api/DeviceMessage.kt +++ b/controls-core/src/commonMain/kotlin/space/kscience/controls/api/DeviceMessage.kt @@ -1,4 +1,4 @@ -@file:OptIn(ExperimentalSerializationApi::class, ExperimentalSerializationApi::class) +@file:OptIn(ExperimentalSerializationApi::class) package space.kscience.controls.api diff --git a/demo/all-things/src/main/kotlin/space/kscience/controls/demo/DemoControllerView.kt b/demo/all-things/src/main/kotlin/space/kscience/controls/demo/DemoControllerView.kt index 8edd90e..2d2086b 100644 --- a/demo/all-things/src/main/kotlin/space/kscience/controls/demo/DemoControllerView.kt +++ b/demo/all-things/src/main/kotlin/space/kscience/controls/demo/DemoControllerView.kt @@ -5,10 +5,17 @@ import javafx.scene.Parent import javafx.scene.control.Slider import javafx.scene.layout.Priority import javafx.stage.Stage +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch +import kotlinx.serialization.json.Json import org.eclipse.milo.opcua.sdk.server.OpcUaServer import org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText +import space.kscience.controls.api.DeviceMessage +import space.kscience.controls.api.GetDescriptionMessage +import space.kscience.controls.api.PropertyChangedMessage import space.kscience.controls.client.launchMagixService +import space.kscience.controls.client.magixFormat import space.kscience.controls.demo.DemoDevice.Companion.cosScale import space.kscience.controls.demo.DemoDevice.Companion.sinScale import space.kscience.controls.demo.DemoDevice.Companion.timeScale @@ -20,6 +27,8 @@ import space.kscience.controls.opcua.server.serveDevices import space.kscience.controls.spec.write import space.kscience.dataforge.context.* import space.kscience.magix.api.MagixEndpoint +import space.kscience.magix.api.send +import space.kscience.magix.api.subscribe import space.kscience.magix.rsocket.rSocketWithTcp import space.kscience.magix.rsocket.rSocketWithWebSockets import space.kscience.magix.server.RSocketMagixFlowPlugin @@ -49,6 +58,7 @@ class DemoController : Controller(), ContextAware { private val deviceManager = context.request(DeviceManager) + fun init() { context.launch { device = deviceManager.install("demo", DemoDevice) @@ -67,6 +77,17 @@ class DemoController : Controller(), ContextAware { //serve devices as OPC-UA namespace opcUaServer.startup() opcUaServer.serveDevices(deviceManager) + + + val listenerEndpoint = MagixEndpoint.rSocketWithWebSockets("localhost") + listenerEndpoint.subscribe(DeviceManager.magixFormat).onEach { (_, deviceMessage)-> + // print all messages that are not property change message + if(deviceMessage !is PropertyChangedMessage){ + println(">> ${Json.encodeToString(DeviceMessage.serializer(), deviceMessage)}") + } + }.launchIn(this) + listenerEndpoint.send(DeviceManager.magixFormat, GetDescriptionMessage(), "listener", "controls-kt") + } }