Class DefaultOperationalModesService

java.lang.Object
net.solarnetwork.service.support.BasicIdentifiable
net.solarnetwork.node.service.support.BaseIdentifiable
net.solarnetwork.node.runtime.DefaultOperationalModesService
All Implemented Interfaces:
InstructionHandler, OperationalModesService, net.solarnetwork.service.Identifiable, net.solarnetwork.service.ServiceLifecycleObserver, net.solarnetwork.settings.SettingSpecifierProvider

public class DefaultOperationalModesService extends BaseIdentifiable implements OperationalModesService, InstructionHandler, net.solarnetwork.settings.SettingSpecifierProvider, net.solarnetwork.service.ServiceLifecycleObserver
Default implementation of OperationalModesService.
Version:
2.1
Author:
matt
  • Field Details

    • SETTING_OP_MODE

      public static final String SETTING_OP_MODE
      The setting key for operational modes.
      See Also:
    • SETTING_OP_MODE_EXPIRE

      public static final String SETTING_OP_MODE_EXPIRE
      The setting key for operational mode expiration dates.
      Since:
      1.1
      See Also:
    • DEFAULT_STARTUP_DELAY

      public static final long DEFAULT_STARTUP_DELAY
      The default startup delay value.
    • DEFAULT_AUTO_EXPIRE_MODES_FREQUENCY

      public static final int DEFAULT_AUTO_EXPIRE_MODES_FREQUENCY
      The default rate at which to look for auto-expired modes, in seconds.
      See Also:
    • NO_EXPIRATION

      public static final Long NO_EXPIRATION
      An expiration value meaning "no expiration".
      Since:
      1.3
  • Constructor Details

    • DefaultOperationalModesService

      public DefaultOperationalModesService(net.solarnetwork.service.OptionalService<SettingDao> settingDao, net.solarnetwork.service.OptionalService<org.osgi.service.event.EventAdmin> eventAdmin)
      Constructor.
      Parameters:
      settingDao - the setting DAO to persist operational mode changes with
      eventAdmin - the event service to post notifications with
      Throws:
      IllegalArgumentException - if settingDao is null
    • DefaultOperationalModesService

      public DefaultOperationalModesService(ConcurrentMap<String,Long> modeCache, net.solarnetwork.service.OptionalService<SettingDao> settingDao, net.solarnetwork.service.OptionalService<org.osgi.service.event.EventAdmin> eventAdmin)
      Constructor.
      Parameters:
      modeCache - the cache to use for active mode tracking
      settingDao - the setting DAO to persist operational mode changes with
      eventAdmin - the event service to post notifications with
      Throws:
      IllegalArgumentException - if settingDao or modeCache is null
      Since:
      1.3
  • Method Details

    • serviceDidStartup

      public void serviceDidStartup()
      Specified by:
      serviceDidStartup in interface net.solarnetwork.service.ServiceLifecycleObserver
    • expireNow

      public void expireNow()
      Manually run the auto-expire task.

      This is primarily designed to support testing.

    • serviceDidShutdown

      public void serviceDidShutdown()
      Specified by:
      serviceDidShutdown in interface net.solarnetwork.service.ServiceLifecycleObserver
    • getSettingUid

      public String getSettingUid()
      Specified by:
      getSettingUid in interface net.solarnetwork.settings.SettingSpecifierProvider
    • getDisplayName

      public String getDisplayName()
      Specified by:
      getDisplayName in interface net.solarnetwork.service.Identifiable
      Specified by:
      getDisplayName in interface net.solarnetwork.settings.SettingSpecifierProvider
      Overrides:
      getDisplayName in class net.solarnetwork.service.support.BasicIdentifiable
    • getSettingSpecifiers

      public List<net.solarnetwork.settings.SettingSpecifier> getSettingSpecifiers()
      Specified by:
      getSettingSpecifiers in interface net.solarnetwork.settings.SettingSpecifierProvider
    • handlesTopic

      public boolean handlesTopic(String topic)
      Description copied from interface: InstructionHandler
      Test if a topic is handled by this handler.
      Specified by:
      handlesTopic in interface InstructionHandler
      Parameters:
      topic - the topic
      Returns:
      true only if this handler can execute the job for the given topic
    • processInstruction

      public InstructionStatus processInstruction(Instruction instruction)
      Description copied from interface: InstructionHandler
      Process an instruction.
      Specified by:
      processInstruction in interface InstructionHandler
      Parameters:
      instruction - the instruction to process
      Returns:
      the status for the instruction, or null if the instruction was not handled
    • isOperationalModeActive

      public boolean isOperationalModeActive(String mode)
      Description copied from interface: OperationalModesService
      Test if a specific mode is active.

      Note that a null or empty mode argument will be treated as testing if the default mode is active, which is always true.

      Specified by:
      isOperationalModeActive in interface OperationalModesService
      Parameters:
      mode - the mode to test; the mode can be prefixed with ! to test if the given mode is not active
      Returns:
      true if mode is active, or if mode starts with ! then true if mode is not active
    • activeOperationalModes

      public Set<String> activeOperationalModes()
      Description copied from interface: OperationalModesService
      Get the set of active operational modes.
      Specified by:
      activeOperationalModes in interface OperationalModesService
      Returns:
      the active operational modes, never null
    • enableOperationalModes

      public Set<String> enableOperationalModes(Set<String> modes)
      Description copied from interface: OperationalModesService
      Enable a set of operational modes.
      Specified by:
      enableOperationalModes in interface OperationalModesService
      Parameters:
      modes - the modes to enable
      Returns:
      the active operational modes, after activating modes, never null
    • enableOperationalModes

      public Set<String> enableOperationalModes(Set<String> modes, Instant expire)
      Description copied from interface: OperationalModesService
      Enable a set of operational modes.
      Specified by:
      enableOperationalModes in interface OperationalModesService
      Parameters:
      modes - the modes to enable
      expire - a date after which modes should be automatically disabled, or null for no expiration
      Returns:
      the active operational modes, after activating modes, never null
    • disableOperationalModes

      public Set<String> disableOperationalModes(Set<String> modes)
      Description copied from interface: OperationalModesService
      Disable a set of operational modes.
      Specified by:
      disableOperationalModes in interface OperationalModesService
      Parameters:
      modes - the modes to disable
      Returns:
      the active operational modes, after deactivating modes, never null
    • createOperationalModesChangedEvent

      protected org.osgi.service.event.Event createOperationalModesChangedEvent(Set<String> activeModes)
      Create an operational modes changed event.
      Parameters:
      activeModes - the active modes
      Returns:
      the event
    • postEvent

      protected final void postEvent(org.osgi.service.event.Event event)
      Post an event.
      Parameters:
      event - the event to post
    • setStartupDelay

      public void setStartupDelay(long startupDelay)
      A startup delay before posting an event of the active operational modes.

      Note this requires a setTaskScheduler(TaskScheduler) to be configured if set to anything > 0.

      Parameters:
      startupDelay - a startup delay, in milliseconds, or 0 for no delay; defaults to DEFAULT_STARTUP_DELAY
    • setTaskScheduler

      public void setTaskScheduler(org.springframework.scheduling.TaskScheduler taskScheduler)
      Configure a task scheduler.

      This is required by setStartupDelay(long) as well as for supporting auto-expiring modes.

      Parameters:
      taskScheduler - a task executor
      See Also:
    • setAutoExpireModesFrequency

      public void setAutoExpireModesFrequency(int autoExpireModesFrequency)
      Set the frequency, in seconds, at which to look for auto-expired operational modes.
      Parameters:
      autoExpireModesFrequency - the frequency, in seconds
      Throws:
      IllegalArgumentException - if autoExpireModesFrequency is less than 1
      Since:
      1.1
    • getTransactionManager

      public net.solarnetwork.service.OptionalService<org.springframework.transaction.PlatformTransactionManager> getTransactionManager()
      Get the transaction manager.
      Returns:
      the transaction manager to use
      Since:
      1.3
    • setTransactionManager

      public void setTransactionManager(net.solarnetwork.service.OptionalService<org.springframework.transaction.PlatformTransactionManager> transactionManager)
      Set the transaction manager.
      Parameters:
      transactionManager - the transaction manager to use
      Since:
      1.3