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_target_enabled, #cmd_target_names, #config_params, #connect_on_startup, #disable_disconnect, #interfaces, #name, #num_clients, #options, #protocol_info, #read_count, #read_protocols, #read_queue_size, #read_raw_data, #read_raw_data_time, #reconnect_delay, #routers, #save_raw_data, #scheduler, #secrets, #state, #stream_log_pair, #target_names, #tlm_target_enabled, #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

#_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_available, #get_tlm_buffer, #get_tlm_cnt, #get_tlm_cnts, #get_tlm_packet, #get_tlm_values, #inject_tlm, #interface_cmd, #interface_details, #interface_protocol_cmd, #interface_target_disable, #interface_target_enable, #limits_enabled?, #list_configs, #list_settings, #load_config, #map_target_to_interface, #map_target_to_router, #normalize_tlm, #offline_access_needed, #override_tlm, #router_cmd, #router_details, #router_protocol_cmd, #router_target_disable, #router_target_enable, #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_with_units, #unmap_target_from_interface, #unmap_target_from_router, #update_news, #update_plugin_store

Methods included from CmdLog

#_build_cmd_output_string

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



34
35
36
37
38
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
# File 'lib/openc3/interfaces/file_interface.rb', line 34

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.



24
25
26
# File 'lib/openc3/interfaces/file_interface.rb', line 24

def filename
  @filename
end

Instance Method Details

#connectObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/openc3/interfaces/file_interface.rb', line 77

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)


89
90
91
# File 'lib/openc3/interfaces/file_interface.rb', line 89

def connected?
  return @connected
end

#convert_data_to_packet(data, extra = nil) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/openc3/interfaces/file_interface.rb', line 151

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

#create_unique_filenameObject



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/openc3/interfaces/file_interface.rb', line 207

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

#detailsObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/openc3/interfaces/file_interface.rb', line 221

def details
  result = super()
  result['command_write_folder'] = @command_write_folder
  result['telemetry_read_folder'] = @telemetry_read_folder
  result['telemetry_archive_folder'] = @telemetry_archive_folder
  result['file_read_size'] = @file_read_size
  result['stored'] = @stored
  result['filename'] = @filename
  result['extension'] = @extension
  result['label'] = @label
  result['queue_length'] = @queue.length
  result['polling'] = @polling
  result['recursive'] = @recursive
  result['throttle'] = @throttle
  result['discard_file_header_bytes'] = @discard_file_header_bytes
  return result
end

#disconnectObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/openc3/interfaces/file_interface.rb', line 93

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



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/openc3/interfaces/file_interface.rb', line 182

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



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/openc3/interfaces/file_interface.rb', line 195

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



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

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)



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/openc3/interfaces/file_interface.rb', line 163

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



141
142
143
144
145
146
147
148
149
# File 'lib/openc3/interfaces/file_interface.rb', line 141

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