Class: Cosmos::Interfaces

Inherits:
Connections show all
Defined in:
lib/cosmos/tools/cmd_tlm_server/interfaces.rb

Overview

Controls the interfaces as defined in the Command/Telemetry configuration file. Interfaces provide the connection between the Command and Telemetry Server and targets. They send command packets to and receive telemetry packets from targets.

Instance Method Summary collapse

Methods inherited from Connections

#all, #clear_counters, #connect, #disconnect, #names, #start, #start_raw_logging, #state, #stop, #stop_raw_logging

Constructor Details

#initialize(cmd_tlm_server_config, identified_packet_callback = nil) ⇒ Interfaces

Returns a new instance of Interfaces.

Parameters:

  • cmd_tlm_server_config (CmdTlmServerConfig)

    The configuration which defines all the routers

  • identified_packet_callback (#call(Packet)) (defaults to: nil)

    Callback which is called when a packet has been received from the interface and identified.



25
26
27
28
# File 'lib/cosmos/tools/cmd_tlm_server/interfaces.rb', line 25

def initialize(cmd_tlm_server_config, identified_packet_callback = nil)
  super(:INTERFACES, cmd_tlm_server_config)
  @identified_packet_callback = identified_packet_callback
end

Instance Method Details

#map_all_targets(interface_name) ⇒ Object

Determines all targets in the system and maps them to the given interface

Parameters:

  • interface_name (String)

    The interface to map all targets to



33
34
35
36
37
38
39
40
# File 'lib/cosmos/tools/cmd_tlm_server/interfaces.rb', line 33

def map_all_targets(interface_name)
  interface = @config.interfaces[interface_name.upcase]
  raise "Unknown interface: #{interface_name}" unless interface
  System.targets.each do |target_name, target|
    target.interface = interface
    interface.target_names << target_name
  end
end

#map_target(target_name, interface_name) ⇒ Object

Maps a target to an interface and unmaps the target from its existing interface if present.

Parameters:

  • target_name (String)

    The name of the target to map

  • interface_name (String)

    The name of the interface to map the target to



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cosmos/tools/cmd_tlm_server/interfaces.rb', line 48

def map_target(target_name, interface_name)
  # Find the new interface
  new_interface = @config.interfaces[interface_name.upcase]
  raise "Unknown interface: #{interface_name}" unless new_interface

  # Find the target
  target = System.targets[target_name.upcase]
  raise "Unknown target: #{target_name}" unless target

  # Find the old interface
  old_interface = System.targets[target_name.upcase].interface

  # Remove target from old interface
  old_interface.target_names.delete(target_name.upcase) if old_interface

  # Add target to new interface
  new_interface.target_names << target_name.upcase unless new_interface.target_names.include? target_name.upcase

  # Update targets
  System.targets[target_name.upcase].interface = new_interface
end

#recreate(interface_name, *params) ⇒ Object

Recreate an interface with new initialization parameters

Parameters:

  • interface_name (String)

    Name of the interface

  • params (Array)

    Array of parameters to pass to the interface constructor



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
# File 'lib/cosmos/tools/cmd_tlm_server/interfaces.rb', line 75

def recreate(interface_name, *params)
  interface = @config.interfaces[interface_name.upcase]
  raise "Unknown interface: #{interface_name}" unless interface

  # Build New Interface
  new_interface = interface.class.new(*params)
  interface.copy_to(new_interface)

  # Replace interface for targets
  System.targets.each do |target_name, target|
    target.interface = new_interface if target.interface == interface
  end

  # Replace interface for routers
  @config.routers.each do |router_name, router|
    if router.interfaces.include?(interface)
      router.interfaces.delete(interface)
      router.interfaces << new_interface
    end
  end

  # Replace interface in @interfaces array
  @config.interfaces[interface_name.upcase] = new_interface

  # Make sure there is no thread
  stop_thread(interface)

  return new_interface
end