diff --git a/src/pkjs/app.js b/src/pkjs/app.js new file mode 100644 index 0000000..9cd6ede --- /dev/null +++ b/src/pkjs/app.js @@ -0,0 +1,362 @@ +var BASE_CONFIG_URL = 'http://singleserveapps.github.io/91-dub-weather-config/'; +//var BASE_CONFIG_URL = 'http://singleserveapps.github.io/tictoc-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 apliteURL = BASE_CONFIG_URL + 'aplite.html'; + var roundURL = BASE_CONFIG_URL + 'config_round.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(apliteURL); + } 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.weatherFrequency) { + 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("weatherFrequency: ", parseInt(configData.weatherFrequency, 10)); + console.log("blinkColon: ", parseInt(configData.blinkColon, 10)); + console.log("hourlyVibrate: ", parseInt(configData.hourlyVibrate, 10)); + console.log("hideBattery: ", parseInt(configData.hideBattery, 10)); + console.log("displaySeconds: ", parseInt(configData.displaySeconds, 10)); + console.log("weatherReadability: ", parseInt(configData.weatherReadability, 10)); + console.log("vibrateBT: ", parseInt(configData.vibrateBT, 10)); + console.log("invert: ", parseInt(configData.invert, 10)); + + // Assemble dictionary using our keys + var options_dictionary = { + "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_WEATHER_FREQUENCY": parseInt(configData.weatherFrequency, 10), + "KEY_BLINK_COLON": parseInt(configData.blinkColon, 10), + "KEY_HOURLY_VIBRATE": parseInt(configData.hourlyVibrate, 10), + "KEY_HIDE_BATTERY": parseInt(configData.hideBattery, 10), + "KEY_DISPLAY_SECONDS": parseInt(configData.displaySeconds, 10), + "KEY_WEATHER_READABILITY": parseInt(configData.weatherReadability, 10), + "KEY_VIBBRATE_BT_STATUS": parseInt(configData.vibrateBT, 10), + "KEY_INVERT": parseInt(configData.invert, 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(); + } + } +); \ No newline at end of file