Class: OpenC3::System

Inherits:
Object show all
Defined in:
lib/openc3/system/system.rb,
ext/openc3/ext/telemetry/telemetry.c

Constant Summary collapse

@@instance =

Variable that holds the singleton instance

nil
@@instance_mutex =

Mutex used to ensure that only one instance of System is created

Mutex.new
@@limits_set =

The current limits set

nil
@@post_instance_callbacks =

Callbacks to call once @@instance is created

[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_names, target_config_dir) ⇒ System

Create a new System object.

Parameters:

  • target_names (Array of target names)
  • target_config_dir

    Directory where target config folders are



174
175
176
177
178
179
180
181
182
# File 'lib/openc3/system/system.rb', line 174

def initialize(target_names, target_config_dir)
  OpenC3.add_to_search_path(target_config_dir, true) if target_config_dir
  @targets = {}
  @packet_config = PacketConfig.new
  @commands = Commands.new(@packet_config)
  @telemetry = Telemetry.new(@packet_config)
  @limits = Limits.new(@packet_config)
  target_names.each { |target_name| add_target(target_name, target_config_dir) }
end

Class Method Details

.add_post_instance_callback(callback) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/openc3/system/system.rb', line 72

def self.add_post_instance_callback(callback)
  if @@instance
    callback.call()
  else
    @@post_instance_callbacks << callback
  end
end

.dynamic_update(dynamic_packets, cmd_or_tlm = :TELEMETRY, affect_ids: false) ⇒ Object

Dynamically add packets to the system instance

Parameters:

  • dynamic_packets (Array of packets)
  • cmd_or_tlm (Symbol) (defaults to: :TELEMETRY)

    :COMMAND or :TELEMETRY

  • affect_ids (Boolean) (defaults to: false)

    Whether to affect packet id lookup or not



160
161
162
163
164
165
166
167
168
# File 'lib/openc3/system/system.rb', line 160

def self.dynamic_update(dynamic_packets, cmd_or_tlm = :TELEMETRY, affect_ids: false)
  dynamic_packets.each do |packet|
    if cmd_or_tlm == :TELEMETRY
      @@instance.telemetry.dynamic_add_packet(packet, affect_ids: affect_ids)
    else
      @@instance.commands.dynamic_add_packet(packet, affect_ids: affect_ids)
    end
  end
end

.instance(target_names = nil, target_config_dir = nil) ⇒ System

Get the singleton instance of System

Parameters:

  • target_names (Array of target_names) (defaults to: nil)
  • target_config_dir (defaults to: nil)

    Directory where target config folders are

Returns:

  • (System)

    The System singleton



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/openc3/system/system.rb', line 141

def self.instance(target_names = nil, target_config_dir = nil)
  return @@instance if @@instance
  raise "System.instance parameters are required on first call" unless target_names and target_config_dir

  @@instance_mutex.synchronize do
    return @@instance if @@instance
    @@instance ||= self.new(target_names, target_config_dir)
    @@post_instance_callbacks.each do |callback|
      callback.call
    end
    return @@instance
  end
end

.limits_setSymbol

Returns The current limits_set of the system returned from Redis.

Returns:

  • (Symbol)

    The current limits_set of the system returned from Redis



61
62
63
64
65
66
# File 'lib/openc3/system/system.rb', line 61

def self.limits_set
  unless @@limits_set
    @@limits_set = LimitsEventTopic.current_set(scope: $openc3_scope).to_s.intern
  end
  @@limits_set
end

.limits_set=(value) ⇒ Object



68
69
70
# File 'lib/openc3/system/system.rb', line 68

def self.limits_set=(value)
  @@limits_set = value.to_s.intern
end

.reset_instance!Object

Clears the System singleton so the next call to setup_targets or instance rebuilds it. Intended for admin flows (e.g. reingest) that need to load a specific target_version distinct from whatever is currently loaded. Callers must hold an external lock if they need to protect other threads from observing a nil @@instance briefly.



130
131
132
133
134
# File 'lib/openc3/system/system.rb', line 130

def self.reset_instance!
  @@instance_mutex.synchronize do
    @@instance = nil
  end
end

.setup_targets(target_names, base_dir, target_version: 'current', scope:) ⇒ Object

target_version can also be the actual hash used in the target_archives folder



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/openc3/system/system.rb', line 81

def self.setup_targets(target_names, base_dir, target_version: 'current', scope:)
  # Nothing to do if there are no targets
  return if target_names.nil? or target_names.length == 0
  if @@instance.nil?
    targets_path = "#{base_dir}/_targets"
    FileUtils.mkdir_p(targets_path)
    bucket = Bucket.getClient()
    target_names.each do |target_name|
      # Remove any prior extraction so re-running setup_targets (e.g. after
      # reset_instance! during reingest) starts from a clean slate. Without
      # this, Zip::File#extract fails on files left behind by a previous run.
      FileUtils.rm_rf("#{targets_path}/#{target_name}")

      # Retrieve bucket/targets/target_name/<TARGET>_current.zip
      zip_path = "#{targets_path}/#{target_name}_#{target_version}.zip"
      FileUtils.mkdir_p(File.dirname(zip_path))
      bucket_key = "#{scope}/target_archives/#{target_name}/#{target_name}_#{target_version}.zip"
      Logger.info("Retrieving #{bucket_key} from targets bucket")
      bucket.get_object(bucket: ENV['OPENC3_CONFIG_BUCKET'], key: bucket_key, path: zip_path)
      Zip::File.open(zip_path) do |zip_file|
        zip_file.each do |entry|
          zip_file.extract(entry.name, destination_directory: targets_path)
        end
      end
      FileUtils.rm(zip_path) if File.exist?(zip_path)

      # Now add any modifications in targets_modified/TARGET/cmd_tlm
      # This adds support for remembering dynamically created packets
      # target.txt must be configured to either use all files in cmd_tlm folder (default)
      # or have a predetermined empty file like dynamic_tlm.txt
      bucket_path = "#{scope}/targets_modified/#{target_name}/cmd_tlm"
      _, files = bucket.list_files(bucket: ENV['OPENC3_CONFIG_BUCKET'], path: bucket_path)
      files.each do |file|
        bucket_key = File.join(bucket_path, file['name'])
        local_path = "#{targets_path}/#{target_name}/cmd_tlm/#{file['name']}"
        bucket.get_object(bucket: ENV['OPENC3_CONFIG_BUCKET'], key: bucket_key, path: local_path)
      end
    end

    # Build System from targets
    System.instance(target_names, targets_path)
  end
end

Instance Method Details

#add_target(target_name, target_config_dir) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/openc3/system/system.rb', line 184

def add_target(target_name, target_config_dir)
  parser = ConfigParser.new
  folder_name = File.join(target_config_dir, target_name)
  raise parser.error("Target folder must exist '#{folder_name}'.") unless Dir.exist?(folder_name)

  target = Target.new(target_name, target_config_dir)
  @targets[target.name] = target
  errors = [] # Store all errors processing the cmd_tlm files
  target.cmd_tlm_files.each do |cmd_tlm_file|
    @packet_config.process_file(cmd_tlm_file, target.name, target.language)
  rescue Exception => e
    errors << "Error processing #{cmd_tlm_file}:\n#{e.message}"
  end
  unless errors.empty?
    raise parser.error(errors.join("\n"))
  end
end

#commandsCommands

Returns Access to the command definition.

Returns:

  • (Commands)

    Access to the command definition



40
# File 'lib/openc3/system/system.rb', line 40

instance_attr_reader :commands

#limitsLimits

Returns Access to the limits definition.

Returns:

  • (Limits)

    Access to the limits definition



46
# File 'lib/openc3/system/system.rb', line 46

instance_attr_reader :limits

#packet_configPacketConfig

Returns Access to the packet configuration.

Returns:



37
# File 'lib/openc3/system/system.rb', line 37

instance_attr_reader :packet_config

#targetsHash<String,Target>

Returns Hash of all the known targets.

Returns:



34
# File 'lib/openc3/system/system.rb', line 34

instance_attr_reader :targets

#telemetryTelemetry

Returns Access to the telemetry definition.

Returns:

  • (Telemetry)

    Access to the telemetry definition



43
# File 'lib/openc3/system/system.rb', line 43

instance_attr_reader :telemetry