module Subaru::Definitions::Xytronix::Common
Attributes
options[RW]
Public Class Methods
new(opts = {})
click to toggle source
@param url [String] device url; e.g., `10.1.1.11:8888`. @param opts [Hash] the device options.
# File lib/subaru/d-xytronix.rb, line 17 def initialize(opts = {}) @options = opts end
Public Instance Methods
generate_query(query_hash)
click to toggle source
Generate query string. @param query_hash [hash] resources and their state to set to. @return [String] query string.
# 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
send_request(query = nil)
click to toggle source
Access the device. @param query [String] encoded query string. @return [Object] XML document returned from the device.
# 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)
click to toggle source
Search the XML document. @param doc [Object] XML document. @param resource [String] resource name to search. @param ch [Fixnum] resource channel number. @return [String] state of the resource; e.g., `on`, `off`.
# 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