Upload files to "src/pkjs"
This commit is contained in:
377
src/pkjs/app.js
Normal file
377
src/pkjs/app.js
Normal file
@@ -0,0 +1,377 @@
|
||||
var BASE_CONFIG_URL = 'http://singleserveapps.github.io/analog-wd-config/';
|
||||
//var BASE_CONFIG_URL = 'http://singleserveapps.github.io/analog-wd-beta-config/';
|
||||
|
||||
var APPID = 'd54c895c6e05649ee2ddef0d64532069';
|
||||
|
||||
var weatherProvider = 'ow';
|
||||
var useWeatherGPS = 1;
|
||||
var weatherLoc;
|
||||
|
||||
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 rectURL = BASE_CONFIG_URL + 'index.html';
|
||||
var roundURL = BASE_CONFIG_URL + 'index.html';
|
||||
var watch;
|
||||
|
||||
if(Pebble.getActiveWatchInfo) {
|
||||
try {
|
||||
watch = Pebble.getActiveWatchInfo();
|
||||
} catch(err) {
|
||||
watch = {
|
||||
platform: "basalt",
|
||||
};
|
||||
}
|
||||
} else {
|
||||
watch = {
|
||||
platform: "aplite",
|
||||
};
|
||||
}
|
||||
|
||||
if(watch.platform == "aplite"){
|
||||
Pebble.openURL(rectURL);
|
||||
} else if(watch.platform == "chalk") {
|
||||
Pebble.openURL(roundURL);
|
||||
} else {
|
||||
Pebble.openURL(rectURL);
|
||||
}
|
||||
});
|
||||
|
||||
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: ", parseInt(configData.backgroundColor, 16));
|
||||
console.log("textColor: ", parseInt(configData.textColor, 16));
|
||||
console.log("hourHandColor: ", parseInt(configData.hourHandColor, 16));
|
||||
console.log("minuteHandColor: ", parseInt(configData.minuteHandColor, 16));
|
||||
console.log("secondsHandColor: ", parseInt(configData.secondsHandColor, 16));
|
||||
console.log("hourMarkersColor: ", parseInt(configData.hourMarkersColor, 16));
|
||||
console.log("minorMarkersColor: ", parseInt(configData.minorMarkersColor, 16));
|
||||
console.log("weatherFrequency: ", parseInt(configData.weatherFrequency, 10));
|
||||
console.log("useGPS: ", parseInt(configData.useGPS, 10));
|
||||
console.log("weatherLocation: ", configData.weatherLocation);
|
||||
console.log("shakeforLoHi: ", parseInt(configData.shakeforLoHi, 10));
|
||||
console.log("useCelsius: ", parseInt(configData.useCelsius, 10));
|
||||
console.log("displayDate: ", parseInt(configData.displayDate, 10));
|
||||
console.log("displayDigitalTime: ", parseInt(configData.displayDigitalTime, 10));
|
||||
console.log("weatherDateDTimeReadability: ", parseInt(configData.weatherDateDTimeReadability, 10));
|
||||
console.log("vibrateBT: ", parseInt(configData.vibrateBT, 10));
|
||||
console.log("useThinHands: ", parseInt(configData.useThinHands, 10));
|
||||
console.log("displayBattery: ", parseInt(configData.displayBattery, 10));
|
||||
console.log("displayHourDigits: ", parseInt(configData.displayHourDigits, 10));
|
||||
console.log("displaySecondsHand: ", parseInt(configData.displaySecondsHand, 10));
|
||||
|
||||
// Assemble dictionary using our keys
|
||||
var options_dictionary = {
|
||||
"KEY_BACKGROUND_COLOR": parseInt(configData.backgroundColor, 16),
|
||||
"KEY_TEXT_COLOR": parseInt(configData.textColor, 16),
|
||||
"KEY_HOUR_HAND_COLOR": parseInt(configData.hourHandColor, 16),
|
||||
"KEY_MINUTE_HAND_COLOR": parseInt(configData.minuteHandColor, 16),
|
||||
"KEY_SECONDS_HAND_COLOR": parseInt(configData.secondsHandColor, 16),
|
||||
"KEY_HOUR_MARKERS_COLOR": parseInt(configData.hourMarkersColor, 16),
|
||||
"KEY_MINOR_MARKERS_COLOR": parseInt(configData.minorMarkersColor, 16),
|
||||
"KEY_WEATHER_FREQUENCY": parseInt(configData.weatherFrequency, 10),
|
||||
"KEY_WEATHER_USE_GPS": parseInt(configData.useGPS, 10),
|
||||
"KEY_WEATHER_LOCATION": configData.weatherLocation,
|
||||
"KEY_SHAKE_FOR_LOHI": parseInt(configData.shakeforLoHi, 10),
|
||||
"KEY_USE_CELSIUS": parseInt(configData.useCelsius, 10),
|
||||
"KEY_READABILITY": parseInt(configData.weatherDateDTimeReadability, 10),
|
||||
"KEY_DISPLAY_DATE": parseInt(configData.displayDate, 10),
|
||||
"KEY_DISPLAY_DIGITAL_TIME": parseInt(configData.displayDigitalTime, 10),
|
||||
"KEY_VIBBRATE_BT_STATUS": parseInt(configData.vibrateBT, 10),
|
||||
"KEY_USE_THIN_HANDS": parseInt(configData.useThinHands, 10),
|
||||
"KEY_DISPLAY_BATTERY": parseInt(configData.displayBattery, 10),
|
||||
"KEY_DISPLAY_HOUR_DIGITS": parseInt(configData.displayHourDigits, 10),
|
||||
"KEY_DISPLAY_SECONDS_HAND": parseInt(configData.displaySecondsHand, 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: Unable to deliver message with transactionId=' + e.data.transactionId + ' Error is: ' + e.error.message);
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
function locationSuccessYahoo(pos) {
|
||||
|
||||
var url = 'http://nominatim.openstreetmap.org/reverse?format=json&lat=' + pos.coords.latitude + '&lon=' + pos.coords.longitude;
|
||||
xhrRequest(url, 'GET',
|
||||
function(responseText) {
|
||||
|
||||
// responseText contains a JSON object with weather info
|
||||
var json = JSON.parse(responseText);
|
||||
var location = json.display_name;
|
||||
|
||||
localStorage.setItem('weather_loc', location);
|
||||
}
|
||||
);
|
||||
|
||||
getGPSweatherYahoo();
|
||||
}
|
||||
|
||||
function getGPSweatherYahoo() {
|
||||
|
||||
// Construct URL
|
||||
var location = localStorage.getItem('weather_loc');
|
||||
|
||||
var url = 'https://query.yahooapis.com/v1/public/yql?q=' +
|
||||
encodeURIComponent('select item.condition, item.forecast from weather.forecast where woeid in (select woeid from geo.places(1) where text="' +
|
||||
location + '") and u="c" limit 1') + '&format=json';
|
||||
|
||||
console.log(url);
|
||||
|
||||
//getGPSweather();
|
||||
updateWeatherData(url);
|
||||
|
||||
}
|
||||
|
||||
function staticLocationYahoo() {
|
||||
if(weatherLoc !== '') {
|
||||
var url = 'https://query.yahooapis.com/v1/public/yql?q=' +
|
||||
encodeURIComponent('select item.condition, item.forecast from weather.forecast where woeid in (select woeid from geo.places(1) where text="' +
|
||||
weatherLoc + '") and u="c" limit 1') + '&format=json';
|
||||
console.log(url);
|
||||
|
||||
updateWeatherData(url);
|
||||
} else {
|
||||
useWeatherGPS = 1;
|
||||
getWeather();
|
||||
}
|
||||
}
|
||||
|
||||
function updateWeatherData(url) {
|
||||
try {
|
||||
xhrRequest(url, 'GET',
|
||||
function(responseText) {
|
||||
// responseText contains a JSON object with weather info
|
||||
var json = JSON.parse(responseText);
|
||||
var condition = json.query.results.channel.item.condition;
|
||||
var forecast = json.query.results.channel.item.forecast;
|
||||
|
||||
if(json.query.count == "1") {
|
||||
|
||||
var temperature = Math.round(((condition.temp) * 1.8) + 32);
|
||||
console.log("Temperature in Fahrenheit is " + temperature);
|
||||
|
||||
var temperaturec = Math.round(condition.temp);
|
||||
console.log("Temperature in Celsius is " + temperaturec);
|
||||
|
||||
// Conditions
|
||||
var conditions = condition.text;
|
||||
console.log("Conditions are " + conditions);
|
||||
|
||||
var conditioncode = Math.round(condition.code);
|
||||
console.log("Condition code is " + conditioncode);
|
||||
|
||||
var tempFLo = Math.round(((forecast.low) * 1.8) + 32);
|
||||
console.log("Temperature in Fahrenheit Lo is " + tempFLo);
|
||||
|
||||
var tempFHi = Math.round(((forecast.high) * 1.8) + 32);
|
||||
console.log("Temperature in Fahrenheit Hi is " + tempFHi);
|
||||
|
||||
var tempCLo = Math.round(forecast.low);
|
||||
console.log("Temperature in Celsius Lo is " + tempCLo);
|
||||
|
||||
var tempCHi = Math.round(forecast.high);
|
||||
console.log("Temperature in Celsius Hi is " + tempCHi);
|
||||
|
||||
// Assemble dictionary using our keys
|
||||
var weather_dictionary = {
|
||||
"KEY_TEMPERATURE": temperature,
|
||||
"KEY_TEMPERATURE_IN_C": temperaturec,
|
||||
"KEY_CONDITIONS": conditions,
|
||||
"KEY_TEMPERATURE_LO": tempFLo,
|
||||
"KEY_TEMPERATURE_HI": tempFHi,
|
||||
"KEY_TEMPERATURE_IN_C_LO": tempCLo,
|
||||
"KEY_TEMPERATURE_IN_C_HI": tempCHi,
|
||||
"KEY_CONDITION_CODE": conditioncode
|
||||
};
|
||||
|
||||
// Send to Pebble
|
||||
Pebble.sendAppMessage(weather_dictionary,
|
||||
function(e) {
|
||||
console.log("updateWeatherData: Weather info sent to Pebble successfully!");
|
||||
},
|
||||
function(e) {
|
||||
console.log('updateWeatherData: Unable to deliver message with transactionId=' + e.data.transactionId + ' Error is: ' + e.error.message);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
catch (exception){
|
||||
console.log(JSON.stringify(exception));
|
||||
}
|
||||
}
|
||||
|
||||
function locationSuccessOW(pos) {
|
||||
|
||||
// Construct URL
|
||||
var url = 'http://api.openweathermap.org/data/2.5/weather?lat=' + pos.coords.latitude + '&lon=' + pos.coords.longitude + '&APPID=' + APPID;
|
||||
var urlForecast = 'http://api.openweathermap.org/data/2.5/forecast/daily?lat=' + pos.coords.latitude + '&lon=' + pos.coords.longitude + '&cnt=1&APPID=' + APPID;
|
||||
console.log('url: ' + url);
|
||||
console.log('urlForecast: ' + urlForecast);
|
||||
|
||||
updateWeatherDataOW(url, urlForecast);
|
||||
}
|
||||
|
||||
function staticLocationOW() {
|
||||
|
||||
if(weatherLoc !== '') {
|
||||
var url = "http://api.openweathermap.org/data/2.5/weather?q=" + encodeURIComponent(weatherLoc) + '&APPID=' + APPID;
|
||||
var urlForecast = 'http://api.openweathermap.org/data/2.5/forecast/daily?q=' + encodeURIComponent(weatherLoc) + '&cnt=1&APPID=' + APPID;
|
||||
console.log('url: ' + url);
|
||||
console.log('urlForecast: ' + urlForecast);
|
||||
updateWeatherDataOW(url, urlForecast);
|
||||
} else {
|
||||
useWeatherGPS = 1;
|
||||
updateWeatherDataOW();
|
||||
}
|
||||
}
|
||||
|
||||
function updateWeatherDataOW(url, urlForecast) {
|
||||
|
||||
// Send request to forecast.io
|
||||
xhrRequest(url, 'GET', function(responseText) {
|
||||
try {
|
||||
|
||||
// responseText contains a JSON object with weather info
|
||||
var json = 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);
|
||||
|
||||
//placeholder
|
||||
var conditioncode = 0;
|
||||
console.log("Condition code is " + conditioncode);
|
||||
|
||||
xhrRequest(urlForecast, 'GET', function(forecastRespText) {
|
||||
try {
|
||||
|
||||
console.log('Retrieving forecast data from OpenWeatherMap');
|
||||
var fResp = JSON.parse(forecastRespText);
|
||||
|
||||
var tempFLo = Math.round(((fResp.list[0].temp.min - 273.15) * 1.8) + 32);
|
||||
console.log("Temperature in Fahrenheit Lo is " + tempFLo);
|
||||
|
||||
var tempFHi = Math.round(((fResp.list[0].temp.max - 273.15) * 1.8) + 32);
|
||||
console.log("Temperature in Fahrenheit Hi is " + tempFHi);
|
||||
|
||||
var tempCLo = Math.round(fResp.list[0].temp.min - 273.15);
|
||||
console.log("Temperature in Celsius Lo is " + tempCLo);
|
||||
|
||||
var tempCHi = Math.round(fResp.list[0].temp.max - 273.15);
|
||||
console.log("Temperature in Celsius Hi is " + tempCHi);
|
||||
|
||||
// Assemble dictionary using our keys
|
||||
var weather_dictionary = {
|
||||
"KEY_TEMPERATURE": temperature,
|
||||
"KEY_TEMPERATURE_IN_C": temperaturec,
|
||||
"KEY_CONDITIONS": conditions,
|
||||
"KEY_TEMPERATURE_LO": tempFLo,
|
||||
"KEY_TEMPERATURE_HI": tempFHi,
|
||||
"KEY_TEMPERATURE_IN_C_LO": tempCLo,
|
||||
"KEY_TEMPERATURE_IN_C_HI": tempCHi,
|
||||
"KEY_CONDITION_CODE": conditioncode
|
||||
};
|
||||
|
||||
// Send to Pebble
|
||||
Pebble.sendAppMessage(weather_dictionary,
|
||||
function(e) {
|
||||
console.log("locationSuccessOW: Weather info sent to Pebble successfully!");
|
||||
},
|
||||
function(e) {
|
||||
console.log('locationSuccessOW: Unable to deliver message with transactionId=' + e.data.transactionId + ' Error is: ' + e.error.message);
|
||||
});
|
||||
}
|
||||
catch (exception){
|
||||
console.log(JSON.stringify(exception));
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (ex) {
|
||||
console.log('Failure requesting current weather from OpenWeatherMap');
|
||||
console.log(ex.stack);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function locationError(err) {
|
||||
console.log('Error requesting location!');
|
||||
}
|
||||
|
||||
function getWeather() {
|
||||
|
||||
if (weatherProvider=='yahoo') {
|
||||
|
||||
if (useWeatherGPS) {
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
locationSuccessYahoo,
|
||||
locationError,
|
||||
{timeout: 15000, maximumAge: 60000}
|
||||
);
|
||||
}
|
||||
else {
|
||||
staticLocationYahoo();
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (useWeatherGPS) {
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
locationSuccessOW,
|
||||
locationError,
|
||||
{timeout: 15000, maximumAge: 60000}
|
||||
);
|
||||
}
|
||||
else {
|
||||
staticLocationOW();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Pebble.addEventListener('ready', function() {
|
||||
console.log('PebbleKit JS Ready!');
|
||||
|
||||
// Notify the watchapp that it is now safe to send messages
|
||||
Pebble.sendAppMessage({ 'KEY_JS_READY': 1 });
|
||||
|
||||
//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.');
|
||||
useWeatherGPS = e.payload.KEY_WEATHER_USE_GPS;
|
||||
weatherLoc = e.payload.KEY_WEATHER_LOCATION;
|
||||
getWeather();
|
||||
}
|
||||
}
|
||||
);
|
||||
Reference in New Issue
Block a user