2 * Copyright (c) 2010-2020 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.io.transport.modbus;
15 import java.util.concurrent.Future;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.io.transport.modbus.endpoint.ModbusSlaveEndpoint;
21 * Interface for interacting with a particular modbus slave.
23 * When no further communication is expected with the slave, close the interface so that any underlying resources can be
26 * Close unregisters all the regular polls registered with registerRegularPoll. When endpoint's last
27 * communication interface is closed, the connection is closed as well, no matter the what EndpointPoolConfiguration
30 * @author Sami Salonen - Initial contribution
34 public interface ModbusCommunicationInterface extends AutoCloseable {
37 * Get endpoint associated with this communication interface
39 * @return modbus slave endpoint
41 public ModbusSlaveEndpoint getEndpoint();
44 * Submit one-time poll task. The method returns immediately, and the execution of the poll task will happen in
47 * @param request request to send
48 * @param callback callback to call with data
49 * @param callback callback to call in case of failure
50 * @return future representing the polled task
51 * @throws IllegalStateException when this communication has been closed already
53 public Future<?> submitOneTimePoll(ModbusReadRequestBlueprint request, ModbusReadCallback resultCallback,
54 ModbusFailureCallback<ModbusReadRequestBlueprint> failureCallback);
57 * Register regularly polled task. The method returns immediately, and the execution of the poll task will happen in
60 * One can register only one regular poll task for triplet of (endpoint, request, callback).
62 * @param request request to send
63 * @param pollPeriodMillis poll interval, in milliseconds
64 * @param initialDelayMillis initial delay before starting polling, in milliseconds
65 * @param callback callback to call with data
66 * @param callback callback to call in case of failure
67 * @return poll task representing the regular poll
68 * @throws IllegalStateException when this communication has been closed already
70 public PollTask registerRegularPoll(ModbusReadRequestBlueprint request, long pollPeriodMillis,
71 long initialDelayMillis, ModbusReadCallback resultCallback,
72 ModbusFailureCallback<ModbusReadRequestBlueprint> failureCallback);
75 * Unregister regularly polled task
77 * If this communication interface is closed already, the method returns immediately with false return value
79 * @param task poll task to unregister
80 * @return whether poll task was unregistered. Poll task is not unregistered in case of unexpected errors or
81 * in the case where the poll task is not registered in the first place
83 public boolean unregisterRegularPoll(PollTask task);
86 * Submit one-time write task. The method returns immediately, and the execution of the task will happen in
89 * @param request request to send
90 * @param callback callback to call with response
91 * @param callback callback to call in case of failure
92 * @return future representing the task
93 * @throws IllegalStateException when this communication has been closed already
95 public Future<?> submitOneTimeWrite(ModbusWriteRequestBlueprint request, ModbusWriteCallback resultCallback,
96 ModbusFailureCallback<ModbusWriteRequestBlueprint> failureCallback);
99 * Close this communication interface and try to free all resources associated with it
101 * Upon close, all polling tasks registered by this instance are unregistered. In addition, connections are closed
102 * eagerly if this was the last connection interface pointing to the endpoint.
104 * After close, the communication interface cannot be used to communicate with the device.
108 public void close() throws Exception;