From 6aa8afd70131f5a85843122ee23ce72a992e2f08 Mon Sep 17 00:00:00 2001 From: 00asdf Date: Tue, 22 Apr 2025 19:00:26 +0200 Subject: [PATCH] pid value interface on web page --- .idea/libraries/json.xml | 10 +++ VFHeatControl.iml | 1 + .../asdf00/visionfive/hc/GlobalDataStore.kt | 5 ++ .../dev/asdf00/visionfive/hc/server/Main.kt | 70 ++++++++++++++++++- 4 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 .idea/libraries/json.xml diff --git a/.idea/libraries/json.xml b/.idea/libraries/json.xml new file mode 100644 index 0000000..0ae7dc6 --- /dev/null +++ b/.idea/libraries/json.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/VFHeatControl.iml b/VFHeatControl.iml index 4eba30b..a38f787 100644 --- a/VFHeatControl.iml +++ b/VFHeatControl.iml @@ -11,5 +11,6 @@ + \ No newline at end of file diff --git a/src/main/kotlin/dev/asdf00/visionfive/hc/GlobalDataStore.kt b/src/main/kotlin/dev/asdf00/visionfive/hc/GlobalDataStore.kt index 3eed029..72cbb6b 100644 --- a/src/main/kotlin/dev/asdf00/visionfive/hc/GlobalDataStore.kt +++ b/src/main/kotlin/dev/asdf00/visionfive/hc/GlobalDataStore.kt @@ -6,5 +6,10 @@ import java.util.concurrent.CopyOnWriteArraySet object GlobalDataStore { @Volatile var temperature = 0.0 @Volatile var tempUpdated = Date() + @Volatile var isSunny = true val allowedTempUpdates = CopyOnWriteArraySet() + + @Volatile var pval = 0.0 + @Volatile var ival = 1.0 + @Volatile var dval = 2.0 } \ No newline at end of file diff --git a/src/main/kotlin/dev/asdf00/visionfive/hc/server/Main.kt b/src/main/kotlin/dev/asdf00/visionfive/hc/server/Main.kt index 42e916d..f545a33 100644 --- a/src/main/kotlin/dev/asdf00/visionfive/hc/server/Main.kt +++ b/src/main/kotlin/dev/asdf00/visionfive/hc/server/Main.kt @@ -5,6 +5,8 @@ import dev.asdf00.visionfive.hc.GlobalDataStore import java.nio.charset.StandardCharsets import java.util.* +import org.json.JSONObject + fun main() { val server = buildWebServer(8030) { // data collection @@ -43,10 +45,39 @@ fun main() {

Welcome to HeatControl

You are logged in as ${uname}. To log out, click here.

-

- The current recorded temperature is currently ${String.format("%.1f",GlobalDataStore.temperature)} C + The current recorded temperature is currently ${String.format("%.1f", GlobalDataStore.temperature)} C
+ Last updated ${GlobalDataStore.tempUpdated}
+ Current forecast for today: ${if (GlobalDataStore.isSunny) "sunny" else "cloudy"} +

+

PID values

+

+ + + + +
P:
I:
D:
+

+ """.trimIndent().uft8() @@ -97,7 +128,7 @@ fun main() { if (it.size == 2) Pair(it[0], it[1]) else Pair("", "") } 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( 200, 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()