04-02-2019 04:13 - edited 04-02-2019 04:15
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

04-02-2019 04:13 - edited 04-02-2019 04:15
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
I am trying to get my companion to fetch sleep data every 30min from the web api from 6am to 11am.
But for some reason I do not get the data even thou my phone is nearby, I have synced my watch with fitbit servers. As soon as I go into settings for my watchface my clock is updating with the latest sleep data.
I have used this example as a base - https://github.com/Fitbit/sdk-oauth
And this is my companion code
import * as messaging from "messaging"; import { me } from "companion" import { settingsStorage } from "settings"; // When failing to fetch tell clock to use its cached value if there is any const sendCached = () => messaging.peerSocket.send({ cached: true }); const toJson = r => r.json(); const onSuccess = (data, created) => { if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) { messaging.peerSocket.send({ sleep: { totalMinutesAsleep: data.summary.totalMinutesAsleep, totalTimeInBed: data.summary.totalTimeInBed }, created }); } }; function fetchSleepData(accessToken) { let todayFull = new Date(); const month = todayFull.getMonth(); const date = todayFull.getDate(); let todayDate = `${todayFull.getFullYear()}-${month + 1}-${date}`; fetch(`https://api.fitbit.com/1.2/user/-/sleep/date/${todayDate}.json`, { method: "GET", headers: { "Authorization": `Bearer ${accessToken}` } }).then(toJson) .then(d => onSuccess(d, { month, date })) .catch(sendCached); } settingsStorage.onchange = evt => { if (evt.key === "oauth") { const token = JSON.parse(evt.newValue).access_token; fetchSleepData(token); } }; function restoreSettings() { for (let index = 0; index < settingsStorage.length; index++) { let key = settingsStorage.key(index); if (key && key === "oauth") { let data = JSON.parse(settingsStorage.getItem(key)) fetchSleepData(data.access_token); } } } messaging.peerSocket.onopen = () => { restoreSettings(); }; function setWakeInterval(minutes) { me.wakeInterval = minutes * (1000 * 60); } setWakeInterval(30); if (me.launchReasons.wokenUp) { const currentHour = new Date().getHours(); // Only fetch from 6am to 11am. if (currentHour >= 6 && currentHour <= 11) { restoreSettings(); } } else { me.yield() }
"requestedPermissions": [ "access_activity", "access_user_profile", "access_heart_rate", "access_internet", "run_background" ],

04-02-2019 08:47
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


04-02-2019 08:47
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Try changing your wakeInterval to something like this:
const MILLISECONDS_PER_MINUTE = 1000 * 60; me.wakeInterval = 5 * MILLISECONDS_PER_MINUTE;
// listen for the event if the companion is already running me.addEventListener("wakeinterval", restoreSettings); // companion was woken
if (me.launchReasons.wokenUp) { restoreSettings(); }

04-02-2019 13:33
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

04-02-2019 13:33
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Ok. So the only diff I can see here is that you set the wakeInteval outside of my function and adding an event listener.
I will test this and see if I get anything in the logs
const MILLISECONDS_PER_MINUTE = 1000 * 60; me.wakeInterval = 60 * MILLISECONDS_PER_MINUTE; // listen for the event if the companion is already running me.addEventListener("wakeinterval", () => { console.log('Woke up! "wakeinterval"') restoreSettings(); }); if (me.launchReasons.wokenUp) { console.log('Woke up! "if"') restoreSettings(); } else { me.yield(); }
Did not get the sideloaded apps to work before so could only debug on the simulator, but it started to work today so lets hope this works 🙂

