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