pid value interface on web page
This commit is contained in:
parent
abafed20e1
commit
6aa8afd701
10
.idea/libraries/json.xml
Normal file
10
.idea/libraries/json.xml
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
<component name="libraryTable">
|
||||||
|
<library name="json" type="repository">
|
||||||
|
<properties maven-id="org.json:json:20250107" />
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/json/json/20250107/json-20250107.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
</library>
|
||||||
|
</component>
|
||||||
|
|
@ -11,5 +11,6 @@
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
|
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
|
||||||
|
<orderEntry type="library" name="json" level="project" />
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
||||||
|
|
@ -6,5 +6,10 @@ import java.util.concurrent.CopyOnWriteArraySet
|
||||||
object GlobalDataStore {
|
object GlobalDataStore {
|
||||||
@Volatile var temperature = 0.0
|
@Volatile var temperature = 0.0
|
||||||
@Volatile var tempUpdated = Date()
|
@Volatile var tempUpdated = Date()
|
||||||
|
@Volatile var isSunny = true
|
||||||
val allowedTempUpdates = CopyOnWriteArraySet<String>()
|
val allowedTempUpdates = CopyOnWriteArraySet<String>()
|
||||||
|
|
||||||
|
@Volatile var pval = 0.0
|
||||||
|
@Volatile var ival = 1.0
|
||||||
|
@Volatile var dval = 2.0
|
||||||
}
|
}
|
||||||
|
|
@ -5,6 +5,8 @@ import dev.asdf00.visionfive.hc.GlobalDataStore
|
||||||
import java.nio.charset.StandardCharsets
|
import java.nio.charset.StandardCharsets
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
|
import org.json.JSONObject
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
val server = buildWebServer(8030) {
|
val server = buildWebServer(8030) {
|
||||||
// data collection
|
// data collection
|
||||||
|
|
@ -43,10 +45,39 @@ fun main() {
|
||||||
<body>
|
<body>
|
||||||
<h1>Welcome to HeatControl</h1>
|
<h1>Welcome to HeatControl</h1>
|
||||||
<p>You are logged in as ${uname}. To log out, click <a href="/login/out">here</a>.</p>
|
<p>You are logged in as ${uname}. To log out, click <a href="/login/out">here</a>.</p>
|
||||||
<br />
|
|
||||||
<p>
|
<p>
|
||||||
The current recorded temperature is currently ${String.format("%.1f",GlobalDataStore.temperature)} C
|
The current recorded temperature is currently ${String.format("%.1f", GlobalDataStore.temperature)} C<br />
|
||||||
|
Last updated ${GlobalDataStore.tempUpdated}<br />
|
||||||
|
Current forecast for today: ${if (GlobalDataStore.isSunny) "sunny" else "cloudy"}
|
||||||
|
</p><p />
|
||||||
|
<h3>PID values</h3>
|
||||||
|
<p>
|
||||||
|
<table>
|
||||||
|
<tr><td>P:</td><td><input id="p_val" type="number" /></td></tr>
|
||||||
|
<tr><td>I:</td><td><input id="i_val" type="number" /></td></tr>
|
||||||
|
<tr><td>D:</td><td><input id="d_val" type="number" /></td></tr>
|
||||||
|
</table>
|
||||||
|
<button onclick="onSubmit()">submit</button> <button onclick="onFetch()">fetch</button>
|
||||||
</p>
|
</p>
|
||||||
|
<script>
|
||||||
|
function onSubmit() {
|
||||||
|
fetch("/pid", {method: "POST", body: JSON.stringify({
|
||||||
|
p: document.getElementById("p_val").value,
|
||||||
|
i: document.getElementById("i_val").value,
|
||||||
|
d: document.getElementById("d_val").value
|
||||||
|
}),
|
||||||
|
headers: { "Content-type": "application/json; charset=UTF-8"}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
function onFetch() {
|
||||||
|
fetch("/pid", {method: "GET"}).then(response => response.json()).then(data => {
|
||||||
|
document.getElementById("p_val").value = data.p
|
||||||
|
document.getElementById("i_val").value = data.i
|
||||||
|
document.getElementById("d_val").value = data.d
|
||||||
|
})
|
||||||
|
}
|
||||||
|
onFetch()
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
""".trimIndent().uft8()
|
""".trimIndent().uft8()
|
||||||
|
|
@ -97,7 +128,7 @@ fun main() {
|
||||||
if (it.size == 2) Pair(it[0], it[1]) else Pair("", "")
|
if (it.size == 2) Pair(it[0], it[1]) else Pair("", "")
|
||||||
}
|
}
|
||||||
LoginBehavior.login(userName, pwd)?.let {
|
LoginBehavior.login(userName, pwd)?.let {
|
||||||
exchange.responseHeaders.add("Set-Cookie", "auth=${it}; Max-Age=600; Path=/")
|
exchange.responseHeaders.add("Set-Cookie", "auth=${it}; Path=/; HttpOnly; SameSite=Strict")
|
||||||
exchange.sendReply(
|
exchange.sendReply(
|
||||||
200,
|
200,
|
||||||
ContentType.HTML,
|
ContentType.HTML,
|
||||||
|
|
@ -130,6 +161,39 @@ fun main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pid control
|
||||||
|
endpoint("pid") handler@{ exchange ->
|
||||||
|
if (LoginBehavior.isLoggedIn(getLoginToken(exchange)) == null) {
|
||||||
|
// not logged in
|
||||||
|
exchange.replyCat(401)
|
||||||
|
return@handler
|
||||||
|
}
|
||||||
|
if (exchange.requestMethod == "POST") {
|
||||||
|
// new values
|
||||||
|
try {
|
||||||
|
val data = JSONObject(exchange.requestBody.readAllBytes().utf8()).toMap()
|
||||||
|
if (!data.containsKey("p") || !data.containsKey("i") || ! data.containsKey("d")) {
|
||||||
|
exchange.replyCat(400)
|
||||||
|
} else {
|
||||||
|
val p = data["p"].toString().toDouble()
|
||||||
|
val i = data["i"].toString().toDouble()
|
||||||
|
val d = data["d"].toString().toDouble()
|
||||||
|
GlobalDataStore.pval = p
|
||||||
|
GlobalDataStore.ival = i
|
||||||
|
GlobalDataStore.dval = d
|
||||||
|
exchange.replyCat(200)
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
exchange.replyCat(400)
|
||||||
|
}
|
||||||
|
} else if (exchange.requestMethod == "GET") {
|
||||||
|
// get current values
|
||||||
|
exchange.sendReply(200, ContentType.JSON, "{\"p\": ${GlobalDataStore.pval}, \"i\": ${GlobalDataStore.ival}, \"d\": ${GlobalDataStore.dval}}".uft8())
|
||||||
|
} else {
|
||||||
|
exchange.replyCat(400)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
server.start()
|
server.start()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user