]> git.basschouten.com Git - openhab-addons.git/blob
e583d2bcc1de7a73c0bb57be32e323cf99cc1903
[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.bsblan.internal.handler;
14
15 import static org.openhab.binding.bsblan.internal.BsbLanBindingConstants.*;
16
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;
33
34 /**
35  * The {@link BsbLanParameterHandler} is responsible for updating the data, which are
36  * sent to one of the channels.
37  *
38  * @author Peter Schraffl - Initial contribution
39  */
40 @NonNullByDefault
41 public class BsbLanParameterHandler extends BsbLanBaseThingHandler {
42
43     private final Logger logger = LoggerFactory.getLogger(BsbLanParameterHandler.class);
44     private BsbLanParameterConfiguration parameterConfig = new BsbLanParameterConfiguration();
45
46     public BsbLanParameterHandler(Thing thing) {
47         super(thing);
48     }
49
50     public Integer getParameterId() {
51         return parameterConfig.id;
52     }
53
54     @Override
55     protected String getDescription() {
56         return "BSB-LAN Parameter";
57     }
58
59     @Override
60     public void refresh(BsbLanBridgeConfiguration bridgeConfiguration) {
61         updateChannels();
62     }
63
64     @Override
65     public void initialize() {
66         parameterConfig = getConfigAs(BsbLanParameterConfiguration.class);
67         super.initialize();
68
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;
72         }
73
74         // validate 'setType' configuration -> fallback to 'SET' if invalid or not specified
75         parameterConfig.setType = Type.getTypeWithFallback(parameterConfig.setType).toString();
76
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);
80     }
81
82     /**
83      * Update the channel from the last data retrieved
84      *
85      * @param channelId the id identifying the channel to be updated
86      */
87     @Override
88     protected void updateChannel(String channelId) {
89         BsbLanApiParameterQueryResponseDTO data = null;
90         BsbLanBridgeHandler bridgeHandler = getBridgeHandler();
91         if (bridgeHandler != null) {
92             data = bridgeHandler.getCachedParameterQueryResponse();
93         }
94         updateChannel(channelId, data);
95     }
96
97     private void updateChannel(String channelId, @Nullable BsbLanApiParameterQueryResponseDTO data) {
98         if (data == null) {
99             logger.debug("no data available while updating channel '{}' of parameter {}", channelId,
100                     parameterConfig.id);
101             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.BRIDGE_OFFLINE,
102                     "No data received from BSB-LAN device");
103             return;
104         }
105
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,
109                     channelId);
110             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
111                     String.format("No data received for parameter %s", parameterConfig.id));
112             return;
113         }
114
115         updateStatus(ThingStatus.ONLINE);
116
117         if (!isLinked(channelId)) {
118             return;
119         }
120
121         State state = BsbLanParameterConverter.getState(channelId, parameter);
122         if (state == null) {
123             return;
124         }
125
126         updateState(channelId, state);
127     }
128
129     /**
130      * Update the channel from the last data retrieved
131      *
132      * @param channelId the id identifying the channel to be updated
133      * @param command the value to be set
134      */
135     @Override
136     protected void setChannel(String channelId, Command command) {
137         logger.trace("Received command '{}' for channel '{}'", command, channelId);
138
139         if (!WRITEABLE_CHANNELS.contains(channelId)) {
140             logger.warn("Channel '{}' is read only. Ignoring command", channelId);
141             return;
142         }
143
144         String value = BsbLanParameterConverter.getValue(channelId, command);
145         if (value == null) {
146             logger.warn("Channel '{}' is read only or conversion failed. Ignoring command", channelId);
147             return;
148         }
149
150         BsbLanApiCaller api = getApiCaller();
151         if (api == null) {
152             logger.debug("Failed to set parameter {} (API unavailable)", parameterConfig.setId);
153             return;
154         }
155
156         boolean success = api.setParameter(parameterConfig.setId, value,
157                 Type.getTypeWithFallback(parameterConfig.setType));
158         if (!success) {
159             logger.debug("Failed to set parameter {} to '{}' for channel '{}'", parameterConfig.setId, value,
160                     channelId);
161         }
162
163         // refresh value
164         BsbLanApiParameterQueryResponseDTO queryResponse = api.queryParameter(parameterConfig.id);
165         if (queryResponse == null) {
166             logger.debug("Failed to refresh parameter {} after set request", parameterConfig.id);
167             return;
168         }
169
170         updateChannel(channelId, queryResponse);
171     }
172 }