Created
September 20, 2020 00:22
-
-
Save walquis/c0c1b97c66c56f2d2390b47a0446b751 to your computer and use it in GitHub Desktop.
Simple open_weather implementation - used to collect temperature data for Nest stats website until sep 2019 when Lennox system installed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'httparty' | |
def outside_temp_f zip | |
# Only read the temp every ten minutes, or if no current-temperature-file established. | |
# | |
curr_temperature_fname = 'current-temperature-file' | |
if File.exists? curr_temperature_fname | |
age_in_seconds = DateTime.now.to_time.to_i - File.stat(curr_temperature_fname).mtime.to_time.to_i | |
# Is file less than ten minutes old? | |
if age_in_seconds < 600 | |
return File.read(curr_temperature_fname).strip.to_f | |
end | |
end | |
uri = 'http://api.openweathermap.org/data/2.5/weather' | |
appid = ENV['OPEN_WEATHER_APPID'] | |
response = HTTParty.get "#{uri}?APPID=#{appid}&zip=#{zip}" | |
# Kelvin to Fahrenheit ... | |
temp_f = (JSON.parse(response.body)['main']['temp'] * 1.8) - 459.67 | |
File.open(curr_temperature_fname, 'w') do |f| | |
puts "#{DateTime.now} - initializing or updating current-temperature-file with '#{temp_f}'..." | |
f.write temp_f.to_s | |
end | |
temp_f.to_f | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment