web page tweaks
This commit is contained in:
parent
6aa8afd701
commit
d24f0b9432
|
|
@ -1,14 +1,13 @@
|
||||||
package dev.asdf00.visionfive.hc
|
package dev.asdf00.visionfive.hc
|
||||||
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
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
|
@Volatile var isSunny = true
|
||||||
val allowedTempUpdates = CopyOnWriteArraySet<String>()
|
|
||||||
|
|
||||||
|
@Volatile var targetTemp = 22.5
|
||||||
@Volatile var pval = 0.0
|
@Volatile var pval = 0.0
|
||||||
@Volatile var ival = 1.0
|
@Volatile var ival = 1.0
|
||||||
@Volatile var dval = 2.0
|
@Volatile var dval = 2.0
|
||||||
|
|
|
||||||
|
|
@ -8,27 +8,8 @@ import java.util.*
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
|
// out-facing web page
|
||||||
val server = buildWebServer(8030) {
|
val server = buildWebServer(8030) {
|
||||||
// data collection
|
|
||||||
endpoint("temp") handler@{ exchange ->
|
|
||||||
if (exchange.remoteAddress.address.toString() !in GlobalDataStore.allowedTempUpdates) {
|
|
||||||
exchange.replyCat(403)
|
|
||||||
}
|
|
||||||
exchange.requestURI.query.split("&").forEach {
|
|
||||||
if (it.startsWith("t=")) {
|
|
||||||
it.substring(2).toDoubleOrNull()?.let {
|
|
||||||
GlobalDataStore.temperature = it
|
|
||||||
GlobalDataStore.tempUpdated = Date()
|
|
||||||
exchange.replyCat(200)
|
|
||||||
} ?: run {
|
|
||||||
exchange.replyCat(400)
|
|
||||||
}
|
|
||||||
return@handler
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exchange.replyCat(400)
|
|
||||||
}
|
|
||||||
|
|
||||||
// main landing page
|
// main landing page
|
||||||
endpoint("") { exchange ->
|
endpoint("") { exchange ->
|
||||||
exchange.responseHeaders.add("Cache-Control", "no-store")
|
exchange.responseHeaders.add("Cache-Control", "no-store")
|
||||||
|
|
@ -53,6 +34,7 @@ fun main() {
|
||||||
<h3>PID values</h3>
|
<h3>PID values</h3>
|
||||||
<p>
|
<p>
|
||||||
<table>
|
<table>
|
||||||
|
<tr><td>Temp:</td><td><input id="temp_val" type="number" /></td></tr>
|
||||||
<tr><td>P:</td><td><input id="p_val" type="number" /></td></tr>
|
<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>I:</td><td><input id="i_val" type="number" /></td></tr>
|
||||||
<tr><td>D:</td><td><input id="d_val" type="number" /></td></tr>
|
<tr><td>D:</td><td><input id="d_val" type="number" /></td></tr>
|
||||||
|
|
@ -62,6 +44,7 @@ fun main() {
|
||||||
<script>
|
<script>
|
||||||
function onSubmit() {
|
function onSubmit() {
|
||||||
fetch("/pid", {method: "POST", body: JSON.stringify({
|
fetch("/pid", {method: "POST", body: JSON.stringify({
|
||||||
|
temp: document.getElementById("temp_val").value,
|
||||||
p: document.getElementById("p_val").value,
|
p: document.getElementById("p_val").value,
|
||||||
i: document.getElementById("i_val").value,
|
i: document.getElementById("i_val").value,
|
||||||
d: document.getElementById("d_val").value
|
d: document.getElementById("d_val").value
|
||||||
|
|
@ -71,6 +54,7 @@ fun main() {
|
||||||
}
|
}
|
||||||
function onFetch() {
|
function onFetch() {
|
||||||
fetch("/pid", {method: "GET"}).then(response => response.json()).then(data => {
|
fetch("/pid", {method: "GET"}).then(response => response.json()).then(data => {
|
||||||
|
document.getElementById("temp_val").value = data.temp
|
||||||
document.getElementById("p_val").value = data.p
|
document.getElementById("p_val").value = data.p
|
||||||
document.getElementById("i_val").value = data.i
|
document.getElementById("i_val").value = data.i
|
||||||
document.getElementById("d_val").value = data.d
|
document.getElementById("d_val").value = data.d
|
||||||
|
|
@ -129,6 +113,7 @@ fun main() {
|
||||||
}
|
}
|
||||||
LoginBehavior.login(userName, pwd)?.let {
|
LoginBehavior.login(userName, pwd)?.let {
|
||||||
exchange.responseHeaders.add("Set-Cookie", "auth=${it}; Path=/; HttpOnly; SameSite=Strict")
|
exchange.responseHeaders.add("Set-Cookie", "auth=${it}; Path=/; HttpOnly; SameSite=Strict")
|
||||||
|
exchange.responseHeaders.add("Set-Cookie", "test=2; Path=/; HttpOnly; SameSite=Strict")
|
||||||
exchange.sendReply(
|
exchange.sendReply(
|
||||||
200,
|
200,
|
||||||
ContentType.HTML,
|
ContentType.HTML,
|
||||||
|
|
@ -173,12 +158,14 @@ fun main() {
|
||||||
// new values
|
// new values
|
||||||
try {
|
try {
|
||||||
val data = JSONObject(exchange.requestBody.readAllBytes().utf8()).toMap()
|
val data = JSONObject(exchange.requestBody.readAllBytes().utf8()).toMap()
|
||||||
if (!data.containsKey("p") || !data.containsKey("i") || ! data.containsKey("d")) {
|
if (!data.containsKey("temp") || !data.containsKey("p") || !data.containsKey("i") || ! data.containsKey("d")) {
|
||||||
exchange.replyCat(400)
|
exchange.replyCat(400)
|
||||||
} else {
|
} else {
|
||||||
|
val temp = data["temp"].toString().toDouble()
|
||||||
val p = data["p"].toString().toDouble()
|
val p = data["p"].toString().toDouble()
|
||||||
val i = data["i"].toString().toDouble()
|
val i = data["i"].toString().toDouble()
|
||||||
val d = data["d"].toString().toDouble()
|
val d = data["d"].toString().toDouble()
|
||||||
|
GlobalDataStore.targetTemp = temp
|
||||||
GlobalDataStore.pval = p
|
GlobalDataStore.pval = p
|
||||||
GlobalDataStore.ival = i
|
GlobalDataStore.ival = i
|
||||||
GlobalDataStore.dval = d
|
GlobalDataStore.dval = d
|
||||||
|
|
@ -189,17 +176,35 @@ fun main() {
|
||||||
}
|
}
|
||||||
} else if (exchange.requestMethod == "GET") {
|
} else if (exchange.requestMethod == "GET") {
|
||||||
// get current values
|
// get current values
|
||||||
exchange.sendReply(200, ContentType.JSON, "{\"p\": ${GlobalDataStore.pval}, \"i\": ${GlobalDataStore.ival}, \"d\": ${GlobalDataStore.dval}}".uft8())
|
exchange.sendReply(200, ContentType.JSON, "{\"temp\": ${GlobalDataStore.targetTemp}, \"p\": ${GlobalDataStore.pval}, \"i\": ${GlobalDataStore.ival}, \"d\": ${GlobalDataStore.dval}}".uft8())
|
||||||
} else {
|
} else {
|
||||||
exchange.replyCat(400)
|
exchange.replyCat(400)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val tempServer = buildWebServer(8088) {
|
||||||
|
// data collection
|
||||||
|
endpoint("temp") handler@{ exchange ->
|
||||||
|
exchange.requestURI.query.split("&").forEach {
|
||||||
|
if (it.startsWith("t=")) {
|
||||||
|
it.substring(2).toDoubleOrNull()?.let {
|
||||||
|
GlobalDataStore.temperature = it
|
||||||
|
GlobalDataStore.tempUpdated = Date()
|
||||||
|
exchange.replyCat(200)
|
||||||
|
} ?: run {
|
||||||
|
exchange.replyCat(400)
|
||||||
|
}
|
||||||
|
return@handler
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exchange.replyCat(400)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
server.start()
|
server.start()
|
||||||
|
tempServer.start()
|
||||||
println("HttpServer is running, press CTRL+C to exit")
|
println("HttpServer is running, press CTRL+C to exit")
|
||||||
print("IP address of temperature sensor: ")
|
|
||||||
GlobalDataStore.allowedTempUpdates += "/${readln()}"
|
|
||||||
while (!Thread.interrupted()) {
|
while (!Thread.interrupted()) {
|
||||||
println()
|
println()
|
||||||
print("Username to add: ")
|
print("Username to add: ")
|
||||||
|
|
@ -217,5 +222,9 @@ fun ByteArray.utf8() = String(this, StandardCharsets.UTF_8)
|
||||||
private fun getLoginToken(exchange: HttpExchange) =
|
private fun getLoginToken(exchange: HttpExchange) =
|
||||||
exchange.requestHeaders["Cookie"]
|
exchange.requestHeaders["Cookie"]
|
||||||
?.let { if (it.size > 0) it[0] else null }
|
?.let { if (it.size > 0) it[0] else null }
|
||||||
|
?.let { it.split("; ") }
|
||||||
|
?.filter { it.startsWith("auth=") }
|
||||||
|
?.firstOrNull()
|
||||||
?.let { it.split("=").let { if (it.size == 2) it[1] else "" } }
|
?.let { it.split("=").let { if (it.size == 2) it[1] else "" } }
|
||||||
?: ""
|
?: ""
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user