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 ]
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
- #heartbeats(net) ⇒ Object
-
#initialize(attrs) ⇒ Device
constructor
A new instance of Device.
- #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.
15 16 17 |
# File 'lib/wfa/device.rb', line 15 def initialize attrs update attrs end |
Class Method Details
.all(net) ⇒ Object
74 75 76 77 78 |
# File 'lib/wfa/device.rb', line 74 def all net @all ||= devices_list(net)[:devices].map do |device| new device end end |
.by_tag(net, tag) ⇒ Object
65 66 67 68 |
# File 'lib/wfa/device.rb', line 65 def by_tag net, tag tag = tag.sub(/^@/,'') all(net).detect {|device| device.tag == tag } end |
.device_list_cache ⇒ Object
61 62 63 |
# File 'lib/wfa/device.rb', line 61 def device_list_cache "#{ENV['HOME']}/.wfa_device_list.json" end |
.devices_list(net) ⇒ Object
80 81 82 |
# File 'lib/wfa/device.rb', line 80 def devices_list net get_cached_device_list || fetch_and_cache_device_list(net) end |
.fetch_and_cache_device_list(net) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/wfa/device.rb', line 90 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
70 71 72 |
# File 'lib/wfa/device.rb', line 70 def find net, id all(net).detect {|device| device.id == id } end |
.get_cached_device_list ⇒ Object
84 85 86 87 88 |
# File 'lib/wfa/device.rb', line 84 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
#heartbeats(net) ⇒ Object
39 40 41 |
# File 'lib/wfa/device.rb', line 39 def heartbeats net net.get(path("heartbeats")).body[:heartbeats].map{|h| WFA::Heartbeat.new(h) } end |
#path(*args) ⇒ Object
56 57 58 |
# File 'lib/wfa/device.rb', line 56 def path *args (["devices/#{id}"]+args).join("/") end |
#time_ago(timestamp) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/wfa/device.rb', line 26 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
19 20 21 22 23 24 |
# File 'lib/wfa/device.rb', line 19 def to_s "@%s: %s (%d) [%s] %s" % [ tag||"---", name, id, last_ip_addr, last_heartbeat ? time_ago(Time.parse(last_heartbeat)) : '' ] end |
#update(attrs) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/wfa/device.rb', line 48 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
43 44 45 46 |
# File 'lib/wfa/device.rb', line 43 def update_remote net, attrs update attrs net.put(path, name:name, tag:tag).success? end |