]> git.basschouten.com Git - openhab-addons.git/blob
131253da0406a27883c5c1583e46b5a1d5411de8
[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.adorne.internal.handler;
14
15 import static org.openhab.binding.adorne.internal.AdorneBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.adorne.internal.configuration.AdorneHubConfiguration;
20 import org.openhab.binding.adorne.internal.hub.AdorneHubChangeNotify;
21 import org.openhab.binding.adorne.internal.hub.AdorneHubController;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.PercentType;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.thing.ThingStatusDetail;
28 import org.openhab.core.thing.binding.BaseBridgeHandler;
29 import org.openhab.core.types.Command;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * The {@link AdorneHubHandler} manages the state and status of the Adorne Hub's devices.
35  *
36  * @author Mark Theiding - Initial contribution
37  */
38 @NonNullByDefault
39 public class AdorneHubHandler extends BaseBridgeHandler implements AdorneHubChangeNotify {
40     private final Logger logger = LoggerFactory.getLogger(AdorneHubHandler.class);
41     private @Nullable AdorneHubController adorneHubController = null;
42
43     public AdorneHubHandler(Bridge bridge) {
44         super(bridge);
45     }
46
47     /**
48      * The {@link AdorneHubHandler} does not support any commands itself. This method is a NOOP and only provided since
49      * its implementation is required.
50      *
51      */
52     @Override
53     public void handleCommand(ChannelUID channelUID, Command command) {
54         // Unfortunately BaseBridgeHandler doesn't provide a default implementation of handleCommand. However, hub
55         // commands could be added as a future enhancement e.g. to support hub firmware upgrades.
56     }
57
58     /**
59      * Establishes the hub controller for communication with the hub.
60      */
61     @Override
62     public void initialize() {
63         logger.debug("Initializing hub {}", getThing().getLabel());
64
65         updateStatus(ThingStatus.UNKNOWN);
66         AdorneHubConfiguration config = getConfigAs(AdorneHubConfiguration.class);
67         logger.debug("Configuration host:{} port:{}", config.host, config.port);
68
69         AdorneHubController adorneHubController = new AdorneHubController(config, scheduler, this);
70         this.adorneHubController = adorneHubController;
71         // Kick off the hub controller that handles all interactions with the hub for us
72         adorneHubController.start();
73     }
74
75     /**
76      * Disposes resources by stopping the hub controller.
77      */
78     @Override
79     public void dispose() {
80         AdorneHubController adorneHubController = this.adorneHubController;
81         if (adorneHubController != null) {
82             adorneHubController.stop();
83         }
84     }
85
86     /**
87      * Returns the hub controller. Returns <code>null</code> if hub controller has not been created yet.
88      *
89      * @return hub controller
90      */
91     public @Nullable AdorneHubController getAdorneHubController() {
92         return adorneHubController;
93     }
94
95     /**
96      * The {@link AdorneHubHandler} is notified that the state of one of its physical devices has changed. The
97      * {@link AdorneHubHandler} then asks the appropriate thing handler to update the thing to match the new state.
98      *
99      */
100     @Override
101     public void stateChangeNotify(int zoneId, boolean onOff, int brightness) {
102         logger.debug("State changed (zoneId:{} onOff:{} brightness:{})", zoneId, onOff, brightness);
103         getThing().getThings().forEach(thing -> {
104             AdorneSwitchHandler thingHandler = (AdorneSwitchHandler) thing.getHandler();
105             if (thingHandler != null && thingHandler.getZoneId() == zoneId) {
106                 thingHandler.updateState(CHANNEL_POWER, OnOffType.from(onOff));
107                 if (thing.getThingTypeUID().equals(THING_TYPE_DIMMER)) {
108                     thingHandler.updateState(CHANNEL_BRIGHTNESS, new PercentType(brightness));
109                 }
110             }
111         });
112     }
113
114     /**
115      * The {@link AdorneHubHandler} is notified that its connectivity has changed.
116      *
117      */
118     @Override
119     public void connectionChangeNotify(boolean connected) {
120         logger.debug("Status changed (connected:{})", connected);
121
122         if (connected) {
123             // Refresh all of our things in case thing states changed while we were disconnected
124             getThing().getThings().forEach(thing -> {
125                 AdorneSwitchHandler thingHandler = (AdorneSwitchHandler) thing.getHandler();
126                 if (thingHandler != null) {
127                     thingHandler.refresh();
128                 }
129             });
130             updateStatus(ThingStatus.ONLINE);
131         } else {
132             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
133         }
134     }
135 }