You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
2.9 KiB

9 years ago
  1. var xhrRequest = function (url, type, callback) {
  2. var xhr = new XMLHttpRequest();
  3. xhr.onload = function () {
  4. callback(this.responseText);
  5. };
  6. xhr.open(type, url);
  7. xhr.send();
  8. };
  9. function locationSuccess(pos) {
  10. // generic API key, has to be replaced by developers one
  11. var api_key = 'ca82d3c964da82f54d033abf702a46a5';
  12. var lat = Math.round(pos.coords.latitude*10) / 10;
  13. var lon = Math.round(pos.coords.longitude*10) / 10;
  14. // Construct URL
  15. var url = 'http://api.openweathermap.org/data/2.5/weather?lat=' +
  16. lat + '&lon=' + lon + '&APPID=' + api_key;
  17. // Send request to OpenWeatherMap
  18. xhrRequest(url, 'GET',
  19. function(responseText) {
  20. // responseText contains a JSON object with weather info
  21. var json = JSON.parse(responseText);
  22. // Temperature in Kelvin requires adjustment
  23. var temperature = Math.round(json.main.temp - 273.15);
  24. console.log('Temperature is ' + temperature);
  25. // Conditions
  26. var conditions = json.weather[0].main;
  27. console.log('Conditions are ' + conditions);
  28. // Assemble dictionary using our keys
  29. var dictionary = {
  30. 'KEY_TEMPERATURE': temperature,
  31. 'KEY_CONDITIONS': conditions
  32. };
  33. // Send to Pebble
  34. Pebble.sendAppMessage(dictionary,
  35. function(e) {
  36. console.log('Weather info sent to Pebble successfully!');
  37. },
  38. function(e) {
  39. console.log('Error sending weather info to Pebble!');
  40. }
  41. );
  42. }
  43. );
  44. }
  45. function locationError(err) {
  46. console.log('Error requesting location!');
  47. }
  48. function getWeather() {
  49. navigator.geolocation.getCurrentPosition(
  50. locationSuccess,
  51. locationError,
  52. {timeout: 15000, maximumAge: 60000}
  53. );
  54. }
  55. function getHomeTemp() {
  56. var url = "https://raspi.goodcleanfun.de/cgi-bin/raspiweb.py?pwd=d3Vyc3RnZXNpY2h0&period=3";
  57. console.log('Try to get room temp');
  58. xhrRequest(url, 'POST',
  59. function(responseText) {
  60. // responseText contains a JSON object with weather info
  61. var json = JSON.parse(responseText);
  62. // Conditions
  63. var room_temp = (Math.round(json.room[0]*10) / 10) + '°C @ home';
  64. console.log('Room temp is ' + room_temp);
  65. // Assemble dictionary using our keys
  66. var dictionary = {
  67. 'KEY_ROOM_TEMP': room_temp
  68. };
  69. // Send to Pebble
  70. Pebble.sendAppMessage(dictionary,
  71. function(e) {
  72. console.log('Room temp sent to Pebble successfully!');
  73. },
  74. function(e) {
  75. console.log('Error sending room temp to Pebble!');
  76. }
  77. );
  78. }
  79. );
  80. }
  81. // Listen for when the watchface is opened
  82. Pebble.addEventListener('ready',
  83. function(e) {
  84. console.log('PebbleKit JS ready!');
  85. // Get the initial weather
  86. getWeather();
  87. getHomeTemp();
  88. }
  89. );
  90. // Listen for when an AppMessage is received
  91. Pebble.addEventListener('appmessage',
  92. function(e) {
  93. console.log('AppMessage received!');
  94. getWeather();
  95. getHomeTemp();
  96. }
  97. );