implement devices config endpoint

This commit is contained in:
Sabina Abdiganieva 2024-01-15 22:04:19 +03:00
parent 650e789fc5
commit e367bd62f1
5 changed files with 53 additions and 20 deletions

View File

@ -9,7 +9,7 @@ import hep.dataforge.optional
import javafx.scene.Scene
import javafx.stage.Stage
import org.slf4j.LoggerFactory
import tornadofx.*
import tornadofx.App
import java.util.*
/**
@ -46,8 +46,10 @@ abstract class NumassControlApplication<in D : Device> : App() {
abstract fun getDeviceMeta(config: Meta): Meta
fun getDeviceConfig() : Meta = getConfig(this).optional.orElseGet { readResourceMeta("config/devices.xml") }
private fun setupDevice(): D {
val config = getConfig(this).optional.orElseGet { readResourceMeta("config/devices.xml") }
val config = getDeviceConfig()
val ctx = setupContext(config)
val deviceConfig = getDeviceMeta(config)

View File

@ -100,7 +100,6 @@ fun getConfig(app: Application): Meta? {
}
}
fun findDeviceMeta(config: Meta, criterion: (Meta) -> Boolean): Meta? {
return config.getMetaList("device").stream().filter(criterion).findFirst().nullable
}

View File

@ -14,7 +14,7 @@ import javafx.stage.Stage
/**
* @author Alexander Nozik
*/
class ReadVac : NumassControlApplication<VacCollectorDevice>() {
open class ReadVac : NumassControlApplication<VacCollectorDevice>() {
override val deviceFactory = VacDeviceFactory()

View File

@ -1,5 +1,6 @@
plugins {
application
id("org.openjfx.javafxplugin") version "0.1.0"
}
group = "inr.numass"
@ -8,11 +9,18 @@ repositories {
mavenCentral()
}
javafx {
modules("javafx.controls", "javafx.web")
version = "16"
}
dependencies {
testImplementation(platform("org.junit:junit-bom:5.9.1"))
testImplementation("org.junit.jupiter:junit-jupiter")
implementation("io.ktor:ktor-server-core:2.3.7")
implementation("io.ktor:ktor-server-netty:2.3.7")
api(project(":numass-control:vac"))
api(project(":dataforge-core:dataforge-json"))
}

View File

@ -1,26 +1,50 @@
package inr.numass.webcontrol
import io.ktor.server.routing.*
import hep.dataforge.io.JSONMetaWriter
import inr.numass.control.readvac.ReadVac
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.request.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import java.nio.file.Paths
import io.ktor.server.http.content.*
fun main() {
import io.ktor.server.netty.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import javafx.application.Application
import javafx.stage.Stage
import java.nio.file.Paths
class ReadVacSvr : ReadVac() {
var server : NettyApplicationEngine? = null
var clickNumber = 0
embeddedServer(Netty, port = 8000) {
override fun start(stage: Stage) {
super.start(stage)
routing {
staticFiles("/", Paths.get(this.javaClass.classLoader.getResource("index.html").toURI()).toFile().parentFile)
get ("/api") {
call.respondText(call.parameters.toString())
}
get ("/api/clicker") {
call.respondText(clickNumber++.toString())
this.server = embeddedServer(Netty, port = 8000) {
routing {
staticFiles("/", Paths.get(this.javaClass.classLoader.getResource("index.html").toURI()).toFile().parentFile)
get ("/api") {
call.respondText(call.parameters.toString())
}
get ("/api/devices") {
call.respondOutputStream(ContentType.Application.Json, HttpStatusCode.OK, null) {
JSONMetaWriter.write(this, this@ReadVacSvr.getDeviceMeta(this@ReadVacSvr.getDeviceConfig()))
}
}
get ("/api/clicker") {
call.respondText(this@ReadVacSvr.clickNumber++.toString())
}
}
}
}.start(wait = true)
}
this.server!!.start(wait = false)
}
override fun stop() {
super.stop()
this.server?.stop()
}
}
fun main() {
Application.launch(ReadVacSvr::class.java)
}