Add MQTT prototype (not tested)

This commit is contained in:
Alexander Nozik 2023-04-15 20:00:47 +03:00
parent 00c66da847
commit 197675fc15
14 changed files with 110 additions and 14 deletions

View File

@ -15,6 +15,6 @@ kscience {
}
dependencies {
api("space.kscience:dataforge-io:$dataforgeVersion")
api(npmlibs.kotlinx.datetime)
api(spclibs.kotlinx.datetime)
}
}

View File

@ -11,7 +11,7 @@ dependencies {
// implementation("org.jetbrains.xodus:xodus-environment:$xodusVersion")
// implementation("org.jetbrains.xodus:xodus-vfs:$xodusVersion")
testImplementation(npmlibs.kotlinx.coroutines.test)
testImplementation(spclibs.kotlinx.coroutines.test)
}
readme{

View File

@ -29,9 +29,13 @@ dependencies {
implementation("ch.qos.logback:logback-classic:1.2.11")
}
kotlin{
jvmToolchain(11)
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = "11"
freeCompilerArgs = freeCompilerArgs + listOf("-Xjvm-default=all", "-Xopt-in=kotlin.RequiresOptIn")
}
}

View File

@ -35,9 +35,12 @@ dependencies {
// implementation("org.litote.kmongo:kmongo-coroutine-serialization:4.4.0")
}
kotlin{
jvmToolchain(11)
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = "11"
freeCompilerArgs = freeCompilerArgs + listOf("-Xjvm-default=all", "-Xopt-in=kotlin.RequiresOptIn")
}
}

View File

@ -38,7 +38,7 @@ class VirtualCarController : Controller(), ContextAware {
plugin(DeviceManager)
}
private val deviceManager = context.fetch(DeviceManager, Meta {
private val deviceManager = context.request(DeviceManager, Meta {
"xodusConfig" put {
"entityStorePath" put deviceEntityStorePath.toString()
}

View File

@ -19,10 +19,12 @@ dependencies {
implementation("ch.qos.logback:logback-classic:1.2.11")
}
kotlin{
jvmToolchain(11)
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = "11"
freeCompilerArgs = freeCompilerArgs + listOf("-Xjvm-default=all", "-Xopt-in=kotlin.RequiresOptIn")
}
}

View File

@ -8,7 +8,7 @@ dependencies{
implementation(projects.magix.magixServer)
implementation(projects.magix.magixZmq)
implementation(projects.magix.magixRsocket)
implementation("ch.qos.logback:logback-classic:1.2.3")
implementation(spclibs.logback.classic)
}
kotlin{

View File

@ -17,7 +17,7 @@ import ru.mipt.npm.devices.pimotionmaster.PiMotionMasterDevice.Axis.Companion.po
import space.kscience.controls.manager.DeviceManager
import space.kscience.controls.manager.installing
import space.kscience.dataforge.context.Context
import space.kscience.dataforge.context.fetch
import space.kscience.dataforge.context.request
import tornadofx.*
class PiMotionMasterApp : App(PiMotionMasterView::class)
@ -29,7 +29,7 @@ class PiMotionMasterController : Controller() {
}
//initialize deviceManager plugin
val deviceManager: DeviceManager = context.fetch(DeviceManager)
val deviceManager: DeviceManager = context.request(DeviceManager)
// install device
val motionMaster: PiMotionMasterDevice by deviceManager.installing(PiMotionMasterDevice)

View File

@ -7,4 +7,4 @@ org.gradle.parallel=true
publishing.github=false
publishing.sonatype=false
toolsVersion=0.14.3-kotlin-1.8.10
toolsVersion=0.14.6-kotlin-1.8.20

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -0,0 +1,18 @@
plugins {
id("space.kscience.gradle.jvm")
`maven-publish`
}
description = """
MQTT client magix endpoint
""".trimIndent()
dependencies {
api(projects.magix.magixApi)
implementation("com.hivemq:hivemq-mqtt-client:1.3.1")
implementation(spclibs.kotlinx.coroutines.jdk8)
}
readme{
maturity = space.kscience.gradle.Maturity.PROTOTYPE
}

View File

@ -0,0 +1,69 @@
package space.ksceince.magix.mqtt
import com.hivemq.client.mqtt.MqttClient
import com.hivemq.client.mqtt.datatypes.MqttQos
import com.hivemq.client.mqtt.mqtt5.Mqtt5AsyncClient
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.future.await
import space.kscience.magix.api.MagixEndpoint
import space.kscience.magix.api.MagixMessage
import space.kscience.magix.api.MagixMessageFilter
import java.util.*
public class MqttMagixEndpoint(
serverHost: String,
clientId: String = UUID.randomUUID().toString(),
public val topic: String = DEFAULT_MAGIX_TOPIC_NAME,
public val qos: MqttQos = MqttQos.AT_LEAST_ONCE,
) : MagixEndpoint, AutoCloseable {
private val client: Mqtt5AsyncClient by lazy {
MqttClient.builder()
.identifier(clientId)
.serverHost(serverHost)
.useMqttVersion5()
.buildAsync()
}
private val connection by lazy {
client.connect()
}
override fun subscribe(filter: MagixMessageFilter): Flow<MagixMessage> = callbackFlow {
connection.await()
client.subscribeWith()
.topicFilter(topic)
.qos(qos)
.callback { published ->
val message = MagixEndpoint.magixJson.decodeFromString(
MagixMessage.serializer(),
published.payloadAsBytes.decodeToString()
)
trySendBlocking(message)
}
.send()
awaitClose {
client.disconnect()
}
}
override suspend fun broadcast(message: MagixMessage) {
connection.await()
client.publishWith().topic(topic).qos(qos).payload(
MagixEndpoint.magixJson.encodeToString(MagixMessage.serializer(), message).encodeToByteArray()
).send()
}
override fun close() {
client.disconnect()
}
public companion object {
public const val DEFAULT_MAGIX_TOPIC_NAME: String = "magix"
}
}

View File

@ -13,7 +13,7 @@ dependencies {
api(projects.magix.magixApi)
implementation("org.jetbrains.xodus:xodus-entity-store:$xodusVersion")
testImplementation(npmlibs.kotlinx.coroutines.test)
testImplementation(spclibs.kotlinx.coroutines.test)
}
readme{

View File

@ -1,7 +1,6 @@
rootProject.name = "controls-kt"
enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")
enableFeaturePreview("VERSION_CATALOGS")
pluginManagement {
@ -34,7 +33,7 @@ dependencyResolutionManagement {
}
versionCatalogs {
create("npmlibs") {
create("spclibs") {
from("space.kscience:version-catalog:$toolsVersion")
}
}
@ -56,6 +55,7 @@ include(
":magix:magix-java-client",
":magix:magix-zmq",
":magix:magix-rabbit",
":magix:magix-mqtt",
// ":magix:magix-storage",
":magix:magix-storage:magix-storage-xodus",