2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mybmw.internal.utils;
15 import static org.openhab.binding.mybmw.internal.MyBMWConstants.*;
17 import java.time.DayOfWeek;
18 import java.util.HashMap;
21 import java.util.stream.Collectors;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.mybmw.internal.utils.ChargingProfileWrapper.ProfileKey;
28 * The {@link ChargingProfileUtils} utility functions for charging profiles
30 * @author Norbert Truchsess - initial contribution
33 public class ChargingProfileUtils {
36 public static class TimedChannel {
37 public final String time;
38 public final @Nullable String timer;
39 public final boolean hasDays;
41 TimedChannel(final String time, @Nullable final String timer, final boolean hasDays) {
44 this.hasDays = hasDays;
48 @SuppressWarnings("serial")
49 private static final Map<ProfileKey, TimedChannel> TIMED_CHANNELS = new HashMap<>() {
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));
60 @SuppressWarnings("serial")
61 private static final Map<DayOfWeek, String> DAY_CHANNELS = new HashMap<>() {
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);
73 public static class ChargeKeyDay {
74 public final ProfileKey key;
75 public final DayOfWeek day;
77 ChargeKeyDay(final ProfileKey key, final DayOfWeek day) {
83 @SuppressWarnings("serial")
84 private static final Map<String, ProfileKey> CHARGE_ENABLED_CHANNEL_KEYS = new HashMap<>() {
86 TIMED_CHANNELS.forEach((key, channel) -> {
87 put(channel.timer + CHARGE_ENABLED, key);
89 put(CHARGE_PROFILE_CLIMATE, ProfileKey.CLIMATE);
93 @SuppressWarnings("serial")
94 private static final Map<String, ProfileKey> CHARGE_TIME_CHANNEL_KEYS = new HashMap<>() {
96 TIMED_CHANNELS.forEach((key, channel) -> {
97 put(channel.time, key);
102 @SuppressWarnings("serial")
103 private static final Map<String, ChargeKeyDay> CHARGE_DAYS_CHANNEL_KEYS = new HashMap<>() {
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));
114 public static @Nullable TimedChannel getTimedChannel(ProfileKey key) {
115 return TIMED_CHANNELS.get(key);
118 public static @Nullable String getDaysChannel(DayOfWeek day) {
119 return DAY_CHANNELS.get(day);
122 public static @Nullable ProfileKey getEnableKey(final String id) {
123 return CHARGE_ENABLED_CHANNEL_KEYS.get(id);
126 public static @Nullable ChargeKeyDay getKeyDay(final String id) {
127 return CHARGE_DAYS_CHANNEL_KEYS.get(id);
130 public static @Nullable ProfileKey getTimeKey(final String id) {
131 return CHARGE_TIME_CHANNEL_KEYS.get(id);
134 public static String formatDays(final Set<DayOfWeek> weekdays) {
135 return weekdays.stream().map(day -> Constants.DAYS.get(day)).collect(Collectors.joining(Constants.COMMA));