Skip to content

Commit

Permalink
Fix router status, tool initial load, preidentified protocol, empty s…
Browse files Browse the repository at this point in the history
…et to complete for extractor
  • Loading branch information
ryanmelt committed Jan 9, 2021
1 parent 6d1f37a commit 4018499
Show file tree
Hide file tree
Showing 15 changed files with 194 additions and 134 deletions.
11 changes: 6 additions & 5 deletions lib/cosmos/api/interface_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

require 'cosmos/models/interface_model'
require 'cosmos/models/interface_status_model'
require 'cosmos/topics/interface_topics'

module Cosmos
module Api
Expand Down Expand Up @@ -62,23 +63,23 @@ def get_interface_names(scope: $cosmos_scope, token: $cosmos_token)
# @param params [Array] Parameters to pass to the interface.
def connect_interface(interface_name, *params, scope: $cosmos_scope, token: $cosmos_token)
authorize(permission: 'system_set', interface_name: interface_name, scope: scope, token: token)
Store.instance.write_interface(interface_name, {'connect' => true}, scope: scope)
InterfaceTopics.connect_interface(interface_name, scope: scope)
end

# Disconnects from an interface and kills its telemetry gathering thread
#
# @param interface_name (see #connect_interface)
def disconnect_interface(interface_name, scope: $cosmos_scope, token: $cosmos_token)
authorize(permission: 'system_set', interface_name: interface_name, scope: scope, token: token)
Store.instance.write_interface(interface_name, {'disconnect' => true }, scope: scope)
InterfaceTopics.disconnect_interface(interface_name, scope: scope)
end

# @param interface_name (see #connect_interface)
# @return [String] The state of the interface which is one of 'CONNECTED',
# 'ATTEMPTING' or 'DISCONNECTED'.
def interface_state(interface_name, scope: $cosmos_scope, token: $cosmos_token)
authorize(permission: 'system', interface_name: interface_name, scope: scope, token: token)
CmdTlmServer.interfaces.state(interface_name)
InterfaceStatusModel.get(name: interface_name, scope: scope)['state']
end

# Associates a target and all its commands and telemetry with a particular
Expand Down Expand Up @@ -112,7 +113,7 @@ def get_interface_info(interface_name, scope: $cosmos_scope, token: $cosmos_toke
authorize(permission: 'system', interface_name: interface_name, scope: scope, token: token)
int = InterfaceStatusModel.get(name: interface_name, scope: scope)
return [int['state'], int['clients'], int['txsize'], int['rxsize'],
int['txbytes'], int['rxbytes'], int['cmdcnt'], int['tlmcnt']]
int['txbytes'], int['rxbytes'], int['txcnt'], int['rxcnt']]
end

# Get information about all interfaces
Expand All @@ -126,7 +127,7 @@ def get_all_interface_info(scope: $cosmos_scope, token: $cosmos_token)
info = []
InterfaceStatusModel.all(scope: scope).each do |int_name, int|
info << [int['name'], int['state'], int['clients'], int['txsize'], int['rxsize'],
int['txbytes'], int['rxbytes'], int['cmdcnt'], int['tlmcnt']]
int['txbytes'], int['rxbytes'], int['txcnt'], int['rxcnt']]
end
info.sort! {|a,b| a[0] <=> b[0] }
info
Expand Down
18 changes: 9 additions & 9 deletions lib/cosmos/api/router_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

require 'cosmos/models/router_model'
require 'cosmos/models/router_status_model'
require 'cosmos/topics/router_topics'

module Cosmos
module Api
Expand Down Expand Up @@ -46,28 +47,25 @@ def get_router_names(scope: $cosmos_scope, token: $cosmos_token)
# @param params [Array] Parameters to pass to the router.
def connect_router(router_name, *params, scope: $cosmos_scope, token: $cosmos_token)
authorize(permission: 'system_set', router_name: router_name, scope: scope, token: token)
CmdTlmServer.routers.connect(router_name, *params)
nil
RouterTopics.connect_router(router_name, scope: scope)
end

# Disconnects a router and kills its command gathering thread
#
# @param router_name (see #connect_router)
def disconnect_router(router_name, scope: $cosmos_scope, token: $cosmos_token)
authorize(permission: 'system_set', router_name: router_name, scope: scope, token: token)
CmdTlmServer.routers.disconnect(router_name)
nil
RouterTopics.disconnect_router(router_name, scope: scope)
end

# @param router_name (see #connect_router)
# @return [String] The state of the router which is one of 'CONNECTED',
# 'ATTEMPTING' or 'DISCONNECTED'.
def router_state(router_name, scope: $cosmos_scope, token: $cosmos_token)
authorize(permission: 'system', router_name: router_name, scope: scope, token: token)
CmdTlmServer.routers.state(router_name)
RouterStatusModel.get(name: router_name, scope: scope)['state']
end


# Get information about a router
#
# @param router_name [String] Router name
Expand All @@ -77,7 +75,9 @@ def router_state(router_name, scope: $cosmos_scope, token: $cosmos_token)
# Pkts sent] for the router
def get_router_info(router_name, scope: $cosmos_scope, token: $cosmos_token)
authorize(permission: 'system', router_name: router_name, scope: scope, token: token)
RouterStatusModel.get(name: router_name, scope: scope)
int = RouterStatusModel.get(name: router_name, scope: scope)
return [int['state'], int['clients'], int['txsize'], int['rxsize'],
int['txbytes'], int['rxbytes'], int['rxcnt'], int['txcnt']]
end

# Get information about all routers
Expand All @@ -89,9 +89,9 @@ def get_router_info(router_name, scope: $cosmos_scope, token: $cosmos_token)
def get_all_router_info(scope: $cosmos_scope, token: $cosmos_token)
authorize(permission: 'system', scope: scope, token: token)
info = []
Store.instance.get_routers(scope: scope).each do |int|
RouterStatusModel.all(scope: scope).each do |int_name, int|
info << [int['name'], int['state'], int['clients'], int['txsize'], int['rxsize'],\
int['txbytes'], int['rxbytes'], int['cmdcnt'], int['tlmcnt']]
int['txbytes'], int['rxbytes'], int['rxcnt'], int['txcnt']]
end
info.sort! {|a,b| a[0] <=> b[0] }
info
Expand Down
4 changes: 2 additions & 2 deletions lib/cosmos/interfaces/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ def as_json
config['rxsize'] = @read_queue_size
config['txbytes'] = @bytes_written
config['rxbytes'] = @bytes_read
config['cmdcnt'] = @write_count
config['tlmcnt'] = @read_count
config['txcnt'] = @write_count
config['rxcnt'] = @read_count
config
end

Expand Down
12 changes: 7 additions & 5 deletions lib/cosmos/interfaces/protocols/preidentified_protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
# copyright holder

require 'cosmos/interfaces/protocols/burst_protocol'
require 'cosmos/packet_logs/packet_log_reader'

module Cosmos
# Delineates packets using the COSMOS preidentification system
class PreidentifiedProtocol < BurstProtocol

COSMOS4_STORED_FLAG_MASK = 0x80
COSMOS4_EXTRA_FLAG_MASK = 0x40

# @param sync_pattern (see BurstProtocol#initialize)
# @param max_length [Integer] The maximum allowed value of the length field
# @param allow_empty_data [true/false/nil] See Protocol#initialize
Expand Down Expand Up @@ -61,10 +63,10 @@ def write_packet(packet)
@write_packet_name = 'UNKNOWN' unless @write_packet_name
if @mode == 4 # COSMOS4.3+ Protocol
@write_flags = 0
@write_flags |= PacketLogReader::COSMOS4_STORED_FLAG_MASK if packet.stored
@write_flags |= COSMOS4_STORED_FLAG_MASK if packet.stored
@write_extra = nil
if packet.extra
@write_flags |= PacketLogReader::COSMOS4_EXTRA_FLAG_MASK
@write_flags |= COSMOS4_EXTRA_FLAG_MASK
@write_extra = packet.extra.to_json
end
end
Expand Down Expand Up @@ -141,9 +143,9 @@ def reduce_to_single_packet
flags = @data[0].unpack('C')[0] # byte
@data.replace(@data[1..-1])
@read_stored = false
@read_stored = true if (flags & PacketLogReader::COSMOS4_STORED_FLAG_MASK) != 0
@read_stored = true if (flags & COSMOS4_STORED_FLAG_MASK) != 0
@read_extra = nil
if (flags & PacketLogReader::COSMOS4_EXTRA_FLAG_MASK) != 0
if (flags & COSMOS4_EXTRA_FLAG_MASK) != 0
@reduction_state = :NEED_EXTRA
else
@reduction_state = :FLAGS_REMOVED
Expand Down
32 changes: 17 additions & 15 deletions lib/cosmos/microservices/interface_microservice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def start
def run
InterfaceTopics.receive_commands(@interface, scope: @scope) do |topic, msg_hash|
# Check for a raw write to the interface
if topic =~ /CMDINTERFACE/
if topic =~ /CMDINTERFACE/ or topic =~ /CMDROUTER/
if msg_hash['connect']
Logger.info "#{@interface.name}: Connect requested"
@tlm.attempting()
Expand Down Expand Up @@ -171,22 +171,24 @@ def run
next 'SUCCESS'
end

target_name = msg_hash["target_name"]
packet_name = msg_hash["packet_name"]
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"]
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, scope: @scope)
next 'SUCCESS'
rescue => e
Logger.error "#{@router.name}: #{e.formatted}"
next e.message
begin
@router.write(packet)
RouterStatusModel.set(@router.as_json, scope: @scope)
next 'SUCCESS'
rescue => e
Logger.error "#{@router.name}: #{e.formatted}"
next e.message
end
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions lib/cosmos/models/interface_status_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class InterfaceStatusModel < Model
attr_accessor :rxsize
attr_accessor :txbytes
attr_accessor :rxbytes
attr_accessor :cmdcnt
attr_accessor :tlmcnt
attr_accessor :txcnt
attr_accessor :rxcnt

def initialize(
name:,
Expand All @@ -41,8 +41,8 @@ def initialize(
rxsize: 0,
txbytes: 0,
rxbytes: 0,
cmdcnt: 0,
tlmcnt: 0,
txcnt: 0,
rxcnt: 0,
updated_at: nil,
plugin: nil,
scope:)
Expand All @@ -58,8 +58,8 @@ def initialize(
@rxsize = rxsize
@txbytes = txbytes
@rxbytes = rxbytes
@cmdcnt = cmdcnt
@tlmcnt = tlmcnt
@txcnt = txcnt
@rxcnt = rxcnt
end

def as_json
Expand All @@ -71,8 +71,8 @@ def as_json
'rxsize' => @rxsize,
'txbytes' => @txbytes,
'rxbytes' => @rxbytes,
'cmdcnt' => @cmdcnt,
'tlmcnt' => @tlmcnt,
'txcnt' => @txcnt,
'rxcnt' => @rxcnt,
'plugin' => @plugin,
'updated_at' => @updated_at
}
Expand Down
1 change: 0 additions & 1 deletion lib/cosmos/models/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def create(update: false, force: false)
end
end
@updated_at = Time.now.to_nsec_from_epoch
STDOUT.puts @primary_key, @name, JSON.generate(self.as_json).to_s
Store.hset(@primary_key, @name, JSON.generate(self.as_json))
end

Expand Down
64 changes: 32 additions & 32 deletions lib/cosmos/models/tool_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,52 +136,52 @@ def self.unordered_all(scope: nil)
if tools.length < 1
tools = {}
tools['CmdTlmServer'] = {
name: 'CmdTlmServer',
icon: 'mdi-server-network',
url: '/cmd-tlm-server',
position: 1,
'name' => 'CmdTlmServer',
'icon' => 'mdi-server-network',
'url' => '/cmd-tlm-server',
'position' => 1,
}
tools['Limits Monitor'] = {
name: 'Limits Monitor',
icon: 'mdi-alert',
url: '/limits-monitor',
position: 2,
'name' => 'Limits Monitor',
'icon' => 'mdi-alert',
'url' => '/limits-monitor',
'position' => 2,
}
tools['Command Sender'] = {
name: 'Command Sender',
icon: 'mdi-satellite-uplink',
url: '/command-sender',
position: 3,
'name' => 'Command Sender',
'icon' => 'mdi-satellite-uplink',
'url' => '/command-sender',
'position' => 3,
}
tools['Script Runner'] = {
name: 'Script Runner',
icon: 'mdi-run-fast',
url: '/script-runner',
position: 4,
'name' => 'Script Runner',
'icon' => 'mdi-run-fast',
'url' => '/script-runner',
'position' => 4,
}
tools['Packet Viewer'] = {
name: 'Packet Viewer',
icon: 'mdi-format-list-bulleted',
url: '/packet-viewer',
position: 5,
'name' => 'Packet Viewer',
'icon' => 'mdi-format-list-bulleted',
'url' => '/packet-viewer',
'position' => 5,
}
tools['Telemetry Viewer'] = {
name: 'Telemetry Viewer',
icon: 'mdi-monitor-dashboard',
url: '/telemetry-viewer',
position: 6,
'name' => 'Telemetry Viewer',
'icon' => 'mdi-monitor-dashboard',
'url' => '/telemetry-viewer',
'position' => 6,
}
tools['Telemetry Grapher'] = {
name: 'Telemetry Grapher',
icon: 'mdi-chart-line',
url: '/telemetry-grapher',
position: 7,
'name' => 'Telemetry Grapher',
'icon' => 'mdi-chart-line',
'url' => '/telemetry-grapher',
'position' => 7,
}
tools['Data Extractor'] = {
name: 'Data Extractor',
icon: 'mdi-archive-arrow-down',
url: '/data-extractor',
position: 8,
'name' => 'Data Extractor',
'icon' => 'mdi-archive-arrow-down',
'url' => '/data-extractor',
'position' => 8,
}
tools.each do |name, tool|
Store.hset("#{scope}__#{PRIMARY_KEY}", name, JSON.generate(tool))
Expand Down
8 changes: 8 additions & 0 deletions lib/cosmos/topics/interface_topics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,13 @@ def self.receive_commands(interface, scope:)
end
end
end

def self.connect_interface(interface_name, scope:)
Store.write_topic("#{scope}__CMDINTERFACE__#{interface_name}", {'connect' => true})
end

def self.disconnect_interface(interface_name, scope:)
Store.write_topic("#{scope}__CMDINTERFACE__#{interface_name}", {'disconnect' => true})
end
end
end
8 changes: 8 additions & 0 deletions lib/cosmos/topics/router_topics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,13 @@ def self.route_command(packet, target_names, scope:)
end
end

def self.connect_router(router_name, scope:)
Store.write_topic("#{scope}__CMDROUTER__#{router_name}", {'connect' => true})
end

def self.disconnect_router(router_name, scope:)
Store.write_topic("#{scope}__CMDROUTER__#{router_name}", {'disconnect' => true})
end

end
end
Loading

0 comments on commit 4018499

Please sign in to comment.