Class: Cosmos::InterfaceTopic

Inherits:
Topic show all
Defined in:
lib/cosmos/topics/interface_topic.rb

Class Method Summary collapse

Methods inherited from Topic

clear_topics, initialize_streams, read_topics

Class Method Details

.connect_interface(interface_name, scope:) ⇒ Object



51
52
53
# File 'lib/cosmos/topics/interface_topic.rb', line 51

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

.disconnect_interface(interface_name, scope:) ⇒ Object



55
56
57
# File 'lib/cosmos/topics/interface_topic.rb', line 55

def self.disconnect_interface(interface_name, scope:)
  Store.write_topic("{#{scope}__CMD}INTERFACE__#{interface_name}", { 'disconnect' => 'true' }, '*', 100)
end

.receive_commands(interface, scope:) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cosmos/topics/interface_topic.rb', line 35

def self.receive_commands(interface, scope:)
  while true
    Store.read_topics(InterfaceTopic.topics(interface, scope: scope)) do |topic, msg_id, msg_hash, redis|
      result = yield topic, msg_hash
      ack_topic = topic.split("__")
      ack_topic[1] = 'ACK' + ack_topic[1]
      ack_topic = ack_topic.join("__")
      Store.write_topic(ack_topic, { 'result' => result, 'id' => msg_id }, '*', 100)
    end
  end
end

.shutdown(interface, scope:) ⇒ Object



67
68
69
70
71
# File 'lib/cosmos/topics/interface_topic.rb', line 67

def self.shutdown(interface, scope:)
  Store.write_topic("{#{scope}__CMD}INTERFACE__#{interface.name}", { 'shutdown' => 'true' }, '*', 100)
  sleep 1 # Give some time for the interface to shutdown
  InterfaceTopic.clear_topics(InterfaceTopic.topics(interface, scope: scope))
end

.start_raw_logging(interface_name, scope:) ⇒ Object



59
60
61
# File 'lib/cosmos/topics/interface_topic.rb', line 59

def self.start_raw_logging(interface_name, scope:)
  Store.write_topic("{#{scope}__CMD}INTERFACE__#{interface_name}", { 'log_raw' => 'true' }, '*', 100)
end

.stop_raw_logging(interface_name, scope:) ⇒ Object



63
64
65
# File 'lib/cosmos/topics/interface_topic.rb', line 63

def self.stop_raw_logging(interface_name, scope:)
  Store.write_topic("{#{scope}__CMD}INTERFACE__#{interface_name}", { 'log_raw' => 'false' }, '*', 100)
end

.topics(interface, scope:) ⇒ Object

Generate a list of topics for this interface. This includes the interface itself and all the targets which are assigned to this interface.



26
27
28
29
30
31
32
33
# File 'lib/cosmos/topics/interface_topic.rb', line 26

def self.topics(interface, scope:)
  topics = []
  topics << "{#{scope}__CMD}INTERFACE__#{interface.name}"
  interface.target_names.each do |target_name|
    topics << "{#{scope}__CMD}TARGET__#{target_name}"
  end
  topics
end

.write_raw(interface_name, data, scope:) ⇒ Object



47
48
49
# File 'lib/cosmos/topics/interface_topic.rb', line 47

def self.write_raw(interface_name, data, scope:)
  Store.write_topic("{#{scope}__CMD}INTERFACE__#{interface_name}", { 'raw' => data }, '*', 100)
end