-
Notifications
You must be signed in to change notification settings - Fork 0
/
airryr.rb
54 lines (48 loc) · 1.61 KB
/
airryr.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
# TODO: mockをロードするかどうかは ENV["TEST"] とかを参照したい
require_relative './co2mini_mock.rb'
require 'co2mini'
require 'dotenv/load'
require 'net/https'
require "json"
class Airryr
def initialize(watcher_class)
@watcher = watcher_class.new
@log_buffer = []
end
def post()
co2 = @watcher.read_once(:co2)
temp = @watcher.read_once(:temp)
now = Time.now.to_i
params = [
{name: "AirCondition.CO2", time: now, value: co2},
{name: "AirCondition.Temperature", time: now, value: temp}
]
send_to_mackerel(params)
end
def forever
@watcher.on(:co2) do |op, value|
@log_buffer.push({name: "AirCondition.CO2", time: Time.now.to_i, value: value})
self.push_log
end
@watcher.on(:temp) do |op, value|
@log_buffer.push({name: "AirCondition.Temperature", time: Time.now.to_i, value: value})
self.push_log
end
@watcher.loop
end
def push_log
return unless @log_buffer.first[:time] + ENV["SEND_INTERVAL_SECONDS"].to_i < Time.now.to_i
params = @log_buffer
@log_buffer = []
self.send_to_mackerel(params)
end
def send_to_mackerel(params)
uri = URI.parse("https://api.mackerelio.com/api/v0/services/#{ENV["MACKEREL_SERVICE_NAME"]}/tsdb")
header = {'X-Api-Key': ENV["MACKEREL_API_KEY"], 'Content-Type': 'application/json'}
response = Net::HTTP.post(uri, JSON.dump(params), header)
puts "status code:#{response.code} / body:#{response.body}"
end
end
# TODO: 起動時に引数とって `bundle exec airryr forever` とかできると便利っぽい
airryr = Airryr.new(CO2mini)
airryr.forever