Class AbstractTask<REQUEST extends com.ghgande.j2mod.modbus.msg.ModbusRequest,RESPONSE extends com.ghgande.j2mod.modbus.msg.ModbusResponse>
java.lang.Object
io.openems.edge.bridge.modbus.api.task.AbstractTask<REQUEST,RESPONSE>
- All Implemented Interfaces:
Task
,ManagedTask
- Direct Known Subclasses:
AbstractReadTask
,AbstractWriteTask
public abstract non-sealed class AbstractTask<REQUEST extends com.ghgande.j2mod.modbus.msg.ModbusRequest,RESPONSE extends com.ghgande.j2mod.modbus.msg.ModbusResponse>
extends Object
implements Task
An abstract Modbus 'AbstractTask' is holding references to one or more Modbus
ModbusElement
s which have register addresses in the same range.-
Nested Class Summary
Nested classes/interfaces inherited from interface io.openems.edge.bridge.modbus.api.task.Task
Task.ExecuteState
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ModbusElement[]
protected final int
protected final String
protected final Consumer
<Task.ExecuteState> protected final int
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractTask
(String name, Consumer<Task.ExecuteState> onExecute, Class<RESPONSE> responseClazz, int startAddress, ModbusElement... elements) -
Method Summary
Modifier and TypeMethodDescriptionvoid
Deactivate.debug()
Activate Debug-Mode.abstract Task.ExecuteState
execute
(AbstractModbusBridge bridge) Executes the tasks - i.e.protected RESPONSE
executeRequest
(AbstractModbusBridge bridge, REQUEST request) Actually executes aModbusRequest
and returns itsModbusResponse
.Gets the ModbusElements.int
Gets the length from first to last Modbus register address.protected LogVerbosity
getLogVerbosity
(AbstractModbusBridge bridge) Combines the global and local (viaisDebug
log verbosity.Gets the parent.int
Gets the start Modbus register address.protected static void
protected static void
protected RESPONSE
logRequest
(io.openems.edge.bridge.modbus.api.task.AbstractTask.TryExecute tryExecute, BridgeModbus bridge, LogVerbosity logVerbosity, REQUEST request, ThrowingSupplier<RESPONSE, Exception> supplier) Logs the execution of aModbusRequest
.protected abstract String
payloadToString
(REQUEST request) Converts the actual payload of the REQUEST to a human readable format suitable for logs; without header data (like Unit-ID, function code, checksum, etc).protected abstract String
payloadToString
(RESPONSE response) Converts the actual payload of the RESPONSE to a human readable format suitable for logs; without header data (like Unit-ID, function code, checksum, etc).void
Sets the parent.protected final String
toLogMessage
(LogVerbosity logVerbosity, int startAddress, int length, REQUEST request, RESPONSE response, Exception exception) Generates a log message for this task.protected final String
toLogMessage
(LogVerbosity logVerbosity, REQUEST request, Exception e) protected final String
toLogMessage
(LogVerbosity logVerbosity, REQUEST request, RESPONSE response) protected String
toLogMessage
(LogVerbosity logVerbosity, REQUEST request, RESPONSE response, Exception e) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.openems.edge.common.taskmanager.ManagedTask
getPriority
-
Field Details
-
name
-
onExecute
-
responseClazz
-
startAddress
protected final int startAddress -
length
protected final int length -
elements
-
-
Constructor Details
-
AbstractTask
public AbstractTask(String name, Consumer<Task.ExecuteState> onExecute, Class<RESPONSE> responseClazz, int startAddress, ModbusElement... elements)
-
-
Method Details
-
getElements
Description copied from interface:Task
Gets the ModbusElements.- Specified by:
getElements
in interfaceTask
- Returns:
- an array of ModbusElements
-
getLength
public int getLength()Description copied from interface:Task
Gets the length from first to last Modbus register address. -
getStartAddress
public int getStartAddress()Description copied from interface:Task
Gets the start Modbus register address.- Specified by:
getStartAddress
in interfaceTask
- Returns:
- the address
-
setParent
Description copied from interface:Task
Sets the parent.- Specified by:
setParent
in interfaceTask
- Parameters:
parent
- the parentAbstractOpenemsModbusComponent
.
-
getParent
Description copied from interface:Task
Gets the parent. -
execute
Executes the tasks - i.e. sends the query of a ReadTask or writes a WriteTask.- Specified by:
execute
in interfaceTask
- Parameters:
bridge
- the Modbus-Bridge- Returns:
- the
Task.ExecuteState
-
executeRequest
Actually executes aModbusRequest
and returns itsModbusResponse
.If first request fails, the implementation reconnects the Modbus connection and tries again.
Successful execution is produces a log message if
LogVerbosity
!= 'NONE' was configured. Errors are always logged.- Parameters:
bridge
- theAbstractModbusBridge
request
- the typedModbusRequest
- Returns:
- the typed
ModbusResponse
- Throws:
OpenemsException
- on errorException
-
logRequest
protected RESPONSE logRequest(io.openems.edge.bridge.modbus.api.task.AbstractTask.TryExecute tryExecute, BridgeModbus bridge, LogVerbosity logVerbosity, REQUEST request, ThrowingSupplier<RESPONSE, Exception> supplier) throws ExceptionLogs the execution of aModbusRequest
.- Parameters:
tryExecute
- marker for execute first/second trybridge
- theBridgeModbus
logVerbosity
- theLogVerbosity
request
- theModbusRequest
supplier
-ThrowingSupplier
that executes the Request and returns a Response- Returns:
- typed
ModbusResponse
- Throws:
Exception
- on error
-
debug
Activate Debug-Mode.- Returns:
- myself
-
getLogVerbosity
Combines the global and local (viaisDebug
log verbosity.- Parameters:
bridge
- the parent Bridge- Returns:
- the combined LogVerbosity
-
deactivate
public void deactivate()Deactivate.- Specified by:
deactivate
in interfaceTask
-
logInfo
-
logError
-
toLogMessage
-
toLogMessage
-
toLogMessage
protected String toLogMessage(LogVerbosity logVerbosity, REQUEST request, RESPONSE response, Exception e) -
toLogMessage
protected final String toLogMessage(LogVerbosity logVerbosity, int startAddress, int length, REQUEST request, RESPONSE response, Exception exception) Generates a log message for this task.StartAddress and length need to be provided explicitly, because FC16 task might be split to multiple requests.
For certain Exceptions we internally increase the LogVerbosity to always show helpful information
- Parameters:
logVerbosity
- theLogVerbosity
startAddress
- the start address of the requestlength
- the length of the request payloadrequest
- theModbusRequest
response
- theModbusResponse
, possibly nullexception
- aException
, possibly null- Returns:
- a log message String
-
payloadToString
Converts the actual payload of the REQUEST to a human readable format suitable for logs; without header data (like Unit-ID, function code, checksum, etc).- Parameters:
request
- the request- Returns:
- a string
-
payloadToString
Converts the actual payload of the RESPONSE to a human readable format suitable for logs; without header data (like Unit-ID, function code, checksum, etc).- Parameters:
response
- the response- Returns:
- a string
-