]> git.basschouten.com Git - openhab-addons.git/blob
cd34a97fbd16e65062c364b6cd6fad4eb27cd0ed
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.smartthings.internal.handler;
14
15 import java.util.Collection;
16 import java.util.LinkedList;
17
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;
31
32 /**
33  * The {@link SmartthingsBridgeHandler} is responsible for handling commands, which are
34  * sent to one of the channels.
35  *
36  * @author Bob Raker - Initial contribution
37  */
38 @NonNullByDefault
39 public class SmartthingsBridgeHandler extends ConfigStatusBridgeHandler {
40     private final Logger logger = LoggerFactory.getLogger(SmartthingsBridgeHandler.class);
41
42     private SmartthingsBridgeConfig config;
43
44     private SmartthingsHandlerFactory smartthingsHandlerFactory;
45     private BundleContext bundleContext;
46
47     public SmartthingsBridgeHandler(Bridge bridge, SmartthingsHandlerFactory smartthingsHandlerFactory,
48             BundleContext bundleContext) {
49         super(bridge);
50         this.smartthingsHandlerFactory = smartthingsHandlerFactory;
51         this.bundleContext = bundleContext;
52         config = getThing().getConfiguration().as(SmartthingsBridgeConfig.class);
53     }
54
55     @Override
56     public void handleCommand(ChannelUID channelUID, Command command) {
57         // Commands are handled by the "Things"
58     }
59
60     @Override
61     public void initialize() {
62         // Validate the config
63         if (!validateConfig(this.config)) {
64             return;
65         }
66
67         updateStatus(ThingStatus.ONLINE);
68     }
69
70     @Override
71     public void dispose() {
72         super.dispose();
73     }
74
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");
79             return false;
80         }
81
82         if (config.smartthingsPort <= 0) {
83             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
84                     "Smartthings Port is not specified");
85             return false;
86         }
87
88         return true;
89     }
90
91     public SmartthingsHandlerFactory getSmartthingsHandlerFactory() {
92         return smartthingsHandlerFactory;
93     }
94
95     public BundleContext getBundleContext() {
96         return bundleContext;
97     }
98
99     public String getSmartthingsIp() {
100         return config.smartthingsIp;
101     }
102
103     public int getSmartthingsPort() {
104         return config.smartthingsPort;
105     }
106
107     @Override
108     public Collection<ConfigStatusMessage> getConfigStatus() {
109         Collection<ConfigStatusMessage> configStatusMessages = new LinkedList<ConfigStatusMessage>();
110
111         // The IP must be provided
112         String ip = config.smartthingsIp;
113         if (ip.isEmpty()) {
114             configStatusMessages.add(ConfigStatusMessage.Builder.error(SmartthingsBindingConstants.IP_ADDRESS)
115                     .withMessageKeySuffix(SmartthingsBridgeConfigStatusMessage.IP_MISSING)
116                     .withArguments(SmartthingsBindingConstants.IP_ADDRESS).build());
117         }
118
119         // The PORT must be provided
120         int port = config.smartthingsPort;
121         if (port <= 0) {
122             configStatusMessages.add(ConfigStatusMessage.Builder.error(SmartthingsBindingConstants.PORT)
123                     .withMessageKeySuffix(SmartthingsBridgeConfigStatusMessage.PORT_MISSING)
124                     .withArguments(SmartthingsBindingConstants.PORT).build());
125         }
126
127         return configStatusMessages;
128     }
129 }