Class: OpenC3::TableConfig

Inherits:
PacketConfig show all
Defined in:
lib/openc3/tools/table_manager/table_config.rb

Overview

Processes the Table Manager configuration files which define tables. Since this class inherits from PacketConfig it only needs to implement Table Manager specific keywords. All tables are accessed through the table and tables methods.

Constant Summary

Constants inherited from PacketConfig

PacketConfig::COMMAND, PacketConfig::TELEMETRY

Instance Attribute Summary collapse

Attributes inherited from PacketConfig

#cmd_id_value_hash, #commands, #latest_data, #limits_groups, #limits_sets, #name, #telemetry, #tlm_id_value_hash, #warnings

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PacketConfig

#to_config, #to_xtce

Constructor Details

#initializeTableConfig

Create the table configuration



42
43
44
45
46
47
48
# File 'lib/openc3/tools/table_manager/table_config.rb', line 42

def initialize
  super
  # Override commands with the Table::TARGET name to store tables
  @commands[Table::TARGET] = {}
  @definitions = {}
  @last_config = [] # Stores array of [filename, contents]
end

Instance Attribute Details

#filenameString (readonly)

Returns Table configuration filename.

Returns:

  • (String)

    Table configuration filename



33
34
35
# File 'lib/openc3/tools/table_manager/table_config.rb', line 33

def filename
  @filename
end

Class Method Details

.process_file(filename) ⇒ Object



35
36
37
38
39
# File 'lib/openc3/tools/table_manager/table_config.rb', line 35

def self.process_file(filename)
  instance = self.new()
  instance.process_file(filename)
  instance
end

Instance Method Details

#definition(table_name) ⇒ String

Returns Table definition for the specific table.

Returns:

  • (String)

    Table definition for the specific table



56
57
58
# File 'lib/openc3/tools/table_manager/table_config.rb', line 56

def definition(table_name)
  @definitions[table_name.upcase]
end

#finish_packetObject

If the table is ROW_COLUMN all currently defined items are duplicated until the specified number of rows are created.



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/openc3/tools/table_manager/table_config.rb', line 231

def finish_packet
  if @current_packet
    warnings = @current_packet.check_bit_offsets
    if warnings.length > 0
      raise "Overlapping items not allowed in tables.\n#{warnings}"
    end
    if @current_packet.type == :ROW_COLUMN
      items = @current_packet.sorted_items.clone
      (@current_packet.num_rows - 1).times do |row|
        items.each do |item|
          new_item = item.clone
          new_item.name = "#{new_item.name[0...-1]}#{row + 1}"
          @current_packet.append(new_item)
        end
      end
    end
    unless @defaults.empty?
      @current_packet.sorted_items.each_with_index do |item, index|
        case item.data_type
        when :INT, :UINT
          begin
            # Integer handles hex strings, e.g. 0xDEADBEEF
            item.default = Integer(@defaults[index])
          rescue ArgumentError
            value = item.states[@defaults[index]]
            if value
              item.default = value
            else
              raise "Unknown DEFAULT #{@defaults[index]} for item #{item.name}. Valid states are #{item.states.keys.join(', ')}."
            end
          end
        when :FLOAT
          item.default = @defaults[index].to_f
        when :STRING, :BLOCK
          item.default = @defaults[index]
        end
      end
    end
  end
  super()
end

#process_current_item(parser, keyword, params) ⇒ Object

Overridden method to handle the unique table item parameters: UNEDITABLE and HIDDEN. (see PacketConfig#process_current_item)



209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/openc3/tools/table_manager/table_config.rb', line 209

def process_current_item(parser, keyword, params)
  super(parser, keyword, params)
  case keyword
  when 'UNEDITABLE'
    usage = "#{keyword}"
    parser.verify_num_parameters(0, 0, usage)
    @current_item.editable = false
  when 'HIDDEN'
    usage = "#{keyword}"
    parser.verify_num_parameters(0, 0, usage)
    @current_item.hidden = true
  end
end

#process_current_packet(parser, keyword, params) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/openc3/tools/table_manager/table_config.rb', line 193

def process_current_packet(parser, keyword, params)
  super(parser, keyword, params)
rescue => err
  if err.message.include?('not found')
    raise parser.error(
            "#{params[0]} not found in table #{@current_packet.table_name}",
            'SELECT_PARAMETER <PARAMETER NAME>',
          )
  else
    raise err
  end
end

#process_file(filename) ⇒ Object

Processes a OpenC3 table configuration file and uses the keywords to build up knowledge of the tables.

Parameters:

  • filename (String)

    The name of the configuration file



75
76
77
78
79
80
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
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
# File 'lib/openc3/tools/table_manager/table_config.rb', line 75

def process_file(filename)
  # Partial files are included into another file and thus aren't directly processed
  return if File.basename(filename)[0] == '_' # Partials start with underscore
  @filename = filename
  @last_config = [File.basename(filename), File.read(filename)]
  @converted_type = nil
  @converted_bit_size = nil
  @proc_text = ''
  @building_generic_conversion = false
  @defaults = []

  parser =
    ConfigParser.new(
      'https://openc3.com/docs/tools/#table-manager-configuration-openc3--39',
    )
  parser.parse_file(filename) do |keyword, params|
    if @building_generic_conversion
      case keyword
      # Complete a generic conversion
      when 'GENERIC_READ_CONVERSION_END', 'GENERIC_WRITE_CONVERSION_END'
        parser.verify_num_parameters(0, 0, keyword)
        @current_item.read_conversion =
          GenericConversion.new(
            @proc_text,
            @converted_type,
            @converted_bit_size,
          ) if keyword.include? 'READ'
        @current_item.write_conversion =
          GenericConversion.new(
            @proc_text,
            @converted_type,
            @converted_bit_size,
          ) if keyword.include? 'WRITE'
        @building_generic_conversion = false
        # Add the current config.line to the conversion being built
      else
        @proc_text << parser.line << "\n"
      end # case keyword
    else
      # not building generic conversion
      case keyword
      when 'TABLEFILE'
        usage = "#{keyword} <File name>"
        parser.verify_num_parameters(1, 1, usage)
        table_filename = File.join(File.dirname(filename), params[0])
        unless File.exist?(table_filename)
          raise parser.error(
                  "Table file #{table_filename} not found",
                  usage,
                )
        end
        process_file(table_filename)

      when 'TABLE'
        finish_packet
        @current_packet =
          TableParser.parse_table(parser, @commands, @warnings)
        @definitions[@current_packet.packet_name] = @last_config
        @current_cmd_or_tlm = COMMAND
        @default_index = 0

        # Select an existing table for editing
      when 'SELECT_TABLE'
        usage = "#{keyword} <TABLE NAME>"
        finish_packet
        parser.verify_num_parameters(1, 1, usage)
        table_name = params[0].upcase
        @current_packet = table(table_name)
        unless @current_packet
          raise parser.error("Table #{table_name} not found", usage)
        end

        #######################################################################
        # All the following keywords must have a current packet defined
        #######################################################################
      when 'SELECT_PARAMETER', 'PARAMETER', 'ID_PARAMETER',
           'ARRAY_PARAMETER', 'APPEND_PARAMETER', 'APPEND_ID_PARAMETER',
           'APPEND_ARRAY_PARAMETER', 'ALLOW_SHORT', 'HAZARDOUS',
           'PROCESSOR', 'META', 'DISABLE_MESSAGES', 'DISABLED'
        unless @current_packet
          raise parser.error("No current packet for #{keyword}")
        end
        process_current_packet(parser, keyword, params)

        #######################################################################
        # All the following keywords must have a current item defined
        #######################################################################
      when 'STATE', 'READ_CONVERSION', 'WRITE_CONVERSION',
           'POLY_READ_CONVERSION', 'POLY_WRITE_CONVERSION',
           'SEG_POLY_READ_CONVERSION', 'SEG_POLY_WRITE_CONVERSION',
           'GENERIC_READ_CONVERSION_START',
           'GENERIC_WRITE_CONVERSION_START', 'REQUIRED', 'LIMITS',
           'LIMITS_RESPONSE', 'UNITS', 'FORMAT_STRING', 'DESCRIPTION',
           'HIDDEN', 'MINIMUM_VALUE', 'MAXIMUM_VALUE', 'DEFAULT_VALUE',
           'OVERFLOW', 'UNEDITABLE'
        unless @current_item
          raise parser.error("No current item for #{keyword}")
        end
        process_current_item(parser, keyword, params)

      when 'DEFAULT'
        if params.length != @current_packet.sorted_items.length
          raise parser.error("DEFAULT #{params.join(' ')} length of #{params.length} doesn't match item length of #{@current_packet.sorted_items.length}")
        end
        @defaults.concat(params)

      else
        # blank config.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

  # Complete the last defined packet
  finish_packet
end

#start_item(parser) ⇒ Object



224
225
226
227
# File 'lib/openc3/tools/table_manager/table_config.rb', line 224

def start_item(parser)
  finish_item
  @current_item = TableItemParser.parse(parser, @current_packet, @warnings)
end

#table(table_name) ⇒ Table

Parameters:

  • table_name (String)

    Table name to return

Returns:



67
68
69
# File 'lib/openc3/tools/table_manager/table_config.rb', line 67

def table(table_name)
  tables[table_name.upcase]
end

#table_namesArray<String>

Returns All the table names.

Returns:



61
62
63
# File 'lib/openc3/tools/table_manager/table_config.rb', line 61

def table_names
  tables.keys
end

#tablesArray<Table>

Returns All tables defined in the configuration file.

Returns:

  • (Array<Table>)

    All tables defined in the configuration file



51
52
53
# File 'lib/openc3/tools/table_manager/table_config.rb', line 51

def tables
  @commands[Table::TARGET]
end