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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/openc3/system/target.rb', line 77

def initialize(target_name, path, gem_path = nil)
  @language = 'ruby'
  @requires = []
  @ignored_parameters = []
  @ignored_items = []
  @cmd_tlm_files = []
  @interface = nil
  @routers = []
  @cmd_cnt = 0
  @tlm_cnt = 0
  @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



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

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



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

def cmd_tlm_files
  @cmd_tlm_files
end

#dirString (readonly)

Returns The directory which contains this target.

Returns:

  • (String)

    The directory which contains this target



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

def dir
  @dir
end

#filenameString (readonly)

Returns Target filename for this target.

Returns:

  • (String)

    Target filename for this target



52
53
54
# File 'lib/openc3/system/target.rb', line 52

def filename
  @filename
end

#idString

Returns Id of the target configuration.

Returns:

  • (String)

    Id of the target configuration



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

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.



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

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.



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

def ignored_parameters
  @ignored_parameters
end

#interfaceInterface

Returns The interface used to access the target.

Returns:

  • (Interface)

    The interface used to access the target



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

def interface
  @interface
end

#languageString (readonly)

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

Returns:

  • (String)

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



31
32
33
# File 'lib/openc3/system/target.rb', line 31

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.



28
29
30
# File 'lib/openc3/system/target.rb', line 28

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



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

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



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

def tlm_cnt
  @tlm_cnt
end

Instance Method Details

#as_json(*_a) ⇒ Object



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

def as_json(*_a)
  config = {}
  config['name'] = @name
  config['requires'] = @requires
  config['ignored_parameters'] = @ignored_parameters
  config['ignored_items'] = @ignored_items
  config['cmd_tlm_files'] = @cmd_tlm_files
  config['id'] = @id
  config
end

#process_file(filename) ⇒ Object

Parses the target configuration file

Parameters:

  • filename (String)

    The target configuration file to parse



102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
# File 'lib/openc3/system/target.rb', line 102

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 'CMD_UNIQUE_ID_MODE', 'TLM_UNIQUE_ID_MODE'
      # Deprecated - Now autodetected

    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