Class: Macro

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, title: '') ⇒ Macro

Returns a new instance of Macro.



334
335
336
337
338
339
340
341
342
# File 'lib/macrohub.rb', line 334

def initialize(node, title: '')

  @title = title

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

end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



332
333
334
# File 'lib/macrohub.rb', line 332

def actions
  @actions
end

#constraintsObject (readonly)

Returns the value of attribute constraints.



332
333
334
# File 'lib/macrohub.rb', line 332

def constraints
  @constraints
end

#envObject

Returns the value of attribute env.



331
332
333
# File 'lib/macrohub.rb', line 331

def env
  @env
end

#titleObject

Returns the value of attribute title.



331
332
333
# File 'lib/macrohub.rb', line 331

def title
  @title
end

#triggersObject (readonly)

Returns the value of attribute triggers.



332
333
334
# File 'lib/macrohub.rb', line 332

def triggers
  @triggers
end

Instance Method Details

#import_xml(node) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# 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 = {}) ⇒ Boolean

Returns:

  • (Boolean)


432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# 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

#runObject



455
456
457
# File 'lib/macrohub.rb', line 455

def run()
  @actions.map(&:invoke)
end

#to_nodeObject



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# 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_rowxObject



475
476
477
478
479
480
# 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