Class: Cosmos::TlmViewerConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmos/tools/tlm_viewer/tlm_viewer_config.rb

Defined Under Namespace

Classes: ScreenInfo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil) ⇒ TlmViewerConfig

Returns a new instance of TlmViewerConfig.



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
191
# File 'lib/cosmos/tools/tlm_viewer/tlm_viewer_config.rb', line 99

def initialize(filename = nil)
  # Handle nil filename
  filename = File.join(Cosmos::USERPATH, 'config', 'tools', 'tlm_viewer', 'tlm_viewer.txt') unless filename
  @filename = filename

  # Ensure the file exists
  raise "Telemetry Viewer configuration file #{filename} does not exist" unless test ?f, filename

  # Initialize instance variables
  @columns = []
  @columns << {}
  @screen_infos = {}

  # Process File
  @current_column = @columns[0]
  @current_target = nil
  @current_screens = nil
  @current_screen_info = nil
  @current_group = nil
  parser = ConfigParser.new
  parser.parse_file(filename) do |keyword, parameters|
    case keyword

    when 'NEW_COLUMN'
      @columns << {}
      @current_column = @columns[-1]

    when 'AUTO_TARGETS'
      parser.verify_num_parameters(0, 0, 'AUTO_TARGETS')
      System.targets.each do |target_name, target|
        screen_dir = File.join(target.dir, 'screens')
        if File.exist?(screen_dir) and num_screens(screen_dir) > 0
          start_target(target.name, parser)
          auto_screens()
        end
      end

    when 'AUTO_TARGET'
      parser.verify_num_parameters(1, 1, 'AUTO_TARGET <Target Name>')
      target_name = parameters[0].upcase
      target = System.targets[target_name]
      raise parser.error("Unknown target #{target_name}") unless target
      screen_dir = File.join(target.dir, 'screens')
      if File.exist?(screen_dir) and num_screens(screen_dir) > 0
        start_target(target.name, parser)
        auto_screens()
      end

    when 'TARGET'
      parser.verify_num_parameters(1, 1, 'TARGET <Target Name>')
      target_name = parameters[0].upcase
      start_target(target_name, parser)

    when 'SCREEN'
      raise parser.error("No target defined. SCREEN must follow TARGET.") unless @current_target
      parser.verify_num_parameters(1, 3, 'SCREEN <Filename> <X Position (optional)> <Y Position (optional)>')
      screen_filename = File.join(@current_target.dir, 'screens', parameters[0])
      start_screen(screen_filename, parameters[1], parameters[2])

    when 'SHOW_ON_STARTUP'
      raise parser.error("No screen defined. SHOW_ON_STARTUP must follow SCREEN or GROUP_SCREEN.") unless @current_screen_info
      parser.verify_num_parameters(0, 0, 'SHOW_ON_STARTUP')
      @current_screen_info.show_on_startup = true

    when 'ADD_SHOW_ON_STARTUP'
      parser.verify_num_parameters(2, 4, 'ADD_SHOW_ON_STARTUP <Target Name> <Screen Name> <X Position (optional)> <Y Position (optional)>')
      @current_screen_info = @screen_infos["#{parameters[0]} #{parameters[1]}".upcase]
      raise parser.error("Screen #{parameters[0]} #{parameters[1]} does not exist") unless @current_screen_info
      @current_screen_info.show_on_startup = true
      @current_screen_info.x_pos = parameters[2].to_i if parameters[2]
      @current_screen_info.y_pos = parameters[3].to_i if parameters[3]

    when 'GROUP'
      parser.verify_num_parameters(1, 1, 'GROUP <Group Name>')
      @current_group = parameters[0]
      @current_screens = {}
      @current_column[@current_group] = @current_screens

    when 'GROUP_SCREEN'
      raise parser.error("No group defined. GROUP_SCREEN must follow GROUP.") unless @current_group
      parser.verify_num_parameters(2, 4, 'GROUP_SCREEN <Target Name> <Screen Filename> <X Position (optional)> <Y Position (Optional)>')
      start_target(parameters[0].upcase, parser, @current_group)
      screen_filename = File.join(@current_target.dir, 'screens', parameters[1])
      start_screen(screen_filename, parameters[2], parameters[3])

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

  build_completion_list()
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



93
94
95
# File 'lib/cosmos/tools/tlm_viewer/tlm_viewer_config.rb', line 93

def columns
  @columns
end

#completion_listObject

Returns the value of attribute completion_list.



96
97
98
# File 'lib/cosmos/tools/tlm_viewer/tlm_viewer_config.rb', line 96

def completion_list
  @completion_list
end

#filenameObject

Returns the value of attribute filename.



95
96
97
# File 'lib/cosmos/tools/tlm_viewer/tlm_viewer_config.rb', line 95

def filename
  @filename
end

#screen_infosObject

Returns the value of attribute screen_infos.



94
95
96
# File 'lib/cosmos/tools/tlm_viewer/tlm_viewer_config.rb', line 94

def screen_infos
  @screen_infos
end

#tlm_to_screen_mappingObject

Returns the value of attribute tlm_to_screen_mapping.



97
98
99
# File 'lib/cosmos/tools/tlm_viewer/tlm_viewer_config.rb', line 97

def tlm_to_screen_mapping
  @tlm_to_screen_mapping
end

Instance Method Details

#auto_screensObject



250
251
252
253
254
255
256
257
258
259
260
# File 'lib/cosmos/tools/tlm_viewer/tlm_viewer_config.rb', line 250

def auto_screens
  @current_group = nil
  screen_dir = File.join(@current_target.dir, 'screens')
  if File.exist?(screen_dir)
    Dir.new(screen_dir).each do |filename|
      if filename[0..0] != '.'
        start_screen(File.join(screen_dir, filename))
      end
    end
  end
end

#build_completion_listObject



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/cosmos/tools/tlm_viewer/tlm_viewer_config.rb', line 270

def build_completion_list
  @completion_list = []
  @tlm_to_screen_mapping = {}
  total = @screen_infos.length.to_f
  index = 1
  ConfigParser.splash.message = "Building completion list" if ConfigParser.splash
  @screen_infos.each do |name, info|
    ConfigParser.splash.progress = index / total if ConfigParser.splash
    info.items.each do |item|
      @tlm_to_screen_mapping[item] ||= []
      @tlm_to_screen_mapping[item] << name
    end
    @completion_list.concat(info.items)
    index += 1
  end
  @completion_list.uniq!
end

#num_screens(screen_dir) ⇒ Object



262
263
264
265
266
267
268
# File 'lib/cosmos/tools/tlm_viewer/tlm_viewer_config.rb', line 262

def num_screens(screen_dir)
  count = 0
  Dir.new(screen_dir).each do |filename|
    count += 1 if filename[0..0] != '.'
  end
  count
end

#save(filename) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/cosmos/tools/tlm_viewer/tlm_viewer_config.rb', line 193

def save(filename)
  @filename = filename

  File.open(filename, 'w') do |file|
    @columns.each_with_index do |target_screen_infos, column_index|
      if column_index != 0
        file.puts ''
        file.puts "NEW_COLUMN"
        file.puts ''
      end
      target_screen_infos.each do |target_name, screen_infos|
        file.puts "TARGET \"#{target_name}\""
        screen_infos.each do |screen_name, screen_info|
          # Grab the filename by indexing the full path for 'screens' and going past
          # to capture the filename such as 'status.txt' below
          #   C:/COSMOS/config/targets/TGT/screens/status.txt
          screen_filename = screen_info.filename[(screen_info.filename.index("screens").to_i + 8)..-1]
          string = "  SCREEN"
          string << " \"#{screen_filename}\""
          string << " #{screen_info.x_pos}" if screen_info.x_pos
          string << " #{screen_info.y_pos}" if screen_info.y_pos
          file.puts string
          if screen_info.screen
            file.puts "    SHOW_ON_STARTUP"
          end
        end
        file.puts ""
      end
    end
  end
end

#start_screen(screen_filename, x_pos = nil, y_pos = nil) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/cosmos/tools/tlm_viewer/tlm_viewer_config.rb', line 236

def start_screen(screen_filename, x_pos = nil, y_pos = nil)
  screen_name = File.basename(screen_filename, '.txt').upcase
  x_pos = x_pos.to_i if x_pos
  y_pos = y_pos.to_i if y_pos

  @current_screen_info = ScreenInfo.new(@current_group, @current_target.name, screen_name, screen_filename, x_pos, y_pos)
  @screen_infos[@current_screen_info.full_name] = @current_screen_info
  @current_screens["#{@current_target.name}_#{screen_name}"] = @current_screen_info
  @current_screen_info.force_substitute = true if @current_target.auto_screen_substitute
  @current_screen_info.original_target_name = @current_target.original_name
  @current_screen_info.substitute = @current_target.name if @current_target.substitute or @current_target.auto_screen_substitute
  @current_screen_info.read_items
end

#start_target(target_name, parser, group = nil) ⇒ Object



225
226
227
228
229
230
231
232
233
234
# File 'lib/cosmos/tools/tlm_viewer/tlm_viewer_config.rb', line 225

def start_target(target_name, parser, group = nil)
  @current_target = System.targets[target_name]
  raise parser.error("Unknown target #{target_name}") unless @current_target
  # If no group was passed setup a new column and screen hash
  unless group
    @current_group = nil
    @current_screens = {}
    @current_column[target_name] = @current_screens
  end
end