]> git.basschouten.com Git - openhab-addons.git/blob
df79ddd21af6303a3d6ed837042c14b592b93c19
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.freeboxos.internal.handler;
14
15 import java.math.BigDecimal;
16 import java.util.Comparator;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
23 import org.openhab.binding.freeboxos.internal.api.rest.HomeManager;
24 import org.openhab.binding.freeboxos.internal.api.rest.HomeManager.Endpoint;
25 import org.openhab.binding.freeboxos.internal.api.rest.HomeManager.EndpointState;
26 import org.openhab.binding.freeboxos.internal.api.rest.HomeManager.EpType;
27 import org.openhab.binding.freeboxos.internal.api.rest.HomeManager.HomeNode;
28 import org.openhab.binding.freeboxos.internal.config.ApiConsumerConfiguration;
29 import org.openhab.core.config.core.Configuration;
30 import org.openhab.core.thing.Channel;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.State;
34 import org.openhab.core.types.UnDefType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * The {@link HomeNodeHandler} is the base class for handler of home node things.
40  *
41  * @author GaĆ«l L'hopital - Initial contribution
42  */
43 @NonNullByDefault
44 public abstract class HomeNodeHandler extends ApiConsumerHandler {
45     private final Logger logger = LoggerFactory.getLogger(HomeNodeHandler.class);
46
47     public HomeNodeHandler(Thing thing) {
48         super(thing);
49     }
50
51     @Override
52     void initializeProperties(Map<String, String> properties) throws FreeboxException {
53         HomeNode node = getManager(HomeManager.class).getHomeNode(getClientId());
54
55         // Gets the lowest refresh time or else, we'll keep configuration default
56         node.showEndpoints().stream().filter(ep -> ep.epType() == EpType.SIGNAL).filter(ep -> ep.refresh() != 0)
57                 .min(Comparator.comparing(Endpoint::refresh)).map(Endpoint::refresh).ifPresent(rate -> {
58                     Configuration thingConfig = editConfiguration();
59                     thingConfig.put(ApiConsumerConfiguration.REFRESH_INTERVAL, Integer.toString(rate / 1000));
60                     updateConfiguration(thingConfig);
61                 });
62
63         properties.putAll(node.props());
64
65         getThing().getChannels().forEach(channel -> {
66             Configuration conf = channel.getConfiguration();
67             node.type().endpoints().stream().filter(ep -> ep.name().equals(channel.getUID().getIdWithoutGroup()))
68                     .forEach(endPoint -> conf.put(endPoint.epType().asConfId(), endPoint.id()));
69             internalConfigureChannel(channel.getUID().getIdWithoutGroup(), conf, node.type().endpoints());
70         });
71     }
72
73     protected void internalConfigureChannel(String channelId, Configuration conf, List<Endpoint> endpoints) {
74     }
75
76     @Override
77     protected void internalPoll() throws FreeboxException {
78         HomeManager homeManager = getManager(HomeManager.class);
79         getThing().getChannels().stream().filter(channel -> isLinked(channel.getUID())).forEach(channel -> {
80             State result = UnDefType.UNDEF;
81             Integer slotId = getSlotId(channel.getConfiguration(), EpType.SIGNAL.asConfId());
82             if (slotId instanceof Integer) {
83                 try {
84                     EndpointState state = homeManager.getEndpointsState(getClientId(), slotId);
85                     if (state != null) {
86                         result = getChannelState(homeManager, channel.getUID().getIdWithoutGroup(), state);
87                     } else {
88                         result = getChannelState(homeManager, channel.getUID().getIdWithoutGroup());
89                     }
90                 } catch (FreeboxException e) {
91                     logger.warn("Error updating channel: {}", e.getMessage());
92                 }
93             } else {
94                 result = getChannelState(homeManager, channel.getUID().getIdWithoutGroup());
95             }
96             updateState(channel.getUID(), result);
97         });
98     }
99
100     @Override
101     protected boolean internalHandleCommand(String channelId, Command command) throws FreeboxException {
102         Channel channel = getThing().getChannel(channelId);
103         if (channel != null) {
104             Configuration config = channel.getConfiguration();
105             String channelWG = channel.getUID().getIdWithoutGroup();
106             Integer slotId = getSlotId(config, EpType.SLOT.asConfId());
107             HomeManager homeManager = getManager(HomeManager.class);
108             return slotId instanceof Integer ? executeSlotCommand(homeManager, channelWG, command, config, slotId)
109                     : executeChannelCommand(homeManager, channelWG, command, config);
110         }
111         return super.internalHandleCommand(channelId, command);
112     }
113
114     protected @Nullable Integer getSlotId(Configuration configuration, String endPoint) {
115         Object slot = configuration.get(endPoint);
116         return slot instanceof BigDecimal slotId ? slotId.intValue() : null;
117     }
118
119     protected boolean executeChannelCommand(HomeManager homeManager, String channelId, Command command,
120             Configuration config) throws FreeboxException {
121         return false;
122     }
123
124     protected boolean executeSlotCommand(HomeManager homeManager, String channelId, Command command,
125             Configuration config, int slotId) throws FreeboxException {
126         return false;
127     }
128
129     protected State getChannelState(HomeManager homeManager, String channelWG) {
130         return UnDefType.UNDEF;
131     }
132
133     protected abstract State getChannelState(HomeManager homeManager, String channelId, EndpointState state);
134 }