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.modbus.internal.handler;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.modbus.handler.EndpointNotInitializedException;
18 import org.openhab.binding.modbus.handler.ModbusEndpointThingHandler;
19 import org.openhab.binding.modbus.internal.ModbusConfigurationException;
20 import org.openhab.core.io.transport.modbus.ModbusCommunicationInterface;
21 import org.openhab.core.io.transport.modbus.ModbusManager;
22 import org.openhab.core.io.transport.modbus.endpoint.EndpointPoolConfiguration;
23 import org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.thing.ThingStatusDetail;
28 import org.openhab.core.thing.binding.BaseBridgeHandler;
29 import org.openhab.core.types.Command;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Base class for Modbus Slave endpoint thing handlers
36 * @author Sami Salonen - Initial contribution
38 * @param <E> endpoint class
39 * @param <C> config class
42 public abstract class AbstractModbusEndpointThingHandler<E extends ModbusSlaveEndpoint, C> extends BaseBridgeHandler
43 implements ModbusEndpointThingHandler {
45 protected volatile @Nullable C config;
46 protected volatile @Nullable E endpoint;
47 protected ModbusManager modbusManager;
48 protected volatile @NonNullByDefault({}) EndpointPoolConfiguration poolConfiguration;
49 private final Logger logger = LoggerFactory.getLogger(AbstractModbusEndpointThingHandler.class);
50 private @NonNullByDefault({}) ModbusCommunicationInterface comms;
52 public AbstractModbusEndpointThingHandler(Bridge bridge, ModbusManager modbusManager) {
54 this.modbusManager = modbusManager;
58 public void handleCommand(ChannelUID channelUID, Command command) {
62 public void initialize() {
64 logger.trace("Initializing {} from status {}", this.getThing().getUID(), this.getThing().getStatus());
65 if (this.getThing().getStatus().equals(ThingStatus.ONLINE)) {
66 // If the bridge was online then first change it to offline.
67 // this ensures that children will be notified about the change
68 updateStatus(ThingStatus.OFFLINE);
73 E endpoint = this.endpoint;
74 if (endpoint == null) {
75 throw new IllegalStateException("endpoint null after configuration!");
78 comms = modbusManager.newModbusCommunicationInterface(endpoint, poolConfiguration);
79 updateStatus(ThingStatus.ONLINE);
80 } catch (IllegalArgumentException e) {
81 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
82 formatConflictingParameterError());
84 } catch (ModbusConfigurationException e) {
85 logger.debug("Exception during initialization", e);
86 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, String.format(
87 "Exception during initialization: %s (%s)", e.getMessage(), e.getClass().getSimpleName()));
89 logger.trace("initialize() of thing {} '{}' finished", thing.getUID(), thing.getLabel());
95 public void dispose() {
97 ModbusCommunicationInterface localComms = comms;
98 if (localComms != null) {
101 } catch (Exception e) {
102 logger.warn("Error closing modbus communication interface", e);
109 public @Nullable ModbusCommunicationInterface getCommunicationInterface() {
114 public E getEndpoint() {
119 public abstract int getSlaveId() throws EndpointNotInitializedException;
122 * Must be overriden by subclasses to initialize config, endpoint, and poolConfiguration
124 protected abstract void configure() throws ModbusConfigurationException;
127 * Format error message in case some other endpoint has been configured with different
128 * {@link EndpointPoolConfiguration}
130 protected abstract String formatConflictingParameterError();