Class: OpenC3::RouterTlmHandlerThread

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

Instance Method Summary collapse

Constructor Details

#initialize(router, tlm, logger: nil, scope:) ⇒ RouterTlmHandlerThread

Returns a new instance of RouterTlmHandlerThread.



205
206
207
208
209
210
211
# File 'lib/openc3/microservices/interface_microservice.rb', line 205

def initialize(router, tlm, logger: nil, scope:)
  @router = router
  @tlm = tlm
  @scope = scope
  @logger = logger
  @logger = Logger unless @logger
end

Instance Method Details

#graceful_killObject



226
227
228
# File 'lib/openc3/microservices/interface_microservice.rb', line 226

def graceful_kill
  RouterTopic.shutdown(@router, scope: @scope)
end

#runObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/openc3/microservices/interface_microservice.rb', line 230

def run
  RouterTopic.receive_telemetry(@router, scope: @scope) do |topic, msg_hash|
    # Check for commands to the router itself
    if /CMD}ROUTER/.match?(topic)
      if msg_hash['shutdown']
        @logger.info "#{@router.name}: Shutdown requested"
        return
      end
      if msg_hash['connect']
        @logger.info "#{@router.name}: Connect requested"
        params = []
        if msg_hash['params']
          params = JSON.parse(msg_hash['params'], :allow_nan => true, :create_additions => true)
        end
        @router = @tlm.attempting(*params)
      end
      if msg_hash['disconnect']
        @logger.info "#{@router.name}: Disconnect requested"
        @tlm.disconnect(false)
      end
      if msg_hash.key?('log_raw')
        if msg_hash['log_raw'] == 'true'
          @logger.info "#{@router.name}: Enable raw logging"
          @router.start_raw_logging
        else
          @logger.info "#{@router.name}: Disable raw logging"
          @router.stop_raw_logging
        end
      end
      if msg_hash.key?('router_cmd')
        params = JSON.parse(msg_hash['router_cmd'], allow_nan: true, create_additions: true)
        begin
          @logger.info "#{@router.name}: router_cmd: #{params['cmd_name']} #{params['cmd_params'].join(' ')}"
          @router.interface_cmd(params['cmd_name'], params['cmd_params'])
        rescue => e
          @logger.error "#{@router.name}: router_cmd: #{e.formatted}"
          next e.message
        end
        next 'SUCCESS'
      end
      if msg_hash.key?('protocol_cmd')
        params = JSON.parse(msg_hash['protocol_cmd'], allow_nan: true, create_additions: true)
        begin
          @logger.info "#{@router.name}: protocol_cmd: #{params['cmd_name']} #{params['cmd_params'].join(' ')} read_write: #{params['read_write']} index: #{params['index']}"
          @router.protocol_cmd(params['cmd_name'], params['cmd_params'], read_write: params['read_write'], index: params['index'])
        rescue => e
          @logger.error "#{@router.name}: protoco_cmd: #{e.formatted}"
          next e.message
        end
        next 'SUCCESS'
      end
      next 'SUCCESS'
    end

    if @router.connected?
      target_name = msg_hash["target_name"]
      packet_name = msg_hash["packet_name"]

      packet = System.telemetry.packet(target_name, packet_name)
      packet.stored = ConfigParser.handle_true_false(msg_hash["stored"])
      packet.received_time = Time.from_nsec_from_epoch(msg_hash["time"].to_i)
      packet.received_count = msg_hash["received_count"].to_i
      packet.buffer = msg_hash["buffer"]

      begin
        @router.write(packet)
        RouterStatusModel.set(@router.as_json(:allow_nan => true), scope: @scope)
        next 'SUCCESS'
      rescue => e
        @logger.error "#{@router.name}: #{e.formatted}"
        next e.message
      end
    end
  end
end

#startObject



213
214
215
216
217
218
219
220
# File 'lib/openc3/microservices/interface_microservice.rb', line 213

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

#stopObject



222
223
224
# File 'lib/openc3/microservices/interface_microservice.rb', line 222

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