Update to PiPlugin logic

This commit is contained in:
Alexander Nozik 2023-09-18 09:00:04 +03:00
parent bc5037b256
commit 8b6a6abd92
4 changed files with 42 additions and 9 deletions

View File

@ -13,7 +13,7 @@ val xodusVersion by extra("2.0.1")
allprojects { allprojects {
group = "space.kscience" group = "space.kscience"
version = "0.2.1" version = "0.2.2-dev-1"
repositories{ repositories{
maven("https://maven.pkg.jetbrains.space/spc/p/sci/dev") maven("https://maven.pkg.jetbrains.space/spc/p/sci/dev")
} }

View File

@ -1,22 +1,46 @@
package space.kscience.controls.pi 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.controls.ports.Ports
import space.kscience.dataforge.context.AbstractPlugin import space.kscience.dataforge.context.AbstractPlugin
import space.kscience.dataforge.context.Context import space.kscience.dataforge.context.Context
import space.kscience.dataforge.context.PluginFactory import space.kscience.dataforge.context.PluginFactory
import space.kscience.dataforge.context.PluginTag import space.kscience.dataforge.context.PluginTag
import space.kscience.dataforge.meta.Meta 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 class PiPlugin : AbstractPlugin() {
public val ports: Ports by require(Ports) public val ports: Ports by require(Ports)
public val devices: DeviceManager by require(DeviceManager)
override val tag: PluginTag get() = Companion.tag override val tag: PluginTag get() = Companion.tag
public val piContext: PiContext by lazy { createPiContext(context, meta) }
override fun content(target: String): Map<Name, Any> = 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<PiPlugin> { public companion object : PluginFactory<PiPlugin> {
override val tag: PluginTag = PluginTag("controls.ports.pi", group = PluginTag.DATAFORGE_GROUP) override val tag: PluginTag = PluginTag("controls.ports.pi", group = PluginTag.DATAFORGE_GROUP)
override fun build(context: Context, meta: Meta): PiPlugin = PiPlugin() override fun build(context: Context, meta: Meta): PiPlugin = PiPlugin()
public fun createPiContext(context: Context, meta: Meta): PiContext = Pi4J.newAutoContext()
} }
} }

View File

@ -1,6 +1,5 @@
package space.kscience.controls.pi package space.kscience.controls.pi
import com.pi4j.Pi4J
import com.pi4j.io.serial.Baud import com.pi4j.io.serial.Baud
import com.pi4j.io.serial.Serial import com.pi4j.io.serial.Serial
import com.pi4j.io.serial.SerialConfigBuilder 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.Context
import space.kscience.dataforge.context.error import space.kscience.dataforge.context.error
import space.kscience.dataforge.context.logger import space.kscience.dataforge.context.logger
import space.kscience.dataforge.context.request
import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.meta.enum import space.kscience.dataforge.meta.enum
import space.kscience.dataforge.meta.get import space.kscience.dataforge.meta.get
import space.kscience.dataforge.meta.string import space.kscience.dataforge.meta.string
import java.nio.ByteBuffer import java.nio.ByteBuffer
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
import com.pi4j.context.Context as PiContext
public class PiSerialPort( public class PiSerialPort(
context: Context, context: Context,
coroutineContext: CoroutineContext = context.coroutineContext, coroutineContext: CoroutineContext = context.coroutineContext,
public val serialBuilder: () -> Serial, public val serialBuilder: PiContext.() -> Serial,
) : AbstractPort(context, coroutineContext) { ) : 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) { private val listenerJob = this.scope.launch(Dispatchers.IO) {
@ -57,15 +61,18 @@ public class PiSerialPort(
public companion object : PortFactory { public companion object : PortFactory {
override val type: String get() = "pi" override val type: String get() = "pi"
public fun open(context: Context, device: String, block: SerialConfigBuilder.() -> Unit): PiSerialPort = public fun open(
PiSerialPort(context) { context: Context,
Pi4J.newAutoContext().serial(device, block) device: String,
block: SerialConfigBuilder.() -> Unit,
): PiSerialPort = PiSerialPort(context) {
serial(device, block)
} }
override fun build(context: Context, meta: Meta): Port = PiSerialPort(context) { override fun build(context: Context, meta: Meta): Port = PiSerialPort(context) {
val device: String = meta["device"].string ?: error("Device name not defined") val device: String = meta["device"].string ?: error("Device name not defined")
val baudRate: Baud = meta["baudRate"].enum<Baud>() ?: Baud._9600 val baudRate: Baud = meta["baudRate"].enum<Baud>() ?: Baud._9600
Pi4J.newAutoContext().serial(device) { serial(device) {
baud8N1(baudRate) baud8N1(baudRate)
} }
} }

View File

@ -1,5 +1,7 @@
[![JetBrains Research](https://jb.gg/badges/research.svg)](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub) [![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
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. 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.