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 8d3af6f..aecd04d 100644
--- a/src/main/kotlin/dev/asdf00/visionfive/hc/server/Main.kt
+++ b/src/main/kotlin/dev/asdf00/visionfive/hc/server/Main.kt
@@ -92,15 +92,15 @@ fun main() {
Welcome to HeatControl
- Please log in to view the current temperature
+ Please log in to view the current temperature.
Login Form
@@ -224,7 +224,7 @@ fun main() {
}
val forecastQuery = Thread {
- while (!Thread.interrupted()) {
+ whileNotInterrupted {
var success = false
val request = HttpRequest.newBuilder(URI.create(GlobalDataStore.API)).GET().build()
HttpClient.newHttpClient()
@@ -320,7 +320,7 @@ fun main() {
val pidController = PidController(c1, c2, c3)
var cycles = -1
- while (!Thread.interrupted()) {
+ whileNotInterrupted {
if (cycles < 0) {
c1.runCalibrationSequence()
c2.runCalibrationSequence()
@@ -372,8 +372,17 @@ private fun getAuthToken(exchange: HttpExchange) =
exchange.requestHeaders["Cookie"]
?.let { if (it.size > 0) it[0] else null }
?.let { it.split("; ") }
- ?.filter { it.startsWith("auth=") }
- ?.firstOrNull()
+ ?.firstOrNull { it.startsWith("auth=") }
?.let { it.split("=").let { if (it.size == 2) it[1] else "" } }
?: ""
+fun whileNotInterrupted(body: () -> Unit) {
+ try {
+ while (!Thread.interrupted()) {
+ body()
+ }
+ Thread.currentThread().interrupt()
+ } catch (e: InterruptedException) {
+ Thread.currentThread().interrupt()
+ }
+}