]> git.basschouten.com Git - openhab-addons.git/blob
93b3cd971b937253a7e1f5e418d78d5d80618f0c
[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 homeEvent) {
52                 result = internalGetHomeEvent(channelId, groupId, homeEvent);
53                 if (result != null) {
54                     return result;
55                 }
56             }
57             if (localData instanceof Event event) {
58                 result = internalGetEvent(channelId, event);
59                 if (result != null) {
60                     return result;
61                 }
62             }
63             if (localData instanceof NAThing naThing) {
64                 result = internalGetProperty(channelId, naThing, config);
65                 if (result != null) {
66                     return result;
67                 }
68                 Dashboard dashboard = naThing.getDashboardData();
69                 if (dashboard != null) {
70                     result = internalGetDashboard(channelId, dashboard);
71                     if (result != null) {
72                         return result;
73                     }
74                 }
75             }
76             if (localData instanceof NAObject) {
77                 result = internalGetObject(channelId, localData);
78                 if (result != null) {
79                     return result;
80                 }
81             }
82             result = internalGetOther(channelId);
83         }
84         return result;
85     }
86
87     protected @Nullable State internalGetObject(String channelId, NAObject localData) {
88         return null;
89     }
90
91     protected @Nullable State internalGetOther(String channelId) {
92         return null;
93     }
94
95     protected @Nullable State internalGetDashboard(String channelId, Dashboard dashboard) {
96         return null;
97     }
98
99     protected @Nullable State internalGetProperty(String channelId, NAThing naThing, Configuration config) {
100         return null;
101     }
102
103     protected @Nullable State internalGetEvent(String channelId, Event event) {
104         return null;
105     }
106
107     protected @Nullable State internalGetHomeEvent(String channelId, @Nullable String groupId, HomeEvent event) {
108         return null;
109     }
110 }