136 lines
4.4 KiB
JavaScript
136 lines
4.4 KiB
JavaScript
var APPID = 'd54c895c6e05649ee2ddef0d64532069';
|
|
|
|
var xhrRequest = function (url, type, callback) {
|
|
var xhr = new XMLHttpRequest();
|
|
xhr.onload = function () {
|
|
callback(this.responseText);
|
|
};
|
|
xhr.open(type, url);
|
|
xhr.send();
|
|
};
|
|
|
|
Pebble.addEventListener('showConfiguration', function() {
|
|
var url = 'http://singleserveapps.github.io/sliding-text-wd-config/index.html';
|
|
|
|
console.log('Showing configuration page: ' + url);
|
|
|
|
Pebble.openURL(url);
|
|
});
|
|
|
|
Pebble.addEventListener('webviewclosed', function(e) {
|
|
var configData = JSON.parse(decodeURIComponent(e.response));
|
|
|
|
console.log('Configuration page returned: ' + JSON.stringify(configData));
|
|
|
|
if (configData.backgroundColor) {
|
|
console.log("backgroundColor: %d", parseInt(configData.backgroundColor, 16));
|
|
console.log("textColor: %d", parseInt(configData.textColor, 16));
|
|
console.log("weatherFrequency: %d", parseInt(configData.weatherFrequency, 10));
|
|
console.log("useCelsius: %d", parseInt(configData.useCelsius, 2));
|
|
console.log("displayPrefix: %d", parseInt(configData.displayPrefix, 2));
|
|
console.log("weatherDateAlignment %s", parseInt(configData.weatherDateAlignment, 10));
|
|
console.log("hourMinutesAlignment %s", parseInt(configData.hourMinutesAlignment, 10));
|
|
|
|
// Assemble dictionary using our keys
|
|
var options_dictionary = {
|
|
"KEY_BACKGROUND_COLOR": parseInt(configData.backgroundColor, 16),
|
|
"KEY_TEXT_COLOR": parseInt(configData.textColor, 16),
|
|
"KEY_WEATHER_FREQUENCY": parseInt(configData.weatherFrequency, 10),
|
|
"KEY_USE_CELSIUS": parseInt(configData.useCelsius, 2),
|
|
"KEY_DISPLAY_O_PREFIX": parseInt(configData.displayPrefix, 2),
|
|
"KEY_WEATHERDATE_ALIGNMENT": parseInt(configData.weatherDateAlignment, 10),
|
|
"KEY_HOURMINUTES_ALIGNMENT": parseInt(configData.hourMinutesAlignment, 10)
|
|
};
|
|
|
|
//getWeather();
|
|
|
|
// Send to Pebble
|
|
Pebble.sendAppMessage(options_dictionary,
|
|
function(e) {
|
|
console.log("webviewclosed: Watchface options successfully sent to Pebble");
|
|
},
|
|
function(e) {
|
|
console.log("webviewclosed: Error sending watchface options info to Pebble!");
|
|
}
|
|
);
|
|
}
|
|
});
|
|
|
|
function locationSuccess(pos) {
|
|
// Construct URL
|
|
var url = 'http://api.openweathermap.org/data/2.5/weather?lat=' + pos.coords.latitude + '&lon=' + pos.coords.longitude + '&APPID=' + APPID;
|
|
|
|
console.log("Lat is " + pos.coords.latitude);
|
|
console.log("Lon is " + pos.coords.longitude);
|
|
|
|
try {
|
|
// Send request to forecast.io
|
|
xhrRequest(url, 'GET',
|
|
function(responseText) {
|
|
console.log("Parsing JSON");
|
|
// responseText contains a JSON object with weather info
|
|
var json = JSON.parse(responseText);
|
|
console.log(JSON.parse(responseText));
|
|
|
|
var temperature = Math.round(((json.main.temp - 273.15) * 1.8) + 32);
|
|
console.log("Temperature in Fahrenheit is " + temperature);
|
|
|
|
var temperaturec = Math.round(json.main.temp - 273.15);
|
|
console.log("Temperature in Celsius is " + temperaturec);
|
|
|
|
// Conditions
|
|
var conditions = json.weather[0].main;
|
|
console.log("Conditions are " + conditions);
|
|
|
|
// Assemble dictionary using our keys
|
|
var weather_dictionary = {
|
|
"KEY_TEMPERATURE": temperature,
|
|
"KEY_TEMPERATURE_IN_C": temperaturec,
|
|
"KEY_CONDITIONS": conditions,
|
|
};
|
|
|
|
// Send to Pebble
|
|
Pebble.sendAppMessage(weather_dictionary,
|
|
function(e) {
|
|
console.log("locationSuccess: Weather info sent to Pebble successfully!");
|
|
},
|
|
function(e) {
|
|
console.log("locationSuccess: Error sending weather info to Pebble!");
|
|
}
|
|
);
|
|
}
|
|
);
|
|
}
|
|
catch (exception){
|
|
console.log(JSON.stringify(exception));
|
|
}
|
|
}
|
|
|
|
function locationError(err) {
|
|
console.log('Error requesting location!');
|
|
}
|
|
|
|
function getWeather() {
|
|
navigator.geolocation.getCurrentPosition(
|
|
locationSuccess,
|
|
locationError,
|
|
{timeout: 15000, maximumAge: 120000}
|
|
);
|
|
}
|
|
|
|
Pebble.addEventListener('ready', function() {
|
|
console.log('PebbleKit JS Ready!');
|
|
|
|
//getWeather();
|
|
});
|
|
|
|
Pebble.addEventListener('appmessage',
|
|
function(e) {
|
|
console.log("Got message: " + JSON.stringify(e));
|
|
|
|
if (e.payload.KEY_GET_WEATHER) {
|
|
console.log('AppMessage received! Updating weather.');
|
|
getWeather();
|
|
}
|
|
}
|
|
); |