Class: SAAL::Denkovi::OutletGroup

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

Constant Summary collapse

DEFAULT_TIMEOUT =
2
DEFAULT_CACHE_TIMEOUT =
5
DEFAULT_OUTLETS =
{}
DEFAULT_DESCRIPTIONS =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ OutletGroup

Returns a new instance of OutletGroup.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/denkovi.rb', line 39

def initialize(opts={})
  @host = opts[:host] || opts['host'] || 'localhost'
  @port = opts[:port] || opts['port'] || 80
  @pass = opts[:pass] || opts['pass'] || 'admin'
  @timeout = opts[:timeout] || opts['timeout'] || DEFAULT_TIMEOUT
  @cache_timeout = opts[:cache_timeout] || opts['cache_timeout'] || DEFAULT_CACHE_TIMEOUT
  @outlets = opts[:outlets] || opts["outlets"] || DEFAULT_OUTLETS
  @descriptions = opts[:descriptions] || opts["descriptions"] || DEFAULT_DESCRIPTIONS
  @cache = nil
  @cachehit = nil
  @cachetime = nil
end

Instance Attribute Details

#cache_timeoutObject

Returns the value of attribute cache_timeout.



37
38
39
# File 'lib/denkovi.rb', line 37

def cache_timeout
  @cache_timeout
end

#hostObject

Returns the value of attribute host.



37
38
39
# File 'lib/denkovi.rb', line 37

def host
  @host
end

#passObject

Returns the value of attribute pass.



37
38
39
# File 'lib/denkovi.rb', line 37

def pass
  @pass
end

#portObject

Returns the value of attribute port.



37
38
39
# File 'lib/denkovi.rb', line 37

def port
  @port
end

#timeoutObject

Returns the value of attribute timeout.



37
38
39
# File 'lib/denkovi.rb', line 37

def timeout
  @timeout
end

Instance Method Details

#create_sensorsObject



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/denkovi.rb', line 83

def create_sensors
  sensors = {}
  (1..16).each do |num|
    name = @outlets[num]
    if name
      description = @descriptions[num] || ""
      sensors[name] = [Outlet.new(num, self), description]
    end
  end
  sensors
end

#set_state(num, state) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/denkovi.rb', line 72

def set_state(num, state)
  @cachetime = nil
  val = {"ON" => "1", "OFF" => "0"}[state]
  if val
    response = do_get("/current_state.json?pw=#{@pass}&Relay#{num}=#{val}")
    response != nil
  else
    false
  end
end

#state(num) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/denkovi.rb', line 52

def state(num)
  if !@cachetime or @cachetime < Time.now - @cache_timeout
    @cache = do_get("/current_state.json?pw=#{@pass}")
    @cachetime = Time.now
  end
  return nil if !@cache
  json = JSON.parse(@cache.body)
  num = num - 1
  if json &&
     json["CurrentState"] &&
     json["CurrentState"]["Output"] &&
     json["CurrentState"]["Output"][num] &&
     json["CurrentState"]["Output"][num]["Value"]
     val = json["CurrentState"]["Output"][num]["Value"]
     {"1" => "ON", "0" => "OFF"}[val]
  else
    nil
  end
end