Use a single hour state for all weather services
[rhynodge.git] / src / main / kotlin / net / pterodactylus / rhynodge / webpages / weather / wettercom / WetterComState.kt
1 package net.pterodactylus.rhynodge.webpages.weather.wettercom
2
3 import com.fasterxml.jackson.annotation.JsonGetter
4 import com.fasterxml.jackson.annotation.JsonProperty
5 import net.pterodactylus.rhynodge.states.AbstractState
6 import net.pterodactylus.rhynodge.webpages.weather.HourState
7 import java.time.Instant
8 import java.time.ZoneId
9 import java.time.ZonedDateTime
10
11 /**
12  * Contains the state parsed from [wetter.com](https://www.wetter.com/).
13  *
14  * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net)
15  */
16 class WetterComState(val dateTime: ZonedDateTime) : AbstractState(true), Iterable<HourState> {
17
18     constructor(@JsonProperty("dateTime") time: Long) :
19     this(Instant.ofEpochMilli(time).atZone(ZoneId.of("Europe/Berlin")))
20
21     @JsonProperty("hours")
22     val hours: List<HourState> = mutableListOf()
23
24     val timeMillis: Long
25         @JsonGetter("dateTime")
26         get() = dateTime.toInstant().toEpochMilli()
27
28     operator fun plusAssign(hourState: HourState) {
29         (hours as MutableList<HourState>).add(hourState)
30     }
31
32     override fun iterator(): Iterator<HourState> {
33         return hours.iterator()
34     }
35
36     override fun equals(other: Any?): Boolean {
37         other as? WetterComState ?: return false
38         return (dateTime == other.dateTime) and (hours == other.hours)
39     }
40
41 }