package net.pterodactylus.rhynodge.webpages.weather.wettercom
+import com.fasterxml.jackson.annotation.JsonProperty
+
/**
* Container for weather conditions of a single hour.
*
* @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net)
*/
-data class HourState(val hourIndex: Int, val temperature: Double, val rainProbability: Double, val rainAmount: Double, val windDirection: WindDirection, val windSpeed: Double, val description: String, val image: String) {
+data class HourState(
+ @JsonProperty("hourIndex") val hourIndex: Int,
+ @JsonProperty("temperature") val temperature: Double,
+ @JsonProperty("rainProbability") val rainProbability: Double,
+ @JsonProperty("rainAmount") val rainAmount: Double,
+ @JsonProperty("windDirection") val windDirection: WindDirection,
+ @JsonProperty("windSpeed") val windSpeed: Double,
+ @JsonProperty("description") val description: String,
+ @JsonProperty("image") val image: String) {
class Builder(private val hourIndex: Int) {
package net.pterodactylus.rhynodge.webpages.weather.wettercom
+import com.fasterxml.jackson.annotation.JsonGetter
+import com.fasterxml.jackson.annotation.JsonProperty
import net.pterodactylus.rhynodge.states.AbstractState
+import java.time.Instant
+import java.time.ZoneId
import java.time.ZonedDateTime
/**
*/
class WetterComState(val dateTime: ZonedDateTime) : AbstractState(true), Iterable<HourState> {
+ constructor(@JsonProperty("dateTime") time: Long) :
+ this(Instant.ofEpochMilli(time).atZone(ZoneId.of("Europe/Berlin")))
+
+ @JsonProperty("hours")
val hours: List<HourState> = mutableListOf()
+ val timeMillis: Long
+ @JsonGetter("dateTime")
+ get() = dateTime.toInstant().toEpochMilli()
+
fun addHour(hourState: HourState) {
(hours as MutableList<HourState>).add(hourState)
}
package net.pterodactylus.rhynodge.webpages.weather.wettercom
+import com.fasterxml.jackson.databind.ObjectMapper
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.`is`
import org.junit.Test
assertThat(firstState.iterator().asSequence().toList(), `is`(firstState.hours as Iterable<HourState>))
}
+ @Test
+ fun stateIsSerializableAsJson() {
+ val objectMapper = ObjectMapper()
+ val now = Instant.now().atZone(ZoneId.of("Europe/Berlin"))
+ val originalState = WetterComState(ZonedDateTime.from(now))
+ originalState.addHour(HourState(0, 10.0, 0.05, 0.0, WindDirection.NORTH, 5.0, "Fine", "http://1"))
+ originalState.addHour(HourState(1, 12.0, 0.1, 2.0, WindDirection.WEST, 8.0, "Superb", "http://2"))
+ val json = objectMapper.writeValueAsString(originalState)
+ println(json)
+ val parsedState = objectMapper.readValue(json, WetterComState::class.java)
+ assertThat(parsedState, `is`(originalState))
+ }
+
}