]> git.basschouten.com Git - openhab-addons.git/blob
e2e35351c265a6bba63a9fd3f1f603b0d244ead0
[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.bmwconnecteddrive.internal.utils;
14
15 import static org.openhab.binding.bmwconnecteddrive.internal.ConnectedDriveConstants.*;
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.bmwconnecteddrive.internal.utils.ChargeProfileWrapper.ProfileKey;
26
27 /**
28  * The {@link ChargeProfileUtils} utility functions for charging profiles
29  *
30  * @author Norbert Truchsess - initial contribution
31  */
32 @NonNullByDefault
33 public class ChargeProfileUtils {
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.OVERRIDE, new TimedChannel(CHARGE_OVERRIDE + CHARGE_DEPARTURE, CHARGE_OVERRIDE, false));
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             });
110         }
111     };
112
113     public static @Nullable TimedChannel getTimedChannel(ProfileKey key) {
114         return TIMED_CHANNELS.get(key);
115     }
116
117     public static @Nullable String getDaysChannel(DayOfWeek day) {
118         return DAY_CHANNELS.get(day);
119     }
120
121     public static @Nullable ProfileKey getEnableKey(final String id) {
122         return CHARGE_ENABLED_CHANNEL_KEYS.get(id);
123     }
124
125     public static @Nullable ChargeKeyDay getKeyDay(final String id) {
126         return CHARGE_DAYS_CHANNEL_KEYS.get(id);
127     }
128
129     public static @Nullable ProfileKey getTimeKey(final String id) {
130         return CHARGE_TIME_CHANNEL_KEYS.get(id);
131     }
132
133     public static String formatDays(final Set<DayOfWeek> weekdays) {
134         return weekdays.stream().map(day -> Constants.DAYS.get(day)).collect(Collectors.joining(Constants.COMMA));
135     }
136 }