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.bsblan.internal.handler;
15 import static org.openhab.binding.bsblan.internal.BsbLanBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.bsblan.internal.api.BsbLanApiCaller;
20 import org.openhab.binding.bsblan.internal.api.dto.BsbLanApiParameterDTO;
21 import org.openhab.binding.bsblan.internal.api.dto.BsbLanApiParameterQueryResponseDTO;
22 import org.openhab.binding.bsblan.internal.api.dto.BsbLanApiParameterSetRequestDTO.Type;
23 import org.openhab.binding.bsblan.internal.configuration.BsbLanBridgeConfiguration;
24 import org.openhab.binding.bsblan.internal.configuration.BsbLanParameterConfiguration;
25 import org.openhab.binding.bsblan.internal.helper.BsbLanParameterConverter;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.State;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The {@link BsbLanParameterHandler} is responsible for updating the data, which are
36 * sent to one of the channels.
38 * @author Peter Schraffl - Initial contribution
41 public class BsbLanParameterHandler extends BsbLanBaseThingHandler {
43 private final Logger logger = LoggerFactory.getLogger(BsbLanParameterHandler.class);
44 private BsbLanParameterConfiguration parameterConfig = new BsbLanParameterConfiguration();
46 public BsbLanParameterHandler(Thing thing) {
50 public Integer getParameterId() {
51 return parameterConfig.id;
55 protected String getDescription() {
56 return "BSB-LAN Parameter";
60 public void refresh(BsbLanBridgeConfiguration bridgeConfiguration) {
65 public void initialize() {
66 parameterConfig = getConfigAs(BsbLanParameterConfiguration.class);
69 // validate 'setId' configuration -> fallback to value of 'id' if invalid or not specified
70 if (parameterConfig.setId == null || parameterConfig.setId <= 0) {
71 parameterConfig.setId = parameterConfig.id;
74 // validate 'setType' configuration -> fallback to 'SET' if invalid or not specified
75 parameterConfig.setType = Type.getTypeWithFallback(parameterConfig.setType).toString();
77 // it will take up to refreshInterval seconds until we receive a value and thing goes online
78 // see notes in {@link BsbLanBridgeHandler#registerThing(BsbLanBaseThingHandler)}
79 updateStatus(ThingStatus.UNKNOWN);
83 * Update the channel from the last data retrieved
85 * @param channelId the id identifying the channel to be updated
88 protected void updateChannel(String channelId) {
89 BsbLanApiParameterQueryResponseDTO data = null;
90 BsbLanBridgeHandler bridgeHandler = getBridgeHandler();
91 if (bridgeHandler != null) {
92 data = bridgeHandler.getCachedParameterQueryResponse();
94 updateChannel(channelId, data);
97 private void updateChannel(String channelId, @Nullable BsbLanApiParameterQueryResponseDTO data) {
99 logger.debug("no data available while updating channel '{}' of parameter {}", channelId,
101 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.BRIDGE_OFFLINE,
102 "No data received from BSB-LAN device");
106 BsbLanApiParameterDTO parameter = data.getOrDefault(parameterConfig.id, null);
107 if (parameter == null) {
108 logger.debug("parameter {} is not part of response data while updating channel '{}' ", parameterConfig.id,
110 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
111 String.format("No data received for parameter %s", parameterConfig.id));
115 updateStatus(ThingStatus.ONLINE);
117 if (!isLinked(channelId)) {
121 State state = BsbLanParameterConverter.getState(channelId, parameter);
126 updateState(channelId, state);
130 * Update the channel from the last data retrieved
132 * @param channelId the id identifying the channel to be updated
133 * @param command the value to be set
136 protected void setChannel(String channelId, Command command) {
137 logger.trace("Received command '{}' for channel '{}'", command, channelId);
139 if (!WRITEABLE_CHANNELS.contains(channelId)) {
140 logger.warn("Channel '{}' is read only. Ignoring command", channelId);
144 String value = BsbLanParameterConverter.getValue(channelId, command);
146 logger.warn("Channel '{}' is read only or conversion failed. Ignoring command", channelId);
150 BsbLanApiCaller api = getApiCaller();
152 logger.debug("Failed to set parameter {} (API unavailable)", parameterConfig.setId);
156 boolean success = api.setParameter(parameterConfig.setId, value,
157 Type.getTypeWithFallback(parameterConfig.setType));
159 logger.debug("Failed to set parameter {} to '{}' for channel '{}'", parameterConfig.setId, value,
164 BsbLanApiParameterQueryResponseDTO queryResponse = api.queryParameter(parameterConfig.id);
165 if (queryResponse == null) {
166 logger.debug("Failed to refresh parameter {} after set request", parameterConfig.id);
170 updateChannel(channelId, queryResponse);