Class: OpenC3::Target

Inherits:
Object show all
Defined in:
lib/openc3/system/target.rb

Overview

Target encapsulates the information about a OpenC3 target. Targets are accessed through interfaces and have command and telemetry definition files which define their access.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_name, path, gem_path = nil) ⇒ Target

Creates a new target by processing the target.txt file in the directory given by the path joined with the target_name. Records all the command and telemetry definition files found in the targets cmd_tlm directory. System uses this list and processes them using PacketConfig.

Parameters:

  • target_name (String)

    The name of the target.

  • path (String)

    Path to the target directory

  • gem_path (String) (defaults to: nil)

    Path to the gem file or nil if there is no gem



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/openc3/system/target.rb', line 88

def initialize(target_name, path, gem_path = nil)
  @language = 'ruby'
  @requires = []
  @ignored_parameters = []
  @ignored_items = []
  @cmd_tlm_files = []
  # @auto_screen_substitute = false
  @interface = nil
  @routers = []
  @cmd_cnt = 0
  @tlm_cnt = 0
  @cmd_unique_id_mode = false
  @tlm_unique_id_mode = false
  @name = target_name.clone.upcase.freeze
  get_target_dir(path, gem_path)
  process_target_config_file()

  # If target.txt didn't specify specific cmd/tlm files then add everything
  if @cmd_tlm_files.empty?
    @cmd_tlm_files = add_all_cmd_tlm()
  else
    add_cmd_tlm_partials()
  end
end

Instance Attribute Details

#cmd_cntInteger

Returns The number of command packets send to this target.

Returns:

  • (Integer)

    The number of command packets send to this target



66
67
68
# File 'lib/openc3/system/target.rb', line 66

def cmd_cnt
  @cmd_cnt
end

#cmd_tlm_filesArray<String> (readonly)

Returns List of configuration files which define the commands and telemetry for this target.

Returns:

  • (Array<String>)

    List of configuration files which define the commands and telemetry for this target



54
55
56
# File 'lib/openc3/system/target.rb', line 54

def cmd_tlm_files
  @cmd_tlm_files
end

#cmd_unique_id_modeBoolean

Returns Indicates if all command packets identify using different fields.

Returns:

  • (Boolean)

    Indicates if all command packets identify using different fields



72
73
74
# File 'lib/openc3/system/target.rb', line 72

def cmd_unique_id_mode
  @cmd_unique_id_mode
end

#dirString (readonly)

Returns The directory which contains this target.

Returns:

  • (String)

    The directory which contains this target



60
61
62
# File 'lib/openc3/system/target.rb', line 60

def dir
  @dir
end

#filenameString (readonly)

Returns Target filename for this target.

Returns:

  • (String)

    Target filename for this target



57
58
59
# File 'lib/openc3/system/target.rb', line 57

def filename
  @filename
end

#idString

Returns Id of the target configuration.

Returns:

  • (String)

    Id of the target configuration



78
79
80
# File 'lib/openc3/system/target.rb', line 78

def id
  @id
end

#ignored_itemsArray<String> (readonly)

Returns List of items that should be ignored. Tools which access this target should not display or manipulate these items.

Returns:

  • (Array<String>)

    List of items that should be ignored. Tools which access this target should not display or manipulate these items.



50
51
52
# File 'lib/openc3/system/target.rb', line 50

def ignored_items
  @ignored_items
end

#ignored_parametersArray<String> (readonly)

Returns List of parameters that should be ignored. Tools which access this target should not display or manipulate these parameters.

Returns:

  • (Array<String>)

    List of parameters that should be ignored. Tools which access this target should not display or manipulate these parameters.



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

def ignored_parameters
  @ignored_parameters
end

#interfaceInterface

Returns The interface used to access the target.

Returns:

  • (Interface)

    The interface used to access the target



63
64
65
# File 'lib/openc3/system/target.rb', line 63

def interface
  @interface
end

#languageString (readonly)

Returns Programming language. Must be ‘ruby’ or ‘python’.

Returns:

  • (String)

    Programming language. Must be 'ruby' or 'python'.



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

def language
  @language
end

#nameString (readonly)

Returns Name of the target. This can be overridden when the system processes the target.

Returns:

  • (String)

    Name of the target. This can be overridden when the system processes the target.



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

def name
  @name
end

#requiresArray<String> (readonly)

Returns List of filenames that must be required by Ruby before parsing the command and telemetry definitions for this target.

Returns:

  • (Array<String>)

    List of filenames that must be required by Ruby before parsing the command and telemetry definitions for this target



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

def requires
  @requires
end

#tlm_cntInteger

Returns The number of telemetry packets received from this target.

Returns:

  • (Integer)

    The number of telemetry packets received from this target



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

def tlm_cnt
  @tlm_cnt
end

#tlm_unique_id_modeBoolean

Returns Indicates if telemetry packets identify using different fields.

Returns:

  • (Boolean)

    Indicates if telemetry packets identify using different fields



75
76
77
# File 'lib/openc3/system/target.rb', line 75

def tlm_unique_id_mode
  @tlm_unique_id_mode
end

Instance Method Details

#as_json(*_a) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/openc3/system/target.rb', line 199

def as_json(*_a)
  config = {}
  config['name'] = @name
  config['requires'] = @requires
  config['ignored_parameters'] = @ignored_parameters
  config['ignored_items'] = @ignored_items
  # config['auto_screen_substitute'] = true if @auto_screen_substitute
  config['cmd_tlm_files'] = @cmd_tlm_files
  # config['filename'] = @filename
  # config['interface'] = @interface.name if @interface
  # config['dir'] = @dir
  # config['cmd_cnt'] = @cmd_cnt
  # config['tlm_cnt'] = @tlm_cnt
  config['cmd_unique_id_mode'] = true if @cmd_unique_id_mode
  config['tlm_unique_id_mode'] = true if @tlm_unique_id_mode
  config['id'] = @id
  config
end

#process_file(filename) ⇒ Object

Parses the target configuration file

Parameters:

  • filename (String)

    The target configuration file to parse



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/openc3/system/target.rb', line 116

def process_file(filename)
  Logger.instance.info "Processing target definition in file '#{filename}'"
  parser = ConfigParser.new("https://docs.openc3.com/docs/configuration/target")
  parser.parse_file(filename) do |keyword, parameters|
    case keyword
    when 'LANGUAGE'
      usage = "#{keyword} <ruby | python>"
      parser.verify_num_parameters(1, 1, usage)
      @language = parameters[0].downcase

    when 'REQUIRE'
      usage = "#{keyword} <FILENAME>"
      parser.verify_num_parameters(1, 1, usage)
      filename = File.join(@dir, 'lib', parameters[0])
      begin
        # Require absolute path to file in target lib folder. Prevents name
        # conflicts at the require step
        OpenC3.disable_warnings do
          OpenC3.require_file(filename, false)
        end
      rescue LoadError
        begin
          # If we couldn't load at the target/lib level check everywhere
          OpenC3.disable_warnings do
            filename = parameters[0]
            OpenC3.require_file(parameters[0])
          end
        rescue Exception => e
          raise parser.error(e.formatted)
        end
      rescue Exception => e
        raise parser.error(e.formatted)
      end

      # This code resolves any relative paths to absolute before putting into the @requires array
      unless Pathname.new(filename).absolute?
        $:.each do |search_path|
          test_filename = File.join(search_path, filename).gsub("\\", "/")
          if File.exist?(test_filename)
            filename = test_filename
            break
          end
        end
      end

      @requires << filename

    when 'IGNORE_PARAMETER', 'IGNORE_ITEM'
      usage = "#{keyword} <#{keyword.split('_')[1]} NAME>"
      parser.verify_num_parameters(1, 1, usage)
      @ignored_parameters << parameters[0].upcase if keyword.include?("PARAMETER")
      @ignored_items << parameters[0].upcase if keyword.include?("ITEM")

    when 'COMMANDS', 'TELEMETRY'
      usage = "#{keyword} <FILENAME>"
      parser.verify_num_parameters(1, 1, usage)
      filename = File.join(@dir, 'cmd_tlm', parameters[0])
      raise parser.error("#{filename} not found") unless File.exist?(filename)

      @cmd_tlm_files << filename

      # when 'AUTO_SCREEN_SUBSTITUTE'
      # usage = "#{keyword}"
      # parser.verify_num_parameters(0, 0, usage)
      # @auto_screen_substitute = true

    when 'CMD_UNIQUE_ID_MODE'
      usage = "#{keyword}"
      parser.verify_num_parameters(0, 0, usage)
      @cmd_unique_id_mode = true

    when 'TLM_UNIQUE_ID_MODE'
      usage = "#{keyword}"
      parser.verify_num_parameters(0, 0, usage)
      @tlm_unique_id_mode = true

    else
      # blank lines will have a nil keyword and should not raise an exception
      raise parser.error("Unknown keyword '#{keyword}'") if keyword
    end # case keyword
  end
end