From e7768d4dc846acfe963444c8655ab7d2797f1034 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 28 May 2016 18:24:28 +0200 Subject: [PATCH] Add wetter.de trigger --- .../webpages/weather/wetterde/WetterDeTrigger.kt | 84 +++++++++++++++++++++ .../weather/wetterde/WetterDeTriggerTest.kt | 87 ++++++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wetterde/WetterDeTrigger.kt create mode 100644 src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wetterde/WetterDeTriggerTest.kt diff --git a/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wetterde/WetterDeTrigger.kt b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wetterde/WetterDeTrigger.kt new file mode 100644 index 0000000..39ec116 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wetterde/WetterDeTrigger.kt @@ -0,0 +1,84 @@ +package net.pterodactylus.rhynodge.webpages.weather.wetterde + +import kotlinx.html.body +import kotlinx.html.div +import kotlinx.html.h1 +import kotlinx.html.head +import kotlinx.html.html +import kotlinx.html.img +import kotlinx.html.stream.createHTML +import kotlinx.html.style +import kotlinx.html.unsafe +import net.pterodactylus.rhynodge.Reaction +import net.pterodactylus.rhynodge.State +import net.pterodactylus.rhynodge.Trigger +import net.pterodactylus.rhynodge.output.DefaultOutput +import net.pterodactylus.rhynodge.output.Output +import java.text.DateFormat +import java.time.temporal.ChronoUnit +import java.util.Locale + +/** + * TODO + * + * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net) + */ +class WetterDeTrigger : Trigger { + + private val dateFormatter = DateFormat.getDateInstance(DateFormat.LONG, Locale.ENGLISH) + private lateinit var state: WetterDeState + private var changed = false + + override fun mergeStates(previousState: State, currentState: State): State { + changed = previousState != currentState + state = currentState as WetterDeState + return currentState + } + + override fun triggers(): Boolean { + return changed + } + + override fun output(reaction: Reaction): Output { + val output = DefaultOutput("The Weather (according to wetter.de) on %s".format(dateFormatter.format(state.dateTime.toInstant().toEpochMilli()))) + output.addText("text/html", generateHtmlOutput()) + return output + } + + private fun generateHtmlOutput(): String { + return createHTML().html { + head { + style("text/css") { + unsafe { + +".hour-state { display: table-row; } " + +".hour-state > div { display: table-cell; padding-right: 1em; } " + } + } + } + body { + val startTime = state.dateTime.toInstant() + h1 { +"The Weather (according to wetter.de) on %s".format(dateFormatter.format(startTime.toEpochMilli())) } + state.forEach { + div("hour-state") { + div("time") { +"%tH:%