Class: OpenC3::TableManagerCore

Inherits:
Object
  • Object
show all
Defined in:
lib/openc3/tools/table_manager/table_manager_core.rb

Overview

Provides the low level Table Manager methods which do not require a GUI.

Defined Under Namespace

Classes: CoreError, MismatchError

Class Method Summary collapse

Class Method Details

.binary(binary, definition_filename, table_name) ⇒ Object



37
38
39
40
41
# File 'lib/openc3/tools/table_manager/table_manager_core.rb', line 37

def self.binary(binary, definition_filename, table_name)
  config = TableConfig.process_file(definition_filename)
  load_binary(config, binary)
  return config.table(table_name).buffer
end

.build_json_hash(binary, definition_filename) ⇒ Object

We build the json hash without converting to a json string to allow modifying the hash app/models/table.rb uses this ability to add the real definition filename



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
# File 'lib/openc3/tools/table_manager/table_manager_core.rb', line 134

def self.build_json_hash(binary, definition_filename)
  config = TableConfig.process_file(definition_filename)
  tables = []
  json = { tables: tables }
  begin
    load_binary(config, binary)
  rescue CoreError => err
    json['errors'] = err.message
  end
  config.tables.each do |table_name, table|
    tables << {
      name: table_name,
      numRows: table.num_rows,
      numColumns: table.num_columns,
      headers: [],
      rows: [],
    }
    col = 0
    row = 0
    table.sorted_items.each_with_index do |item, index|
      next if item.hidden
      if table.num_columns == 1
        if row == 0
          tables[-1][:headers] = [ "INDEX", "NAME", "VALUE" ]
        end
        tables[-1][:rows] << [
          {
            index: row + 1,
            name: item.name,
            value: table.read(item.name, :FORMATTED),
            states: item.states,
            editable: item.editable,
          },
        ]
      else
        if row == 0 && col == 0
          tables[-1][:headers] << "INDEX"
        end
        if row == 0
          tables[-1][:headers] << item.name[0..-2]
        end
        if col == 0
          # Each row is an array of items
          tables[-1][:rows][row] = []
        end
        tables[-1][:rows][row] << {
          index: row + 1,
          name: item.name,
          value: table.read(item.name, :FORMATTED),
          states: item.states,
          editable: item.editable,
        }
      end
      col += 1
      if col == table.num_columns
        col = 0
        row += 1
      end
    end
  end
  json.as_json(:allow_nan => true)
end

.definition(definition_filename, table_name) ⇒ Object



43
44
45
46
# File 'lib/openc3/tools/table_manager/table_manager_core.rb', line 43

def self.definition(definition_filename, table_name)
  config = TableConfig.process_file(definition_filename)
  return config.definition(table_name) # This returns an array: [filename, contents]
end

.generate(definition_filename) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/openc3/tools/table_manager/table_manager_core.rb', line 104

def self.generate(definition_filename)
  config = TableConfig.process_file(definition_filename)
  binary = ''
  config.tables.each do |table_name, table|
    table.restore_defaults
    binary += table.buffer
  end
  binary
end

.load_binary(config, data) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/openc3/tools/table_manager/table_manager_core.rb', line 197

def self.load_binary(config, data)
  binary_data_index = 0
  total_table_length = 0
  config.tables.each do |table_name, table|
    total_table_length += table.length
  end
  config.tables.each do |table_name, table|
    if binary_data_index + table.length > data.length
      table.buffer = data[binary_data_index..-1]
      raise MismatchError,
        "Binary size of #{data.length} not large enough to fully represent table definition of length #{total_table_length}. "+
        "The remaining table definition (starting with byte #{data.length - binary_data_index} in #{table.table_name}) will be filled with 0."
    end
    table.buffer = data[binary_data_index...binary_data_index + table.length]
    binary_data_index += table.length
  end
  if binary_data_index < data.length
    raise MismatchError,
      "Binary size of #{data.length} larger than table definition of length #{total_table_length}. "+
      "Discarding the remaing #{data.length - binary_data_index} bytes."
  end
end

.report(binary, definition_filename, requested_table_name = nil) ⇒ Object



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/openc3/tools/table_manager/table_manager_core.rb', line 48

def self.report(binary, definition_filename, requested_table_name = nil)
  report = StringIO.new
  config = TableConfig.process_file(definition_filename)
  begin
    load_binary(config, binary)
  rescue CoreError => err
    report.puts "Error: #{err.message}\n"
  end

  config.tables.each do |table_name, table|
    next if requested_table_name && table_name != requested_table_name
    items = table.sorted_items
    report.puts(table.table_name)

    # Write the column headers
    if table.type == :ROW_COLUMN
      columns = ['Item']

      # Remove the '0' from the 'itemname0'
      table.num_columns.times.each do |x|
        columns << items[x].name[0...-1]
      end
      report.puts columns.join(', ')
    else
      report.puts 'Label, Value'
    end

    # Write the table item values
    (0...table.num_rows).each do |r|
      if table.type == :ROW_COLUMN
        rowtext = "#{r + 1}"
      else
        rowtext = items[r].name
      end

      report.write "#{rowtext}, "
      (0...table.num_columns).each do |c|
        if table.type == :ROW_COLUMN
          table_item = items[c + r * table.num_columns]
        else
          table_item = items[r]
        end
        value = table.read(table_item.name, :FORMATTED)
        if value.is_printable?
          report.write "#{value}, "
        else
          report.write "#{value.simple_formatted}, "
        end
      end
      report.write("\n") # newline after each row
    end
    report.write("\n") # newline after each table
  end
  report.string
end

.save(definition_filename, tables) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/openc3/tools/table_manager/table_manager_core.rb', line 114

def self.save(definition_filename, tables)
  config = TableConfig.process_file(definition_filename)
  tables.each do |table|
    table_def = config.tables[table['name']]
    table['rows'].each do |row|
      row.each do |item|
        # TODO: I don't know how the frontend could edit an item like this:
        # item:{"name"=>"BINARY", "value"=>{"json_class"=>"String", "raw"=>[222, 173, 190, 239]} }
        next if item['value'].is_a? Hash
        table_def.write(item['name'], item['value'])
      end
    end
  end
  binary = ''
  config.tables.each { |table_name, table| binary += table.buffer }
  binary
end