Class: Cosmos::SimulatedTargetInterface

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

Overview

An interface class that provides simulated telemetry and command responses

Constant Summary

Constants included from Api

Api::SETTINGS_KEY, Api::SUBSCRIPTION_DELIMITER

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, #config_params, #connect_on_startup, #disable_disconnect, #interfaces, #name, #num_clients, #options, #override_tlm, #packet_log_writer_pairs, #protocol_info, #raw_logger_pair, #read_count, #read_protocols, #read_queue_size, #read_raw_data, #read_raw_data_time, #reconnect_delay, #routers, #state, #stored_packet_log_writer_pairs, #target_names, #write_count, #write_protocols, #write_queue_size, #written_raw_data, #written_raw_data_time

Instance Method Summary collapse

Methods inherited from Interface

#_normalize_tlm, #_override, #_override_tlm, #_override_tlm_raw, #_write, #add_protocol, #as_json, #convert_data_to_packet, #convert_packet_to_data, #copy_to, #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

#_get_cnt, #_limits_group, #_validate_tlm_type, #build_cmd_output_string, #cmd, #cmd_implementation, #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, #connect_interface, #connect_router, #delete_config, #disable_limits, #disable_limits_group, #disconnect_interface, #disconnect_router, #enable_limits, #enable_limits_group, #get_all_cmd_info, #get_all_cmd_tlm_info, #get_all_commands, #get_all_commands_list, #get_all_interface_info, #get_all_router_info, #get_all_settings, #get_all_target_info, #get_all_telemetry, #get_all_telemetry_list, #get_all_tlm_info, #get_cmd_buffer, #get_cmd_cnt, #get_cmd_hazardous, #get_cmd_time, #get_cmd_value, #get_command, #get_interface, #get_interface_names, #get_item, #get_limits, #get_limits_events, #get_limits_groups, #get_limits_set, #get_limits_sets, #get_oldest_logfile, #get_out_of_limits, #get_overall_limits_state, #get_packet_derived_items, #get_packets, #get_parameter, #get_router, #get_router_names, #get_saved_config, #get_setting, #get_settings, #get_stale, #get_target, #get_target_list, #get_telemetry, #get_tlm_buffer, #get_tlm_cnt, #get_tlm_packet, #get_tlm_values, #inject_tlm, #limits_enabled?, #list_configs, #list_settings, #load_config, #normalize_tlm, #override_tlm, #save_config, #save_setting, #send_raw, #set_limits, #set_limits_set, #set_tlm, #set_tlm_process_args, #start_raw_logging_interface, #start_raw_logging_router, #stop_raw_logging_interface, #stop_raw_logging_router, #subscribe_packets, #tlm, #tlm_formatted, #tlm_process_args, #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



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

def initialize(sim_target_file)
  super()
  @connected = false
  @initialized = false
  @count_100hz = 0
  @next_tick_time = nil
  @pending_packets = []
  @sim_target_class = Cosmos.require_class sim_target_file
  @sim_target = nil
  @write_raw_allowed = false
  @raw_logger_pair = nil
  add_protocol(OverrideProtocol, [], :READ)
end

Instance Method Details

#connectObject

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



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cosmos/interfaces/simulated_target_interface.rb', line 42

def connect
  unless @initialized
    # Save the current time + 10 ms as the next expected tick time
    @next_tick_time = Time.now.sys + 0.01
    # Create Simulated Target Object
    @sim_target = @sim_target_class.new(@target_names[0])
    # Set telemetry rates
    @sim_target.set_rates

    @initialized = true
  end
  @connected = true
end

#connected?Boolean

Returns Whether the simulated target is connected (initialized).

Returns:

  • (Boolean)

    Whether the simulated target is connected (initialized)



57
58
59
# File 'lib/cosmos/interfaces/simulated_target_interface.rb', line 57

def connected?
  @connected
end

#disconnectObject

Disconnect from the simulator



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

def disconnect
  @connected = false
end

#raw_logger_pair=(raw_logger_pair) ⇒ Object

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



141
142
143
# File 'lib/cosmos/interfaces/simulated_target_interface.rb', line 141

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

#readPacket

Returns a simulated target packet from the simulator

Returns:

  • (Packet)

    Returns a simulated target packet from the simulator



62
63
64
65
66
67
68
69
70
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
# File 'lib/cosmos/interfaces/simulated_target_interface.rb', line 62

def read
  packet = nil
  if @connected
    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
      return packet unless packet == :STOP
    end

    while true
      # Calculate time to sleep to make ticks 10ms apart
      now = Time.now.sys
      delta = @next_tick_time - now
      if delta > 0.0
        sleep(delta) # Sleep up to 10 ms
        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 += 0.01
      @count_100hz += 1

      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

#write(packet) ⇒ Object

Parameters:

  • packet (Packet)

    Command packet to send to the simulator



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/cosmos/interfaces/simulated_target_interface.rb', line 120

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



136
137
138
# File 'lib/cosmos/interfaces/simulated_target_interface.rb', line 136

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