Created
October 1, 2019 19:21
-
-
Save Wowfunhappy/36458696ead4b3f0e1a283314c6596a6 to your computer and use it in GitHub Desktop.
Pebble: Create Timeline Pins When It Will Rain with DarkSky API
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
var darkSkyApiKey = "" | |
/* | |
In theory (untested), this app should work once you (1) add an api key from dark sky and (2) replace the three fantasy functions with real code. | |
*/ | |
var UI = require('ui'); | |
var ajax = require('ajax'); | |
var blackStatusBar = { | |
color: "white", | |
backgroundColor: "black", | |
separator: 'none' | |
}; | |
//fantasyFunction.wakeup(60); //In fantasy land, this function causes the the app to wake up every 60 minutes. | |
// Request current position | |
navigator.geolocation.getCurrentPosition(getWeatherData, function() { | |
console.log("Could not get location"); | |
}, { | |
enableHighAccuracy: true, | |
maximumAge: 10000, | |
timeout: 1000 | |
}); | |
var weatherAlerts = []; | |
function getWeatherData(pos) { | |
ajax({ url: 'https://api.darksky.net/forecast/' + darkSkyApiKey + "/" + pos.coords.latitude + ',' + pos.coords.longitude + '?exclude=currently,minutely,daily,alerts,flags', type: 'json' }, function(data) { | |
for (i = 0; i < data.hourly.data.length; i++) { | |
if (data.hourly.data[i].icon === "rain" || data.hourly.data[i].icon === "snow" || data.hourly.data[i].icon === "sleet") { | |
icon = data.hourly.data[i].icon; | |
duration = 60; | |
while (data.hourly.data[i+1].icon === icon) { | |
i++; | |
duration = duration + 60; | |
} | |
if (icon === "rain") { | |
tinyIcon = "system://images/TIMELINE_HEAVY_RAIN"; | |
smallIcon = "system://images/TIMELINE_HEAVY_RAIN"; | |
largeIcon = "system://images/TIMELINE_HEAVY_RAIN"; | |
} | |
else if (icon === "snow") { | |
tinyIcon = "system://images/TIMELINE_HEAVY_SNOW"; | |
smallIcon = "system://images/TIMELINE_HEAVY_SNOW"; | |
largeIcon = "system://images/TIMELINE_HEAVY_SNOW"; | |
} | |
else if (icon === "sleet") { | |
tinyIcon = "system://images/TIMELINE_RAINING_AND_SNOWING"; | |
smallIcon = "system://images/TIMELINE_RAINING_AND_SNOWING"; | |
largeIcon = "system://images/TIMELINE_RAINING_AND_SNOWING"; | |
} | |
pin = { | |
"id": "wowfunhappy-will-it-rain-" + data.hourly.data[i].time, | |
"time": new Date(data.hourly.data[i].time * 1000).toISOString(), | |
"duration": duration, | |
"lastUpdated": new Date(Date.now()).toISOString(), | |
"layout": { | |
"type": "weatherPin", | |
"title": data.hourly.data[i].summary, | |
"locationName": "", //todo: show something useful here? | |
"tinyIcon": tinyIcon, | |
"smallIcon": smallIcon, | |
"largeIcon": largeIcon, | |
} | |
}; | |
//fantasyFunction.timeline.push(pin); | |
console.log(JSON.stringify(pin)); | |
} | |
else { | |
//fantasyFunction.timeline.delete("wowfunhappy-will-it-rain-" + data.hourly.data[i].time); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment