Class: Cosmos::InterfaceCmdHandlerThread

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmos/microservices/interface_microservice.rb

Instance Method Summary collapse

Constructor Details

#initialize(interface, tlm, scope:) ⇒ InterfaceCmdHandlerThread

Returns a new instance of InterfaceCmdHandlerThread.



33
34
35
36
37
# File 'lib/cosmos/microservices/interface_microservice.rb', line 33

def initialize(interface, tlm, scope:)
  @interface = interface
  @tlm = tlm
  @scope = scope
end

Instance Method Details

#graceful_killObject



52
53
54
# File 'lib/cosmos/microservices/interface_microservice.rb', line 52

def graceful_kill
  InterfaceTopic.shutdown(@interface, scope: @scope)
end

#runObject



56
57
58
59
60
61
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/cosmos/microservices/interface_microservice.rb', line 56

def run
  InterfaceTopic.receive_commands(@interface, scope: @scope) do |topic, msg_hash|
    # Check for a raw write to the interface
    if topic =~ /CMD}INTERFACE/
      if msg_hash['shutdown']
        Logger.info "#{@interface.name}: Shutdown requested"
        return
      end
      if msg_hash['connect']
        Logger.info "#{@interface.name}: Connect requested"
        @tlm.attempting()
        next 'SUCCESS'
      end
      if msg_hash['disconnect']
        Logger.info "#{@interface.name}: Disconnect requested"
        @tlm.disconnect(false)
        next 'SUCCESS'
      end
      if msg_hash['raw']
        Logger.info "#{@interface.name}: Write raw"
        # A raw interface write results in an UNKNOWN packet
        command = System.commands.packet('UNKNOWN', 'UNKNOWN')
        command.received_count += 1
        command = command.clone
        command.buffer = msg_hash['raw']
        command.received_time = Time.now
        CommandTopic.write_packet(command, scope: @scope)
        @interface.write_raw(msg_hash['raw'])
        next 'SUCCESS'
      end
      if msg_hash['inject_tlm']
        Logger.info "#{@interface.name}: Inject telemetry" if msg_hash['log']
        @tlm.inject_tlm(msg_hash)
        next 'SUCCESS'
      end
      if msg_hash.key?('log_raw')
        if msg_hash['log_raw'] == 'true'
          Logger.info "#{@interface.name}: Enable raw logging"
          @interface.start_raw_logging
        else
          Logger.info "#{@interface.name}: Disable raw logging"
          @interface.stop_raw_logging
        end
        next 'SUCCESS'
      end
    end

    target_name = msg_hash['target_name']
    cmd_name = msg_hash['cmd_name']
    cmd_params = nil
    cmd_buffer = nil
    hazardous_check = nil
    if msg_hash['cmd_params']
      cmd_params = JSON.parse(msg_hash['cmd_params'])
      range_check = ConfigParser.handle_true_false(msg_hash['range_check'])
      raw = ConfigParser.handle_true_false(msg_hash['raw'])
      hazardous_check = ConfigParser.handle_true_false(msg_hash['hazardous_check'])
    elsif msg_hash['cmd_buffer']
      cmd_buffer = msg_hash['cmd_buffer']
    end

    begin
      begin
        if cmd_params
          command = System.commands.build_cmd(target_name, cmd_name, cmd_params, range_check, raw)
        elsif cmd_buffer
          if target_name
            command = System.commands.identify(cmd_buffer, [target_name])
          else
            command = System.commands.identify(cmd_buffer, @target_names)
          end
          unless command
            command = System.commands.packet('UNKNOWN', 'UNKNOWN')
            command.received_count += 1
            command = command.clone
            command.buffer = cmd_buffer
          end
        else
          raise "Invalid command received:\n #{msg_hash}"
        end
        command.received_time = Time.now
      rescue => e
        Logger.error "#{@interface.name}: #{e.formatted}"
        next e.message
      end

      if hazardous_check
        hazardous, hazardous_description = System.commands.cmd_pkt_hazardous?(command)
        # Return back the error, description, and the formatted command
        # This allows the error handler to simply re-send the command
        # TODO: Should we set target_name, cmd_name, and cmd_params instead?
        next "HazardousError\n#{hazardous_description}\n#{System.commands.format(command)}" if hazardous
      end

      begin
        @interface.write(command)
        CommandTopic.write_packet(command, scope: @scope)
        CommandDecomTopic.write_packet(command, scope: @scope)
        InterfaceStatusModel.set(@interface.as_json, scope: @scope)
        next 'SUCCESS'
      rescue => e
        Logger.error "#{@interface.name}: #{e.formatted}"
        next e.message
      end
    rescue => e
      Logger.error "#{@interface.name}: #{e.formatted}"
      next e.message
    end
  end
end

#startObject



39
40
41
42
43
44
45
46
# File 'lib/cosmos/microservices/interface_microservice.rb', line 39

def start
  @thread = Thread.new do
    run()
  rescue Exception => err
    Logger.error "#{@interface.name}: Command handler thread died: #{err.formatted}"
    raise err
  end
end

#stopObject



48
49
50
# File 'lib/cosmos/microservices/interface_microservice.rb', line 48

def stop
  Cosmos.kill_thread(self, @thread)
end