dummy_thermo 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 97c2f5218be2c54008bfa16fcec49516f961d7e7
4
+ data.tar.gz: daf028df1ae54c1c0e16d92ab642267c9089bbf8
5
+ SHA512:
6
+ metadata.gz: 3a80674692b114037d10b836e18439be163feb9f516b21a5c0f50667606e663d029e077d0b9aa61411b5b3773e2cd076a42b24934db92d158784d59ab78f5e72
7
+ data.tar.gz: 346f0c75e80791ff0b8378f1a0e54a24f6d987b5cdf7bd0660f13399d44bff9063ea63ebbdd50cb00a3d4ad674ce8ab48bb1cc1fdd9b09ced2ee778d8597b3e5
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ Gemfile.lock
31
+ # .ruby-version
32
+ # .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dummy_thermo.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Tetsuya Hirota
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ # DummyThermo
2
+
3
+ Data generator of dummy thermo sensor.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'dummy_thermo'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install dummy_thermo
20
+
21
+ ## Usage
22
+
23
+ #### Configuration
24
+
25
+ Deploy [dummy_thermo.yml](https://github.com/constdrop/dummy_thermo/blob/master/config/dummy_thermo.yml) to your application's config directory, and edit you like.
26
+
27
+ #### Sample code
28
+
29
+ sensor = DummyThermo::Sensor.new([configuration name])
30
+ sensor.gen # => ex. 23.4 (temparature value based on your configuration)
31
+
32
+ t = Time.new(2015, 10, 25, 12)
33
+ sensor.gen(t, t - 30, 21.0) # => ex. 21.3 (temparature value compared recent time and value(2nd, 3rd args))
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/constdrop/dummy_thermo.
44
+
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dummy_thermo"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ #
2
+ # dummy_thermo gems configuration file
3
+ #
4
+ # Format:
5
+ # <configuration name>:
6
+ # <number of month>:
7
+ # max: <maximum temparature of the month>
8
+ # min: <minimum temparature of the month>
9
+ # maxhour: <the hottest hour in a day>
10
+ # minhour: <the coldest hour in a day>
11
+ #
12
+ # Data type of parameters:
13
+ # * configuration name: string (alphabet, numbers and "_")
14
+ # * number of month: commma separated numbers and ranges
15
+ # * max, min: folat
16
+ # * maxhour, minhour: integer
17
+
18
+ default:
19
+ 1 .. 12:
20
+ max: 25.0
21
+ min: 15.0
22
+ maxhour: 14
23
+ minhour: 5
24
+
25
+ outdoor:
26
+ 1:
27
+ max: 10
28
+ min: -5
29
+ maxhour: 14
30
+ minhour: 6
31
+ 3 .. 5, 10:
32
+ max: 20
33
+ min: 5
34
+ 7, 8:
35
+ max: 35
36
+ min: 20
37
+ minhour: 4
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dummy_thermo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dummy_thermo"
8
+ spec.version = DummyThermo::VERSION
9
+ spec.authors = ["Tetsuya Hirota"]
10
+ spec.email = ["const.drop@gmail.com"]
11
+
12
+ spec.summary = %q{Data generator of dummy thermo sensor.}
13
+ spec.homepage = "https://github.com/constdrop/dummy_thermo"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "random_bell"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,222 @@
1
+ require "dummy_thermo/version"
2
+ require 'date'
3
+ require 'rational'
4
+ require 'yaml'
5
+ require 'random_bell'
6
+
7
+ module DummyThermo
8
+ class Sensor
9
+ include Math
10
+
11
+ STDCONF = {
12
+ max: 25.0, min: 15.0,
13
+ maxhour: 14, minhour: 5
14
+ }
15
+
16
+ attr_reader :conf
17
+
18
+ def initialize( name = "default" )
19
+ yaml = YAML.load_file 'config/dummy_thermo.yml'
20
+
21
+ if name == "default"
22
+ c = yaml.key?("default") ? yaml["default"] : yaml[yaml.keys[0]]
23
+ expand_conf(c)
24
+ else
25
+ raise "Named '#{name}' configuration is not found." unless yaml.key?(name)
26
+ expand_conf(yaml[name])
27
+ end
28
+ end
29
+
30
+ def gen(at_time = Time.now, recent_time = 0, recent_val = 0)
31
+ at_time = at_time.to_time if at_time.class == DateTime
32
+ recent_time = recent_time.to_time if recent_time.class == DateTime
33
+
34
+ c = conf_of_day(at_time)
35
+
36
+ lt8 = (c[:max] - c[:min]) / (100 / 8)
37
+ max8 = c[:max] - lt8
38
+ min8 = c[:min] + lt8
39
+
40
+ if recent_time == 0 || recent_time - at_time > 300
41
+ today = Time.new(at_time.year, at_time.month, at_time.day)
42
+ r = if at_time.hour >= c[:minhour] && at_time.hour < c[:maxhour]
43
+ -0.5 + ((at_time - today) - c[:minhour] * 3600) / ((c[:maxhour] - c[:minhour]) * 3600)
44
+ else
45
+ 0.5 + ((at_time - today) - c[:maxhour] * 3600) / ((c[:minhour] + 24 - c[:maxhour]) * 3600)
46
+ end
47
+
48
+ a = ((max8 - min8) / 2) * sin(r) + (max8 + min8) / 2
49
+
50
+ return RandomBell.new(mu: a, sigma: lt8, range: c[:min] .. c[:max]).rand
51
+ end
52
+
53
+ h = if recent_val > c[:max]
54
+ { mu: -0.01, sigma: 0.03, range: -0.07 .. 0.04 }
55
+ elsif recent_val > max8 && recent_val <= c[:max]
56
+ { mu: -0.005, sigma: 0.02, range: -0.07 .. 0.04 }
57
+ elsif recent_val < min8 && recent_val >= c[:min]
58
+ { mu: 0.005, sigma: 0.02, range: -0.04 .. 0.07 }
59
+ elsif recent_val < c[:min]
60
+ { mu: 0.01, sigma: 0.03, range: -0.04 .. 0.07 }
61
+ else
62
+ adj = if at_time.hour >= c[:minhour] && at_time.hour < c[:maxhour]
63
+ 0.0005
64
+ else
65
+ -0.0003
66
+ end
67
+ { mu: adj, sigma: 0.01, range: -0.07 .. 0.07 }
68
+ end
69
+
70
+ bell = RandomBell.new(h)
71
+
72
+ v = recent_val
73
+ ((at_time - recent_time) / 3 + 1).to_i.times{ v += bell.rand }
74
+
75
+ return v
76
+ end
77
+
78
+ private
79
+ #
80
+ # expand YAML configuration to hash array of 12 months
81
+ #
82
+ def expand_conf(c)
83
+ @conf = []
84
+
85
+ c.each do |k,v|
86
+ k = k.to_s if k.class != String
87
+
88
+ if k.match(/[^0-9., ]/)
89
+ raise "Illegal key is set in sensor_data_generator.yml"
90
+ end
91
+
92
+ # import configurations to hases array
93
+ k.split(",").each do |i|
94
+ if i.include?("..")
95
+ a = i.split("..")
96
+ ( a[0].to_i .. a[1].to_i ).each{ |j| @conf[j] = v }
97
+ else
98
+ @conf[i.to_i] = v
99
+ end
100
+ end
101
+ end
102
+
103
+ # if not defined all month, set STDCONF and exit
104
+ if @conf.size == 0
105
+ (1 .. 12).each { |i| @conf[i] = STDCONF }
106
+ return(@conf)
107
+ end
108
+
109
+ # convert all keys to symbol
110
+ @conf.each do |h|
111
+ next unless h
112
+ h.replace( Hash[ h.map{ |k, v| [k.to_sym, v] } ] )
113
+ end
114
+
115
+ # set blank hash to the nil columns
116
+ (1..12).each{ |i| @conf[i] ||= {} }
117
+
118
+ # set parameters every month nil keys
119
+ STDCONF.each_key do |k|
120
+ # get max and min
121
+ max = @conf[1..12].select{ |h| h[k] }.map{ |h| h[k] }.max
122
+ min = @conf[1..12].select{ |h| h[k] }.map{ |h| h[k] }.min
123
+
124
+ # if max == min that is meant set only one parameter,
125
+ # set same parameter every month
126
+ if max == min
127
+ @conf[1..12].each{ |h| h[k] = max }
128
+ next
129
+ end
130
+
131
+ # set average of previous month and next month
132
+ @conf[1..12].each_with_index do |h, i|
133
+ next if h[k]
134
+
135
+ d = Date.new(2015, i + 1, 1)
136
+
137
+ pm, pv = 0, 0
138
+ (1..11).each do |j|
139
+ pm = j
140
+ if pv = @conf[d.prev_month(pm).month][k]
141
+ break
142
+ end
143
+ end
144
+ nm, nv = 0, 0
145
+ (1..11).each do |j|
146
+ nm = j
147
+ if nv = @conf[d.next_month(nm).month][k]
148
+ break
149
+ end
150
+ end
151
+
152
+ h[k] = (nm / (nm + pm)) * pv + (pm / (nm + pm)) * nv
153
+
154
+ # set value to less than 10% difference of max - min,
155
+ # previous or next month of max or min
156
+ if (pv == max || pv == min || nv == max || nv == min) && (pm > 1 || nm > 1)
157
+ lt10 = (max - min) / 10
158
+ pm1, pv1 = d.prev_month(pm - 1).month, nil
159
+ nm1, nv1 = d.next_month(nm - 1).month, nil
160
+
161
+ if (pv == max || pv == min) && pm > 1
162
+ xv = ((nm + pm - 1) / (nm + pm)) * pv + (1 / (nm + pm)) * nv
163
+ if lt10 > (pv - xv).abs
164
+ pv1 = xv
165
+ else
166
+ pv1 = pv == max ? max - lt10 : min + lt10
167
+ end
168
+ @conf[pm1][k] = pv1
169
+ end
170
+
171
+ if (nv == max || nv == min) && nm > 1
172
+ xv = (1 / (nm + pm)) * pv + ((nm + pm - 1) / (nm + pm)) * nv
173
+ if lt10 > (nv - xv).abs
174
+ nv1 = xv
175
+ else
176
+ nv1 = nv == max ? max - lt10 : min + lt10
177
+ end
178
+ @conf[nm1][k] = nv1
179
+ end
180
+
181
+ pm, pv = [pm1, pv1] if pv1
182
+ nm, nv = [nm1, nv1] if nv1
183
+
184
+ h[k] = (nm / (nm + pm)) * pv + (pm / (nm + pm)) * nv
185
+ end
186
+ end
187
+ end
188
+ end
189
+
190
+ #
191
+ # get configuration hash of the day
192
+ #
193
+ def conf_of_day(d)
194
+ # type check and cast
195
+ d = d.to_date if d.class == Time
196
+ d = Date.strptime(d) if d.class == String
197
+ raise TypeError if d.class != Date
198
+
199
+ # configured by yaml date set to 15th day of the month
200
+ p, n = d.day < 15 ? [(d << 1), d] : [d, (d >>1)]
201
+
202
+ prev_date = Date.new(p.year, p.month, 15)
203
+ next_date = Date.new(n.year, n.month, 15)
204
+
205
+ # calcurate configuration of the day from prev and next month's configurations
206
+ d_diff = next_date - prev_date
207
+ d_past = d - prev_date
208
+ d_come = next_date - d
209
+
210
+ prev_conf = @conf[prev_date.month]
211
+ next_conf = @conf[next_date.month]
212
+
213
+ d_conf = {}
214
+ prev_conf.keys.each do |k|
215
+ v = (d_come/d_diff) * prev_conf[k] + (d_past/d_diff) * next_conf[k]
216
+ d_conf[k.to_sym] = v.to_f
217
+ end
218
+
219
+ d_conf
220
+ end
221
+ end
222
+ end
@@ -0,0 +1,3 @@
1
+ module DummyThermo
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dummy_thermo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tetsuya Hirota
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: random_bell
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - const.drop@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - config/dummy_thermo.yml
86
+ - dummy_thermo.gemspec
87
+ - lib/dummy_thermo.rb
88
+ - lib/dummy_thermo/version.rb
89
+ homepage: https://github.com/constdrop/dummy_thermo
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.4.8
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Data generator of dummy thermo sensor.
113
+ test_files: []