]> git.basschouten.com Git - openhab-addons.git/blob
52f5064b16abcfaa187617e19a90e204c2979a35
[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.boschshc.internal.devices.userdefinedstate;
14
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.CHANNEL_USER_DEFINED_STATE;
16
17 import java.util.List;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.TimeoutException;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.boschshc.internal.devices.BoschSHCConfiguration;
24 import org.openhab.binding.boschshc.internal.devices.BoschSHCHandler;
25 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
26 import org.openhab.binding.boschshc.internal.services.userstate.UserStateService;
27 import org.openhab.binding.boschshc.internal.services.userstate.dto.UserStateServiceState;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingStatusDetail;
33 import org.openhab.core.types.Command;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.google.gson.JsonElement;
38
39 /**
40  * Handler for user defined states
41  *
42  * @author Patrick Gell - Initial contribution
43  *
44  */
45 @NonNullByDefault
46 public class UserStateHandler extends BoschSHCHandler {
47
48     private final Logger logger = LoggerFactory.getLogger(getClass());
49
50     private final UserStateService userStateService;
51     /**
52      * Bosch SHC configuration loaded from openHAB configuration.
53      */
54     private @Nullable BoschSHCConfiguration config;
55
56     public UserStateHandler(Thing thing) {
57         super(thing);
58
59         userStateService = new UserStateService();
60     }
61
62     @Override
63     public void initialize() {
64         var localConfig = this.config = getConfigAs(BoschSHCConfiguration.class);
65         String stateId = localConfig.id;
66         if (stateId == null || stateId.isBlank()) {
67             this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
68                     "@text/offline.conf-error.empty-state-id");
69             return;
70         }
71
72         // Try to get state info to make sure the state exists
73         try {
74             var bridgeHandler = this.getBridgeHandler();
75             var info = bridgeHandler.getUserStateInfo(stateId);
76             logger.trace("User-defined state initialized:\n{}", info);
77         } catch (TimeoutException | ExecutionException | BoschSHCException e) {
78             this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
79             return;
80         } catch (InterruptedException e) {
81             Thread.currentThread().interrupt();
82             this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
83             return;
84         }
85         super.initialize();
86     }
87
88     @Override
89     public @Nullable String getBoschID() {
90         if (config != null) {
91             return config.id;
92         }
93
94         return null;
95     }
96
97     @Override
98     protected void initializeServices() throws BoschSHCException {
99         super.initializeServices();
100
101         logger.debug("Initializing service for UserStateHandler");
102         this.registerService(userStateService, this::updateChannels, List.of(CHANNEL_USER_DEFINED_STATE), true);
103     }
104
105     @Override
106     public void handleCommand(ChannelUID channelUID, Command command) {
107         super.handleCommand(channelUID, command);
108
109         if (channelUID.getId().equals(CHANNEL_USER_DEFINED_STATE) && (command instanceof OnOffType onOffCommand)) {
110             updateUserState(channelUID.getThingUID().getId(), onOffCommand);
111         }
112     }
113
114     private void updateUserState(String stateId, OnOffType userState) {
115         UserStateServiceState serviceState = new UserStateServiceState();
116         serviceState.setState(userState == OnOffType.ON);
117         try {
118             getBridgeHandler().putState(stateId, "", serviceState);
119         } catch (BoschSHCException | ExecutionException | TimeoutException e) {
120             this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
121                     String.format("Error while putting user-defined state for %s", stateId));
122         } catch (InterruptedException e) {
123             Thread.currentThread().interrupt();
124             this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
125                     String.format("Error while putting user-defined state for %s", stateId));
126         }
127     }
128
129     private void updateChannels(UserStateServiceState userState) {
130         super.updateState(CHANNEL_USER_DEFINED_STATE, userState.toOnOffType());
131     }
132
133     @Override
134     public void processUpdate(String serviceName, @Nullable JsonElement stateData) {
135         super.processUpdate("UserDefinedState", stateData);
136     }
137 }