]> git.basschouten.com Git - openhab-addons.git/blob
e8620ca4c2611c8e003c29f8fc36622d11ffcc6b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.modbus.internal.handler;
14
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.Optional;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.modbus.discovery.internal.ModbusEndpointDiscoveryService;
21 import org.openhab.binding.modbus.handler.EndpointNotInitializedException;
22 import org.openhab.binding.modbus.internal.ModbusConfigurationException;
23 import org.openhab.binding.modbus.internal.config.ModbusTcpConfiguration;
24 import org.openhab.core.io.transport.modbus.ModbusManager;
25 import org.openhab.core.io.transport.modbus.endpoint.EndpointPoolConfiguration;
26 import org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint;
27 import org.openhab.core.thing.Bridge;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.thing.binding.ThingHandlerService;
30
31 /**
32  * Endpoint thing handler for TCP slaves
33  *
34  * @author Sami Salonen - Initial contribution
35  */
36 @NonNullByDefault
37 public class ModbusTcpThingHandler
38         extends AbstractModbusEndpointThingHandler<ModbusTCPSlaveEndpoint, ModbusTcpConfiguration> {
39
40     public ModbusTcpThingHandler(Bridge bridge, ModbusManager manager) {
41         super(bridge, manager);
42     }
43
44     @Override
45     protected void configure() throws ModbusConfigurationException {
46         ModbusTcpConfiguration config = getConfigAs(ModbusTcpConfiguration.class);
47
48         String host = config.getHost();
49         if (host == null) {
50             throw new ModbusConfigurationException("host must be non-null!");
51         }
52
53         this.config = config;
54         endpoint = new ModbusTCPSlaveEndpoint(host, config.getPort(), config.getRtuEncoded());
55
56         EndpointPoolConfiguration poolConfiguration = new EndpointPoolConfiguration();
57         this.poolConfiguration = poolConfiguration;
58         poolConfiguration.setConnectMaxTries(config.getConnectMaxTries());
59         poolConfiguration.setConnectTimeoutMillis(config.getConnectTimeoutMillis());
60         poolConfiguration.setInterConnectDelayMillis(config.getTimeBetweenReconnectMillis());
61         poolConfiguration.setInterTransactionDelayMillis(config.getTimeBetweenTransactionsMillis());
62         poolConfiguration.setReconnectAfterMillis(config.getReconnectAfterMillis());
63     }
64
65     @SuppressWarnings("null") // since Optional.map is always called with NonNull argument
66     @Override
67     protected String formatConflictingParameterError() {
68         return String.format(
69                 "Endpoint '%s' has conflicting parameters: parameters of this thing (%s '%s') are different from some other thing's parameter. Ensure that all endpoints pointing to tcp slave '%s:%s' have same parameters.",
70                 endpoint, thing.getUID(), this.thing.getLabel(),
71                 Optional.ofNullable(this.endpoint).map(e -> e.getAddress()).orElse("<null>"),
72                 Optional.ofNullable(this.endpoint).map(e -> String.valueOf(e.getPort())).orElse("<null>"));
73     }
74
75     @Override
76     public int getSlaveId() throws EndpointNotInitializedException {
77         ModbusTcpConfiguration localConfig = config;
78         if (localConfig == null) {
79             throw new EndpointNotInitializedException();
80         }
81         return localConfig.getId();
82     }
83
84     @Override
85     public ThingUID getUID() {
86         return getThing().getUID();
87     }
88
89     /**
90      * Returns true if discovery is enabled
91      */
92     @Override
93     public boolean isDiscoveryEnabled() {
94         if (config != null) {
95             return config.isDiscoveryEnabled();
96         } else {
97             return false;
98         }
99     }
100
101     @Override
102     public Collection<Class<? extends ThingHandlerService>> getServices() {
103         return Collections.singleton(ModbusEndpointDiscoveryService.class);
104     }
105 }