Module: RobotLab::Discovery::Browser

Defined in:
lib/robot_lab/discovery/browser.rb

Class Method Summary collapse

Class Method Details

.browse(timeout: 3) ⇒ Object



9
10
11
12
13
# File 'lib/robot_lab/discovery/browser.rb', line 9

def self.browse(timeout: 3)
  ZeroConf.browse(SERVICE_TYPE, timeout:)
          .filter_map { |r| parse_response(r) }
          .uniq(&:name)
end

.find(name, timeout: 5) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/robot_lab/discovery/browser.rb', line 15

def self.find(name, timeout: 5)
  deadline = Time.now + timeout
  loop do
    remaining = deadline - Time.now
    return nil if remaining <= 0
    result = browse(timeout: [remaining, 1].min).find { |r| r.name == name }
    return result if result
  end
end

.find_by_capability(capability, timeout: 3) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/robot_lab/discovery/browser.rb', line 25

def self.find_by_capability(capability, timeout: 3)
  label   = RobotLab::Discovery.dns_label(capability)
  subtype = "_#{label}._sub.#{SERVICE_TYPE}"
  ZeroConf.browse(subtype, timeout:)
          .filter_map { |r| parse_response(r) }
          .uniq(&:name)
end

.list_capabilities(timeout: 3) ⇒ Object



33
34
35
36
37
38
# File 'lib/robot_lab/discovery/browser.rb', line 33

def self.list_capabilities(timeout: 3)
  browse(timeout:)
    .flat_map(&:capabilities)
    .uniq
    .sort
end