Class: WFA::Device
- Inherits:
-
Object
- Object
- WFA::Device
- Defined in:
- lib/wfa/device.rb
Constant Summary collapse
- WFDEVICE_PORT_FILE =
'/etc/wfdevice_tunnel_port.sh'
- FING_CSV_FILE =
'/var/fing.csv'
- ATTRIBUTES =
[ :id, :name, :tag, :tunnel_port, :macaddr, :secret_key, :state, :vendor, :hostname, :last_heartbeat, :last_ip_addr, :current_network ]
Class Method Summary collapse
- .all(net) ⇒ Object
- .by_tag(net, tag) ⇒ Object
- .device_list_cache ⇒ Object
- .devices_list(net) ⇒ Object
- .fetch_and_cache_device_list(net) ⇒ Object
- .find(net, id) ⇒ Object
- .get_cached_device_list ⇒ Object
Instance Method Summary collapse
- #display_name ⇒ Object
- #heartbeats(net) ⇒ Object
-
#initialize(attrs) ⇒ Device
constructor
A new instance of Device.
- #network_ssid ⇒ Object
- #path(*args) ⇒ Object
- #time_ago(timestamp) ⇒ Object
- #to_s ⇒ Object
- #update(attrs) ⇒ Object
- #update_remote(net, attrs) ⇒ Object
Constructor Details
#initialize(attrs) ⇒ Device
Returns a new instance of Device.
16 17 18 |
# File 'lib/wfa/device.rb', line 16 def initialize attrs update attrs end |
Class Method Details
.all(net) ⇒ Object
83 84 85 86 87 |
# File 'lib/wfa/device.rb', line 83 def all net @all ||= devices_list(net)[:devices].map do |device| new device end end |
.by_tag(net, tag) ⇒ Object
74 75 76 77 |
# File 'lib/wfa/device.rb', line 74 def by_tag net, tag tag = tag.sub(/^@/,'') all(net).detect {|device| device.tag == tag } end |
.device_list_cache ⇒ Object
70 71 72 |
# File 'lib/wfa/device.rb', line 70 def device_list_cache "#{ENV['HOME']}/.wfa_device_list.json" end |
.devices_list(net) ⇒ Object
89 90 91 |
# File 'lib/wfa/device.rb', line 89 def devices_list net get_cached_device_list || fetch_and_cache_device_list(net) end |
.fetch_and_cache_device_list(net) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/wfa/device.rb', line 99 def fetch_and_cache_device_list net net.get('devices').body.tap do |ds| f = File.open(device_list_cache, "w") f.write(Yajl::Encoder.encode(ds)) f.close end end |
.find(net, id) ⇒ Object
79 80 81 |
# File 'lib/wfa/device.rb', line 79 def find net, id all(net).detect {|device| device.id == id } end |
.get_cached_device_list ⇒ Object
93 94 95 96 97 |
# File 'lib/wfa/device.rb', line 93 def get_cached_device_list File.exists?(device_list_cache) \ ? Yajl::Parser.parse(File.read(device_list_cache), symbolize_keys:true) \ : raise('no cache!') end |
Instance Method Details
#display_name ⇒ Object
24 25 26 |
# File 'lib/wfa/device.rb', line 24 def display_name [name, network_ssid].compact.join(" / ") end |
#heartbeats(net) ⇒ Object
48 49 50 |
# File 'lib/wfa/device.rb', line 48 def heartbeats net net.get(path("heartbeats")).body[:heartbeats].map{|h| WFA::Heartbeat.new(h) } end |
#network_ssid ⇒ Object
20 21 22 |
# File 'lib/wfa/device.rb', line 20 def network_ssid current_network && current_network[:ssid] end |
#path(*args) ⇒ Object
65 66 67 |
# File 'lib/wfa/device.rb', line 65 def path *args (["devices/#{id}"]+args).join("/") end |
#time_ago(timestamp) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/wfa/device.rb', line 35 def time_ago seconds = (Time.now - ).to_i minutes = seconds / 60 return "#{seconds}s" unless minutes > 1 hours = minutes / 60 return "#{minutes}m" unless hours > 1 days = hours / 24 return "#{hours}h" unless days > 1 weeks = days / 7 return "#{days}d" unless weeks > 1 "#{weeks}w" end |
#to_s ⇒ Object
28 29 30 31 32 33 |
# File 'lib/wfa/device.rb', line 28 def to_s "@%s: %s (%d) [%s] %s" % [ tag||"---", display_name, id, last_ip_addr, last_heartbeat ? time_ago(Time.parse(last_heartbeat)) : '' ] end |
#update(attrs) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/wfa/device.rb', line 57 def update attrs ATTRIBUTES.each { |attr| if !attrs[attr].nil? && send(attr) != attrs[attr] send("#{attr}=", attrs[attr]) end } end |
#update_remote(net, attrs) ⇒ Object
52 53 54 55 |
# File 'lib/wfa/device.rb', line 52 def update_remote net, attrs update attrs net.put(path, name:name, tag:tag).success? end |