Class: OpenC3::FileInterface

Inherits:
Interface show all
Defined in:
lib/openc3/interfaces/file_interface.rb

Constant Summary

Constants included from Api

Api::DELAY_METRICS, Api::DURATION_METRICS, Api::SUBSCRIPTION_DELIMITER, Api::SUM_METRICS

Constants included from ApiShared

ApiShared::DEFAULT_TLM_POLLING_RATE

Constants included from Extract

Extract::SCANNING_REGULAR_EXPRESSION

Instance Attribute Summary collapse

Attributes inherited from Interface

#auto_reconnect, #bytes_read, #bytes_written, #cmd_routers, #cmd_target_names, #config_params, #connect_on_startup, #disable_disconnect, #interfaces, #name, #num_clients, #options, #packet_log_writer_pairs, #protocol_info, #read_count, #read_protocols, #read_queue_size, #read_raw_data, #read_raw_data_time, #reconnect_delay, #routers, #scheduler, #secrets, #state, #stored_packet_log_writer_pairs, #stream_log_pair, #target_names, #tlm_target_names, #write_count, #write_protocols, #write_queue_size, #written_raw_data, #written_raw_data_time

Instance Method Summary collapse

Methods inherited from Interface

#_write, #add_protocol, #as_json, #connection_string, #convert_packet_to_data, #copy_to, #interface_cmd, #post_connect, #protocol_cmd, #read, #read_allowed?, #read_interface_base, #start_raw_logging, #stop_raw_logging, #write, #write_allowed?, #write_interface_base, #write_raw, #write_raw_allowed?

Methods included from Api

#_build_cmd_output_string, #_cmd_implementation, #_extract_target_command_names, #_extract_target_command_parameter_names, #_extract_target_packet_item_names, #_extract_target_packet_names, #_get_and_set_cmd, #_get_item, #_limits_group, #_set_tlm_process_args, #_tlm_process_args, #_validate_tlm_type, #build_cmd, #cmd, #cmd_no_checks, #cmd_no_hazardous_check, #cmd_no_range_check, #cmd_raw, #cmd_raw_no_checks, #cmd_raw_no_hazardous_check, #cmd_raw_no_range_check, #config_tool_names, #connect_interface, #connect_router, #delete_config, #disable_cmd, #disable_limits, #disable_limits_group, #disconnect_interface, #disconnect_router, #enable_cmd, #enable_limits, #enable_limits_group, #get_all_cmd_names, #get_all_cmds, #get_all_interface_info, #get_all_router_info, #get_all_settings, #get_all_tlm, #get_all_tlm_item_names, #get_all_tlm_names, #get_cmd, #get_cmd_buffer, #get_cmd_cnt, #get_cmd_cnts, #get_cmd_hazardous, #get_cmd_time, #get_cmd_value, #get_interface, #get_interface_names, #get_item, #get_limits, #get_limits_events, #get_limits_groups, #get_limits_set, #get_limits_sets, #get_metrics, #get_out_of_limits, #get_overall_limits_state, #get_overrides, #get_packet_derived_items, #get_packets, #get_param, #get_router, #get_router_names, #get_setting, #get_settings, #get_target, #get_target_interfaces, #get_target_names, #get_tlm, #get_tlm_buffer, #get_tlm_cnt, #get_tlm_cnts, #get_tlm_packet, #get_tlm_values, #inject_tlm, #interface_cmd, #interface_protocol_cmd, #limits_enabled?, #list_configs, #list_settings, #load_config, #map_target_to_interface, #normalize_tlm, #offline_access_needed, #override_tlm, #router_cmd, #router_protocol_cmd, #save_config, #send_raw, #set_limits, #set_limits_set, #set_offline_access, #set_setting, #set_tlm, #start_raw_logging_interface, #start_raw_logging_router, #stash_all, #stash_delete, #stash_get, #stash_keys, #stash_set, #stop_raw_logging_interface, #stop_raw_logging_router, #subscribe_packets, #tlm, #tlm_formatted, #tlm_raw, #tlm_variable, #tlm_with_units, #update_news

Constructor Details

#initialize(command_write_folder, telemetry_read_folder, telemetry_archive_folder, file_read_size = 65536, stored = true, protocol_type = nil, *protocol_args) ⇒ FileInterface

Returns a new instance of FileInterface.

Parameters:

  • command_write_folder (String)

    Folder to write command files to - Set to nil to disallow writes

  • telemetry_read_folder (String)

    Folder to read telemetry files from - Set to nil to disallow reads

  • telemetry_archive_folder (String)

    Folder to move read telemetry files to - Set to DELETE to delete files

  • file_read_size (Integer) (defaults to: 65536)

    Number of bytes to read from the file at a time

  • stored (Boolean) (defaults to: true)

    Whether to set stored flag on read telemetry

  • protocol_type (String) (defaults to: nil)

    Name of the protocol to use with this interface

  • protocol_args (Array<String>)

    Arguments to pass to the protocol



39
40
41
42
43
44
45
46
47
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
# File 'lib/openc3/interfaces/file_interface.rb', line 39

def initialize(
  command_write_folder,
  telemetry_read_folder,
  telemetry_archive_folder,
  file_read_size = 65536,
  stored = true,
  protocol_type = nil,
  *protocol_args
)
  super()

  @protocol_type = ConfigParser.handle_nil(protocol_type)
  @protocol_args = protocol_args
  if @protocol_type
    protocol_class_name = protocol_type.to_s.capitalize << 'Protocol'
    klass = OpenC3.require_class(protocol_class_name.class_name_to_filename)
    add_protocol(klass, protocol_args, :PARAMS)
  end

  @command_write_folder = ConfigParser.handle_nil(command_write_folder)
  @telemetry_read_folder = ConfigParser.handle_nil(telemetry_read_folder)
  @telemetry_archive_folder = ConfigParser.handle_nil(telemetry_archive_folder)
  @file_read_size = Integer(file_read_size)
  @stored = ConfigParser.handle_true_false(stored)

  @read_allowed = false unless @telemetry_read_folder
  @write_allowed = false unless @command_write_folder
  @write_raw_allowed = false unless @command_write_folder

  @file = nil
  @filename = ''
  @listener = nil
  @connected = false
  @extension = ".bin"
  @label = "command"
  @queue = Queue.new
  @polling = false
  @recursive = false
  @throttle = nil
  @discard_file_header_bytes = nil
  @sleeper = nil
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



29
30
31
# File 'lib/openc3/interfaces/file_interface.rb', line 29

def filename
  @filename
end

Instance Method Details

#connectObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/openc3/interfaces/file_interface.rb', line 82

def connect
  super() # Reset the protocols

  if @telemetry_read_folder
    @listener = Listen.to(@telemetry_read_folder, force_polling: @polling) do |modified, added, removed|
      @queue << added if added
    end
    @listener.start # starts a listener thread - does not block
  end
  @connected = true
end

#connected?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/openc3/interfaces/file_interface.rb', line 94

def connected?
  return @connected
end

#convert_data_to_packet(data, extra = nil) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/openc3/interfaces/file_interface.rb', line 156

def convert_data_to_packet(data, extra = nil)
  packet = super(data, extra)
  if packet
    packet.stored = @stored
  end
  return packet
end

#create_unique_filenameObject



212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/openc3/interfaces/file_interface.rb', line 212

def create_unique_filename
  # Create a filename that doesn't exist
  attempt = nil
  while true
    filename = File.join(@command_write_folder, File.build_timestamped_filename([@label, attempt], @extension))
    if File.exist?(filename)
      attempt ||= 0
      attempt += 1
    else
      return filename
    end
  end
end

#disconnectObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/openc3/interfaces/file_interface.rb', line 98

def disconnect
  @file.close if @file and not @file.closed?
  @file = nil
  @sleeper.cancel if @sleeper
  @listener.stop if @listener
  @listener = nil
  @queue << nil
  super()
  @connected = false
end

#finish_fileObject



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/openc3/interfaces/file_interface.rb', line 187

def finish_file
  path = @file.path
  @file.close
  @file = nil

  # Archive (or DELETE) complete file
  if @telemetry_archive_folder == "DELETE"
    FileUtils.rm(path)
  else
    FileUtils.mv(path, @telemetry_archive_folder)
  end
end

#get_next_telemetry_fileObject



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/openc3/interfaces/file_interface.rb', line 200

def get_next_telemetry_file
  files = []
  if @recursive
    files = Dir.glob("#{@telemetry_read_folder}/**/*")
  else
    files = Dir.glob("#{@telemetry_read_folder}/*")
  end
  # Dir.glob includes directories, so filter them out
  files = files.sort.select { |fn| File.file?(fn) }
  return files[0]
end

#read_interfaceObject



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
# File 'lib/openc3/interfaces/file_interface.rb', line 109

def read_interface
  while true
    if @file
      # Read more data from existing file
      data = @file.read(@file_read_size)
      # Throttle after each read size
      if @throttle and @sleeper.sleep(@throttle)
        return nil, nil
      end
      if data and data.length > 0
        read_interface_base(data, nil)
        return data, nil
      else
        finish_file()
      end
    end

    # Find the next file to read
    @filename = get_next_telemetry_file()
    if @filename
      if File.extname(@filename) == ".gz"
        @file = Zlib::GzipReader.open(@filename)
      else
        @file = File.open(@filename, "rb")
      end
      if @discard_file_header_bytes
        @file.read(@discard_file_header_bytes)
      end
      next
    end

    # Wait for a file to read
    result = @queue.pop
    return nil, nil unless result
  end
end

#set_option(option_name, option_values) ⇒ Object

Supported Options LABEL - Label to add to written files EXTENSION - Extension to add to written files (see Interface#set_option)



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/openc3/interfaces/file_interface.rb', line 168

def set_option(option_name, option_values)
  super(option_name, option_values)
  case option_name.upcase
  when 'LABEL'
    @label = option_values[0]
  when 'EXTENSION'
    @extension = option_values[0]
  when 'POLLING'
    @polling = ConfigParser.handle_true_false(option_values[0])
  when 'RECURSIVE'
    @recursive = ConfigParser.handle_true_false(option_values[0])
  when 'THROTTLE'
    @throttle = Float(option_values[0])
    @sleeper = Sleeper.new
  when 'DISCARD_FILE_HEADER_BYTES'
    @discard_file_header_bytes = Integer(option_values[0])
  end
end

#write_interface(data, extra = nil) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/openc3/interfaces/file_interface.rb', line 146

def write_interface(data, extra = nil)
  # Write this data into its own file
  File.open(create_unique_filename(), 'wb') do |file|
    file.write(data)
  end

  write_interface_base(data, extra)
  return data, extra
end