2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.freeboxos.internal.handler;
15 import java.math.BigDecimal;
16 import java.util.Comparator;
17 import java.util.List;
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;
39 * The {@link HomeNodeHandler} is the base class for handler of home node things.
41 * @author Gaƫl L'hopital - Initial contribution
44 public abstract class HomeNodeHandler extends ApiConsumerHandler {
45 private final Logger logger = LoggerFactory.getLogger(HomeNodeHandler.class);
47 public HomeNodeHandler(Thing thing) {
52 void initializeProperties(Map<String, String> properties) throws FreeboxException {
53 HomeNode node = getManager(HomeManager.class).getHomeNode(getClientId());
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);
63 properties.putAll(node.props());
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());
73 protected void internalConfigureChannel(String channelId, Configuration conf, List<Endpoint> endpoints) {
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) {
84 EndpointState state = homeManager.getEndpointsState(getClientId(), slotId);
86 result = getChannelState(homeManager, channel.getUID().getIdWithoutGroup(), state);
88 result = getChannelState(homeManager, channel.getUID().getIdWithoutGroup());
90 } catch (FreeboxException e) {
91 logger.warn("Error updating channel: {}", e.getMessage());
94 result = getChannelState(homeManager, channel.getUID().getIdWithoutGroup());
96 updateState(channel.getUID(), result);
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);
111 return super.internalHandleCommand(channelId, command);
114 protected @Nullable Integer getSlotId(Configuration configuration, String endPoint) {
115 Object slot = configuration.get(endPoint);
116 return slot instanceof BigDecimal slotId ? slotId.intValue() : null;
119 protected boolean executeChannelCommand(HomeManager homeManager, String channelId, Command command,
120 Configuration config) throws FreeboxException {
124 protected boolean executeSlotCommand(HomeManager homeManager, String channelId, Command command,
125 Configuration config, int slotId) throws FreeboxException {
129 protected State getChannelState(HomeManager homeManager, String channelWG) {
130 return UnDefType.UNDEF;
133 protected abstract State getChannelState(HomeManager homeManager, String channelId, EndpointState state);