]> git.basschouten.com Git - openhab-addons.git/blob
246380bebb66304c825854e8a337b613267504d9
[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.netatmo.internal.handler.channelhelper;
14
15 import java.util.HashSet;
16 import java.util.Set;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.MeasureClass;
21 import org.openhab.binding.netatmo.internal.api.dto.Dashboard;
22 import org.openhab.binding.netatmo.internal.api.dto.Event;
23 import org.openhab.binding.netatmo.internal.api.dto.HomeEvent;
24 import org.openhab.binding.netatmo.internal.api.dto.NAObject;
25 import org.openhab.binding.netatmo.internal.api.dto.NAThing;
26 import org.openhab.binding.netatmo.internal.providers.NetatmoThingTypeProvider;
27 import org.openhab.core.config.core.Configuration;
28 import org.openhab.core.types.State;
29
30 /**
31  * The {@link ChannelHelper} is the base class for all channel helpers.
32  *
33  * @author GaĆ«l L'hopital - Initial contribution
34  *
35  */
36 @NonNullByDefault
37 public abstract class ChannelHelper {
38     private @Nullable NAObject data;
39     private final Set<String> channelGroupTypes;
40     private final Set<String> channelGroups = new HashSet<>();
41     private Set<String> extensibleChannels = Set.of();
42
43     ChannelHelper(String... providedGroups) {
44         this.channelGroupTypes = Set.of(providedGroups);
45         channelGroupTypes.forEach(groupType -> channelGroups.add(NetatmoThingTypeProvider.toGroupName(groupType)));
46     }
47
48     ChannelHelper(String providedGroup, MeasureClass measureClass) {
49         this(providedGroup);
50         this.extensibleChannels = measureClass.channels.keySet();
51     }
52
53     public void setNewData(@Nullable NAObject data) {
54         this.data = data;
55     }
56
57     public final @Nullable State getChannelState(String channelId, @Nullable String groupId, Configuration config) {
58         State result = null;
59         if (channelGroups.isEmpty() || (groupId != null && channelGroups.contains(groupId))) {
60             NAObject localData = data;
61             if (localData instanceof HomeEvent) {
62                 result = internalGetHomeEvent(channelId, groupId, (HomeEvent) localData);
63                 if (result != null) {
64                     return result;
65                 }
66             }
67             if (localData instanceof Event) {
68                 result = internalGetEvent(channelId, (Event) localData);
69                 if (result != null) {
70                     return result;
71                 }
72             }
73             if (localData instanceof NAThing) {
74                 NAThing naThing = (NAThing) localData;
75                 result = internalGetProperty(channelId, naThing, config);
76                 if (result != null) {
77                     return result;
78                 }
79                 Dashboard dashboard = naThing.getDashboardData();
80                 if (dashboard != null) {
81                     result = internalGetDashboard(channelId, dashboard);
82                     if (result != null) {
83                         return result;
84                     }
85                 }
86             }
87             if (localData instanceof NAObject) {
88                 result = internalGetObject(channelId, localData);
89                 if (result != null) {
90                     return result;
91                 }
92             }
93             result = internalGetOther(channelId);
94         }
95         return result;
96     }
97
98     protected @Nullable State internalGetObject(String channelId, NAObject localData) {
99         return null;
100     }
101
102     protected @Nullable State internalGetOther(String channelId) {
103         return null;
104     }
105
106     protected @Nullable State internalGetDashboard(String channelId, Dashboard dashboard) {
107         return null;
108     }
109
110     protected @Nullable State internalGetProperty(String channelId, NAThing naThing, Configuration config) {
111         return null;
112     }
113
114     protected @Nullable State internalGetEvent(String channelId, Event event) {
115         return null;
116     }
117
118     protected @Nullable State internalGetHomeEvent(String channelId, @Nullable String groupId, HomeEvent event) {
119         return null;
120     }
121
122     public Set<String> getChannelGroupTypes() {
123         return channelGroupTypes;
124     }
125
126     public Set<String> getExtensibleChannels() {
127         return extensibleChannels;
128     }
129 }