forked from NPM/numass-framework
change protocol to WebSocket
This commit is contained in:
parent
2177dd5b42
commit
9f60c14bd9
@ -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"))
|
||||
|
||||
|
@ -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<Connection?>(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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,16 +8,8 @@
|
||||
</head>
|
||||
<body>
|
||||
<div class="toolbar">
|
||||
<button id="measure" type="button" class="myButton toolbarButton" onclick="measurement()">Measure</button>
|
||||
<button id="store" type="button" class="myButton toolbarButton" onclick="storage()">Store</button>
|
||||
|
||||
<span class="vl"></span>
|
||||
|
||||
Update interval:
|
||||
<input type="radio" value="1" name="interval" checked />
|
||||
<input type="radio" value="5" name="interval" />
|
||||
<input type="radio" value="10" name="interval" />
|
||||
<input type="radio" value="15" name="interval" />
|
||||
<button id="measure" type="button" class="myButton toolbarButton">Measure</button>
|
||||
<button id="store" type="button" class="myButton toolbarButton">Store</button>
|
||||
|
||||
<button id="log" type="button" class="logButton toolbarButton">Log</button>
|
||||
</div>
|
||||
|
@ -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()
|
||||
|
||||
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!!!
|
@ -120,7 +120,7 @@ input.switch:checked:before {
|
||||
padding-right: 0.8em;
|
||||
}
|
||||
|
||||
.measure {
|
||||
.measureValue {
|
||||
font-size: 2.5em;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user