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