=begin
 ******************************************************************************
 * Copyright 1990-2011 MBARI
 * MBARI Proprietary Information. All rights reserved.
 ******************************************************************************
 * Summary  : Implemetation of the SEDEvent behavior
 * Filename : sedevent.rb
 * Author   : Henthorn
 * Project  : Sedimentaion Event Sensor
 * Version  : 1
 * Created  : 05/23/2024
 * Modified :
 ******************************************************************************
=end

# Refer to code home directory as base
#
require "#{ENV['ROVER_HOME']}/utils/rover_environment"
#

require "utils/misc"
require "utils/datalog"
require "control/behavior"
require "control/supervisor"
require "device/ezstepper"

##########
# SEDEvent class.
# Produce a sedimentation event
#
#
class SEDEvent < Behavior

  #####
  # Accept a behavior element and extract parameters
  #
  def initialize(p_behavior_element)
    super

    @value = 0

    load_attrs(p_behavior_element)
  end
  #####


  #####
  # Iterate over attributes in the element, extract parameters.
  #
  def load_attrs(p_behavior_element)
    super

    # Keep for later use
    #
    attrs = p_behavior_element.attributes
    attrs.keys.each do |k|
      case k

      when "value"
        @value = attrs[k].to_i

      else
        puts("Unrecognized attr: #{k}")

      end
    end

    # Option: Throw an exception if there any required attributes
    # missing
    #
  end
  #####


  #####
  # Initialize behavior and make ready for execution.
  # Should be called just before executing the behavior.
  #
  def init(p_seq_number=1)   # Where in the plan behavior sequence I am
    syslog("_ #{@name} init")

    # Perform initialization procedures
    @state = READY
  end
  #####


  #####
  def save_behavior_state()
  end
  #####

  #####
  # Resume from hibernation
  #
  def resume_init()
  end
  #####


  #####
  # Execute the behavior
  #
  def execute
    # Execute if ready.
    unless (@state == READY)
      syslog("! #{name} not in ready state")
      return
    end

    syslog("_ #{@name} executing")
    @state = EXECUTING

    #
    # Perform the behavior
    #
    begin
      su = Supervisor.instance.deployment.current_plan.sed.samples_used
      ns = Supervisor.instance.deployment.current_plan.sed.num_samples

      # Do not use the last available sample
      # When (num_samples - samples_used) is not one, then go ahead
      if ((ns - su) != 1)
        Supervisor.instance.deployment.current_plan.sed_event()
        sleep(20)  # Let the deployment handle the event
      else
        syslog("_ #{name} leaving last sample for an real event")
      end
      @state = FINISHED

    ensure
      self.clean_up()
    end
  end
  #####


  #####
  def clean_up
  end
  #####


  #####
  # Abort the behavior
  #
  def abort
  end
  #####


  #####
  # Test method. Use to run standard test of MyClass from
  # a ruby script.
  #
  def SEDEvent.test
    require 'rexml/document'
    # Test code here
    puts("Done")
  end
  #####

end

#####
# Standalone unit test (recommended)
# Include at the end of the file as a hook to run a unit test of
# the code from the command line including the command line options
# (e.g., "$ ruby my_class.rb --sim").
#
if __FILE__ == $0
  SEDEvent.test
end
