]> git.basschouten.com Git - openhab-addons.git/blob
194bf6732b8a97c395d4657cc890302f411ec51b
[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.mybmw.internal.utils;
14
15 import static org.openhab.binding.mybmw.internal.MyBMWConstants.*;
16
17 import java.time.DayOfWeek;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.stream.Collectors;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.mybmw.internal.utils.ChargingProfileWrapper.ProfileKey;
26
27 /**
28  * The {@link ChargingProfileUtils} utility functions for charging profiles
29  *
30  * @author Norbert Truchsess - initial contribution
31  */
32 @NonNullByDefault
33 public class ChargingProfileUtils {
34
35     // Charging
36     public static class TimedChannel {
37         public final String time;
38         public final @Nullable String timer;
39         public final boolean hasDays;
40
41         TimedChannel(final String time, @Nullable final String timer, final boolean hasDays) {
42             this.time = time;
43             this.timer = timer;
44             this.hasDays = hasDays;
45         }
46     }
47
48     @SuppressWarnings("serial")
49     private static final Map<ProfileKey, TimedChannel> TIMED_CHANNELS = new HashMap<>() {
50         {
51             put(ProfileKey.WINDOWSTART, new TimedChannel(CHARGE_WINDOW_START, null, false));
52             put(ProfileKey.WINDOWEND, new TimedChannel(CHARGE_WINDOW_END, null, false));
53             put(ProfileKey.TIMER1, new TimedChannel(CHARGE_TIMER1 + CHARGE_DEPARTURE, CHARGE_TIMER1, true));
54             put(ProfileKey.TIMER2, new TimedChannel(CHARGE_TIMER2 + CHARGE_DEPARTURE, CHARGE_TIMER2, true));
55             put(ProfileKey.TIMER3, new TimedChannel(CHARGE_TIMER3 + CHARGE_DEPARTURE, CHARGE_TIMER3, true));
56             put(ProfileKey.TIMER4, new TimedChannel(CHARGE_TIMER4 + CHARGE_DEPARTURE, CHARGE_TIMER4, true));
57         }
58     };
59
60     @SuppressWarnings("serial")
61     private static final Map<DayOfWeek, String> DAY_CHANNELS = new HashMap<>() {
62         {
63             put(DayOfWeek.MONDAY, CHARGE_DAY_MON);
64             put(DayOfWeek.TUESDAY, CHARGE_DAY_TUE);
65             put(DayOfWeek.WEDNESDAY, CHARGE_DAY_WED);
66             put(DayOfWeek.THURSDAY, CHARGE_DAY_THU);
67             put(DayOfWeek.FRIDAY, CHARGE_DAY_FRI);
68             put(DayOfWeek.SATURDAY, CHARGE_DAY_SAT);
69             put(DayOfWeek.SUNDAY, CHARGE_DAY_SUN);
70         }
71     };
72
73     public static class ChargeKeyDay {
74         public final ProfileKey key;
75         public final DayOfWeek day;
76
77         ChargeKeyDay(final ProfileKey key, final DayOfWeek day) {
78             this.key = key;
79             this.day = day;
80         }
81     }
82
83     @SuppressWarnings("serial")
84     private static final Map<String, ProfileKey> CHARGE_ENABLED_CHANNEL_KEYS = new HashMap<>() {
85         {
86             TIMED_CHANNELS.forEach((key, channel) -> {
87                 put(channel.timer + CHARGE_ENABLED, key);
88             });
89             put(CHARGE_PROFILE_CLIMATE, ProfileKey.CLIMATE);
90         }
91     };
92
93     @SuppressWarnings("serial")
94     private static final Map<String, ProfileKey> CHARGE_TIME_CHANNEL_KEYS = new HashMap<>() {
95         {
96             TIMED_CHANNELS.forEach((key, channel) -> {
97                 put(channel.time, key);
98             });
99         }
100     };
101
102     @SuppressWarnings("serial")
103     private static final Map<String, ChargeKeyDay> CHARGE_DAYS_CHANNEL_KEYS = new HashMap<>() {
104         {
105             DAY_CHANNELS.forEach((dayOfWeek, dayChannel) -> {
106                 put(CHARGE_TIMER1 + dayChannel, new ChargeKeyDay(ProfileKey.TIMER1, dayOfWeek));
107                 put(CHARGE_TIMER2 + dayChannel, new ChargeKeyDay(ProfileKey.TIMER2, dayOfWeek));
108                 put(CHARGE_TIMER3 + dayChannel, new ChargeKeyDay(ProfileKey.TIMER3, dayOfWeek));
109                 put(CHARGE_TIMER4 + dayChannel, new ChargeKeyDay(ProfileKey.TIMER3, dayOfWeek));
110             });
111         }
112     };
113
114     public static @Nullable TimedChannel getTimedChannel(ProfileKey key) {
115         return TIMED_CHANNELS.get(key);
116     }
117
118     public static @Nullable String getDaysChannel(DayOfWeek day) {
119         return DAY_CHANNELS.get(day);
120     }
121
122     public static @Nullable ProfileKey getEnableKey(final String id) {
123         return CHARGE_ENABLED_CHANNEL_KEYS.get(id);
124     }
125
126     public static @Nullable ChargeKeyDay getKeyDay(final String id) {
127         return CHARGE_DAYS_CHANNEL_KEYS.get(id);
128     }
129
130     public static @Nullable ProfileKey getTimeKey(final String id) {
131         return CHARGE_TIME_CHANNEL_KEYS.get(id);
132     }
133
134     public static String formatDays(final Set<DayOfWeek> weekdays) {
135         return weekdays.stream().map(day -> Constants.DAYS.get(day)).collect(Collectors.joining(Constants.COMMA));
136     }
137 }