Class: ProjectSimulator::Controller

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s, time: Time.now, debug: false) ⇒ Controller

Returns a new instance of Controller.



409
410
411
412
413
414
415
416
417
418
# File 'lib/projectsimulator.rb', line 409

def initialize(s, time: Time.now, debug: false)
  
  @time, @debug = time, debug

  doc = Rexle.new(RowX.new(s).to_xml)

  @events = doc.root.xpath('item')\
      .map {|e| Event.new(e, time: @time, debug: debug) }

end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



406
407
408
# File 'lib/projectsimulator.rb', line 406

def events
  @events
end

#timeObject

Returns the value of attribute time.



407
408
409
# File 'lib/projectsimulator.rb', line 407

def time
  @time
end

Instance Method Details

#trigger(name, location: '') ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/projectsimulator.rb', line 425

def trigger(name, location: '')
  
  events = @events.select do |event|
    
    puts 'event: '  + event.inspect if @debug

    event.match(trigger: 'motion', location: location)
    
  end
  
  puts 'events: ' + events.inspect if @debug
  
  events.each do |event|
    c = event.constraints.select {|x| x.is_a? FrequencyConstraint }
    puts 'c:' + c.inspect
    c.each(&:increment)
  end
  
  events.flat_map(&:messages)
  
end