class RemoteDroid::Model
Public Class Methods
new(obj=nil, root: 'device1', debug: false)
click to toggle source
Calls superclass method
# File lib/remotedroid/model.rb, line 6 def initialize(obj=nil, root: 'device1', debug: false) super() @root, @debug = root, debug @location = nil if obj then s = obj.strip puts 's: ' + s.inspect if @debug if s[0] == '<' or s.lines[1][0..1] == ' ' then puts 'before easydom' if @debug s2 = if s.lines[1][0..1] == ' ' then lines = s.lines.map do |line| line.sub(/(\w+) +is +(\w+)$/) {|x| "#{$1} {switch: #{$2}}" } end lines.join else s end @ed = EasyDom.new(s2) else build(s, root: root) end end end
Public Instance Methods
build(raw_requests, root: @root)
click to toggle source
# File lib/remotedroid/model.rb, line 43 def build(raw_requests, root: @root) @ed = EasyDom.new(debug: false, root: root) raw_requests.lines.each {|line| request(line) } end
get_thing(h)
click to toggle source
# File lib/remotedroid/model.rb, line 51 def get_thing(h) h[:thing].gsub!(/ /,'_') if not h.has_key? :location then location = false h[:location] = find_path(h[:thing]) else location = true end puts 'h: ' + h.inspect if @debug a = [] a += h[:location].split(/ /) a << h[:thing] status = a.inject(@ed) {|r,x| r.send(x)}.send(h[:action]) if location then "The %s %s is %s." % [h[:location], h[:thing], status] else "%s is %s." % [h[:thing].capitalize, status] end end
op()
click to toggle source
Object
Property (op) Helpful for accessing properites in dot notation e.g. op.livingroom.light.switch = 'off'
# File lib/remotedroid/model.rb, line 81 def op() @ed end
query(s)
click to toggle source
# File lib/remotedroid/model.rb, line 85 def query(s) @ed.e.element(s) end
request(s)
click to toggle source
request accepts a string in plain english e.g. request 'switch the livingroom light on'
# File lib/remotedroid/model.rb, line 92 def request(s) params = {request: s} requests(params) h = find_request(s) method(h.first[-1]).call(h).gsub(/_/,' ') end
set_thing(h)
click to toggle source
# File lib/remotedroid/model.rb, line 102 def set_thing(h) h[:thing].gsub!(/ /,'_') h[:location] = find_path(h[:thing]) unless h.has_key? :location a = [] a += h[:location].split(/ /) a << h[:thing] a.inject(@ed) {|r,x| r.send(x)}.send(h[:action], h[:value]) end
to_sliml(level: 0)
click to toggle source
# File lib/remotedroid/model.rb, line 115 def to_sliml(level: 0) s = @ed.to_sliml return s if level.to_i > 0 lines = s.lines.map do |line| line.sub(/\{[^\}]+\}/) do |x| a = x.scan(/\w+: +[^ ]+/) if a.length == 1 and x[/switch:/] then val = x[/(?<=switch: ) *["']([^"']+)/,1] 'is ' + val else x end end end lines.join end
to_xml(options=nil)
click to toggle source
# File lib/remotedroid/model.rb, line 141 def to_xml(options=nil) @ed.xml(pretty: true).gsub(' style=\'\'','') end
Also aliased as: xml
Protected Instance Methods
requests(params)
click to toggle source
to_xml
() is the preferred method
# File lib/remotedroid/model.rb, line 151 def requests(params) # e.g. switch the livingroom gas_fire off # get /(?:switch|turn) the ([^ ]+) +([^ ]+) +(on|off)$/ do |location, device, onoff| {type: :set_thing, action: 'switch=', location: location, thing: device, value: onoff} end # e.g. switch the gas _fire off # get /(?:switch|turn) the ([^ ]+) +(on|off)$/ do |device, onoff| {type: :set_thing, action: 'switch=', thing: device, value: onoff} end # e.g. is the livingroom gas_fire on? # get /is the ([^ ]+) +([^ ]+) +(?:on|off)\??$/ do |location, device| {type: :get_thing, action: 'switch', location: location, thing: device} end # e.g. enable airplane mode # get /((?:dis|en)able) ([^$]+)$/ do |state, service| {type: :set_thing, action: 'switch=', thing: service, value: state + 'd'} end # e.g. switch airplane mode off # get /switch (.*) (on|off)/ do |service, rawstate| state = rawstate == 'on' ? 'enabled' : 'disabled' {type: :set_thing, action: 'switch=', thing: service, value: state} end # e.g. is airplane mode enabed? # get /is (.*) +(?:(?:dis|en)abled)\??$/ do |service| {type: :get_thing, action: 'switch', thing: service.gsub(/ /,'_')} end # e.g. is the gas_fire on? # get /is the ([^ ]+) +(?:on|off)\??$/ do |device| location = find_path(device) {type: :get_thing, action: 'switch', location: location, thing: device} end # e.g. fetch the livingroom temperature reading # get /fetch the ([^ ]+) +([^ ]+) +(?:reading)$/ do |location, device| {type: :get_thing, action: 'reading', location: location, thing: device} end # e.g. fetch the temperature reading # get /fetch the ([^ ]+) +(?:reading)$/ do |device| location = find_path(device) {type: :get_thing, action: 'reading', location: location, thing: device} end end
Private Instance Methods
find_path(s)
click to toggle source
# File lib/remotedroid/model.rb, line 216 def find_path(s) puts 'find_path s: ' + s.inspect if @debug found = query('//'+ s) return unless found a = found.backtrack.to_xpath.split('/') a[1..-2].join(' ') end