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 ModbusElements which have register addresses in the same range.
  • Field Details

    • name

      protected final String name
    • responseClazz

      protected final Class<RESPONSE extends com.ghgande.j2mod.modbus.msg.ModbusResponse> responseClazz
    • startAddress

      protected final int startAddress
    • length

      protected final int length
    • elements

      protected final ModbusElement[] elements
  • Constructor Details

  • Method Details

    • getElements

      public ModbusElement[] getElements()
      Description copied from interface: Task
      Gets the ModbusElements.
      Specified by:
      getElements in interface Task
      Returns:
      an array of ModbusElements
    • getLength

      public int getLength()
      Description copied from interface: Task
      Gets the length from first to last Modbus register address.
      Specified by:
      getLength in interface Task
      Returns:
      the address
    • getStartAddress

      public int getStartAddress()
      Description copied from interface: Task
      Gets the start Modbus register address.
      Specified by:
      getStartAddress in interface Task
      Returns:
      the address
    • setParent

      public void setParent(AbstractOpenemsModbusComponent parent)
      Description copied from interface: Task
      Sets the parent.
      Specified by:
      setParent in interface Task
      Parameters:
      parent - the parent AbstractOpenemsModbusComponent.
    • getParent

      public AbstractOpenemsModbusComponent getParent()
      Description copied from interface: Task
      Gets the parent.
      Specified by:
      getParent in interface Task
      Returns:
      the parent
    • execute

      public abstract Task.ExecuteState execute(AbstractModbusBridge bridge)
      Executes the tasks - i.e. sends the query of a ReadTask or writes a WriteTask.
      Specified by:
      execute in interface Task
      Parameters:
      bridge - the Modbus-Bridge
      Returns:
      the number of executed Sub-Tasks
    • executeRequest

      protected RESPONSE executeRequest(AbstractModbusBridge bridge, REQUEST request) throws Exception
      Actually executes a ModbusRequest and returns its ModbusResponse.

      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 - the AbstractModbusBridge
      request - the typed ModbusRequest
      Returns:
      the typed ModbusResponse
      Throws:
      OpenemsException - on error
      Exception
    • 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 Exception
      Logs the execution of a ModbusRequest.
      Parameters:
      tryExecute - marker for execute first/second try
      bridge - the BridgeModbus
      logVerbosity - the LogVerbosity
      request - the ModbusRequest
      supplier - ThrowingSupplier that executes the Request and returns a Response
      Returns:
      typed ModbusResponse
      Throws:
      Exception - on error
    • debug

      public AbstractTask<REQUEST,RESPONSE> debug()
      Activate Debug-Mode.
      Returns:
      myself
    • getLogVerbosity

      protected LogVerbosity getLogVerbosity(AbstractModbusBridge bridge)
      Combines the global and local (via isDebug log verbosity.
      Parameters:
      bridge - the parent Bridge
      Returns:
      the combined LogVerbosity
    • deactivate

      public void deactivate()
      Deactivate.
      Specified by:
      deactivate in interface Task
    • logInfo

      protected static void logInfo(org.slf4j.Logger log, String... messages)
    • logError

      protected static void logError(org.slf4j.Logger log, Exception e, String... messages)
    • toLogMessage

      protected final String toLogMessage(LogVerbosity logVerbosity, REQUEST request, Exception e)
    • toLogMessage

      protected final String toLogMessage(LogVerbosity logVerbosity, REQUEST request, RESPONSE response)
    • 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 - the LogVerbosity
      startAddress - the start address of the request
      length - the length of the request payload
      request - the ModbusRequest
      response - the ModbusResponse, possibly null
      exception - a Exception, possibly null
      Returns:
      a log message String
    • payloadToString

      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).
      Parameters:
      request - the request
      Returns:
      a string
    • payloadToString

      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).
      Parameters:
      response - the response
      Returns:
      a string