]> git.basschouten.com Git - openhab-addons.git/blob
4e515f49ba7d23c35d67af6752820a763d08c37a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.astro.internal.model;
14
15 import java.time.Duration;
16 import java.time.temporal.ChronoUnit;
17 import java.util.Calendar;
18
19 import javax.measure.quantity.Time;
20
21 import org.openhab.binding.astro.internal.util.DateTimeUtils;
22 import org.openhab.core.library.types.QuantityType;
23 import org.openhab.core.library.unit.Units;
24
25 /**
26  * Holds the season dates of the year and the current name.
27  *
28  * @author Gerhard Riegler - Initial contribution
29  */
30 public class Season {
31     private Calendar spring;
32     private Calendar summer;
33     private Calendar autumn;
34     private Calendar winter;
35
36     private SeasonName name;
37
38     /**
39      * Returns the date of the beginning of spring.
40      */
41     public Calendar getSpring() {
42         return spring;
43     }
44
45     /**
46      * Sets the date of the beginning of spring.
47      */
48     public void setSpring(Calendar spring) {
49         this.spring = spring;
50     }
51
52     /**
53      * Returns the date of the beginning of summer.
54      */
55     public Calendar getSummer() {
56         return summer;
57     }
58
59     /**
60      * Sets the date of the beginning of summer.
61      */
62     public void setSummer(Calendar summer) {
63         this.summer = summer;
64     }
65
66     /**
67      * Returns the date of the beginning of autumn.
68      */
69     public Calendar getAutumn() {
70         return autumn;
71     }
72
73     /**
74      * Sets the date of the beginning of autumn.
75      */
76     public void setAutumn(Calendar autumn) {
77         this.autumn = autumn;
78     }
79
80     /**
81      * Returns the date of the beginning of winter.
82      */
83     public Calendar getWinter() {
84         return winter;
85     }
86
87     /**
88      * Returns the date of the beginning of winter.
89      */
90     public void setWinter(Calendar winter) {
91         this.winter = winter;
92     }
93
94     /**
95      * Returns the current season name.
96      */
97     public SeasonName getName() {
98         return name;
99     }
100
101     /**
102      * Sets the current season name.
103      */
104     public void setName(SeasonName name) {
105         this.name = name;
106     }
107
108     /**
109      * Returns the next season.
110      */
111     public Calendar getNextSeason() {
112         return DateTimeUtils.getNextFromToday(spring, summer, autumn, winter);
113     }
114
115     /**
116      * Returns the next season name.
117      */
118     public SeasonName getNextName() {
119         int ordinal = name.ordinal() + 1;
120         if (ordinal > 3) {
121             ordinal = 0;
122         }
123         return SeasonName.values()[ordinal];
124     }
125
126     /**
127      * Returns the time left for current season
128      */
129     public QuantityType<Time> getTimeLeft() {
130         final Calendar now = Calendar.getInstance();
131         final Calendar next = getNextSeason();
132         final Duration timeLeft = Duration.of(next.getTimeInMillis() - now.getTimeInMillis(), ChronoUnit.MILLIS);
133
134         return new QuantityType<>(timeLeft.toDays(), Units.DAY);
135     }
136 }