class Macro

Attributes

actions[R]
constraints[R]
env[RW]
title[RW]
triggers[R]

Public Class Methods

new(node, title: '') click to toggle source
# File lib/macrohub.rb, line 334
def initialize(node, title: '')

  @title = title

  @actions = []
  @triggers = []
  @constraints = []

end

Public Instance Methods

import_xml(node) click to toggle source
# File lib/macrohub.rb, line 344
def import_xml(node)
  
  @title = node.text('macro')

  if node.element('triggers') then
    
    triggers = {motion: MotionTrigger, timer: TimerTrigger}
    
    # level 2
    @triggers = node.xpath('triggers/*').map do |e| 

      puts 'e.name: ' + e.name.inspect if $debug
      triggers[e.name.to_sym].new(e.attributes.to_h)

    end

    actions = {say: SayAction, webhook: WebhookAction}
    
    @actions = node.xpath('actions/*').map do |e|
      
      actions[e.name.to_sym].new(e.attributes.to_h)
      
    end
    
    constraints = {time: TimeConstraint, frequency: FrequencyConstraint}

    @constraints = node.xpath('constraints/*').map do |e|

      puts 'before Constraints.new' if $debug
      constraints[e.name.to_sym].new(e.attributes.to_h)
      
    end

  else

    # Level 1
    
    tp = TriggersNlp.new      
    
    @triggers = node.xpath('trigger').map do |e|
      
      r = tp.find_trigger e.text
      
      if r then
        r[0].new(r[1])
      end
      
    end
    
    ap = ActionsNlp.new      
    
    @actions = node.xpath('action').map do |e|
      
      r = ap.find_action e.text
      
      if r then
        
        a = e.xpath('item/*')
        
        h = if a.any? then
          a.map {|node| [node.name.to_sym, node.text.to_s]}.to_h
        else
          {}
        end
        
        r[0].new(r[1].merge(h))
      end
      
    end
          
    cn = ConstraintsNlp.new      
    
    @constraints = node.xpath('constraint').map do |e|

      puts 'constraint e: ' + e.xml.inspect
      r = cn.find_constraint e.text
      
      puts 'r: ' + r.inspect if $debug
      
      if r then
        r[0].new(r[1])
      end
      
    end        

  end
end
match?(triggerx, detail={} ) click to toggle source
# File lib/macrohub.rb, line 432
def match?(triggerx, detail={} )
              
  if @triggers.any? {|x| x.type == triggerx and x.match?(detail) } then
    
    if $debug then
      puts 'checking constraints ...' 
      puts '@constraints: ' + @constraints.inspect
    end
    
    if @constraints.all? {|x| x.match?($env.merge(detail)) } then
    
      true
      
    else

      return false
      
    end
    
  end
  
end
run() click to toggle source
# File lib/macrohub.rb, line 455
def run()
  @actions.map(&:invoke)
end
to_node() click to toggle source
# File lib/macrohub.rb, line 459
def to_node()
  
  if $debug then
    puts 'inside to_node' 
    puts '@title: ' + @title.inspect
  end
  
  e = Rexle::Element.new(:macro, attributes: {title: @title})
  
  e.add node_collection(:triggers, @triggers)
  e.add node_collection(:actions, @actions)
  e.add node_collection(:constraints, @constraints)
  
  return e
end
to_rowx() click to toggle source
# File lib/macrohub.rb, line 475
def to_rowx()
  
  s = "macro: %s\n\n" % @title
  s + [@triggers, @actions, @constraints]\
      .map {|x| x.collect(&:to_rowx).join("\n")}.join("\n")
end

Private Instance Methods

node_collection(name, a) click to toggle source
# File lib/macrohub.rb, line 484
def node_collection(name, a)
  
  if $debug then
    puts 'inside node_collection name: ' + name.inspect
    puts 'a: ' + a.inspect
  end
  
  e = Rexle::Element.new(name)
  a.each do |x|
    
    puts 'x: ' + x.inspect if $debug
    e.add x.to_node

  end
  
  return e
  
end