diff --git a/numass-web-control/build.gradle.kts b/numass-web-control/build.gradle.kts index 1cf1d6b..648b9fa 100644 --- a/numass-web-control/build.gradle.kts +++ b/numass-web-control/build.gradle.kts @@ -19,6 +19,7 @@ dependencies { testImplementation("org.junit.jupiter:junit-jupiter") implementation("io.ktor:ktor-server-core:2.3.7") implementation("io.ktor:ktor-server-netty:2.3.7") + implementation("io.ktor:ktor-server-websockets:2.3.7") api(project(":numass-control:vac")) api(project(":dataforge-core:dataforge-json")) diff --git a/numass-web-control/src/main/java/inr/numass/webcontrol/Server.kt b/numass-web-control/src/main/java/inr/numass/webcontrol/Server.kt index 9e9e355..92fed7a 100644 --- a/numass-web-control/src/main/java/inr/numass/webcontrol/Server.kt +++ b/numass-web-control/src/main/java/inr/numass/webcontrol/Server.kt @@ -9,18 +9,60 @@ import io.ktor.server.http.content.* import io.ktor.server.netty.* import io.ktor.server.response.* import io.ktor.server.routing.* +import io.ktor.server.websocket.* +import io.ktor.websocket.* import javafx.application.Application import javafx.stage.Stage +import java.io.ByteArrayOutputStream import java.nio.file.Paths +import java.util.* +import java.util.concurrent.atomic.AtomicInteger + +class Connection(val session: DefaultWebSocketServerSession) { + companion object { + val lastId = AtomicInteger(0) + } + val name = "user${lastId.getAndIncrement()}" +} + class ReadVacSvr : ReadVac() { var server : NettyApplicationEngine? = null - var clickNumber = 0 override fun start(stage: Stage) { super.start(stage) this.server = embeddedServer(Netty, port = 8000) { + install(WebSockets) routing { staticFiles("/", Paths.get(this.javaClass.classLoader.getResource("index.html").toURI()).toFile().parentFile) + val connections = Collections.synchronizedSet(LinkedHashSet()) + webSocket("/echo") { + val thisConnection = Connection(this) + connections += thisConnection + try { + // Sending device list + val stream = ByteArrayOutputStream() + JSONMetaWriter.write(stream, this@ReadVacSvr.getDeviceMeta(this@ReadVacSvr.getDeviceConfig())) + send("{ \"devices\": $stream }") + for (frame in incoming) { + frame as? Frame.Text ?: continue + val receivedText = frame.readText() + when (receivedText) { + "" -> { + println("button was pressed") + connections.forEach { + it.session.send("{ \"measurement\" : true}") + } + } + "bye" -> close(CloseReason(CloseReason.Codes.NORMAL, "Client said BYE")) + } + } + } catch (e: Exception) { + println(e.localizedMessage) + } finally { + println("Removing $thisConnection") + connections -= thisConnection + } + } get ("/api") { call.respondText(call.parameters.toString()) } @@ -29,9 +71,6 @@ class ReadVacSvr : ReadVac() { JSONMetaWriter.write(this, this@ReadVacSvr.getDeviceMeta(this@ReadVacSvr.getDeviceConfig())) } } - get ("/api/clicker") { - call.respondText(this@ReadVacSvr.clickNumber++.toString()) - } } } diff --git a/numass-web-control/src/main/resources/index.html b/numass-web-control/src/main/resources/index.html index 1753ee2..7851ed8 100644 --- a/numass-web-control/src/main/resources/index.html +++ b/numass-web-control/src/main/resources/index.html @@ -8,16 +8,8 @@
- - - - - - Update interval: - - - - + +
diff --git a/numass-web-control/src/main/resources/script.js b/numass-web-control/src/main/resources/script.js index 3a210eb..b22c384 100644 --- a/numass-web-control/src/main/resources/script.js +++ b/numass-web-control/src/main/resources/script.js @@ -1,23 +1,3 @@ -function clickCounter(e) { - fetch("/api/clicker") - .then((response) => response.text()) - .then((text) => { document.querySelector("#click1").innerHTML=text } ) -} - -function measurement(e) { - console.log("measure") -} - -function storage(e) { - console.log("storage") -} - - -function cycle() { - console.log("asdffg") - - setTimeout(cycle, document.querySelector("input[name='interval']:checked").value * 1000) -} // Creates device card function drawCard(device_name = "Default", col = "red", measure = "---", power = false) { let card = document.createElement("div") @@ -41,7 +21,7 @@ function drawCard(device_name = "Default", col = "red", measure = "---", power = let middleSection = document.createElement("div") middleSection.classList = "middleSection" let measurement = document.createElement("span") - measurement.classList = "measure" + measurement.classList = "measureValue" measurement.textContent = measure measurement.style.color = col let mbar = document.createElement("span") @@ -67,10 +47,20 @@ function drawCard(device_name = "Default", col = "red", measure = "---", power = } document.querySelector("#sidebar").appendChild(card) } - var devices = [] - -//document.querySelector("#click1").addEventListener('click', clickCounter) +const nuWebSocket = new WebSocket("ws://" + window.location.host + "/echo") +nuWebSocket.onmessage = (event) => { + console.log(event.data) + const msg = JSON.parse(event.data) + if (msg.devices) { + msg.devices.sensor.forEach((device) => { + devices.push(device.name) + drawCard(device.name, device.color, "---", (device.sensorType == "mks") ) + }) + } +// if (msg.) +} +/* fetch("/api/devices") .then((response) => response.json()) .then((json) => { @@ -80,7 +70,7 @@ fetch("/api/devices") drawCard(device.name, device.color, "---", (device.sensorType == "mks") ) }) }) - +*/ var data = [] var layout = { @@ -93,6 +83,16 @@ var layout = { }; Plotly.newPlot('plot', data, layout, { responsive: true }); -console.log(devices) -//drawCard() -cycle() \ No newline at end of file + +document.querySelector("#measure").addEventListener("click", (e) => { + nuWebSocket.send("pressed") + console.log("pressed") +}) + +let onClick = null +let click = () -> { ... onClick("myBotton") } + +onClick = (name) => { console.log("Button " + name + " pressed") }; +click() // Button myButton pressed +onClick = (name) => { console.log("Button " + name + " pressed!!!") }; +click() // Button myButton pressed!!! \ No newline at end of file diff --git a/numass-web-control/src/main/resources/styles.css b/numass-web-control/src/main/resources/styles.css index 7782537..8b6c407 100644 --- a/numass-web-control/src/main/resources/styles.css +++ b/numass-web-control/src/main/resources/styles.css @@ -120,7 +120,7 @@ input.switch:checked:before { padding-right: 0.8em; } -.measure { +.measureValue { font-size: 2.5em; }