Module: MQTT::Homie::Network

Included in:
Device
Defined in:
lib/mqtt/homie/network.rb

Instance Method Summary collapse

Instance Method Details

#default_interfaceObject



15
16
17
# File 'lib/mqtt/homie/network.rb', line 15

def default_interface
  @default_interface ||= interfaces.values.find { |i| i[:default] }
end

#default_localipObject



6
7
8
9
# File 'lib/mqtt/homie/network.rb', line 6

def default_localip
  addr = default_interface[:addresses][0] if default_interface
  addr ? addr.ip_address : nil
end

#default_macObject



11
12
13
# File 'lib/mqtt/homie/network.rb', line 11

def default_mac
  (default_interface ? default_interface[:hwaddr] : nil) || Mac.addr
end

#interfacesObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mqtt/homie/network.rb', line 19

def interfaces
  @interfaces ||= begin
    interfaces = {}
    found = false
    Socket.getifaddrs.each do |ifaddr|
      ifname = ifaddr.name
      data = interfaces[ifname] ||= { addresses: [] }
      next unless addr = ifaddr.addr
      data[:addresses].push addr if (addr.ipv4? || addr.ipv6?) && usable_address?(addr)
      data[:hwaddr] = $1 if addr.inspect.match(/hwaddr=([0-9a-fA-F:]+)/)  # doesn't work on windows
      data[:default] = true unless found
      data[:name] = ifname
      found = true
    end
    interfaces
  end
end

#usable_address?(addr) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/mqtt/homie/network.rb', line 37

def usable_address?(addr)
  !(addr.ipv4_loopback? || addr.ipv6_loopback? || addr.ipv4_multicast? || addr.ipv6_linklocal?)
end