Class ModbusDeviceSupport

java.lang.Object
net.solarnetwork.service.support.BasicIdentifiable
net.solarnetwork.node.service.support.BaseIdentifiable
net.solarnetwork.node.io.modbus.support.ModbusDeviceSupport
All Implemented Interfaces:
net.solarnetwork.service.Identifiable
Direct Known Subclasses:
ModbusDataDeviceSupport

public abstract class ModbusDeviceSupport extends net.solarnetwork.node.service.support.BaseIdentifiable
A base helper class to support ModbusNetwork based services.
Since:
2.0
Version:
3.0
Author:
matt
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The default value for the Identifiable.getUid() property filter value.
    static final int
    The default value for the unitId property.
    static final String
    Key for the device manufacture date, as a LocalDate.
    protected final org.slf4j.Logger
    A class-level logger.

    Fields inherited from interface net.solarnetwork.service.Identifiable

    GROUP_UID_PROPERTY, UID_PROPERTY
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Get the device info data as a Map.
    protected Map<String,Object>
    Get direct access to the device info data.
    Return an informational message composed of general device info.
    net.solarnetwork.service.OptionalService.OptionalFilterableService<ModbusNetwork>
    Get the Modbus network to use.
    int
    Get the Modbus unit ID.
    Get the configured Modbus device name.
    protected final ModbusNetwork
    Get the ModbusNetwork from the configured modbusNetwork service, or null if not available or not configured.
    static List<net.solarnetwork.settings.SettingSpecifier>
    Get setting specifiers for the unitId and modbusNetwork.propertyFilters['uid'] properties.
    protected final <T> T
    Perform some work with a Modbus ModbusConnection.
    protected abstract Map<String,Object>
    Read general device info and return a map of the results.
    protected void
    Set the device info data.
    void
    setModbusNetwork(net.solarnetwork.service.OptionalService.OptionalFilterableService<ModbusNetwork> modbusDevice)
    Set the Modbus network to use.
    void
    setUnitId(int unitId)
    Set the Modbus unit ID.

    Methods inherited from class net.solarnetwork.node.service.support.BaseIdentifiable

    baseIdentifiableSettings, baseIdentifiableSettings, getExpressionServices, getPlaceholderService, populateExpressionDatumProperties, resolvePlaceholders, resolvePlaceholders, setExpressionServices, setPlaceholderService

    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, smartMetadataValue

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface net.solarnetwork.service.Identifiable

    getDisplayName, getGroupUid, getUid
  • Field Details

    • DEFAULT_UNIT_ID

      public static final int DEFAULT_UNIT_ID
      The default value for the unitId property.
      Since:
      1.2
      See Also:
    • DEFAULT_NETWORK_UID

      public static final String DEFAULT_NETWORK_UID
      The default value for the Identifiable.getUid() property filter value.
      Since:
      1.2
      See Also:
    • INFO_KEY_DEVICE_MANUFACTURE_DATE

      public static final String INFO_KEY_DEVICE_MANUFACTURE_DATE
      Key for the device manufacture date, as a LocalDate.
      See Also:
    • log

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

    • ModbusDeviceSupport

      public ModbusDeviceSupport()
  • Method Details

    • modbusNetworkSettings

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

      protected final ModbusNetwork modbusNetwork()
      Get the ModbusNetwork from the configured modbusNetwork service, or null if not available or not configured.
      Returns:
      ModbusNetwork
    • readDeviceInfo

      protected abstract Map<String,Object> readDeviceInfo(ModbusConnection conn) throws IOException
      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.
      Parameters:
      conn - the connection to use
      Returns:
      a map with general device information populated
      Throws:
      IOException - if any IO error occurs
    • getDeviceInfoMessage

      public String getDeviceInfoMessage()
      Return an informational message composed of general device info. This method will call getDeviceInfo() and return a / (forward slash) delimited string of the resulting values, or null if that method returns null.
      Returns:
      info message
    • getDeviceInfo

      public Map<String,?> getDeviceInfo()
      Get the device info data as a Map. This method will call readDeviceInfo(ModbusConnection). The map is cached so subsequent calls will not attempt to read from the device. Note the returned map cannot be modified.
      Returns:
      the device info, or null
      See Also:
    • performAction

      protected final <T> T performAction(ModbusConnectionAction<T> action) throws IOException
      Perform some work with a Modbus ModbusConnection. This method attempts to obtain a ModbusNetwork from the configured modbusNetwork service, calling ModbusNetwork.performAction(int, ModbusConnectionAction) if one can be obtained.
      Type Parameters:
      T - the result type
      Parameters:
      action - the connection action
      Returns:
      the result of the callback, or null if the action is never invoked
      Throws:
      IOException - if any IO error occurs
    • getDeviceInfoMap

      protected Map<String,Object> getDeviceInfoMap()
      Get direct access to the device info data.
      Returns:
      the device info, or null
    • setDeviceInfoMap

      protected void setDeviceInfoMap(Map<String,Object> deviceInfo)
      Set the device info data. Setting the deviceInfo to null will force the next call to getDeviceInfo() to read from the device to populate this data, and setting this to anything else will force all subsequent calls to getDeviceInfo() to simply return that map.
      Parameters:
      deviceInfo - the device info map to set
    • modbusDeviceName

      public String modbusDeviceName()
      Get the configured Modbus device name.
      Returns:
      the modbus device name
      Since:
      1.1
    • getModbusNetwork

      public net.solarnetwork.service.OptionalService.OptionalFilterableService<ModbusNetwork> getModbusNetwork()
      Get the Modbus network to use.
      Returns:
      the network
    • setModbusNetwork

      public void setModbusNetwork(net.solarnetwork.service.OptionalService.OptionalFilterableService<ModbusNetwork> modbusDevice)
      Set the Modbus network to use.
      Parameters:
      modbusDevice - the network
    • getUnitId

      public int getUnitId()
      Get the Modbus unit ID.
      Returns:
      the unit ID; defaults to DEFAULT_UNIT_ID
    • setUnitId

      public void setUnitId(int unitId)
      Set the Modbus unit ID.

      The unit ID is the unique ID of the device on the Modbus network.

      Parameters:
      unitId - the ID to use