Class: Cosmos::TableManagerCore

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmos/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, NoConfigError, NoTableError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTableManagerCore

Create the instance



54
55
56
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 54

def initialize
  reset
end

Instance Attribute Details

#configTableConfig (readonly)

Returns Configuration instance.

Returns:



51
52
53
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 51

def config
  @config
end

Instance Method Details

#file_checkString

Returns Success string if parameters all check. Raises a CoreError if errors are found.

Returns:

  • (String)

    Success string if parameters all check. Raises a CoreError if errors are found.

Raises:



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 183

def file_check
  raise NoConfigError unless @config
  result = ''
  @config.table_names.each do |name|
    table_result = table_check(name)
    unless table_result.empty?
      result << "Errors in #{name}:\n" + table_result
    end
  end
  raise CoreError, result unless result.empty?
  'All parameters are within their constraints.'
end

#file_hexObject

Create a hex formatted string of all the file data

Raises:



255
256
257
258
259
260
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 255

def file_hex
  raise NoConfigError unless @config
  data = ''
  @config.tables.values.each { |table| data << table.buffer }
  "#{data.formatted}\n\nTotal Bytes Read: #{data.length}"
end

#file_new(def_path, output_dir) ⇒ String

Returns Binary file path.

Parameters:

  • def_path (String)

    Definition file to process

  • output_dir (String)

    Output directory to create the new file

Returns:

  • (String)

    Binary file path



152
153
154
155
156
157
158
159
160
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 152

def file_new(def_path, output_dir)
  process_definition(def_path)
  @config.table_names.each do |table_name|
    set_binary_data_to_default(table_name)
  end
  bin_path = File.join(output_dir, def_to_bin_filename(def_path))
  file_save(bin_path)
  bin_path
end

#file_open(bin_path, def_path) ⇒ Object

Parameters:

  • bin_path (String)

    Binary file to open

  • def_path (String)

    Definition file to use when opening



164
165
166
167
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 164

def file_open(bin_path, def_path)
  process_definition(def_path)
  open_and_load_binary_file(bin_path)
end

#file_report(bin_path, def_path) ⇒ String

Create a CSV report file based on the file contents.

Parameters:

  • bin_path (String)

    Binary filename currently open. Used to generate the report name such that it matches the binary filename.

  • def_path (String)

    Definition filename currently open

Returns:

  • (String)

    Report filename path

Raises:



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 202

def file_report(bin_path, def_path)
  raise NoConfigError unless @config
  file_check

  basename = File.basename(bin_path, '.bin')
  report_path = File.join(File.dirname(bin_path), "#{basename}.csv")
  File.open(report_path, 'w+') do |file|
    file.write("File Definition, #{def_path}\n")
    file.write("File Binary, #{bin_path}\n\n")
    @config.tables.values.each do |table|
      items = table.sorted_items
      file.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
        file.puts columns.join(', ')
      else
        file.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

        file.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

          file.write "#{table.read(table_item.name, :FORMATTED).to_s}, "
        end
        file.write("\n") # newline after each row
      end
      file.write("\n") # newline after each table
    end
  end
  report_path
end

#file_save(filename) ⇒ Object

Saves the current tables in the config instance to the given filename.

Parameters:

  • filename (String)

    Filename to write, overwritten if it exists.

Raises:



172
173
174
175
176
177
178
179
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 172

def file_save(filename)
  raise NoConfigError unless @config
  file_check
  File.open(filename, 'wb') do |file|
    @config.tables.each { |table_name, table| file.write(table.buffer) }
  end
  # file_report(filename, @config.filename)
end

#generate_json(bin_path, def_path) ⇒ Object



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

def generate_json(bin_path, def_path)
  tables = []
  json = { tables: tables }
  begin
    file_open(bin_path, def_path)
  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
    num_cols = table.num_columns
    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.to_json
end

#process_definition(filename) ⇒ Object

Parameters:

  • filename (String)

    Create a new TableConfig instance and process the filename



64
65
66
67
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 64

def process_definition(filename)
  @config = TableConfig.new
  @config.process_file(filename)
end

#resetObject

Clears the configuration



59
60
61
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 59

def reset
  @config = nil
end

#save_tables(bin_path, def_path, tables) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 132

def save_tables(bin_path, def_path, tables)
  file_open(bin_path, def_path)
  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
  file_save(bin_path)
  bin_path
end

#table_check(table_name) ⇒ Object

Parameters:

  • table_name (String)

    Name of the table to check for out of range values

Raises:



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 263

def table_check(table_name)
  raise NoConfigError unless @config
  table = @config.table(table_name)
  raise NoTableError unless table

  result = ''
  table_items = table.sorted_items

  # Check the ranges and constraints for each item in the table
  # We go through it this way (by row and columns) so we can grab the actual
  # user input when we display any errors found
  (0...table.num_rows).each do |r|
    (0...table.num_columns).each do |c|
      # get the table item definition so we know how to save it
      table_item = table_items[r * table.num_columns + c]

      value = table.read(table_item.name)
      unless table_item.range.nil?
        # If the item has states which include the value, then convert
        # the state back to the numeric value for range checking
        if table_item.states && table_item.states.include?(value)
          value = table_item.states[value]
        end

        # check to see if the value lies within its valid range
        unless table_item.range.include?(value)
          if table_item.format_string
            value = table.read(table_item.name, :FORMATTED)
            range_first =
              sprintf(table_item.format_string, table_item.range.first)
            range_last =
              sprintf(table_item.format_string, table_item.range.last)
          else
            range_first = table_item.range.first
            range_last = table_item.range.last
          end
          result <<
            "  #{table_item.name}: #{value} outside valid range of #{range_first}..#{range_last}\n"
        end
      end
    end # end each column
  end # end each row
  result
end

#table_commit(table_name, bin_file, def_file) ⇒ Object

Commit a table from the current configuration into a new binary

Parameters:

  • table_name (String)

    Table name to commit to an existing binary

  • bin_file (String)

    Binary file to open

  • def_file (String)

    Definition file to use when opening

Raises:



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 341

def table_commit(table_name, bin_file, def_file)
  raise NoConfigError unless @config
  save_table = @config.table(table_name)
  raise NoTableError unless save_table

  result = table_check(table_name)
  unless result.empty?
    raise CoreError, "Errors in #{table_name}:\n#{result}"
  end

  config = TableConfig.new
  begin
    config.process_file(def_file)
  rescue => err
    raise CoreError,
          "The table definition file:#{def_file} has the following errors:\n#{err}"
  end

  if !config.table_names.include?(table_name.upcase)
    raise NoTableError,
          "#{table_name} not found in #{def_file} table definition file."
  end

  saved_config = @config
  @config = config
  open_and_load_binary_file(bin_file)

  # Store the saved table data in the new table definition
  table = config.table(save_table.table_name)
  table.buffer = save_table.buffer[0...table.length]
  file_save(bin_file)
  @config = saved_config
end

#table_default(table_name) ⇒ Object

Parameters:

  • table_name (String)

    Name of the table to revert all values to default

Raises:



309
310
311
312
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 309

def table_default(table_name)
  raise NoConfigError unless @config
  set_binary_data_to_default(table_name)
end

#table_hex(table_name) ⇒ Object

Parameters:

  • table_name (String)

    Create a hex formatted string of the given table data

Raises:



315
316
317
318
319
320
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 315

def table_hex(table_name)
  raise NoConfigError unless @config
  table = @config.table(table_name)
  raise NoTableError unless table
  "#{table.buffer.formatted}\n\nTotal Bytes Read: #{table.buffer.length}"
end

#table_save(table_name, filename) ⇒ Object

Parameters:

  • table_name (String)

    Table name to write as a stand alone file

  • filename (String)

    Filename to write the table data to. Existing files will be overwritten.

Raises:



325
326
327
328
329
330
331
332
333
334
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 325

def table_save(table_name, filename)
  raise NoConfigError unless @config
  result = table_check(table_name)
  unless result.empty?
    raise CoreError, "Errors in #{table_name}:\n#{result}"
  end
  File.open(filename, 'wb') do |file|
    file.write(@config.table(table_name).buffer)
  end
end