-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_indoor_air_condition.rb
executable file
·141 lines (121 loc) · 4.3 KB
/
get_indoor_air_condition.rb
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
require 'aws-sdk-s3'
require "csv"
require 'fileutils'
require 'json'
require './lib/bme280'
require './lib/mh_z19b'
require 'faraday'
class IndoorAirMeasure
attr_reader :executed_time
def execute
@executed_time = Time.now
bme280_measured_value = retrive_bme280_measured_value
mh_z19b_measured_value = retrive_mh_z19b_measured_value
measured_value = bme280_measured_value.merge(mh_z19b_measured_value)
save_log(measured_value)
notify_slack_channels(measured_value)
end
private
def retrive_bme280_measured_value
bme280 = Bme280.new
begin
bme280_response = bme280.value
temperature = bme280_response[:temperature]
humidity = bme280_response[:humidity]
pressure = bme280_response[:pressure]
rescue => exception
temperature = humidity = pressure = 0
end
puts "#{Time.now}: temperature: #{temperature}, pressure: #{pressure}, humidity: #{humidity}"
{
temperature: temperature,
humidity: humidity,
pressure: pressure,
}
end
def retrive_mh_z19b_measured_value
mh_z19b = Mh_z19b.new("/dev/ttyAMA0", 9600, 8, 1, 0)
begin
mh_z19b_response = mh_z19b.read_CO2_concentration
co2_concentration = mh_z19b_response[:co2]
rescue => exception
$stderr.puts "Error!: #{exception}"
co2_concentration = 0
end
puts "#{Time.now}: co2_concentration: #{co2_concentration}"
{
co2_concentration: co2_concentration
}
end
def save_log(measured_value)
log_file_name = "#{executed_time.strftime("%Y%m%d")}.csv"
log_file_directory = File.expand_path("./log/#{executed_time.year}/#{executed_time.strftime("%m")}", __dir__)
log_file_path = "#{log_file_directory}/#{log_file_name}"
unless File.exists?(log_file_path)
FileUtils.mkdir_p(log_file_directory)
CSV.open(log_file_path, "wb", force_quotes: true) do |row|
row << [
"measuring_time",
"temperature", # C
"pressure", # hPa
"humidity", # percentage
"co2_concentration" # ppm
]
end
end
CSV.open(log_file_path, "a", force_quotes: true) do |row|
row << [
executed_time.strftime("%Y-%m-%d %H:%M:%S"),
measured_value[:temperature],
measured_value[:pressure],
measured_value[:humidity],
measured_value[:co2_concentration]
]
end
upload_log_to_s3(log_file_name, log_file_path)
end
def upload_log_to_s3(log_file_name, log_file_path)
s3 = Aws::S3::Resource.new(region: 'ap-northeast-1')
s3_object_path = "log/indoor_environment/#{executed_time.year}/#{executed_time.strftime("%m")}/#{log_file_name}"
s3_object = s3.bucket(ENV['S3_LOG_BUCKET']).object(s3_object_path)
s3_object.upload_file(log_file_path)
end
def notify_slack_channels(measured_value)
notify_slack_air_channel(measured_value)
notify_slack_co2_channel(measured_value)
end
def notify_slack_air_channel(measured_value)
air_channel_url = ENV["SLACK_INDOOR_AIR_CANNEL"]
return unless air_channel_url
puts "#{Time.now}: post slack on air_channel start..."
post_result = Faraday.post(
air_channel_url,
{text: "
*******************************************
CO2: #{measured_value[:co2_concentration]} ppm\n
Temperature: #{measured_value[:temperature]} 度\n
Pressure: #{measured_value[:pressure]} hPa\n
Humidity: #{measured_value[:humidity]} %\n
at #{executed_time.strftime("%Y-%m-%d %H:%M:%S")}
"}.to_json,
"Content-Type" => "application/json"
)
puts "#{Time.now}: post slack on air_channel finished successfully!"
puts "#{Time.now}: #{post_result.inspect}"
end
def notify_slack_co2_channel(measured_value)
co2_channel_url = ENV['SLACK_INDOOR_CO2_CANNEL']
return unless co2_channel_url
return unless measured_value[:co2_concentration] >= 1000
puts "#{Time.now}: post slack on co2_channel start..."
post_result = Faraday.post(
co2_channel_url,
{text: "CO2: #{measured_value[:co2_concentration]} ppm at #{executed_time.strftime("%Y-%m-%d %H:%M:%S")}"}.to_json,
"Content-Type" => "application/json"
)
puts "#{Time.now}: post slack on co2_channel finished successfully!"
puts "#{Time.now}: #{post_result.inspect}"
end
end
measure = IndoorAirMeasure.new
measure.execute