1 package net.pterodactylus.rhynodge.webpages.weather.wettercom
3 import kotlinx.html.body
4 import kotlinx.html.div
6 import kotlinx.html.head
7 import kotlinx.html.html
8 import kotlinx.html.img
9 import kotlinx.html.stream.createHTML
10 import kotlinx.html.style
11 import net.pterodactylus.rhynodge.Reaction
12 import net.pterodactylus.rhynodge.State
13 import net.pterodactylus.rhynodge.Trigger
14 import net.pterodactylus.rhynodge.output.DefaultOutput
15 import net.pterodactylus.rhynodge.output.Output
16 import java.text.DateFormat
17 import java.time.temporal.ChronoUnit.HOURS
18 import java.util.Locale
23 * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net)
25 class WetterComTrigger : Trigger {
27 private val dateFormatter = DateFormat.getDateInstance(DateFormat.LONG, Locale.ENGLISH)
28 private var newState = false
29 private lateinit var currentState: WetterComState
31 override fun mergeStates(previousState: State, currentState: State): State? {
32 newState = previousState != currentState
33 this.currentState = currentState as WetterComState
37 override fun triggers(): Boolean {
41 override fun output(reaction: Reaction): Output {
42 val output = DefaultOutput("The weather (according to wetter.com) on %s".format(dateFormatter.format(currentState.dateTime.toInstant().toEpochMilli())))
43 output.addText("text/html", htmlOutput())
47 private fun htmlOutput(): String {
48 return createHTML().html {
51 +".hour-state { display: table-row; }"
52 +".hour-state > div { display: table-cell; }"
56 val startTime = currentState.dateTime.toInstant()
57 h1 { +"The Weather (according to wetter.com) on %s".format(dateFormatter.format(startTime.toEpochMilli())) }
58 currentState.forEach {
60 div("time") { +"%tH:%<tM".format(startTime.plus(it.hourIndex.toLong(), HOURS).toEpochMilli()) }
61 div("temperature") { +"%d °C".format(it.temperature.toInt()) }
62 div("rain-probability") { +"%d%%".format((it.rainProbability * 100).toInt()) }
63 div("rain-amount") { +"%d l/m²".format(it.rainAmount.toInt()) }
64 div("wind-direction") { +it.windDirection.name }
65 div("wind-speed") { +"%d km/h".format(it.windSpeed.toInt()) }
66 div("description") { +it.description }
67 div("image") { img(src = it.image) }