Class AbstractModbusNetwork
- All Implemented Interfaces:
ModbusNetwork,net.solarnetwork.service.Identifiable
ModbusNetwork.- Since:
- 2.4
- Version:
- 3.1
- Author:
- matt
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic booleanA default value for theretriesproperty.static intA default value for theretriesproperty.static final longA default value for theretryDelayproperty.static booleanA default value for theretryReconnectproperty.static final longA default value for thetimeoutproperty.static final StringA default value for theuidproperty.protected final org.slf4j.LoggerA class-level logger.Fields inherited from interface net.solarnetwork.service.Identifiable
GROUP_UID_PROPERTY, UID_PROPERTY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidAcquire a network-wide lock, returning if lock acquired.addClassNamesToTreatAsIoException(Iterable<String> classNames) Add class names to the set of names to convert toIOExceptionif caught at runtime.protected ModbusConnectioncreateLockingConnection(ModbusConnection connection) Create a new connection that wraps the network-wide lock around another connection.protected List<net.solarnetwork.settings.SettingSpecifier>Get a list of base settings.Get the set of class names to convert toIOExceptioninstances if caught at runtime.protected StringGet a description of this network.intGet the number of "retries" to perform on each transaction in the event of errors.longGet the retry delay.Get the retry delay time unit.longGet the timeout value.Get the timeout unit.booleanGet the "headless" operation flag.booleanGet the retry reconnect mode.<T> TperformAction(int unitId, ModbusConnectionAction<T> action) Perform some action that requires aModbusConnection, returning the result.protected voidRelease the network-wide lock previously obtained viaacquireLock().removeClassNamesToTreatAsIoException(Iterable<String> classNames) Remove class names from the set of names to convert toIOExceptionif caught at runtime.voidsetHeadless(boolean headless) Set the "headless" operation flag.voidsetRetries(int retries) Set the number of "retries" to perform on each transaction in the event of errors.voidsetRetryDelay(long retryDelay) Set a retry delay between error retries.voidsetRetryDelayUnit(TimeUnit retryDelayUnit) Set the retry delay time unit.voidsetRetryReconnect(boolean retryReconnect) Toggle the mode to reconnect between error retries.voidsetTimeout(long timeout) Set a timeout value.voidsetTimeoutUnit(TimeUnit unit) Set the timeout unit.toString()Methods inherited from class net.solarnetwork.service.support.BasicIdentifiable
basicIdentifiableMetadataSettings, basicIdentifiableSettings, basicIdentifiableSettings, basicIdentifiableSettings, doubleMetadataValue, getDisplayName, getGroupUid, getGroupUID, getMessageSource, getMetadata, getMetadataCount, getUid, getUID, integerMetadataValue, metadataValue, numberMetadataValue, saveMetadataValue, saveMetadataValue, setDisplayName, setGroupUid, setGroupUID, setMessageSource, setMetadata, setMetadataCount, setUid, setUID, smartMetadataValueMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface net.solarnetwork.service.Identifiable
getDisplayName, getGroupUid, getUidMethods inherited from interface net.solarnetwork.node.io.modbus.ModbusNetwork
createConnection
-
Field Details
-
DEFAULT_RETRY_DELAY_MILLIS
public static final long DEFAULT_RETRY_DELAY_MILLISA default value for theretryDelayproperty.- See Also:
-
DEFAULT_UID
A default value for theuidproperty.- Since:
- 3.0
- See Also:
-
DEFAULT_TIMEOUT_SECS
public static final long DEFAULT_TIMEOUT_SECSA default value for thetimeoutproperty.- Since:
- 3.0
- See Also:
-
DEFAULT_RETRIES
public static int DEFAULT_RETRIESA default value for theretriesproperty.- Since:
- 3.0
-
DEFAULT_HEADLESS
public static boolean DEFAULT_HEADLESSA default value for theretriesproperty.- Since:
- 3.0
-
DEFAULT_RETRY_RECONNECT
public static boolean DEFAULT_RETRY_RECONNECTA default value for theretryReconnectproperty.- Since:
- 3.0
-
log
protected final org.slf4j.Logger logA class-level logger.
-
-
Constructor Details
-
AbstractModbusNetwork
public AbstractModbusNetwork()Constructor.
-
-
Method Details
-
getClassNamesToTreatAsIoException
Get the set of class names to convert toIOExceptioninstances if caught at runtime.- Returns:
- the set of class names
- Since:
- 2.0
-
addClassNamesToTreatAsIoException
Add class names to the set of names to convert toIOExceptionif caught at runtime.- Parameters:
classNames- the names to convert when caught- Returns:
- the final set of configured names
- Since:
- 2.0
-
removeClassNamesToTreatAsIoException
Remove class names from the set of names to convert toIOExceptionif caught at runtime.- Parameters:
classNames- the names to no longer convert when caught- Returns:
- the final set of configured names
- Since:
- 2.0
-
performAction
Description copied from interface:ModbusNetworkPerform some action that requires aModbusConnection, returning the result.The
ModbusConnectionAction.doWithConnection(ModbusConnection)method will be called and the result returned by this method. TheModbusConnectionpassed will already be opened, and it will be closed automatically after the action is complete.- Specified by:
performActionin interfaceModbusNetwork- Type Parameters:
T- the result type- Parameters:
unitId- the Modbus unit ID to addressaction- 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.LockTimeoutExceptionAcquire 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 viaacquireLock().This method is safe to call even if the lock has already been released.
-
createLockingConnection
Create a new connection that wraps the network-wide lock around another connection.The
ModbusConnection.open()call will acquire the lock, andModbusConnection.close()will release it.- Parameters:
connection- the connection to warp- Returns:
- a new connection that uses the network-wide lock
- Since:
- 2.1
-
toString
-
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
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
Get the timeout unit.- Returns:
- the timeout unit; defaults to seconds
-
setTimeoutUnit
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
Get the retry delay time unit.- Returns:
- the time unit; defaults to
TimeUnit.MILLISECONDS
-
setRetryDelayUnit
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
-