Class AbstractModbusNetwork

java.lang.Object
net.solarnetwork.service.support.BasicIdentifiable
net.solarnetwork.node.io.modbus.support.AbstractModbusNetwork
All Implemented Interfaces:
ModbusNetwork, net.solarnetwork.service.Identifiable

public abstract class AbstractModbusNetwork extends net.solarnetwork.service.support.BasicIdentifiable implements ModbusNetwork
Abstract implementation of ModbusNetwork.
Since:
2.4
Version:
3.1
Author:
matt
  • Field Details

    • DEFAULT_RETRY_DELAY_MILLIS

      public static final long DEFAULT_RETRY_DELAY_MILLIS
      A default value for the retryDelay property.
      See Also:
    • DEFAULT_UID

      public static final String DEFAULT_UID
      A default value for the uid property.
      Since:
      3.0
      See Also:
    • DEFAULT_TIMEOUT_SECS

      public static final long DEFAULT_TIMEOUT_SECS
      A default value for the timeout property.
      Since:
      3.0
      See Also:
    • DEFAULT_RETRIES

      public static int DEFAULT_RETRIES
      A default value for the retries property.
      Since:
      3.0
    • DEFAULT_HEADLESS

      public static boolean DEFAULT_HEADLESS
      A default value for the retries property.
      Since:
      3.0
    • DEFAULT_RETRY_RECONNECT

      public static boolean DEFAULT_RETRY_RECONNECT
      A default value for the retryReconnect property.
      Since:
      3.0
    • log

      protected final org.slf4j.Logger log
      A class-level logger.
  • Constructor Details

    • AbstractModbusNetwork

      public AbstractModbusNetwork()
      Constructor.
  • Method Details

    • getClassNamesToTreatAsIoException

      protected final Set<String> getClassNamesToTreatAsIoException()
      Get the set of class names to convert to IOException instances if caught at runtime.
      Returns:
      the set of class names
      Since:
      2.0
    • addClassNamesToTreatAsIoException

      protected final Set<String> addClassNamesToTreatAsIoException(Iterable<String> classNames)
      Add class names to the set of names to convert to IOException if caught at runtime.
      Parameters:
      classNames - the names to convert when caught
      Returns:
      the final set of configured names
      Since:
      2.0
    • removeClassNamesToTreatAsIoException

      protected final Set<String> removeClassNamesToTreatAsIoException(Iterable<String> classNames)
      Remove class names from the set of names to convert to IOException if caught at runtime.
      Parameters:
      classNames - the names to no longer convert when caught
      Returns:
      the final set of configured names
      Since:
      2.0
    • performAction

      public <T> T performAction(int unitId, ModbusConnectionAction<T> action) throws IOException
      Description copied from interface: ModbusNetwork
      Perform some action that requires a ModbusConnection, returning the result.

      The ModbusConnectionAction.doWithConnection(ModbusConnection) method will be called and the result returned by this method. The ModbusConnection passed will already be opened, and it will be closed automatically after the action is complete.

      Specified by:
      performAction in interface ModbusNetwork
      Type Parameters:
      T - the result type
      Parameters:
      unitId - the Modbus unit ID to address
      action - the callback whose result to return
      Returns:
      the result of calling ModbusConnectionAction.doWithConnection(ModbusConnection)
      Throws:
      IOException - if any IO error occurs
    • acquireLock

      protected void acquireLock() throws net.solarnetwork.node.service.LockTimeoutException
      Acquire a network-wide lock, returning if lock acquired.
      Throws:
      net.solarnetwork.node.service.LockTimeoutException - if the lock cannot be obtained
    • releaseLock

      protected void releaseLock()
      Release the network-wide lock previously obtained via acquireLock().

      This method is safe to call even if the lock has already been released.

    • createLockingConnection

      protected ModbusConnection createLockingConnection(ModbusConnection connection)
      Create a new connection that wraps the network-wide lock around another connection.

      The ModbusConnection.open() call will acquire the lock, and ModbusConnection.close() will release it.

      Parameters:
      connection - the connection to warp
      Returns:
      a new connection that uses the network-wide lock
      Since:
      2.1
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getNetworkDescription

      protected String getNetworkDescription()
      Get a description of this network.

      This implementation simply calls toString() on this object. Extending classes may want to provide something more meaningful.

      Returns:
      a description of this network
    • getBaseSettingSpecifiers

      protected List<net.solarnetwork.settings.SettingSpecifier> getBaseSettingSpecifiers()
      Get a list of base settings.
      Returns:
      the base settings
    • getTimeout

      public long getTimeout()
      Get the timeout value.
      Returns:
      the timeout value, defaults to 10
    • setTimeout

      public void setTimeout(long timeout)
      Set a timeout value.
      Parameters:
      timeout - the timeout
    • getTimeoutUnit

      public TimeUnit getTimeoutUnit()
      Get the timeout unit.
      Returns:
      the timeout unit; defaults to seconds
    • setTimeoutUnit

      public void setTimeoutUnit(TimeUnit unit)
      Set the timeout unit.
      Parameters:
      unit - the unit
    • isHeadless

      public boolean isHeadless()
      Get the "headless" operation flag.
      Returns:
      the headless mode; defaults to true
      See Also:
    • setHeadless

      public void setHeadless(boolean headless)
      Set the "headless" operation flag.

      When true, a 6-byte Modbus header with a transaction ID, etc. is left off requests. This is most often used for Modbus RTU over serial connections. When false the header is included. This is most often used with Modbus TCP.

      Parameters:
      headless - true for headless operation, false otherwise
    • getRetries

      public int getRetries()
      Get the number of "retries" to perform on each transaction in the event of errors.
      Returns:
      the number of retries; defaults to 3
    • setRetries

      public void setRetries(int retries)
      Set the number of "retries" to perform on each transaction in the event of errors.
      Parameters:
      retries - the number of retries
    • getRetryDelay

      public long getRetryDelay()
      Get the retry delay.
      Returns:
      the retry delay
    • setRetryDelay

      public void setRetryDelay(long retryDelay)
      Set a retry delay between error retries.
      Parameters:
      retryDelay - the delay, or 0 for no delay
    • getRetryDelayUnit

      public TimeUnit getRetryDelayUnit()
      Get the retry delay time unit.
      Returns:
      the time unit; defaults to TimeUnit.MILLISECONDS
    • setRetryDelayUnit

      public void setRetryDelayUnit(TimeUnit retryDelayUnit)
      Set the retry delay time unit.
      Parameters:
      retryDelayUnit - the unit to set
    • isRetryReconnect

      public boolean isRetryReconnect()
      Get the retry reconnect mode.
      Returns:
      true to reconnect between error retries, false to continue using the same connection; defaults to false
    • setRetryReconnect

      public void setRetryReconnect(boolean retryReconnect)
      Toggle the mode to reconnect between error retries.

      When enabled, if an IO error occurs while executing a transaction the connection will be closed and reopened.

      Parameters:
      retryReconnect - true to reconnect between error retries, false to continue using the same connection