Class: Apc4r::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/apc4r/status.rb

Overview

Apc4r::Status is simple class for accessing power information from an APC UPS device

The running machine must have apcupsd installed and running for this library to function

Example

status = Apc4r::Status.new 60
puts status.to_s

Instance Method Summary collapse

Constructor Details

#initialize(cache_ttl = 0) ⇒ Status

Creates a Status object, which gives access to information related to the connected UPS device

cache_ttl is the number of seconds to cache status on an object leave blank to fetch information from the ups device on every call



21
22
23
# File 'lib/apc4r/status.rb', line 21

def initialize cache_ttl=0
  @cache_ttl = cache_ttl
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/apc4r/status.rb', line 55

def method_missing sym, *args
  if status.has_key?(sym)
    status[sym]
  else
    super
  end
end

Instance Method Details

#estimated_loadObject

estimates the load, in watts, based on loadpct and nompower This may not be even close to accurate



41
42
43
# File 'lib/apc4r/status.rb', line 41

def estimated_load
  (loadpct / 100) * nompower
end

#statusObject

fetches a hash of status information from the UPS device



27
28
29
30
31
32
33
34
35
36
# File 'lib/apc4r/status.rb', line 27

def status
  if @cache_ttl > 0
    if @cached_at == nil || @cached_at < (Time.now - @cache_ttl)
      @status = fetch_status
    end
  else
    @status = fetch_status
  end
  @status
end

#to_sObject

fetches a string represention of the UPS status



47
48
49
50
51
52
53
# File 'lib/apc4r/status.rb', line 47

def to_s
  result = ""
  status.each_pair do |k,v|
    result << "#{k} = #{v}\n"
  end
  result
end