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