]> git.basschouten.com Git - openhab-addons.git/blob
1070912ee4601d2849049697c4aa0fb18d2fa0ff
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.radiothermostat.internal.util;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.radiothermostat.internal.RadioThermostatConfiguration;
18
19 /**
20  * The {@link RadioThermostatScheduleJson} is the class used to convert the heating and cooling schedules from user
21  * configuration into the json that is sent to the thermostat
22  *
23  * @author Michael Lobstein - Initial contribution
24  */
25 @NonNullByDefault
26 public class RadioThermostatScheduleJson {
27     private final @Nullable String monHeat;
28     private final @Nullable String tueHeat;
29     private final @Nullable String wedHeat;
30     private final @Nullable String thuHeat;
31     private final @Nullable String friHeat;
32     private final @Nullable String satHeat;
33     private final @Nullable String sunHeat;
34
35     private final @Nullable String monCool;
36     private final @Nullable String tueCool;
37     private final @Nullable String wedCool;
38     private final @Nullable String thuCool;
39     private final @Nullable String friCool;
40     private final @Nullable String satCool;
41     private final @Nullable String sunCool;
42
43     public RadioThermostatScheduleJson(RadioThermostatConfiguration config) {
44         monHeat = getDaySchedule(config.monMorningHeatTime, config.monDayHeatTime, config.monEveningHeatTime,
45                 config.monNightHeatTime, config.monMorningHeatTemp, config.monDayHeatTemp, config.monEveningHeatTemp,
46                 config.monNightHeatTemp);
47         tueHeat = getDaySchedule(config.tueMorningHeatTime, config.tueDayHeatTime, config.tueEveningHeatTime,
48                 config.tueNightHeatTime, config.tueMorningHeatTemp, config.tueDayHeatTemp, config.tueEveningHeatTemp,
49                 config.tueNightHeatTemp);
50         wedHeat = getDaySchedule(config.wedMorningHeatTime, config.wedDayHeatTime, config.wedEveningHeatTime,
51                 config.wedNightHeatTime, config.wedMorningHeatTemp, config.wedDayHeatTemp, config.wedEveningHeatTemp,
52                 config.wedNightHeatTemp);
53         thuHeat = getDaySchedule(config.thuMorningHeatTime, config.thuDayHeatTime, config.thuEveningHeatTime,
54                 config.thuNightHeatTime, config.thuMorningHeatTemp, config.thuDayHeatTemp, config.thuEveningHeatTemp,
55                 config.thuNightHeatTemp);
56         friHeat = getDaySchedule(config.friMorningHeatTime, config.friDayHeatTime, config.friEveningHeatTime,
57                 config.friNightHeatTime, config.friMorningHeatTemp, config.friDayHeatTemp, config.friEveningHeatTemp,
58                 config.friNightHeatTemp);
59         satHeat = getDaySchedule(config.satMorningHeatTime, config.satDayHeatTime, config.satEveningHeatTime,
60                 config.satNightHeatTime, config.satMorningHeatTemp, config.satDayHeatTemp, config.satEveningHeatTemp,
61                 config.satNightHeatTemp);
62         sunHeat = getDaySchedule(config.sunMorningHeatTime, config.sunDayHeatTime, config.sunEveningHeatTime,
63                 config.sunNightHeatTime, config.sunMorningHeatTemp, config.sunDayHeatTemp, config.sunEveningHeatTemp,
64                 config.sunNightHeatTemp);
65
66         monCool = getDaySchedule(config.monMorningCoolTime, config.monDayCoolTime, config.monEveningCoolTime,
67                 config.monNightCoolTime, config.monMorningCoolTemp, config.monDayCoolTemp, config.monEveningCoolTemp,
68                 config.monNightCoolTemp);
69         tueCool = getDaySchedule(config.tueMorningCoolTime, config.tueDayCoolTime, config.tueEveningCoolTime,
70                 config.tueNightCoolTime, config.tueMorningCoolTemp, config.tueDayCoolTemp, config.tueEveningCoolTemp,
71                 config.tueNightCoolTemp);
72         wedCool = getDaySchedule(config.wedMorningCoolTime, config.wedDayCoolTime, config.wedEveningCoolTime,
73                 config.wedNightCoolTime, config.wedMorningCoolTemp, config.wedDayCoolTemp, config.wedEveningCoolTemp,
74                 config.wedNightCoolTemp);
75         thuCool = getDaySchedule(config.thuMorningCoolTime, config.thuDayCoolTime, config.thuEveningCoolTime,
76                 config.thuNightCoolTime, config.thuMorningCoolTemp, config.thuDayCoolTemp, config.thuEveningCoolTemp,
77                 config.thuNightCoolTemp);
78         friCool = getDaySchedule(config.friMorningCoolTime, config.friDayCoolTime, config.friEveningCoolTime,
79                 config.friNightCoolTime, config.friMorningCoolTemp, config.friDayCoolTemp, config.friEveningCoolTemp,
80                 config.friNightCoolTemp);
81         satCool = getDaySchedule(config.satMorningCoolTime, config.satDayCoolTime, config.satEveningCoolTime,
82                 config.satNightCoolTime, config.satMorningCoolTemp, config.satDayCoolTemp, config.satEveningCoolTemp,
83                 config.satNightCoolTemp);
84         sunCool = getDaySchedule(config.sunMorningCoolTime, config.sunDayCoolTime, config.sunEveningCoolTime,
85                 config.sunNightCoolTime, config.sunMorningCoolTemp, config.sunDayCoolTemp, config.sunEveningCoolTemp,
86                 config.sunNightCoolTemp);
87     }
88
89     public String getHeatProgramJson() throws IllegalStateException {
90         return getProgramJson(monHeat, tueHeat, wedHeat, thuHeat, friHeat, satHeat, sunHeat);
91     }
92
93     public String getCoolProgramJson() throws IllegalStateException {
94         return getProgramJson(monCool, tueCool, wedCool, thuCool, friCool, satCool, sunCool);
95     }
96
97     private String getProgramJson(@Nullable String mon, @Nullable String tue, @Nullable String wed,
98             @Nullable String thu, @Nullable String fri, @Nullable String sat, @Nullable String sun)
99             throws IllegalStateException {
100         // all were null, bypass
101         if (mon == null && tue == null && wed == null && thu == null && fri == null && sat == null && sun == null) {
102             return "";
103         }
104
105         // some were null, the schedule is invalid
106         if (mon == null || tue == null || wed == null || thu == null || fri == null || sat == null || sun == null) {
107             throw new IllegalStateException();
108         }
109
110         return "{\"0\":" + mon + ",\"1\":" + tue + ",\"2\":" + wed + ",\"3\":" + thu + ",\"4\":" + fri + ",\"5\":" + sat
111                 + ",\"6\":" + sun + "}";
112     }
113
114     private @Nullable String getDaySchedule(@Nullable String morningTime, @Nullable String dayTime,
115             @Nullable String eveningTime, @Nullable String nightTime, @Nullable Integer morningTemp,
116             @Nullable Integer dayTemp, @Nullable Integer eveningTemp, @Nullable Integer nightTemp) {
117         // if any null, this day schedule is not valid
118         if (morningTime == null || dayTime == null || eveningTime == null || nightTime == null || morningTemp == null
119                 || dayTemp == null || eveningTemp == null || nightTemp == null) {
120             return null;
121         }
122
123         final int morningMin;
124         final int dayMin;
125         final int eveningMin;
126         final int nightMin;
127
128         try {
129             morningMin = parseMinutes(morningTime);
130             dayMin = parseMinutes(dayTime);
131             eveningMin = parseMinutes(eveningTime);
132             nightMin = parseMinutes(nightTime);
133         } catch (NumberFormatException nfe) {
134             // if any of the times could not be parsed into minutes, the schedule is invalid
135             return null;
136         }
137
138         // the minute value for each period must be greater than the previous period otherwise the schedule is invalid
139         if (morningMin >= dayMin || dayMin >= eveningMin || eveningMin >= nightMin) {
140             return null;
141         }
142
143         return "[" + morningMin + "," + morningTemp + "," + dayMin + "," + dayTemp + "," + eveningMin + ","
144                 + eveningTemp + "," + nightMin + "," + nightTemp + "]";
145     }
146
147     private int parseMinutes(String timeStr) {
148         final String[] hourMin = timeStr.split(":");
149
150         return Integer.parseInt(hourMin[0]) * 60 + Integer.parseInt(hourMin[1]);
151     }
152 }