Module: Subaru::Definitions::Xytronix::Common

Included in:
WebRelay, WebRelay10
Defined in:
lib/subaru/d-xytronix.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject

Returns the value of attribute options


13
14
15
# File 'lib/subaru/d-xytronix.rb', line 13

def options
  @options
end

Instance Method Details

#generate_query(query_hash) ⇒ String

Generate query string.

Parameters:

  • query_hash (hash)

    resources and their state to set to.

Returns:

  • (String)

    query string.


24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/subaru/d-xytronix.rb', line 24

def generate_query(query_hash)
  return nil unless query_hash.class == Hash
  h = {}
  query_hash.each do |k, v|
    case v
    when "off"
      h["#{k}State"] = 0
    when "on"
      h["#{k}State"] = 1
    end
  end
  return nil if h.empty?
  return URI.encode_www_form(h)
end

#initialize(opts = {}) ⇒ Object

Parameters:

  • url (String)

    device url; e.g., `10.1.1.11:8888`.

  • opts (Hash) (defaults to: {})

    the device options.


17
18
19
# File 'lib/subaru/d-xytronix.rb', line 17

def initialize(opts = {})
  @options = opts
end

#send_request(query = nil) ⇒ Object

Access the device.

Parameters:

  • query (String) (defaults to: nil)

    encoded query string.

Returns:

  • (Object)

    XML document returned from the device.


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/subaru/d-xytronix.rb', line 42

def send_request(query = nil)
  uri = "#{@options[:url]}/stateFull.xml"
  uri << "?#{query}" if query
  o = {}
  o[:http_basic_authentication] = [nil, @options[:password]] if @options[:password]
  o[:read_timeout] = @options[:read_timeout] if @options[:read_timeout]
  response = open(uri, o)
  return REXML::Document.new(response.read)
rescue => e
  puts e.message
  return nil
end

#state_from_xml(doc, resource, ch = nil) ⇒ String

Search the XML document.

Parameters:

  • doc (Object)

    XML document.

  • resource (String)

    resource name to search.

  • ch (Fixnum) (defaults to: nil)

    resource channel number.

Returns:

  • (String)

    state of the resource; e.g., `on`, `off`.


60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/subaru/d-xytronix.rb', line 60

def state_from_xml(doc, resource, ch = nil)
  case REXML::XPath.first(doc, "//datavalues/#{resource}#{ch}state").text.to_i
  when 0
    s = "off"
  when 1
    s = "on"
  else
    s = "unknown"
  end
  return s
rescue => e
  puts e.message
  return nil
end