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

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, #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_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

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



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

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
  @listener = nil
  @connected = false
  @extension = ".bin"
  @label = "command"
  @queue = Queue.new
  @polling = false
  @recursive = false
end

Instance Method Details

#connectObject



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

def connect
  super()

  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)


87
88
89
# File 'lib/openc3/interfaces/file_interface.rb', line 87

def connected?
  return @connected
end

#convert_data_to_packet(data, extra = nil) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/openc3/interfaces/file_interface.rb', line 137

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

#create_unique_filenameObject



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

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



91
92
93
94
95
96
97
98
99
# File 'lib/openc3/interfaces/file_interface.rb', line 91

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

#finish_fileObject



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

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



176
177
178
179
180
181
182
# File 'lib/openc3/interfaces/file_interface.rb', line 176

def get_next_telemetry_file
  if @recursive
    return Dir.glob("#{@telemetry_read_folder}/**/*").sort[0]
  else
    return Dir.glob("#{@telemetry_read_folder}/*").sort[0]
  end
end

#read_interfaceObject



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

def read_interface
  while true
    if @file
      # Read more data from existing file
      data = @file.read(@file_read_size)
      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
    file = get_next_telemetry_file()
    if file
      @file = File.open(file, 'rb')
      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)



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/openc3/interfaces/file_interface.rb', line 149

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])
  end
end

#write_interface(data, extra = nil) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/openc3/interfaces/file_interface.rb', line 127

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