Class ModbusDataDeviceSupport<T extends ModbusData & net.solarnetwork.node.domain.DataAccessor>

java.lang.Object
net.solarnetwork.service.support.BasicIdentifiable
net.solarnetwork.node.service.support.BaseIdentifiable
net.solarnetwork.node.io.modbus.support.ModbusDeviceSupport
net.solarnetwork.node.io.modbus.support.ModbusDataDeviceSupport<T>
Type Parameters:
T - the ModbusData type
All Implemented Interfaces:
net.solarnetwork.service.Identifiable

public abstract class ModbusDataDeviceSupport<T extends ModbusData & net.solarnetwork.node.domain.DataAccessor> extends ModbusDeviceSupport
A base helper class to support ModbusNetwork based device implementations using ModbusData as a model object.
Since:
2.14
Version:
2.0
Author:
matt
  • Field Details

    • DEFAULT_SAMPLE_CACHE_MS

      public static final long DEFAULT_SAMPLE_CACHE_MS
      The default value for the sampleCacheMs property.
      See Also:
  • Constructor Details

    • ModbusDataDeviceSupport

      public ModbusDataDeviceSupport(T data)
      Constructor.
      Parameters:
      data - the data
  • Method Details

    • modbusDeviceNetworkSettings

      public static List<net.solarnetwork.settings.SettingSpecifier> modbusDeviceNetworkSettings(String prefix)
      Get setting specifiers for the unitId and modbusNetwork.propertyFilters['uid'] properties.
      Parameters:
      prefix - a setting prefix to prepend
      Returns:
      list of setting specifiers
      Since:
      1.2
    • getCurrentSample

      protected T getCurrentSample() throws IOException
      Get an up-to-date snapshot of the device data.

      If the sample data has expired, or never been read, this method will refresh it from the device by calling refreshDeviceData(ModbusConnection, ModbusData). If the data has never been read, it will also first call refreshDeviceInfo(ModbusConnection, ModbusData). A copy of the sample data is returned via createSampleSnapshot(ModbusData).

      Returns:
      the sample data copy
      Throws:
      IOException - if a communication error occurs
    • getCurrentSample

      protected T getCurrentSample(ModbusConnection connection) throws IOException
      Get an up-to-date snapshot of the device data.

      If the sample data has expired, or never been read, this method will refresh it from the device by calling refreshDeviceData(ModbusConnection, ModbusData). If the data has never been read, it will also first call refreshDeviceInfo(ModbusConnection, ModbusData). A copy of the sample data is returned via createSampleSnapshot(ModbusData).

      Parameters:
      connection - an optional existing connection to use; otherwise a new connection will be acquired
      Returns:
      the sample data copy
      Throws:
      IOException - if a communication error occurs
    • readDeviceInfoFirstTime

      protected void readDeviceInfoFirstTime(ModbusConnection connection, T sample) throws IOException
      Read device information when first attempting to communicate with the device.

      This method will be called only once by getCurrentSample(), before calling refreshDeviceData(ModbusConnection, ModbusData). This implementation simply calls refreshDeviceInfo(ModbusConnection, ModbusData).

      Parameters:
      connection - the Modbus connection
      sample - the sample to refresh
      Throws:
      IOException - if any communication error occurs
    • refreshDeviceInfo

      protected abstract void refreshDeviceInfo(ModbusConnection connection, T sample) throws IOException
      Refresh the device info data.

      This should refresh the Modbus registers that contain device information such as the serial number, name, etc.

      Parameters:
      connection - the Modbus connection
      sample - the sample to refresh
      Throws:
      IOException - if any communication error occurs
    • refreshDeviceData

      protected abstract void refreshDeviceData(ModbusConnection connection, T sample) throws IOException
      Refresh the device data.

      This should refresh the Modbus registers that contain the actual information being captured by this class and stored in datum instances. The getCurrentSample() method calls this when the sample data is expired.

      Parameters:
      connection - the Modbus connection
      sample - the sample to refresh
      Throws:
      IOException - if any communication error occurs
    • createSampleSnapshot

      protected T createSampleSnapshot(T sample)
      Create s snapshot copy of the sample data.

      This implementation calls ModbusData.copy() and casts the result to T.

      Parameters:
      sample - the sample to copy
      Returns:
      the copy of the sample data
    • isCachedSampleExpired

      protected boolean isCachedSampleExpired()
      Test if the sample data has expired.
      Returns:
      true if the sample data has expired
    • readDeviceInfo

      protected Map<String,Object> readDeviceInfo(ModbusConnection conn)
      Description copied from class: ModbusDeviceSupport
      Read general device info and return a map of the results. See the various INFO_KEY_* constants for information on the values returned in the result map.
      Specified by:
      readDeviceInfo in class ModbusDeviceSupport
      Parameters:
      conn - the connection to use
      Returns:
      a map with general device information populated
    • getSample

      public T getSample()
      Get the Modbus data instance.
      Returns:
      the data
    • getSampleCacheMs

      public long getSampleCacheMs()
      Get the sample cache maximum age, in milliseconds.
      Returns:
      the cache milliseconds
    • setSampleCacheMs

      public void setSampleCacheMs(long sampleCacheMs)
      Set the sample cache maximum age, in milliseconds.
      Parameters:
      sampleCacheMs - the cache milliseconds