class SAAL::Denkovi::OutletGroup
Constants
- DEFAULT_CACHE_TIMEOUT
- DEFAULT_DESCRIPTIONS
- DEFAULT_OUTLETS
- DEFAULT_TIMEOUT
Attributes
cache_timeout[RW]
host[RW]
pass[RW]
port[RW]
timeout[RW]
Public Class Methods
new(opts={})
click to toggle source
# File lib/denkovi.rb 39 def initialize(opts={}) 40 @host = opts[:host] || opts['host'] || 'localhost' 41 @port = opts[:port] || opts['port'] || 80 42 @pass = opts[:pass] || opts['pass'] || 'admin' 43 @timeout = opts[:timeout] || opts['timeout'] || DEFAULT_TIMEOUT 44 @cache_timeout = opts[:cache_timeout] || opts['cache_timeout'] || DEFAULT_CACHE_TIMEOUT 45 @outlets = opts[:outlets] || opts["outlets"] || DEFAULT_OUTLETS 46 @descriptions = opts[:descriptions] || opts["descriptions"] || DEFAULT_DESCRIPTIONS 47 @cache = nil 48 @cachehit = nil 49 @cachetime = nil 50 end
Public Instance Methods
create_sensors()
click to toggle source
# File lib/denkovi.rb 83 def create_sensors 84 sensors = {} 85 (1..16).each do |num| 86 name = @outlets[num] 87 if name 88 description = @descriptions[num] || "" 89 sensors[name] = [Outlet.new(num, self), description] 90 end 91 end 92 sensors 93 end
set_state(num, state)
click to toggle source
# File lib/denkovi.rb 72 def set_state(num, state) 73 @cachetime = nil 74 val = {"ON" => "1", "OFF" => "0"}[state] 75 if val 76 response = do_get("/current_state.json?pw=#{@pass}&Relay#{num}=#{val}") 77 response != nil 78 else 79 false 80 end 81 end
state(num)
click to toggle source
# File lib/denkovi.rb 52 def state(num) 53 if !@cachetime or @cachetime < Time.now - @cache_timeout 54 @cache = do_get("/current_state.json?pw=#{@pass}") 55 @cachetime = Time.now 56 end 57 return nil if !@cache 58 json = JSON.parse(@cache.body) 59 num = num - 1 60 if json && 61 json["CurrentState"] && 62 json["CurrentState"]["Output"] && 63 json["CurrentState"]["Output"][num] && 64 json["CurrentState"]["Output"][num]["Value"] 65 val = json["CurrentState"]["Output"][num]["Value"] 66 {"1" => "ON", "0" => "OFF"}[val] 67 else 68 nil 69 end 70 end
Private Instance Methods
do_get(path)
click to toggle source
# File lib/denkovi.rb 96 def do_get(path) 97 SAAL::do_http_get(@host, @port, path, nil, nil, @timeout) 98 end