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.smartthings.internal.handler;
15 import java.util.Collection;
16 import java.util.LinkedList;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.smartthings.internal.SmartthingsBindingConstants;
20 import org.openhab.binding.smartthings.internal.SmartthingsHandlerFactory;
21 import org.openhab.core.config.core.status.ConfigStatusMessage;
22 import org.openhab.core.thing.Bridge;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.thing.ThingStatusDetail;
26 import org.openhab.core.thing.binding.ConfigStatusBridgeHandler;
27 import org.openhab.core.types.Command;
28 import org.osgi.framework.BundleContext;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * The {@link SmartthingsBridgeHandler} is responsible for handling commands, which are
34 * sent to one of the channels.
36 * @author Bob Raker - Initial contribution
39 public class SmartthingsBridgeHandler extends ConfigStatusBridgeHandler {
40 private final Logger logger = LoggerFactory.getLogger(SmartthingsBridgeHandler.class);
42 private SmartthingsBridgeConfig config;
44 private SmartthingsHandlerFactory smartthingsHandlerFactory;
45 private BundleContext bundleContext;
47 public SmartthingsBridgeHandler(Bridge bridge, SmartthingsHandlerFactory smartthingsHandlerFactory,
48 BundleContext bundleContext) {
50 this.smartthingsHandlerFactory = smartthingsHandlerFactory;
51 this.bundleContext = bundleContext;
52 config = getThing().getConfiguration().as(SmartthingsBridgeConfig.class);
56 public void handleCommand(ChannelUID channelUID, Command command) {
57 // Commands are handled by the "Things"
61 public void initialize() {
62 // Validate the config
63 if (!validateConfig(this.config)) {
67 updateStatus(ThingStatus.ONLINE);
71 public void dispose() {
75 private boolean validateConfig(SmartthingsBridgeConfig config) {
76 if (config.smartthingsIp.isEmpty()) {
77 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
78 "Smartthings IP address is not specified");
82 if (config.smartthingsPort <= 0) {
83 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
84 "Smartthings Port is not specified");
91 public SmartthingsHandlerFactory getSmartthingsHandlerFactory() {
92 return smartthingsHandlerFactory;
95 public BundleContext getBundleContext() {
99 public String getSmartthingsIp() {
100 return config.smartthingsIp;
103 public int getSmartthingsPort() {
104 return config.smartthingsPort;
108 public Collection<ConfigStatusMessage> getConfigStatus() {
109 Collection<ConfigStatusMessage> configStatusMessages = new LinkedList<ConfigStatusMessage>();
111 // The IP must be provided
112 String ip = config.smartthingsIp;
114 configStatusMessages.add(ConfigStatusMessage.Builder.error(SmartthingsBindingConstants.IP_ADDRESS)
115 .withMessageKeySuffix(SmartthingsBridgeConfigStatusMessage.IP_MISSING)
116 .withArguments(SmartthingsBindingConstants.IP_ADDRESS).build());
119 // The PORT must be provided
120 int port = config.smartthingsPort;
122 configStatusMessages.add(ConfigStatusMessage.Builder.error(SmartthingsBindingConstants.PORT)
123 .withMessageKeySuffix(SmartthingsBridgeConfigStatusMessage.PORT_MISSING)
124 .withArguments(SmartthingsBindingConstants.PORT).build());
127 return configStatusMessages;