-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathair_horn.rb
38 lines (30 loc) · 999 Bytes
/
air_horn.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
# TODO:
# - Allow averaging of data from multiple sensors
require 'active_support/core_ext/string'
require 'awesome_print'
require 'faraday'
require 'json'
require 'pry-byebug'
require 'twilio-ruby'
require_relative 'lib/modules/conversion'
require_relative 'lib/sensor'
require_relative 'lib/text_message'
conversion = ENV['CONVERSION']
data = Sensor.new(
sensor_id: ENV['PURPLE_AIR_SENSOR_ID'],
conversion: conversion
).read
STDOUT.puts "."
unless data[:aqi_delta].zero?
STDOUT.puts ">>> level: #{data[:new_level]}, new: #{data[:new_value]}, old: #{data[:old_value]}, updated at: #{data[:updated_at]}"
if (data[:old_level] != data[:new_level])
body = <<~MESSAGE
Change in air quality!
Level: #{data[:new_level]}
PM 2.5: #{data[:new_value].round(2)} µg/m³ #{conversion.present? ? "(#{conversion})" : ''}
Updated: #{data[:updated_at]}
MESSAGE
STDOUT.puts "=====> Sending SMS"
TextMessage.new.send(body: body)
end
end