]> git.basschouten.com Git - openhab-addons.git/blob
50e0a63ad1a52c90be76ebd0d36ccaa00c1160a7
[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.satel.internal.handler;
14
15 import static org.openhab.binding.satel.internal.SatelBindingConstants.THING_TYPE_INTRS;
16 import static org.openhab.binding.satel.internal.config.IntRSConfig.PORT;
17
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Set;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.satel.internal.config.IntRSConfig;
24 import org.openhab.binding.satel.internal.protocol.IntRSModule;
25 import org.openhab.binding.satel.internal.protocol.SatelModule;
26 import org.openhab.core.config.core.status.ConfigStatusMessage;
27 import org.openhab.core.io.transport.serial.SerialPortManager;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.ThingStatus;
30 import org.openhab.core.thing.ThingStatusDetail;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * {@link IntRSBridgeHandler} is a bridge handler for INT-RS communication module.
37  * All {@link SatelThingHandler}s use it to receive events and execute commands.
38  *
39  * @author Krzysztof Goworek - Initial contribution
40  */
41 @NonNullByDefault
42 public class IntRSBridgeHandler extends SatelBridgeHandler {
43
44     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_INTRS);
45
46     private final Logger logger = LoggerFactory.getLogger(IntRSBridgeHandler.class);
47
48     private final SerialPortManager serialPortManager;
49
50     public IntRSBridgeHandler(Bridge bridge, SerialPortManager serialPortManager) {
51         super(bridge);
52         this.serialPortManager = serialPortManager;
53     }
54
55     @Override
56     public void initialize() {
57         logger.debug("Initializing handler");
58
59         final IntRSConfig config = getConfigAs(IntRSConfig.class);
60         final String port = config.getPort();
61         if (!port.isBlank()) {
62             SatelModule satelModule = new IntRSModule(port, serialPortManager, config.getTimeout(),
63                     config.hasExtCommandsSupport());
64             super.initialize(satelModule);
65         } else {
66             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
67                     "Cannot connect to Satel INT-RS module. Serial port is not set.");
68         }
69     }
70
71     @Override
72     public Collection<ConfigStatusMessage> getConfigStatus() {
73         // The bridge serial port to be used for checks
74         final String port = (String) getThing().getConfiguration().get(PORT);
75         Collection<ConfigStatusMessage> configStatusMessages;
76
77         // Check whether a serial port is provided
78         if (port.isBlank()) {
79             configStatusMessages = Collections.singletonList(ConfigStatusMessage.Builder.error(PORT)
80                     .withMessageKeySuffix("portEmpty").withArguments(PORT).build());
81         } else {
82             configStatusMessages = Collections.emptyList();
83         }
84
85         return configStatusMessages;
86     }
87 }