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