Class: TEF::Sequencing::EventCollector

Inherits:
Object
  • Object
show all
Includes:
XasLogger::Mix
Defined in:
lib/tef/Sequencing/EventCollector.rb

Overview

Event Collector class

This class provides the means to efficiently fetch the next event from a list of BaseSequences, and is mainly meant for internal purposes. It is created by Player

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEventCollector

Returns a new instance of EventCollector.



122
123
124
125
126
127
128
# File 'lib/tef/Sequencing/EventCollector.rb', line 122

def initialize()
	@current_events = []
	@start_time = Time.at(0);
	@event_time = nil;

	init_x_log("Sequence Player")
end

Instance Attribute Details

#current_eventsArray<Hash> (readonly)

Returns List of current events to execute.

Returns:

  • (Array<Hash>)

    List of current events to execute.



120
121
122
# File 'lib/tef/Sequencing/EventCollector.rb', line 120

def current_events
  @current_events
end

#event_timenil, Time (readonly)

Returns The Time of the current event, or nil if there is no event. Any event later than this will be discarded. Any event equal to this time will be appended to #current_events.

Returns:

  • (nil, Time)

    The Time of the current event, or nil if there is no event. Any event later than this will be discarded. Any event equal to this time will be appended to #current_events



117
118
119
# File 'lib/tef/Sequencing/EventCollector.rb', line 117

def event_time
  @event_time
end

#start_timeTime

Returns The Time to start looking for an event. Any event earlier than this will be discarded!.

Returns:

  • (Time)

    The Time to start looking for an event. Any event earlier than this will be discarded!



111
112
113
# File 'lib/tef/Sequencing/EventCollector.rb', line 111

def start_time
  @start_time
end

Instance Method Details

#add_event(event) ⇒ Object

Internal function to add an event. The event will be discarded if it is earlier than or equal to start time, or later than the event time. It if it is earlier than event time it will set the new event time and set the event list to [event], else append the event to the event list.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/tef/Sequencing/EventCollector.rb', line 136

def add_event(event)
	event = event.clone

	return if event[:time] <= @start_time
	return if (!@event_time.nil?) && (event[:time] > @event_time)
	return unless event[:code].is_a? Proc

	if (!@event_time.nil?) && (event[:time] == @event_time)
		@current_events << event
	else
		@current_events = [event]
		@event_time = event[:time]
	end
end

#execute!Object

Wait until the next event and then execute the code of each event.



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/tef/Sequencing/EventCollector.rb', line 175

def execute!
	return unless has_events?

	wait_until_event

	@current_events.each do |event|
		event[:code].call()
	end

	@start_time = @event_time
	restart();
end

#has_events?true, false

Returns Were any events found?.

Returns:

  • (true, false)

    Were any events found?



152
153
154
# File 'lib/tef/Sequencing/EventCollector.rb', line 152

def has_events?
	!@current_events.empty?
end

#offset_collector(offset, slope) ⇒ Object

Generate a OffsetCollector This is mainly an internal function used by BaseSequence to provide an OffsetCollector. It converts between the global time-frame used by this collector, and the local timeframes of each sub-sequence.



200
201
202
# File 'lib/tef/Sequencing/EventCollector.rb', line 200

def offset_collector(offset, slope)
	OffsetCollector.new(self, offset, slope);
end

#restartObject

Restart this collector. Will clear #current_events and #event_time



190
191
192
193
# File 'lib/tef/Sequencing/EventCollector.rb', line 190

def restart()
	@current_events = []
	@event_time = nil;
end

#wait_until_eventObject

This function will try to wait until #event_time. If no event was found it will return immediately, and Thread.run() can be used to prematurely end the wait.



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/tef/Sequencing/EventCollector.rb', line 159

def wait_until_event
	return unless has_events?

	t_diff = @event_time - Time.now();

	if t_diff < -0.5
		x_logf('Sequence long overdue!')
	elsif t_diff < -0.1
		x_logw('Sequencing overdue')
	end

	sleep t_diff if t_diff > 0
end