Class: Pocolog::SampleEnumerator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pocolog/sample_enumerator.rb

Overview

Sample enumerators are nicer interfaces for data reading built on top of a DataStream object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, read_data) ⇒ SampleEnumerator

Returns a new instance of SampleEnumerator



22
23
24
25
# File 'lib/pocolog/sample_enumerator.rb', line 22

def initialize(stream, read_data)
    @stream = stream
    @read_data = read_data
end

Instance Attribute Details

#every_indexObject

Returns the value of attribute every_index



9
10
11
# File 'lib/pocolog/sample_enumerator.rb', line 9

def every_index
  @every_index
end

#every_timeObject

Returns the value of attribute every_time



8
9
10
# File 'lib/pocolog/sample_enumerator.rb', line 8

def every_time
  @every_time
end

#max_countObject

Returns the value of attribute max_count



10
11
12
# File 'lib/pocolog/sample_enumerator.rb', line 10

def max_count
  @max_count
end

#max_indexObject

Returns the value of attribute max_index



9
10
11
# File 'lib/pocolog/sample_enumerator.rb', line 9

def max_index
  @max_index
end

#max_timeObject

Returns the value of attribute max_time



8
9
10
# File 'lib/pocolog/sample_enumerator.rb', line 8

def max_time
  @max_time
end

#min_indexObject

Returns the value of attribute min_index



9
10
11
# File 'lib/pocolog/sample_enumerator.rb', line 9

def min_index
  @min_index
end

#min_timeObject

Returns the value of attribute min_time



8
9
10
# File 'lib/pocolog/sample_enumerator.rb', line 8

def min_time
  @min_time
end

#read_dataObject (readonly)

Returns the value of attribute read_data



21
22
23
# File 'lib/pocolog/sample_enumerator.rb', line 21

def read_data
  @read_data
end

#sample_countObject (readonly)

Returns the value of attribute sample_count



57
58
59
# File 'lib/pocolog/sample_enumerator.rb', line 57

def sample_count
  @sample_count
end

#streamObject (readonly)

Returns the value of attribute stream



21
22
23
# File 'lib/pocolog/sample_enumerator.rb', line 21

def stream
  @stream
end

#use_rtObject

Returns the value of attribute use_rt



7
8
9
# File 'lib/pocolog/sample_enumerator.rb', line 7

def use_rt
  @use_rt
end

Instance Method Details

#at(pos) ⇒ Object



42
43
44
45
# File 'lib/pocolog/sample_enumerator.rb', line 42

def at(pos)
    from(pos)
    max(1)
end

#between(from, to) ⇒ Object



38
39
40
41
# File 'lib/pocolog/sample_enumerator.rb', line 38

def between(from, to)
    self.from(from)
    self.to(to)
end

#each(&block) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/pocolog/sample_enumerator.rb', line 59

def each(&block)
    return enum_for(__method__) unless block

    raw_each do |rt, lg, raw_data|
        yield(rt, lg, Typelib.to_ruby(raw_data))
    end
end

#every(interval) ⇒ Object



26
27
28
29
# File 'lib/pocolog/sample_enumerator.rb', line 26

def every(interval)
    setvar('every', interval)
    self
end

#from(from) ⇒ Object



30
31
32
33
# File 'lib/pocolog/sample_enumerator.rb', line 30

def from(from);
    setvar("min", from)
    self
end

#max(count) ⇒ Object



52
53
54
55
# File 'lib/pocolog/sample_enumerator.rb', line 52

def max(count)
    @max_count = count
    self
end

#raw_eachObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/pocolog/sample_enumerator.rb', line 67

def raw_each
    return enum_for(__method__) unless block_given?
    return if stream.empty?

    @sample_count = 0
    @next_yield_time = nil
    @next_yield_index = nil

    min_index = self.min_index
    min_time  = self.min_time

    if min_time && stream.interval_lg.first > min_time
        min_time = nil
    elsif min_index || (min_time && !use_rt)
        stream.seek(min_index || min_time)
        stream.previous
    elsif min_time && use_rt
        return unless skip_to_realtime(min_time)

        stream.previous
    end

    stream.each_block(!(min_index || min_time)) do
        sample_index = stream.sample_index
        return self if max_index && max_index < sample_index
        return self if max_count && max_count <= @sample_count

        rt, lg = stream.time
        sample_time = use_rt ? rt : lg
        return self if max_time && max_time < sample_time

        if yield_sample?(sample_time, sample_index)
            @sample_count += 1
            data_block = stream.data_header
            yield(data_block.rt, data_block.lg,
                  (stream.raw_data(data_block) if read_data))
        end
    end
    self
end

#realtime(use_rt = true) ⇒ Object



47
48
49
50
# File 'lib/pocolog/sample_enumerator.rb', line 47

def realtime(use_rt = true)
    @use_rt = use_rt
    self
end

#setvar(name, val) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/pocolog/sample_enumerator.rb', line 11

def setvar(name, val)
    time, index = case val
                  when Integer then [nil, val]
                  when Time then [val, nil]
                  end

    send("#{name}_time=", time)
    send("#{name}_index=", index)
end

#skip_to_realtime(min_time) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/pocolog/sample_enumerator.rb', line 108

def skip_to_realtime(min_time)
    stream.each_block(true) do
        sample_index = stream.sample_index
        return if max_index && max_index < sample_index

        rt, = stream.time
        return true if rt >= min_time
    end
    false
end

#to(to) ⇒ Object



34
35
36
37
# File 'lib/pocolog/sample_enumerator.rb', line 34

def to(to)
    setvar("max", to)
    self
end

#yield_sample?(sample_time, sample_index) ⇒ Boolean

Yield the given sample if required by our configuration

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/pocolog/sample_enumerator.rb', line 120

def yield_sample?(sample_time, sample_index)
    if every_time
        every_time = self.every_time.to_r
        @next_yield_time ||= sample_time
        while @next_yield_time <= sample_time
            do_display = true
            @next_yield_time += every_time
        end
        do_display
    elsif every_index
        @next_yield_index ||= sample_index
        if @next_yield_index <= sample_index
            do_display = true
            @next_yield_index += every_index
        end
        do_display
    else
        true
    end
end