Skip to content

Instantly share code, notes, and snippets.

@nitinthewiz
Last active July 8, 2018 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nitinthewiz/1a91fc90e96dd262575c0665a1450215 to your computer and use it in GitHub Desktop.
Save nitinthewiz/1a91fc90e96dd262575c0665a1450215 to your computer and use it in GitHub Desktop.
weather API for compass
require 'date'
require 'httparty'
require 'json'
MY_COMPASS="https://compass"
COMPASS_LOCATION_READ_TOKEN=""
COMPASS_WEATHER_WRITE_TOKEN=""
DARK_SKY_API=""
data = JSON.parse(HTTParty.get(MY_COMPASS+'/api/last?token='+COMPASS_LOCATION_READ_TOKEN).body)
location = data['data']
timestamp = location['properties']['timestamp']
latitude = location['geometry']['coordinates'][1]
longitude = location['geometry']['coordinates'][0]
horizontal_accuracy = location['properties']['horizontal_accuracy']
vertical_accuracy = location['properties']['vertical_accuracy']
desired_accuracy = location['properties']['desired_accuracy']
speed = location['properties']['speed']
battery_state = location['properties']['battery_state']
pauses = location['properties']['pauses']
deferred = location['properties']['deferred']
significant_change = location['properties']['significant_change']
locations_in_payload = location['properties']['locations_in_payload']
battery_level = location['properties']['battery_level']
wifi = location['properties']['wifi']
activity = location['properties']['activity']
altitude = location['properties']['altitude']
device_id = location['properties']['device_id']
motion = location['properties']['motion']
if location['properties']['temp_f']
puts "No need to update weather information"
elsif latitude
url = "https://api.darksky.net/forecast/#{DARK_SKY_API}/#{latitude},#{longitude}"
weather = JSON.parse(HTTParty.get(url).body)
weather = {
:type => 'Feature',
:geometry => {
:type => 'Point',
:coordinates => [longitude, latitude]
},
:properties => {
:timestamp => timestamp,
:location_timezone => weather['timezone'],
:horizontal_accuracy => horizontal_accuracy,
:vertical_accuracy => vertical_accuracy,
:desired_accuracy => desired_accuracy,
:speed => speed,
:battery_state => battery_state,
:pauses => pauses,
:deferred => deferred,
:significant_change => significant_change,
:locations_in_payload => locations_in_payload,
:battery_level => battery_level,
:wifi => wifi,
:activity => activity,
:altitude => altitude,
:device_id => device_id,
:motion => motion,
:temp_f => weather['currently']['temperature'],
:humidity => weather['currently']['humidity'],
:pressure_mb => weather['currently']['pressure'],
:feelslike_f => weather['currently']['apparentTemperature'],
:description => weather['currently']['summary'],
:icon => weather['currently']['icon'],
:wind_mph => weather['currently']['windSpeed'],
:wind_gust_mph => weather['currently']['windGust'],
:precip_1hr_metric => weather['currently']['precipIntensity'],
}
}
result = HTTParty.post(MY_COMPASS+'/api/input', {
body: {
locations: [weather],
token: COMPASS_WEATHER_WRITE_TOKEN
}.to_json,
headers: {
'Content-type' => 'application/json'
}
})
puts result.body
end
@nitinthewiz
Copy link
Author

Updated from the first version to include a simple "No need to update weather information" if the weather data already exists. This way, you can run the job with cron every 2 minutes or so and not flood your database with a large number of duplicate entries when you're stationary or asleep.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment