]> git.basschouten.com Git - openhab-addons.git/blob
ffeaf49d7f105f17735e083c2d22a6b0acfc87cf
[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
57         var config = this.config = getConfigAs(BoschSHCConfiguration.class);
58
59         String deviceId = config.id;
60         if (deviceId == null || deviceId.isBlank()) {
61             this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
62                     "@text/offline.conf-error.empty-device-id");
63             return;
64         }
65
66         // Try to get device info to make sure the device exists
67         try {
68             var bridgeHandler = this.getBridgeHandler();
69             var info = bridgeHandler.getDeviceInfo(deviceId);
70             logger.trace("Device initialized:\n{}", info);
71         } catch (TimeoutException | ExecutionException | BoschSHCException e) {
72             this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
73             return;
74         } catch (InterruptedException e) {
75             Thread.currentThread().interrupt();
76             this.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
77             return;
78         }
79
80         super.initialize();
81     }
82
83     /**
84      * Returns the unique id of the Bosch device.
85      *
86      * @return Unique id of the Bosch device.
87      */
88     @Override
89     public @Nullable String getBoschID() {
90         if (config != null) {
91             return config.id;
92         }
93
94         return null;
95     }
96 }