From beda6b46bb20379f24b6e1ffc8f0229ad22727b7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 27 May 2016 07:31:59 +0200 Subject: [PATCH] Add wetter.com trigger --- build.gradle | 5 ++ .../webpages/weather/wettercom/WetterComTrigger.kt | 74 +++++++++++++++++++ .../weather/wettercom/WetterComTriggerTest.kt | 84 ++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComTrigger.kt create mode 100644 src/test/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComTriggerTest.kt diff --git a/build.gradle b/build.gradle index 12fa602..e8e89c2 100644 --- a/build.gradle +++ b/build.gradle @@ -30,6 +30,9 @@ buildscript { repositories { mavenCentral() + maven { + url "http://dl.bintray.com/kotlinx/kotlinx" + } } dependencies { @@ -44,6 +47,8 @@ dependencies { compile group: "com.fasterxml.jackson.core", name: "jackson-annotations", version: "2.1.2" compile group: "com.fasterxml.jackson.core", name: "jackson-databind", version: "2.1.2" compile group: "com.google.inject", name: "guice", version: "4.0" + compile group: "org.jetbrains.kotlinx", name: "kotlinx.html", version: "0.5.8" + compile group: "org.jetbrains.kotlinx", name: "kotlinx.html.jvm", version: "0.5.8" testCompile group: "junit", name: "junit", version:"4.12" testCompile group: "org.hamcrest", name: "hamcrest-library", version:"1.3" diff --git a/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComTrigger.kt b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComTrigger.kt new file mode 100644 index 0000000..d25610a --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/rhynodge/webpages/weather/wettercom/WetterComTrigger.kt @@ -0,0 +1,74 @@ +package net.pterodactylus.rhynodge.webpages.weather.wettercom + +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 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.HOURS +import java.util.Locale + +/** + * TODO + * + * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net) + */ +class WetterComTrigger : Trigger { + + private val dateFormatter = DateFormat.getDateInstance(DateFormat.LONG, Locale.ENGLISH) + private var newState = false + private lateinit var currentState: WetterComState + + override fun mergeStates(previousState: State, currentState: State): State? { + newState = previousState != currentState + this.currentState = currentState as WetterComState + return currentState + } + + override fun triggers(): Boolean { + return newState + } + + override fun output(reaction: Reaction): Output { + val output = DefaultOutput("The weather (according to wetter.com) on %s".format(dateFormatter.format(currentState.dateTime.toInstant().toEpochMilli()))) + output.addText("text/html", htmlOutput()) + return output + } + + private fun htmlOutput(): String { + return createHTML().html { + head { + style("text/css") { + +".hour-state { display: table-row; }" + +".hour-state > div { display: table-cell; }" + } + } + body { + val startTime = currentState.dateTime.toInstant() + h1 { +"The Weather (according to wetter.com) on %s".format(dateFormatter.format(startTime.toEpochMilli())) } + currentState.forEach { + div("hour-state") { + div("time") { +"%tH:%