|
|
- var xhrRequest = function (url, type, callback) {
-
- var xhr = new XMLHttpRequest();
-
- xhr.onload = function () {
- callback(this.responseText);
- };
-
- xhr.open(type, url);
- xhr.send();
-
- };
-
-
- function locationSuccess(pos) {
-
- // generic API key, has to be replaced by developers one
- var api_key = 'ca82d3c964da82f54d033abf702a46a5';
-
- var lat = Math.round(pos.coords.latitude*10) / 10;
- var lon = Math.round(pos.coords.longitude*10) / 10;
-
- // Construct URL
- var url = 'http://api.openweathermap.org/data/2.5/weather?lat=' +
- lat + '&lon=' + lon + '&APPID=' + api_key;
-
- // Send request to OpenWeatherMap
- xhrRequest(url, 'GET',
- function(responseText) {
- // responseText contains a JSON object with weather info
- var json = JSON.parse(responseText);
-
- // Temperature in Kelvin requires adjustment
- var temperature = Math.round(json.main.temp - 273.15);
- console.log('Temperature is ' + temperature);
-
- // Conditions
- var conditions = json.weather[0].main;
- console.log('Conditions are ' + conditions);
-
- // Assemble dictionary using our keys
- var dictionary = {
- 'KEY_TEMPERATURE': temperature,
- 'KEY_CONDITIONS': conditions
- };
-
- // Send to Pebble
- Pebble.sendAppMessage(dictionary,
- function(e) {
- console.log('Weather info sent to Pebble successfully!');
- },
- function(e) {
- console.log('Error sending weather info to Pebble!');
- }
- );
- }
- );
-
- }
-
-
- function locationError(err) {
-
- console.log('Error requesting location!');
-
- }
-
-
- function getWeather() {
-
- navigator.geolocation.getCurrentPosition(
- locationSuccess,
- locationError,
- {timeout: 15000, maximumAge: 60000}
- );
-
- }
-
-
- function getHomeTemp() {
-
- var url = "https://raspi.goodcleanfun.de/cgi-bin/raspiweb.py?pwd=d3Vyc3RnZXNpY2h0&period=3";
-
- console.log('Try to get room temp');
-
- xhrRequest(url, 'POST',
- function(responseText) {
- // responseText contains a JSON object with weather info
- var json = JSON.parse(responseText);
-
- // Conditions
- var room_temp = (Math.round(json.room[0]*10) / 10) + '°C @ home';
- console.log('Room temp is ' + room_temp);
-
- // Assemble dictionary using our keys
- var dictionary = {
- 'KEY_ROOM_TEMP': room_temp
- };
-
- // Send to Pebble
- Pebble.sendAppMessage(dictionary,
- function(e) {
- console.log('Room temp sent to Pebble successfully!');
- },
- function(e) {
- console.log('Error sending room temp to Pebble!');
- }
- );
- }
- );
- }
-
-
- // Listen for when the watchface is opened
- Pebble.addEventListener('ready',
- function(e) {
- console.log('PebbleKit JS ready!');
- // Get the initial weather
- getWeather();
- getHomeTemp();
- }
- );
-
-
- // Listen for when an AppMessage is received
- Pebble.addEventListener('appmessage',
- function(e) {
- console.log('AppMessage received!');
- getWeather();
- getHomeTemp();
- }
- );
|