Skip to content

Instantly share code, notes, and snippets.

@vigevenoj
Last active May 2, 2017 18:34
Show Gist options
  • Save vigevenoj/b784f1709aa2257ae2de to your computer and use it in GitHub Desktop.
Save vigevenoj/b784f1709aa2257ae2de to your computer and use it in GitHub Desktop.
Get next-day sunrise/sunset information for most-recent owntracks location push
require 'mqtt'
require 'solareventcalculator'
require 'json'
class LocationBasedSunrise
Version = '0.0.1'
def initialize(arguments, stdin)
@client = MQTT::Client.new('mqtts://sharkbaitextraordinaire.com:8883', :ssl => :TLSv1)
@client.ssl = true
@client.ca_file = 'mqtt_ca.crt'
@client.username = 'jacob'
@client.password = 'lolbutts'
end
def is_location_outdated?(location_timestamp)
location_timestamp < Time.now - (60 * 60 * 6)
end
# Consider location outdated if longer than 6 hours ago
def isLocationOutdated(location_timestamp)
location_timestamp < Time.now - (60 * 60 * 6)
end
def print_next_sunset
civil_sunset = calc.compute_civil_sunset('America/Los_Angeles')
puts "civil sunset at #{civil_sunset}"
end
def print_next_sunrise
civil_sunrise = calc.compute_civil_sunrise('America/Los_Angeles')
puts "civil sunrise at #{civil_sunrise}"
end
# If current time is before sunset, show next sunset and next sunrise
# If current time is after sunset, show next sunrise and next sunset
def show_sunrise_sunset_or_sunset_sunrise
@calc = SolarEventCalculator.new(date, latitude, longitude)
civil_sunset = calc.compute_civil_sunset('America/Los_Angeles')
if civil_sunset < datetime
print_next_sunset
print_next_sunrise
else
calc = SolarEventCalculator.new(date +1 , latitude, longitude)
print_next_sunrise
print_next_sunset
end
end
def parse_message(message)
parsed = JSON.parse(message)
if(parsed["_type"] == "location")
@latitude = BigDecimal( parsed["lat"] )
@longitude = BigDecimal( parsed["lon"] )
@location_timestamp = Time.at(parsed["tst"].to_i)
end
end
def run
@client.connect()
topic,message = @client.get('owntracks/#')
begin
parse_message(message)
puts sprintf("At %.2f, %.2f", @latitude, @longitude)
show_sunrise_sunset_or_sunset_sunrise(new DateTime.now)
rescue Exception => e
puts e.message
puts e.backtrace.inspect
end
@client.disconnect()
end
end
app = LocationBasedSunrise.new(ARGV, STDIN)
app.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment