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

data persistence assistance needed

I am just recently (since the release of 4.0) having issues with data entered by the settings persisting to the watch via a .txt file. I am hoping a fresh set of eyes could help. Here is the framework of what I have. The data will transfer to the watch when entered or changed, but does not seem to be writing to the .txt file.

 

In my settings\index.jsx file I have a TextInput with the settings key userName. 

In the resources folder I have a settings.txt file and the index.gui file has a text placeholder with the ID of userName to pull from the settings key.

 

In my app\index.js I have the following

import document from "document";
import * as messaging from "messaging";

 

let userName = document.getElementById("userName")

 

// Message is received
messaging.peerSocket.onmessage = evt => {
console.log(`App received: ${JSON.stringify(evt)}`);
let data = (evt.data.key);
let newValue = (evt.data.newValue);
let newData = JSON.parse(newValue).name;

// ********** Set Contact Information **********
if (data === "userName" && newValue) {
userName.text = newData;
};

 

// ********** Load data from files to app at startup **********
function loadSettings() {
try {
let json_data = fs.readFileSync("settings.txt", "cbor");
console.log("Loading Data From File")
userName.text = json_data.name;

} catch (ex) {
return {};
}

 

me.addEventListener("unload", saveSettings);

 

// ********** Saves data on app exit **********
function saveSettings() {
let json_data = {};
console.log("Saving Data To File")
json_data.name = userName.text;

fs.writeFileSync("settings.txt", json_data, "cbor");
console.log("Settings Written To File")
}

};

 

Any help as to why this no longer works would be greatly appreciated.

Best Answer
0 Votes
10 REPLIES 10

Figured it out on my own.

Best Answer
0 Votes

What was the solution?

Best Answer
0 Votes

I ended up using the Moment watch face code for persisting the data to the settings.cbor file and modifying it to fit my data. 

 

This is forcing the information to persist on the watch. The trade off is that if you clear the settings, it clears it from mobile settings, but it  does not wipe the data from the watch. I have narrowed it down to that the companion is not sending over the null command to be received as a new settings change.

 

I had to remove the "Clear all Settings" button for now. It will need to wait until the next iteration to figure it out and add it back in. 

Best Answer

Can you share what code you used to persist it? So I can figure out how to adapt it to mine. Seems something has changed as the default code shared on here doesn't work any more

Best Answer
0 Votes

I can see what I can condense and post to help you out.  

 

I am not 100% happy however with this approach.  The app must be open and the companion running for the settings to transfer.  Otherwise the data is in the settings but not on the watch.  Also, the "clear all settings" button wipes the settings data but does not wipe the data on the watch. 

 

I am currently answering about 5 emails a day for data not transferring because they do not have the app open.  

Best Answer
0 Votes

would this help:

 

https://dev.fitbit.com/build/guides/settings/#settings-changed-launch-reason

 

i cant replicate the issue to see if this can fix it which a total pain but i think you have hit the nail on the head with this.

Best Answer
0 Votes

@jomis003 wrote:

...the companion is not sending over the null command to be received as a new settings change...


I had an issue like this once. I discovered that the sim and hardware behave a bit differently when sending empty or null objects (I forget the details). The sim would send them, but physical devices wouldn't.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

 I had something similar, found I needed to test for "undefined" rather than "null" in js file.

 

 

 

Best Answer
0 Votes

I also tried to use the Moment watch face code (which is also mentioned in the docs as *the* example for data persistence)... however it does not seem to work for storing Date() objects. I narrowed it down to an empty app with this code:

 

import { me } from "appbit";
import { me as device } from "device";
import * as fs from "fs";
import * as messaging from "messaging";

const SETTINGS_TYPE = "cbor";
const SETTINGS_FILE = "settings.cbor";

let settings, onsettingschange, loadedset;

// Load settings from filesystem
function loadSettings() {
  try {
    return fs.readFileSync(SETTINGS_FILE, SETTINGS_TYPE);
  } catch (ex) {
    return {};
  }
}

// Save settings to the filesystem
function saveSettings() {
  fs.writeFileSync(SETTINGS_FILE, settings, SETTINGS_TYPE);
}

settings ={}
settings.aDate = new Date()
settings.aString = "Debugging"
console.log("to save:" + JSON.stringify(settings))
saveSettings()
loadedset = loadSettings()
console.log("loaded :" + JSON.stringify(loadedset))

 

This produces the following debug output:

[16:52:04]App Started
[16:52:04]to save:{"aDate":"2022-07-16T14:52:04.717Z","aString":"Debugging"}
[16:52:04]loaded :{"aDate":{},"aString":"Debugging"}

In other words, even though I store a date into the file, a receive an empty object for aDate??

Does anyone have an idea what the problem could be?

Best Answer
0 Votes

I haven't looked into what Moment does in those functions, but I'd be wary about trying to save a Date as an object or string. Some manual type conversion may be safer. Have a look at Date.getTime() and Date.setTime().

Peter McLennan
Gondwana Software
Best Answer
0 Votes