Class: OpenC3::SimulatedTargetInterface

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

Overview

An interface class that provides simulated telemetry and command responses

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, #convert_data_to_packet, #convert_packet_to_data, #copy_to, #interface_cmd, #protocol_cmd, #read_allowed?, #read_interface, #read_interface_base, #set_option, #start_raw_logging, #stop_raw_logging, #write_allowed?, #write_interface, #write_interface_base, #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_target_info, #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(sim_target_file) ⇒ SimulatedTargetInterface

Returns a new instance of SimulatedTargetInterface.

Parameters:

  • sim_target_file (String)

    Filename of the simulator target class



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/openc3/interfaces/simulated_target_interface.rb', line 29

def initialize(sim_target_file)
  super()
  @connected = false
  @initialized = false
  @count_100hz = 0
  @next_tick_time = nil
  @pending_packets = []
  @sim_target_class = OpenC3.require_class sim_target_file
  @sim_target = nil
  @write_raw_allowed = false
end

Instance Method Details

#connectObject

Initialize the simulated target object and “connect” to the target



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/openc3/interfaces/simulated_target_interface.rb', line 46

def connect
  unless @initialized
    # Create Simulated Target Object
    @sim_target = @sim_target_class.new(@target_names[0])
    # Set telemetry rates
    @sim_target.set_rates

    @initialized = true
  end

  @count_100hz = 0

  # Save the current time + delta as the next expected tick time
  @next_tick_time = Time.now.sys + @sim_target.tick_period_seconds

  super()
  @connected = true
end

#connected?Boolean

Returns Whether the simulated target is connected (initialized).

Returns:

  • (Boolean)

    Whether the simulated target is connected (initialized)



66
67
68
# File 'lib/openc3/interfaces/simulated_target_interface.rb', line 66

def connected?
  @connected
end

#connection_stringObject



41
42
43
# File 'lib/openc3/interfaces/simulated_target_interface.rb', line 41

def connection_string
  return @sim_target_class.to_s
end

#disconnectObject

Disconnect from the simulator



156
157
158
159
# File 'lib/openc3/interfaces/simulated_target_interface.rb', line 156

def disconnect
  @connected = false
  super()
end

#readPacket

Returns a simulated target packet from the simulator

Returns:

  • (Packet)

    Returns a simulated target packet from the simulator



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

def read
  packet = nil
  if @connected
    while true
      packet = first_pending_packet()
      break unless packet
      # Support read_packet (but not read data) in protocols
      # Generic protocol use is not supported
      @read_protocols.each do |protocol|
        packet = protocol.read_packet(packet)
        if packet == :DISCONNECT
          Logger.info("#{@name}: Protocol #{protocol.class} read_packet requested disconnect")
          return nil
        end
        break if packet == :STOP
      end
      return packet unless packet == :STOP
    end

    while true
      # Calculate time to sleep to make ticks the right distance apart
      now = Time.now.sys
      delta = @next_tick_time - now
      if delta > 0.0
        sleep(delta) # Sleep between packets
        return nil unless @connected
      elsif delta < -1.0
        # Fell way behind - jump next tick time
        @next_tick_time = Time.now.sys
      end

      @pending_packets = @sim_target.read(@count_100hz, @next_tick_time)
      @next_tick_time += @sim_target.tick_period_seconds
      @count_100hz += @sim_target.tick_increment

      packet = first_pending_packet()
      if packet
        # Support read_packet (but not read data) in protocols
        # Generic protocol use is not supported
        @read_protocols.each do |protocol|
          packet = protocol.read_packet(packet)
          if packet == :DISCONNECT
            Logger.info("#{@name}: Protocol #{protocol.class} read_packet requested disconnect")
            return nil
          end
          break if packet == :STOP
        end
        next if packet == :STOP

        return packet
      end
    end
  else
    raise "Interface not connected"
  end
  return packet
end

#stream_log_pair=(_stream_log_pair) ⇒ Object

Raise an error because raw logging is not supported for this interface



151
152
153
# File 'lib/openc3/interfaces/simulated_target_interface.rb', line 151

def stream_log_pair=(_stream_log_pair)
  raise "Raw logging not supported for SimulatedTargetInterface"
end

#write(packet) ⇒ Object

Parameters:

  • packet (Packet)

    Command packet to send to the simulator



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/openc3/interfaces/simulated_target_interface.rb', line 130

def write(packet)
  if @connected
    # Update count of commands sent through this interface
    @write_count += 1
    @bytes_written += packet.length
    @written_raw_data_time = Time.now
    @written_raw_data = packet.buffer

    # Have simulated target handle the packet
    @sim_target.write(packet)
  else
    raise "Interface not connected"
  end
end

#write_raw(_data) ⇒ Object

write_raw is not implemented and will raise a RuntimeError



146
147
148
# File 'lib/openc3/interfaces/simulated_target_interface.rb', line 146

def write_raw(_data)
  raise "write_raw not implemented for SimulatedTargetInterface"
end