2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.adorne.internal.handler;
15 import static org.openhab.binding.adorne.internal.AdorneBindingConstants.*;
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;
34 * The {@link AdorneHubHandler} manages the state and status of the Adorne Hub's devices.
36 * @author Mark Theiding - Initial contribution
39 public class AdorneHubHandler extends BaseBridgeHandler implements AdorneHubChangeNotify {
40 private final Logger logger = LoggerFactory.getLogger(AdorneHubHandler.class);
41 private @Nullable AdorneHubController adorneHubController = null;
43 public AdorneHubHandler(Bridge bridge) {
48 * The {@link AdorneHubHandler} does not support any commands itself. This method is a NOOP and only provided since
49 * its implementation is required.
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.
59 * Establishes the hub controller for communication with the hub.
62 public void initialize() {
63 logger.debug("Initializing hub {}", getThing().getLabel());
65 updateStatus(ThingStatus.UNKNOWN);
66 AdorneHubConfiguration config = getConfigAs(AdorneHubConfiguration.class);
67 logger.debug("Configuration host:{} port:{}", config.host, config.port);
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();
76 * Disposes resources by stopping the hub controller.
79 public void dispose() {
80 AdorneHubController adorneHubController = this.adorneHubController;
81 if (adorneHubController != null) {
82 adorneHubController.stop();
87 * Returns the hub controller. Returns <code>null</code> if hub controller has not been created yet.
89 * @return hub controller
91 public @Nullable AdorneHubController getAdorneHubController() {
92 return adorneHubController;
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.
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));
115 * The {@link AdorneHubHandler} is notified that its connectivity has changed.
119 public void connectionChangeNotify(boolean connected) {
120 logger.debug("Status changed (connected:{})", 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();
130 updateStatus(ThingStatus.ONLINE);
132 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);