- override fun triggers(): Boolean {
- return changed
- }
-
- override fun output(reaction: Reaction): Output {
- val output = DefaultOutput("The Weather (according to ${state.service}) on ${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 {
- +".weather-states { display: table; } "
- +".hour-state, .header { display: table-row; } "
- +".hour-state > div { display: table-cell; padding: 0em 0.5em; text-align: center; } "
- +".header > div { display: table-cell; padding: 0em 0.5em; font-weight: bold; text-align: center; } "
- }
- }
- }
- body {
- val startTime = state.dateTime.toInstant()
- h1 { +"The Weather (according to wetter.de) on %s".format(dateFormatter.format(startTime.toEpochMilli())) }
- val showFeltTemperature = state.any { it.feltTemperature != null }
- val showGustSpeed = state.any { it.gustSpeed != null }
- val showHumidity = state.any { it.humidity != null }
- div("weather-states") {
- div("header") {
- div { +"Time" }
- div { +"Temperature" }
- if (showHumidity) {
- div { +"feels like" }
- }
- div { +"Chance of Rain" }
- div { +"Amount" }
- div { +"Wind from" }
- div { +"Speed" }
- if (showGustSpeed) {
- div { +"Gusts" }
- }
- if (showHumidity) {
- div { +"Humidity" }
- }
- div { +"Description" }
- div { +"Image" }
- }
- state.forEach {
- div("hour-state") {
- div("time") { +"%tH:%<tM".format(startTime.plus(it.hourIndex.toLong(), ChronoUnit.HOURS).toEpochMilli()) }
- div("temperature") { +"${it.temperature} °C" }
- if (showFeltTemperature) {
- div("felt-temperature") { +if (it.feltTemperature != null) "(${it.feltTemperature} °C)" else "" }
- }
- div("rain-probability") { +"${it.rainProbability.times(100).toInt()}%" }
- div("rain-amount") { +"${it.rainAmount.minDigits()} l/m²" }
- div("wind-direction") { +it.windDirection.arrow }
- div("wind-speed") { +"${it.windSpeed} km/h" }
- if (showGustSpeed) {
- div("gust-speed") { +if (it.gustSpeed != null) "(up to ${it.gustSpeed} km/h)" else "" }
- }
- if (showHumidity) {
- div("humidity") { +if (it.humidity != null) "${it.humidity.times(100).toInt()}%" else "" }
- }
- div("description") { +it.description }
- div("image") { img(src = it.image) }
- }
- }
- }
- }
- }.toString()
- }
-
- private fun Double.minDigits(): String {
- return this.toString().replace(Regex("\\.0*$"), "")
- }