Class: MeasReceiver::MeasTypeReceiver

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

Constant Summary collapse

SCH_MIN_INTERVAL =
0.05
DEFAULT_FETCH_INTERVAL =
0.5
MIN_FETCH_INTERVAL =
0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_options) ⇒ MeasTypeReceiver

TODO convert all keys to Symbols



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/meas_receiver/meas_type_receiver.rb', line 15

def initialize(_options)
  @options = _options

  @options[:logger] ||= Hash.new
  @options[:logger][:output] ||= STDOUT
  @options[:logger][:level] ||= Logger::INFO
  @logger = Logger.new(@options[:logger][:output])
  @logger.level = @options[:logger][:level]
  @debug = (@options[:logger][:level] == Logger::DEBUG)

  @fetch_interval = _options[:fetch_interval] || DEFAULT_FETCH_INTERVAL
  @fetch_interval = MIN_FETCH_INTERVAL if @fetch_interval < MIN_FETCH_INTERVAL

  @name = @options[:name]
  @command = @options[:command]
  @response_size = @options[:response_size]
  @coefficients = @options[:coefficients] || Hash.new
  @coefficients[:linear] ||= 1.0
  @coefficients[:offset] ||= 0.0
  @after_proc = @options[:after_proc]
  @storage = @options[:storage] || Hash.new
  @storage[:min_time_interval] ||= @fetch_interval
  @storage[:min_unit_interval] = (@storage[:min_time_interval] / @fetch_interval).floor
  @storage[:max_time_interval] ||= 3600
  @storage[:max_unit_interval] = (@storage[:max_time_interval] / @fetch_interval).floor
  @storage[:store_interval] ||= 10*60 #2*3600

  @comm_object = CommProtocol.new(@command, @response_size)
  @meas_buffer = MeasTypeBuffer.new(self)
end

Instance Attribute Details

#after_procObject (readonly)

Returns the value of attribute after_proc.



46
47
48
# File 'lib/meas_receiver/meas_type_receiver.rb', line 46

def after_proc
  @after_proc
end

#coefficientsObject (readonly)

Returns the value of attribute coefficients.



46
47
48
# File 'lib/meas_receiver/meas_type_receiver.rb', line 46

def coefficients
  @coefficients
end

#commandObject (readonly)

Returns the value of attribute command.



46
47
48
# File 'lib/meas_receiver/meas_type_receiver.rb', line 46

def command
  @command
end

#debugObject (readonly)

Returns the value of attribute debug.



46
47
48
# File 'lib/meas_receiver/meas_type_receiver.rb', line 46

def debug
  @debug
end

#fetch_intervalObject (readonly)

Returns the value of attribute fetch_interval.



46
47
48
# File 'lib/meas_receiver/meas_type_receiver.rb', line 46

def fetch_interval
  @fetch_interval
end

#loggerObject (readonly)

Returns the value of attribute logger.



46
47
48
# File 'lib/meas_receiver/meas_type_receiver.rb', line 46

def logger
  @logger
end

#meas_bufferObject (readonly)

Returns the value of attribute meas_buffer.



83
84
85
# File 'lib/meas_receiver/meas_type_receiver.rb', line 83

def meas_buffer
  @meas_buffer
end

#nameObject (readonly)

Returns the value of attribute name.



46
47
48
# File 'lib/meas_receiver/meas_type_receiver.rb', line 46

def name
  @name
end

#response_sizeObject (readonly)

Returns the value of attribute response_size.



46
47
48
# File 'lib/meas_receiver/meas_type_receiver.rb', line 46

def response_size
  @response_size
end

#storageObject (readonly)

Returns the value of attribute storage.



46
47
48
# File 'lib/meas_receiver/meas_type_receiver.rb', line 46

def storage
  @storage
end

Instance Method Details

#[](i) ⇒ Object



85
86
87
# File 'lib/meas_receiver/meas_type_receiver.rb', line 85

def [](i)
  @meas_buffer[i]
end

#cleanObject



79
80
81
# File 'lib/meas_receiver/meas_type_receiver.rb', line 79

def clean
  @meas_buffer.clean_up!
end

#fetchObject



66
67
68
69
70
71
72
73
# File 'lib/meas_receiver/meas_type_receiver.rb', line 66

def fetch
  v = @comm_object.g
  @meas_buffer.add!(v)

  @logger.debug("Fetched #{self.name.red} = #{v.to_s.yellow}") if @debug

  return v
end

#firstObject



89
90
91
# File 'lib/meas_receiver/meas_type_receiver.rb', line 89

def first
  @meas_buffer.first
end

#lastObject



93
94
95
# File 'lib/meas_receiver/meas_type_receiver.rb', line 93

def last
  @meas_buffer.last
end

#startObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/meas_receiver/meas_type_receiver.rb', line 48

def start
  @logger.debug("MeasReceiver started for #{self.name.red}") if @debug

  @scheduler = Rufus::Scheduler.start_new(frequency: SCH_MIN_INTERVAL)
  @scheduler.every "#{@fetch_interval}s" do
    fetch
  end

  @scheduler.every "#{@storage[:store_interval]}s" do
    store
  end
end

#stopObject



61
62
63
64
# File 'lib/meas_receiver/meas_type_receiver.rb', line 61

def stop
  @scheduler.stop
  @logger.debug("MeasReceiver stopped for #{self.name.red}") if @debug
end

#storeObject



75
76
77
# File 'lib/meas_receiver/meas_type_receiver.rb', line 75

def store
  @meas_buffer.perform_storage!
end