]> git.basschouten.com Git - openhab-addons.git/blob
21d8d2ad4f70f9f9b53543c253f1c70580bf01c9
[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;
14
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeoutException;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.thing.ThingStatus;
23 import org.openhab.core.thing.ThingStatusDetail;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Handler for physical Bosch devices with configurable IDs (as opposed to system services, which have static IDs).
29  * <p>
30  * The device ID of physical devices has to be configured in the thing configuration.
31  * <p>
32  * Examples for physical device IDs are:
33  *
34  * <pre>
35  * hdm:Cameras:d20354de-44b5-3acc-924c-24c98d59da42
36  * hdm:ZigBee:000d6f0016d1cdae
37  * </pre>
38  *
39  * @author Stefan Kästle - Initial contribution
40  * @author Christian Oeing - refactorings of e.g. server registration
41  * @author David Pace - Handler abstraction
42  *
43  */
44 @NonNullByDefault
45 public abstract class BoschSHCDeviceHandler extends BoschSHCHandler {
46
47     private final Logger logger = LoggerFactory.getLogger(getClass());
48
49     /**
50      * Bosch SHC configuration loaded from openHAB configuration.
51      */
52     private @Nullable BoschSHCConfiguration config;
53
54     protected BoschSHCDeviceHandler(Thing thing) {
55         super(thing);
56     }
57
58     @Override
59     public void initialize() {
60         var config = this.config = getConfigAs(BoschSHCConfiguration.class);
61
62         String deviceId = config.id;
63         if (deviceId == null || deviceId.isBlank()) {
64             this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
65                     "@text/offline.conf-error.empty-device-id");
66             return;
67         }
68
69         // Try to get device info to make sure the device exists
70         try {
71             var bridgeHandler = this.getBridgeHandler();
72             var info = bridgeHandler.getDeviceInfo(deviceId);
73             logger.trace("Device initialized:\n{}", info);
74         } catch (TimeoutException | ExecutionException | BoschSHCException e) {
75             this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
76             return;
77         } catch (InterruptedException e) {
78             Thread.currentThread().interrupt();
79             this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
80             return;
81         }
82
83         super.initialize();
84     }
85
86     /**
87      * Returns the unique id of the Bosch device.
88      *
89      * @return Unique id of the Bosch device.
90      */
91     @Override
92     public @Nullable String getBoschID() {
93         if (config != null) {
94             return config.id;
95         }
96
97         return null;
98     }
99 }