home_io_meas_receiver 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +11 -0
- data/Gemfile.lock +38 -0
- data/LICENSE.txt +165 -0
- data/README.rdoc +19 -0
- data/Rakefile +59 -0
- data/VERSION +1 -0
- data/lib/meas_receiver/comm_protocol.rb +106 -0
- data/lib/meas_receiver/meas_type_buffer.rb +216 -0
- data/lib/meas_receiver/meas_type_receiver.rb +80 -0
- data/lib/meas_receiver/receiver.rb +8 -0
- data/lib/meas_receiver.rb +8 -0
- metadata +127 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.3)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.6.4)
|
7
|
+
bundler (~> 1.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
mean (0.1.0)
|
11
|
+
multi_json (1.3.7)
|
12
|
+
rake (0.9.2.2)
|
13
|
+
rspec (2.3.0)
|
14
|
+
rspec-core (~> 2.3.0)
|
15
|
+
rspec-expectations (~> 2.3.0)
|
16
|
+
rspec-mocks (~> 2.3.0)
|
17
|
+
rspec-core (2.3.1)
|
18
|
+
rspec-expectations (2.3.0)
|
19
|
+
diff-lcs (~> 1.1.2)
|
20
|
+
rspec-mocks (2.3.0)
|
21
|
+
rufus-scheduler (2.0.17)
|
22
|
+
tzinfo (>= 0.3.23)
|
23
|
+
simplecov (0.7.1)
|
24
|
+
multi_json (~> 1.0)
|
25
|
+
simplecov-html (~> 0.7.1)
|
26
|
+
simplecov-html (0.7.1)
|
27
|
+
tzinfo (0.3.34)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
ruby
|
31
|
+
|
32
|
+
DEPENDENCIES
|
33
|
+
bundler (~> 1.0.0)
|
34
|
+
jeweler (~> 1.6.4)
|
35
|
+
mean
|
36
|
+
rspec (~> 2.3.0)
|
37
|
+
rufus-scheduler
|
38
|
+
simplecov
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= meas_receiver
|
2
|
+
|
3
|
+
Simple gem for receiving measurements using HomeIO protocol. It is / will be part of HomeIO.
|
4
|
+
|
5
|
+
== Contributing to meas_receiver
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2012 Aleksander Kwiatkowski. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "home_io_meas_receiver"
|
18
|
+
gem.homepage = "http://github.com/akwiatkowski/meas_receiver"
|
19
|
+
gem.license = "LGPLv3"
|
20
|
+
gem.summary = %Q{Measurements receiver}
|
21
|
+
gem.description = %Q{Receive measurements from IoServer (part if HomeIO).}
|
22
|
+
gem.email = "bobikx@poczta.fm"
|
23
|
+
gem.authors = ["Aleksander Kwiatkowski"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
|
26
|
+
gem.files = FileList[
|
27
|
+
"[A-Z]*", "{bin,generators,lib,test}/**/*"
|
28
|
+
]
|
29
|
+
end
|
30
|
+
Jeweler::RubygemsDotOrgTasks.new
|
31
|
+
|
32
|
+
require 'rspec/core'
|
33
|
+
require 'rspec/core/rake_task'
|
34
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
35
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
36
|
+
end
|
37
|
+
|
38
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
39
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
40
|
+
spec.rcov = true
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :spec
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "simple_metar_parser #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
54
|
+
|
55
|
+
desc "Run RSpec with code coverage"
|
56
|
+
task :coverage do
|
57
|
+
`rake spec COVERAGE=true`
|
58
|
+
#`open coverage/index.html`
|
59
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'socket'
|
2
|
+
|
3
|
+
module MeasReceiver
|
4
|
+
class CommProtocol
|
5
|
+
|
6
|
+
def initialize(_command, _response_size)
|
7
|
+
@command = _command
|
8
|
+
@response_size = _response_size
|
9
|
+
@command_string = self.class.prepare_command_string(_command, _response_size)
|
10
|
+
end
|
11
|
+
|
12
|
+
def g
|
13
|
+
get
|
14
|
+
end
|
15
|
+
|
16
|
+
def get
|
17
|
+
d = self.class.send_command(@command_string)
|
18
|
+
return self.class.byte_array_to_i(d)
|
19
|
+
end
|
20
|
+
|
21
|
+
@@host = 'localhost'
|
22
|
+
@@port = '12345'
|
23
|
+
|
24
|
+
def self.port=(_port)
|
25
|
+
@@port = _port
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.host=(_host)
|
29
|
+
@@host = _host
|
30
|
+
end
|
31
|
+
|
32
|
+
# Create String command to IoServer and send it
|
33
|
+
def self.create_send_command(command_array, response_size, hostname = @@host, port = @@port)
|
34
|
+
str = prepare_command_string(command_array, response_size)
|
35
|
+
puts "command_string2 #{str.inspect}"
|
36
|
+
return send_command(str, hostname, port)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.create_and_send_command(command_array, response_size, hostname = @@host, port = @@port)
|
40
|
+
create_send_command(command_array, response_size, hostname, port)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Send command to IoServer and send it
|
44
|
+
def self.send_command(command_byte_array, hostname = @@host, port = @@port)
|
45
|
+
s = TCPSocket.open(hostname, port)
|
46
|
+
s.puts(command_byte_array)
|
47
|
+
data = s.gets
|
48
|
+
s.close
|
49
|
+
return data
|
50
|
+
end
|
51
|
+
|
52
|
+
# Create command string to IoServer
|
53
|
+
# command_array can be Array, String or even Fixnum
|
54
|
+
def self.prepare_command_string(command_array, response_size)
|
55
|
+
if command_array.kind_of?(Array)
|
56
|
+
command_byte_array = command_array
|
57
|
+
elsif command_array.kind_of?(String)
|
58
|
+
command_byte_array = command_array.split(//)
|
59
|
+
elsif command_array.kind_of?(Fixnum)
|
60
|
+
command_byte_array = [command_array.chr]
|
61
|
+
else
|
62
|
+
raise ArgumentError
|
63
|
+
end
|
64
|
+
|
65
|
+
command_byte_array.size.chr + response_size.chr + command_byte_array.collect { |c|
|
66
|
+
if c.kind_of? Fixnum
|
67
|
+
c.chr
|
68
|
+
else
|
69
|
+
c.to_s
|
70
|
+
end
|
71
|
+
}.join('')
|
72
|
+
end
|
73
|
+
|
74
|
+
# Convert string/byte array to number
|
75
|
+
def self.byte_array_to_i(ba)
|
76
|
+
number = 0
|
77
|
+
ba.each_byte do |b|
|
78
|
+
number *= 256
|
79
|
+
number += b
|
80
|
+
end
|
81
|
+
number
|
82
|
+
end
|
83
|
+
|
84
|
+
# Convert number to string/byte
|
85
|
+
def self.i_to_byte_array(number, zero_fill = 0)
|
86
|
+
ba = ''
|
87
|
+
n = number.to_i
|
88
|
+
while n > 256
|
89
|
+
r = n % 256
|
90
|
+
ba += r.chr
|
91
|
+
n = (n - r) / 256
|
92
|
+
end
|
93
|
+
ba += n.chr
|
94
|
+
|
95
|
+
# fill with zeroes
|
96
|
+
if ba.size < zero_fill
|
97
|
+
fill_count = zero_fill - ba.size
|
98
|
+
ba += 0.chr * fill_count
|
99
|
+
end
|
100
|
+
# reverse magic
|
101
|
+
|
102
|
+
ba.reverse
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,216 @@
|
|
1
|
+
require 'rufus/scheduler'
|
2
|
+
require 'mean'
|
3
|
+
|
4
|
+
# All stuff related to data manipulation and storage
|
5
|
+
|
6
|
+
module MeasReceiver
|
7
|
+
class MeasTypeBuffer
|
8
|
+
def initialize(_meas_type)
|
9
|
+
@meas_type = _meas_type
|
10
|
+
|
11
|
+
@coefficients = _meas_type.coefficients
|
12
|
+
@storage = _meas_type.storage
|
13
|
+
# index from which start storage algorithm
|
14
|
+
@storage_last_i = 0
|
15
|
+
# last storage buffer
|
16
|
+
@storage_buffer = Array.new
|
17
|
+
|
18
|
+
@buffer = Array.new
|
19
|
+
@size = 0
|
20
|
+
|
21
|
+
@mutex = Mutex.new
|
22
|
+
end
|
23
|
+
|
24
|
+
attr_accessor :buffer, :time_from, :time_to, :coefficients, :storage
|
25
|
+
|
26
|
+
attr_reader :storage_last_i, :storage_buffer, :size
|
27
|
+
|
28
|
+
# add raw value
|
29
|
+
def add!(v)
|
30
|
+
@mutex.synchronize do
|
31
|
+
@size += 1
|
32
|
+
@buffer << v
|
33
|
+
@time_from ||= Time.now
|
34
|
+
@time_to = Time.now
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def interval
|
39
|
+
(@time_to - @time_from).to_f / @size.to_f
|
40
|
+
end
|
41
|
+
|
42
|
+
def [](i)
|
43
|
+
return nil if i < 0 and i >= @size
|
44
|
+
{ time: @time_from + interval * i, raw: @buffer[i], value: raw_to_value(@buffer[i]) }
|
45
|
+
end
|
46
|
+
|
47
|
+
def raw_to_value(raw)
|
48
|
+
_r = raw.to_f * @coefficients[:linear].to_f + @coefficients[:offset]
|
49
|
+
if @coefficients[:proc]
|
50
|
+
_r = @coefficients[:proc].call(_r)
|
51
|
+
end
|
52
|
+
return _r
|
53
|
+
end
|
54
|
+
|
55
|
+
def first
|
56
|
+
self[0]
|
57
|
+
end
|
58
|
+
|
59
|
+
def last
|
60
|
+
self[@size - 1]
|
61
|
+
end
|
62
|
+
|
63
|
+
# Get measurement index for given time
|
64
|
+
def index_by_time(_time)
|
65
|
+
return nil if _time < @time_from or _time > @time_to
|
66
|
+
return ((_time.to_f - @time_from.to_f)/interval.to_f).round
|
67
|
+
end
|
68
|
+
|
69
|
+
# Search measurement in buffer
|
70
|
+
def at(_time)
|
71
|
+
_i = index_by_time(_time)
|
72
|
+
return nil if _i.nil?
|
73
|
+
return self[_i]
|
74
|
+
end
|
75
|
+
|
76
|
+
# Average/mean raw within buffer
|
77
|
+
def mean_raw(i, count = @storage[:avg_side_count])
|
78
|
+
return @buffer[i] if count == 0
|
79
|
+
|
80
|
+
_from = i - count
|
81
|
+
_to = i + count
|
82
|
+
|
83
|
+
# not in range
|
84
|
+
_from = 0 if _from < 0
|
85
|
+
_to = @buffer.size - 1 if _to >= @buffer.size
|
86
|
+
_a = @buffer[_from..._to]
|
87
|
+
|
88
|
+
return _a.mean
|
89
|
+
end
|
90
|
+
|
91
|
+
# Get mean value within buffer
|
92
|
+
def mean_value(i, count = @storage[:avg_side_count])
|
93
|
+
raw_to_value(mean_raw(i, count))
|
94
|
+
end
|
95
|
+
|
96
|
+
### STORAGE
|
97
|
+
|
98
|
+
# Executed by scheduler to store important values
|
99
|
+
def perform_storage!
|
100
|
+
@mutex.synchronize do
|
101
|
+
_range = storage_calculate_range
|
102
|
+
_avg = storage_calculate_averaged(_range)
|
103
|
+
_indexes = storage_get_ranges_to_store(_avg, _range)
|
104
|
+
_m = storage_measurements_to_store(_indexes, _range)
|
105
|
+
@storage_buffer = _m
|
106
|
+
|
107
|
+
# mark from where continue next time, it is r
|
108
|
+
if _indexes.size > 0
|
109
|
+
@storage_last_i = _indexes.last[1] + @storage_last_i.to_i
|
110
|
+
end
|
111
|
+
|
112
|
+
# call proc
|
113
|
+
if @storage[:proc]
|
114
|
+
@storage[:proc].call(@storage_buffer)
|
115
|
+
end
|
116
|
+
|
117
|
+
return @storage_buffer
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# Calculate range to storage algorithm
|
122
|
+
def storage_calculate_range
|
123
|
+
# from where continue
|
124
|
+
_from = @storage_last_i
|
125
|
+
# only check measurements up to
|
126
|
+
_to = @buffer.size - 1
|
127
|
+
return _from.._to
|
128
|
+
end
|
129
|
+
|
130
|
+
# Prepare averaged values
|
131
|
+
def storage_calculate_averaged(_range)
|
132
|
+
_range.collect { |i| mean_value(i) }
|
133
|
+
end
|
134
|
+
|
135
|
+
# Check value deviation
|
136
|
+
def storage_should_store_value?(value_a, value_b)
|
137
|
+
return true if value_a.nil? or value_b.nil?
|
138
|
+
(value_a - value_b).abs > @storage[:value_deviation].to_f
|
139
|
+
end
|
140
|
+
|
141
|
+
# Store if value if different more than X and is newer tan Y, force when it is newer than Z
|
142
|
+
def storage_should_store?(_value, _ref_value, _value_time, _ref_value_time)
|
143
|
+
_ref_value_nil = _ref_value.nil?
|
144
|
+
_value_diff = storage_should_store_value?(_ref_value, _value)
|
145
|
+
_time_min = (_value_time - _ref_value_time).abs > @storage[:min_unit_interval]
|
146
|
+
_time_max = (_value_time - _ref_value_time) > @storage[:max_unit_interval]
|
147
|
+
_r = ((_value_diff and _time_min) or _time_max)
|
148
|
+
# puts "#{_r} - v: #{_value}, #{_ref_value}, #{_value_time}, #{_ref_value_time} -> #{_ref_value_nil} or (#{_value_diff} and #{_time_min}) or #{_time_max}"
|
149
|
+
return _r
|
150
|
+
end
|
151
|
+
|
152
|
+
# Array of indexes measurements to store
|
153
|
+
def storage_get_ranges_to_store(_values, _range)
|
154
|
+
_array = Array.new
|
155
|
+
|
156
|
+
_from = 0
|
157
|
+
_ref_value = _values.first
|
158
|
+
|
159
|
+
(0...(_values.size)).each do |_time|
|
160
|
+
_value = _values[_time]
|
161
|
+
|
162
|
+
if storage_should_store?(_value, _ref_value, _time, _from)
|
163
|
+
_array << [_from, _time]
|
164
|
+
|
165
|
+
_from = _time
|
166
|
+
_ref_value = _value
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
return _array
|
171
|
+
end
|
172
|
+
|
173
|
+
# Fill time_from using previous measurement
|
174
|
+
def storage_measurements_to_store(_indexes, _range)
|
175
|
+
# need to add @storage_last_i
|
176
|
+
r = _indexes.collect { |is|
|
177
|
+
_from = self[is[0] + @storage_last_i]
|
178
|
+
_to = self[is[1] + @storage_last_i]
|
179
|
+
|
180
|
+
_m = _from.clone
|
181
|
+
_m[:time_from] = _m[:time]
|
182
|
+
_m[:time_to] = _to[:time]
|
183
|
+
_m.delete(:time)
|
184
|
+
|
185
|
+
_m
|
186
|
+
}
|
187
|
+
return r
|
188
|
+
end
|
189
|
+
|
190
|
+
### CLEANING
|
191
|
+
|
192
|
+
# Clean measurements from buffer older than X seconds from last archived one
|
193
|
+
def clean_up!(_interval = 10*60)
|
194
|
+
_before_count = (_interval / self.interval).round
|
195
|
+
clean_up_stored!(_before_count)
|
196
|
+
end
|
197
|
+
|
198
|
+
def clean_up_stored!(_before = 0)
|
199
|
+
_i = @storage_last_i - _before
|
200
|
+
_i = 0 if _i < 0
|
201
|
+
clean_up_to!(_i)
|
202
|
+
end
|
203
|
+
|
204
|
+
# Remove everything before "i"
|
205
|
+
def clean_up_to!(i)
|
206
|
+
@mutex.synchronize do
|
207
|
+
_interval = self.interval
|
208
|
+
@buffer = @buffer[i..-1]
|
209
|
+
@size -= i
|
210
|
+
@storage_last_i -= i
|
211
|
+
@time_from += _interval * i
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
end
|
216
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'rufus/scheduler'
|
2
|
+
require 'meas_receiver/meas_type_buffer'
|
3
|
+
|
4
|
+
module MeasReceiver
|
5
|
+
class MeasTypeReceiver
|
6
|
+
|
7
|
+
SCH_MIN_INTERVAL = 0.05
|
8
|
+
DEFAULT_FETCH_INTERVAL = 0.5
|
9
|
+
MIN_FETCH_INTERVAL = 0.1
|
10
|
+
|
11
|
+
def initialize(_options)
|
12
|
+
@options = _options
|
13
|
+
|
14
|
+
@fetch_interval = _options[:fetch_interval] || DEFAULT_FETCH_INTERVAL
|
15
|
+
@fetch_interval = MIN_FETCH_INTERVAL if @fetch_interval < MIN_FETCH_INTERVAL
|
16
|
+
|
17
|
+
@command = _options[:command]
|
18
|
+
@response_size = _options[:response_size]
|
19
|
+
@coefficients = _options[:coefficients] || Hash.new
|
20
|
+
@coefficients[:linear] ||= 1.0
|
21
|
+
@coefficients[:offset] ||= 0.0
|
22
|
+
@storage = _options[:storage] || Hash.new
|
23
|
+
@storage[:min_time_interval] ||= @fetch_interval
|
24
|
+
@storage[:min_unit_interval] = (@storage[:min_time_interval] / @fetch_interval).floor
|
25
|
+
@storage[:max_time_interval] ||= 3600
|
26
|
+
@storage[:max_unit_interval] = (@storage[:max_time_interval] / @fetch_interval).floor
|
27
|
+
@storage[:store_interval] ||= 10*60 #2*3600
|
28
|
+
|
29
|
+
@comm_object = CommProtocol.new(@command, @response_size)
|
30
|
+
@meas_buffer = MeasTypeBuffer.new(self)
|
31
|
+
end
|
32
|
+
|
33
|
+
attr_reader :fetch_interval, :command, :response_size, :coefficients, :storage
|
34
|
+
|
35
|
+
def start
|
36
|
+
@scheduler = Rufus::Scheduler.start_new(frequency: SCH_MIN_INTERVAL)
|
37
|
+
@scheduler.every "#{@fetch_interval}s" do
|
38
|
+
fetch
|
39
|
+
end
|
40
|
+
|
41
|
+
@scheduler.every "#{@storage[:store_interval]}s" do
|
42
|
+
store
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def stop
|
47
|
+
@scheduler.stop
|
48
|
+
end
|
49
|
+
|
50
|
+
def fetch
|
51
|
+
v = @comm_object.g
|
52
|
+
puts v
|
53
|
+
@meas_buffer.add!(v)
|
54
|
+
return v
|
55
|
+
end
|
56
|
+
|
57
|
+
def store
|
58
|
+
@meas_buffer.perform_storage!
|
59
|
+
end
|
60
|
+
|
61
|
+
def clean
|
62
|
+
@meas_buffer.clean_up!
|
63
|
+
end
|
64
|
+
|
65
|
+
attr_reader :meas_buffer
|
66
|
+
|
67
|
+
def [](i)
|
68
|
+
@meas_buffer[i]
|
69
|
+
end
|
70
|
+
|
71
|
+
def first
|
72
|
+
@meas_buffer.first
|
73
|
+
end
|
74
|
+
|
75
|
+
def last
|
76
|
+
@meas_buffer.last
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: home_io_meas_receiver
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Aleksander Kwiatkowski
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-04 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rufus-scheduler
|
16
|
+
requirement: &23002300 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *23002300
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mean
|
27
|
+
requirement: &23001820 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *23001820
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
requirement: &23001260 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.3.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *23001260
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: &23000780 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.0.0
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *23000780
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: jeweler
|
60
|
+
requirement: &22947700 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ~>
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 1.6.4
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *22947700
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: &22945600 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *22945600
|
80
|
+
description: Receive measurements from IoServer (part if HomeIO).
|
81
|
+
email: bobikx@poczta.fm
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files:
|
85
|
+
- LICENSE.txt
|
86
|
+
- README.rdoc
|
87
|
+
files:
|
88
|
+
- Gemfile
|
89
|
+
- Gemfile.lock
|
90
|
+
- LICENSE.txt
|
91
|
+
- README.rdoc
|
92
|
+
- Rakefile
|
93
|
+
- VERSION
|
94
|
+
- lib/meas_receiver.rb
|
95
|
+
- lib/meas_receiver/comm_protocol.rb
|
96
|
+
- lib/meas_receiver/meas_type_buffer.rb
|
97
|
+
- lib/meas_receiver/meas_type_receiver.rb
|
98
|
+
- lib/meas_receiver/receiver.rb
|
99
|
+
homepage: http://github.com/akwiatkowski/meas_receiver
|
100
|
+
licenses:
|
101
|
+
- LGPLv3
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ! '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
hash: 1437030628123017691
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 1.8.15
|
124
|
+
signing_key:
|
125
|
+
specification_version: 3
|
126
|
+
summary: Measurements receiver
|
127
|
+
test_files: []
|