Class: Cosmos::LimitsGroupsBackgroundTask

Inherits:
BackgroundTask show all
Defined in:
lib/cosmos/tools/cmd_tlm_server/limits_groups_background_task.rb

Overview

Monitors telemetry and enables and disables limits groups

Constant Summary collapse

FUTURE_TIME =

Time in the future that we will never hit to allow the logic to work

Time.new("3000")
PAST_TIME =
Time.new("1900")

Constants included from ApiShared

ApiShared::DEFAULT_TLM_POLLING_RATE

Constants included from Extract

Extract::SCANNING_REGULAR_EXPRESSION

Instance Attribute Summary collapse

Attributes inherited from BackgroundTask

#name, #status, #stopped, #thread

Instance Method Summary collapse

Methods included from Api

#cmd, #cmd_no_checks, #cmd_no_hazardous_check, #cmd_no_range_check, #cmd_raw, #cmd_raw_no_checks, #cmd_raw_no_hazardous_check, #cmd_raw_no_range_check, #cmd_tlm_clear_counters, #cmd_tlm_reload, #connect_interface, #connect_router, #disable_limits, #disable_limits_group, #disconnect_interface, #disconnect_router, #enable_limits, #enable_limits_group, #get_all_cmd_info, #get_all_interface_info, #get_all_packet_logger_info, #get_all_router_info, #get_all_target_info, #get_all_tlm_info, #get_background_tasks, #get_cmd_buffer, #get_cmd_cnt, #get_cmd_hazardous, #get_cmd_list, #get_cmd_log_filename, #get_cmd_param_list, #get_cmd_time, #get_cmd_value, #get_interface_info, #get_interface_names, #get_interface_targets, #get_limits, #get_limits_event, #get_limits_groups, #get_limits_set, #get_limits_sets, #get_out_of_limits, #get_output_logs_filenames, #get_overall_limits_state, #get_packet, #get_packet_data, #get_packet_logger_info, #get_packet_loggers, #get_router_info, #get_router_names, #get_saved_config, #get_screen_definition, #get_screen_list, #get_server_message, #get_server_message_log_filename, #get_server_status, #get_stale, #get_target_ignored_items, #get_target_ignored_parameters, #get_target_info, #get_target_list, #get_tlm_buffer, #get_tlm_cnt, #get_tlm_details, #get_tlm_item_list, #get_tlm_list, #get_tlm_log_filename, #get_tlm_packet, #get_tlm_values, #inject_tlm, #interface_state, #limits_enabled?, #map_target_to_interface, #normalize_tlm, #override_tlm, #override_tlm_raw, #replay_move_end, #replay_move_index, #replay_move_start, #replay_play, #replay_reverse_play, #replay_select_file, #replay_set_playback_delay, #replay_status, #replay_step_back, #replay_step_forward, #replay_stop, #router_state, #send_raw, #set_limits, #set_limits_set, #set_tlm, #set_tlm_raw, #start_background_task, #start_cmd_log, #start_logging, #start_new_server_message_log, #start_raw_logging_interface, #start_raw_logging_router, #start_tlm_log, #stop_background_task, #stop_cmd_log, #stop_logging, #stop_raw_logging_interface, #stop_raw_logging_router, #stop_tlm_log, #subscribe_limits_events, #subscribe_packet_data, #subscribe_server_messages, #tlm, #tlm_formatted, #tlm_raw, #tlm_variable, #tlm_with_units, #unsubscribe_limits_events, #unsubscribe_packet_data, #unsubscribe_server_messages

Constructor Details

#initialize(initial_delay = 0, task_delay = 0.5) ⇒ LimitsGroupsBackgroundTask



21
22
23
24
25
26
27
28
29
30
# File 'lib/cosmos/tools/cmd_tlm_server/limits_groups_background_task.rb', line 21

def initialize(initial_delay = 0, task_delay = 0.5)
  super()
  @initial_delay = Float(initial_delay)
  @task_delay = Float(task_delay)
  @name = "Limits Groups"
  @groups = get_limits_groups()
  @sleeper = Sleeper.new
  # Initialize all the group names as instance variables
  @groups.each {|group| self.instance_variable_set("@#{group.downcase}", nil) }
end

Instance Attribute Details

#groupsObject (readonly)

Returns the value of attribute groups.



16
17
18
# File 'lib/cosmos/tools/cmd_tlm_server/limits_groups_background_task.rb', line 16

def groups
  @groups
end

Instance Method Details

#callObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cosmos/tools/cmd_tlm_server/limits_groups_background_task.rb', line 82

def call
  @status = "Starting the LimitsGroupsBackgroundTask"
  @sleeper = Sleeper.new
  check_methods = find_check_methods()
  return if @sleeper.sleep(@initial_delay)
  loop do
    start = Time.now
    check_methods.each {|method| self.send(method.intern) }
    now = Time.now
    @status = "#{now.formatted}: Checking groups took #{now - start}s"
    sleep_time = @task_delay - (now - start)
    sleep_time = 0 if sleep_time < 0
    return if @sleeper.sleep(sleep_time)
  end
end

#process_group(delay = 0, enable_code = nil, disable_code = nil) ⇒ Object

Enables and disables COSMOS limits groups. The group named after the passed group variable is automatically enabled and disabled when the yielded to block returns true and false respectively. In addition, any groups with the same name and a _PRI or _RED extension are also enabled or disabled with the base group.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cosmos/tools/cmd_tlm_server/limits_groups_background_task.rb', line 43

def process_group(delay = 0, enable_code = nil, disable_code = nil)
  group = calling_method.to_s[6..-1]

  # Grab the instance variable based on the group name
  var_name = "@#{group}"
  var = self.instance_variable_get(var_name)
  # Yield to the block to perform the telemetry check
  if yield
    # If the instance variable is not set or set in the past
    # (by the disable logic) it means the group isn't enabled
    # so store the current time to allow for an enable delay
    if !var || var == PAST_TIME
      self.instance_variable_set(var_name, Time.now)
      # After setting the variable we need to get it again
      var = self.instance_variable_get(var_name)
    end
    # After the requested delay after power on we enable the group
    if Time.now > (var + delay)
      # Reset the instance variable to a distance future time
      # so it won't satisfy any of the other checks and enable the group
      self.instance_variable_set(var_name, FUTURE_TIME)
      enable_limits_group(group)
      # Call any additional enable code passed to the method
      enable_code.call if enable_code
    end
  else
    # If the instance variable is not set or set in the future
    # (by the enable logic) it means the group isn't disabled
    # Reset the instance variable to a distance past time so it
    # won't satisfy any of the other checks and disable the group
    if !var || var == FUTURE_TIME
      self.instance_variable_set(var_name, PAST_TIME)
      disable_limits_group(group)
      # Call any additional disable code passed to the method
      disable_code.call if disable_code
    end
  end
end

#stopObject



98
99
100
101
# File 'lib/cosmos/tools/cmd_tlm_server/limits_groups_background_task.rb', line 98

def stop
  @sleeper.cancel
  @status = "Stopped at #{Time.now.sys.formatted}"
end