forked from ossforumjp-app-IoT/rubyiot_server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_dummydata.rb
More file actions
98 lines (83 loc) · 1.85 KB
/
create_dummydata.rb
File metadata and controls
98 lines (83 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
require 'active_record'
require 'json'
require_relative './models'
SensorID = 9
START = Time.now - 5 * 24 * 60 * 60
INTERVAL = 3
SPAN = 5 * 24 * 60 * 60 / INTERVAL
rails_env = ENV["RAILS_ENV"] ? ENV["RAILS_ENV"].to_sym : :development
ActiveRecord::Base.configurations = YAML.load_file('db/database.yml')
ActiveRecord::Base.establish_connection(rails_env)
ActiveRecord::Base.default_timezone = :local
Time.zone = "Tokyo"
r = Random.new(Random.new_seed)
t = START
newest_sd = SensorData.where(device_property_id: SensorID).order(:measured_at).last
v = if newest_sd == nil
case t.mon
when 12, 1, 2; 16
when 3, 4, 10, 11; 18
when 5, 6, 9; 20
when 7, 8; 24
end
else
newest_sd.value.to_f * 0.1
end
(0..SPAN).each {
adj = case t.hour
when 15..23, 0..5
-0.0003
when 6..14
0.0005
end
v += r.rand * 0.04 - 0.02 + adj
t += INTERVAL
case t.mon
when 12, 1, 2
if v > 20
v -= 0.1
elsif v < 12
v += 0.1
end
when 3, 4, 10, 11
if v > 25
v -= 0.1
elsif v < 15
v += 0.1
end
when 5, 6, 9
if v > 27
v -= 0.1
elsif v < 20
v += 0.1
end
when 7, 8
if v > 33
v -= 0.1
elsif v < 25
v += 0.1
end
end
SensorData.create(
device_property_id: SensorID,
value: (v * 10).round.to_s,
measured_at: t
)
w = "device_property_id = #{SensorID.to_s}"
w += " AND updated_at < #{t.strftime("%Y-%m-%d %H:%M:%S")}"
if MonitorRange.exists?(w)
mons = MonitorRange.where(w)
min = BigDecimal(mons[0].min_value)
max = BigDecimal(mons[0].max_value)
val = BigDecimal(v * 10)
if val <= min || val >= max
alrt = SensorAlert.create(
device_property_id: id.to_i,
value: val.round.to_s,
monitor_min_value: mons[0].min_value,
monitor_max_value: mons[0].max_value,
measured_at: t
)
end
end
}