d25610a97f6fec0407de1e7247c6e4b907c1d2cb
[rhynodge.git] / src / main / kotlin / net / pterodactylus / rhynodge / webpages / weather / wettercom / WetterComTrigger.kt
1 package net.pterodactylus.rhynodge.webpages.weather.wettercom
2
3 import kotlinx.html.body
4 import kotlinx.html.div
5 import kotlinx.html.h1
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
19
20 /**
21  * TODO
22  *
23  * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net)
24  */
25 class WetterComTrigger : Trigger {
26
27     private val dateFormatter = DateFormat.getDateInstance(DateFormat.LONG, Locale.ENGLISH)
28     private var newState = false
29     private lateinit var currentState: WetterComState
30
31     override fun mergeStates(previousState: State, currentState: State): State? {
32         newState = previousState != currentState
33         this.currentState = currentState as WetterComState
34         return currentState
35     }
36
37     override fun triggers(): Boolean {
38         return newState
39     }
40
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())
44         return output
45     }
46
47     private fun htmlOutput(): String {
48         return createHTML().html {
49             head {
50                 style("text/css") {
51                     +".hour-state { display: table-row; }"
52                     +".hour-state > div { display: table-cell; }"
53                 }
54             }
55             body {
56                 val startTime = currentState.dateTime.toInstant()
57                 h1 { +"The Weather (according to wetter.com) on %s".format(dateFormatter.format(startTime.toEpochMilli())) }
58                 currentState.forEach {
59                     div("hour-state") {
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) }
68                     }
69                 }
70             }
71         }.toString()
72     }
73
74 }