From 8b6a6abd9245e07d53402f724ff6f9f6f7c47f09 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 18 Sep 2023 09:00:04 +0300 Subject: [PATCH] Update to PiPlugin logic --- build.gradle.kts | 2 +- .../space/kscience/controls/pi/PiPlugin.kt | 24 +++++++++++++++++++ .../kscience/controls/pi/PiSerialPort.kt | 23 +++++++++++------- docs/templates/README-TEMPLATE.md | 2 ++ 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 619948b..d188449 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -13,7 +13,7 @@ val xodusVersion by extra("2.0.1") allprojects { group = "space.kscience" - version = "0.2.1" + version = "0.2.2-dev-1" repositories{ maven("https://maven.pkg.jetbrains.space/spc/p/sci/dev") } diff --git a/controls-pi/src/main/kotlin/space/kscience/controls/pi/PiPlugin.kt b/controls-pi/src/main/kotlin/space/kscience/controls/pi/PiPlugin.kt index 547a142..20d4c80 100644 --- a/controls-pi/src/main/kotlin/space/kscience/controls/pi/PiPlugin.kt +++ b/controls-pi/src/main/kotlin/space/kscience/controls/pi/PiPlugin.kt @@ -1,22 +1,46 @@ package space.kscience.controls.pi +import com.pi4j.Pi4J +import space.kscience.controls.manager.DeviceManager +import space.kscience.controls.ports.PortFactory import space.kscience.controls.ports.Ports import space.kscience.dataforge.context.AbstractPlugin import space.kscience.dataforge.context.Context import space.kscience.dataforge.context.PluginFactory import space.kscience.dataforge.context.PluginTag import space.kscience.dataforge.meta.Meta +import space.kscience.dataforge.names.Name +import space.kscience.dataforge.names.parseAsName +import com.pi4j.context.Context as PiContext public class PiPlugin : AbstractPlugin() { public val ports: Ports by require(Ports) + public val devices: DeviceManager by require(DeviceManager) override val tag: PluginTag get() = Companion.tag + public val piContext: PiContext by lazy { createPiContext(context, meta) } + + override fun content(target: String): Map = when (target) { + PortFactory.TYPE -> mapOf( + PiSerialPort.type.parseAsName() to PiSerialPort, + ) + + else -> super.content(target) + } + + override fun detach() { + piContext.shutdown() + super.detach() + } + public companion object : PluginFactory { override val tag: PluginTag = PluginTag("controls.ports.pi", group = PluginTag.DATAFORGE_GROUP) override fun build(context: Context, meta: Meta): PiPlugin = PiPlugin() + public fun createPiContext(context: Context, meta: Meta): PiContext = Pi4J.newAutoContext() + } } \ No newline at end of file diff --git a/controls-pi/src/main/kotlin/space/kscience/controls/pi/PiSerialPort.kt b/controls-pi/src/main/kotlin/space/kscience/controls/pi/PiSerialPort.kt index 4924b8d..6b9662d 100644 --- a/controls-pi/src/main/kotlin/space/kscience/controls/pi/PiSerialPort.kt +++ b/controls-pi/src/main/kotlin/space/kscience/controls/pi/PiSerialPort.kt @@ -1,6 +1,5 @@ package space.kscience.controls.pi -import com.pi4j.Pi4J import com.pi4j.io.serial.Baud import com.pi4j.io.serial.Serial import com.pi4j.io.serial.SerialConfigBuilder @@ -13,20 +12,25 @@ import space.kscience.controls.ports.toArray import space.kscience.dataforge.context.Context import space.kscience.dataforge.context.error import space.kscience.dataforge.context.logger +import space.kscience.dataforge.context.request import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.enum import space.kscience.dataforge.meta.get import space.kscience.dataforge.meta.string import java.nio.ByteBuffer import kotlin.coroutines.CoroutineContext +import com.pi4j.context.Context as PiContext public class PiSerialPort( context: Context, coroutineContext: CoroutineContext = context.coroutineContext, - public val serialBuilder: () -> Serial, + public val serialBuilder: PiContext.() -> Serial, ) : AbstractPort(context, coroutineContext) { - private val serial: Serial by lazy { serialBuilder() } + private val serial: Serial by lazy { + val pi = context.request(PiPlugin) + pi.piContext.serialBuilder() + } private val listenerJob = this.scope.launch(Dispatchers.IO) { @@ -57,15 +61,18 @@ public class PiSerialPort( public companion object : PortFactory { override val type: String get() = "pi" - public fun open(context: Context, device: String, block: SerialConfigBuilder.() -> Unit): PiSerialPort = - PiSerialPort(context) { - Pi4J.newAutoContext().serial(device, block) - } + public fun open( + context: Context, + device: String, + block: SerialConfigBuilder.() -> Unit, + ): PiSerialPort = PiSerialPort(context) { + serial(device, block) + } override fun build(context: Context, meta: Meta): Port = PiSerialPort(context) { val device: String = meta["device"].string ?: error("Device name not defined") val baudRate: Baud = meta["baudRate"].enum() ?: Baud._9600 - Pi4J.newAutoContext().serial(device) { + serial(device) { baud8N1(baudRate) } } diff --git a/docs/templates/README-TEMPLATE.md b/docs/templates/README-TEMPLATE.md index 11e0905..ef2cb74 100644 --- a/docs/templates/README-TEMPLATE.md +++ b/docs/templates/README-TEMPLATE.md @@ -1,5 +1,7 @@ [![JetBrains Research](https://jb.gg/badges/research.svg)](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub) +[![](https://maven.sciprog.center/api/badge/latest/kscience/space/kscience/controls-core-jvm?color=40c14a&name=repo.kotlin.link&prefix=v)](https://maven.sciprog.center/) + # Controls.kt Controls.kt (former DataForge-control) is a data acquisition framework (work in progress). It is based on DataForge, a software framework for automated data processing.