Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Keeping fetched weather data until new data is available.

So I created a clockface with weather data from OpenWeather but every time I open an app or open Fitbit today the info is gone and I have to sync my watch again. Is there a way that I could keep the "out of date" weather data until I can get new data? (even when closing the clockface) (I'm using a Fitbit Versa 2)

Best Answer
3 REPLIES 3

Yes: save the data to the file system on the watch. See fs API doco.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

MY REPLY GOT DELETED???!!!

@Gondwana  As I was saying I have no idea what to do and the FS API is complicated, so I was wondering if you could explain me how to do it.

My code is very simple:

In app I have this:

 

// Import the messaging module
import * as messaging from "messaging";
import document from "document";

// Request weather data from the companion
function fetchWeather() {
  if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
    // Send a command to the companion
    messaging.peerSocket.send({
      command: 'weather'
    });
  }
}

// Display the weather data received from the companion
function processWeatherData(data) {
  console.log("The temperature is: " + data.temperature);
  console.log("The location is : " + data.location);
  console.log("The forecast is: " + data.meteo);
  // Get handle on text fields
let tempText = document.getElementById("tempText");
  let locationText = document.getElementById("locationText");
  let forecastText = document.getElementById("forecastText");
  // Inside the processWeatherData block

 tempText.text = Math.round(data.temperature) + "°C";
 locationText.text = (data.location);
 forecastText.text = (data.meteo);
}

// Listen for the onopen event
messaging.peerSocket.onopen = function() {
  // Fetch weather when the connection opens
  fetchWeather();
}

// Listen for messages from the companion
messaging.peerSocket.onmessage = function(evt) {
  if (evt.data) {
    processWeatherData(evt.data);
  }
}

// Listen for the onerror event
messaging.peerSocket.onerror = function(err) {
  // Handle any errors
  console.log("Connection error: " + err.code + " - " + err.message);
}
  setInterval(fetchWeather, 60 * 1000 * 60);

And this in the companion:

// Import the messaging module
import * as messaging from "messaging";
import { geolocation } from "geolocation";

var API_KEY = "API KEY";

// Fetch the weather from OpenWeather
function queryOpenWeather() {
  geolocation.getCurrentPosition(locationSuccess, locationError);
  function locationSuccess(position) {
    var lat = position.coords.latitude;
    var long = position.coords.longitude;
    console.log("latitude: " + lat);
    console.log("langitude: " + long);
    var linkApi = "https://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon="  + long + "&units=metric" + "&APPID=" + API_KEY;
  fetch(linkApi)
  .then(function (response) {
      response.json()
      .then(function(data) {
        // We just want some data
        var weather = {
          temperature: data["main"]["temp"], humidity: data["main"]["humidity"], location: data["name"], meteo: data["weather"][0]["main"]
        }
        // Send the weather data to the device
        returnWeatherData(weather);
      });
  })
  .catch(function (err) {
    console.log("Error fetching weather: " + err);
  });
 };
 function locationError(error) {
  console.log("Error: " + error.code,
              "Message: " + error.message);
}
}

// Send the weather data to the device
function returnWeatherData(data) {
  if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
    // Send a command to the device
    messaging.peerSocket.send(data);
  } else {
    console.log("Error: Connection is not open");
  }
}

// Listen for messages from the device
messaging.peerSocket.onmessage = function(evt) {
  if (evt.data && evt.data.command == "weather") {
    // The device requested weather data
    queryOpenWeather();
  }
}

// Listen for the onerror event
messaging.peerSocket.onerror = function(err) {
  // Handle any errors
  console.log("Connection error: " + err.code + " - " + err.message);
}

 

Do you have any idea what to do?

Best Answer
0 Votes

Moved post to SDK forums

Gordon Crenshaw
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google
Best Answer
0 Votes