12-08-2021 18:49
12-08-2021 18:49
Hey all,
I am trying to migrate my app from Fitbit SDK 4.0 to 5.0 and before used the "simple-settings" infrastructure. This utilized messaging and callback when setting and updating settings. It worked great, but now my "data" variable is returning null. I have included my code from the companion-settings and device-settings.
FROM COMPANION SETTINGS
import * as messaging from "messaging";
import { settingsStorage } from "settings";
export function initialize() {
settingsStorage.addEventListener("change", evt => {
if (evt.oldValue !== evt.newValue) {
sendValue(evt.key, evt.newValue);
}
});
}
function sendValue(key, val) {
if (val) {
sendSettingData({
key: key,
value: JSON.parse(val)
});
}
}
function sendSettingData(data) {
if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
messaging.peerSocket.send(data);
} else {
console.log("No peerSocket connection");
}
}
FROM DEVICE_SETTINGS
/*
Responsible for loading, applying and saving settings.
Requires companion/simple/companion-settings.js
Callback should be used to update your UI.
*/
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;
export function initialize(callback) {
settings = loadSettings();
onsettingschange = callback;
onsettingschange(settings);
}
// Received message containing settings data
messaging.peerSocket.addEventListener("message", function(evt) {
settings[evt.data.key] = evt.data.value;
onsettingschange(settings);
})
// Register for the unload event
me.addEventListener("unload", saveSettings);
// 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);
}
FROM INDEX.JS IN THE APP DIRECTORY
function settingsCallback(data) {
console.log(data);
if (!data) {
return;
}
if (data.colorB) {
body.style.fill = data.colorB;
}
if (data.colorH) {
mixedtext.style.fill = data.colorH;
}
if (data.selection) {
versionNum = data.selection.selected;
}
}
settings.initialize(settingsCallback);
FROM INDEX.JS IN THE COMPANION DIRECTORY
import * as settings from "./companion-settings";
settings.initialize();
Thanks for any help anyone can provide. If this is not a good way to do settings, please lead me on the right path. Thanks again.
12-15-2021 08:24
12-15-2021 08:24
This is answered in you other request.