11-05-2018 12:52
11-05-2018 12:52
My clock sends a message to the companion every 15 minutes to check on the current time zone (the horrible solution to how do I check the current time zone. (This is that thread https://community.fitbit.com/t5/SDK-Development/Force-companion-to-pass-info/m-p/3010132#M6532). The problem is limiting to how often this happens. My granularity is set to "minutes", yet this thing seems to be running continuously. loadInfo(myOptions) calls a file on the tracker to populate myOptions which is used to then display the multiple parts of my clock face, including the current time zone.
clock.granularity = "minutes";
//loadInfo calls from files to load clock -line 409 clock.ontick = () => loadInfo(myOptions);
Here is my current fill-in for a solution. It runs over 40 times in that 0.5 Seconds and is part of my main clock display function.
//Force a new timeZone from the companion if (mins == "00" || mins == "30" || mins == "15" || mins == "45"){ let secs = today.getSeconds(); if (secs < 0.5) { if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) { messaging.peerSocket.send("event"); } } };
Thoughts?
Answered! Go to the Best Answer.
11-05-2018 14:34
11-05-2018 14:34
I've just tried it here, and it's ticking on minutes.
import clock from "clock"; clock.granularity = "minutes"; clock.ontick = (evt) => { const now = evt.date; if (now.getMinutes() % 15 === 0) { console.log("do it!"); } else { console.log("don't do it"); } }
Maybe change the syntax of your ontick declaration?
clock.granularity = "minutes";
let myOptions = {
blah: "test"
}
clock.ontick = (evt) => {
ticker(evt, myOptions);
}
function ticker(evt, options) {
console.log(options.blah)
const now = evt.date;
if (now.getMinutes() % 15 === 0) {
console.log("do it!");
} else {
console.log("don't do it");
}
}
11-05-2018 13:07
11-05-2018 13:07
Have you considered setInterval()?
The issue with clock.ontick (as far is I know) is that it will not actually fire when the display is off. Will fire one the display gets on, and at the start of every minute in your case.
11-05-2018 13:58
11-05-2018 13:58
It shouldn't be ticking that often with the granularity set. Are you sure you aren't setting the granularity elsewhere?
You can use modulo to check every 15 mins:
const now = new Date(); if (now.getMinutes() % 15 === 0) { console.log("do it!"); } else { console.log("don't do it"); }
it's true that setInterval() runs even if the display is off, but it will also effect the battery usage.
11-05-2018 14:20
11-05-2018 14:20
Jon...I did a search for the word "granularity" in ALL my files and it only appears the once. Your method, while prettier, causes the check to run for a solid minute, once again apparently ignoring the clock.ontick. I was thinking I might be missing something with the operation of the clock.ontick function. Apparently not?
Zolika...I actually only want the clock to update every minute. I do not display the seconds on my clock.
11-05-2018 14:34
11-05-2018 14:34
I've just tried it here, and it's ticking on minutes.
import clock from "clock"; clock.granularity = "minutes"; clock.ontick = (evt) => { const now = evt.date; if (now.getMinutes() % 15 === 0) { console.log("do it!"); } else { console.log("don't do it"); } }
Maybe change the syntax of your ontick declaration?
clock.granularity = "minutes";
let myOptions = {
blah: "test"
}
clock.ontick = (evt) => {
ticker(evt, myOptions);
}
function ticker(evt, options) {
console.log(options.blah)
const now = evt.date;
if (now.getMinutes() % 15 === 0) {
console.log("do it!");
} else {
console.log("don't do it");
}
}
11-05-2018 16:25
11-05-2018 16:25
Thank you for working with me on this. I modified my code a bit.
//Force a new timeZone from the companion if (today.getMinutes() % 15 === 0) { if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) { messaging.peerSocket.send("event"); } } else { console.log("don't do it"); }
The "don't do it" part only executes once per minute which leads me to believe that it may be my peerSocket line.
Here is the companion code that gets executed...
//Send the time zone back to the tracker messaging.peerSocket.onmessage = () => { console.log("peerSocket called"); sendTZ(); };
function sendTZ() { let myTZ = new Date().toString().match(/\(([A-Za-z\s].*)\)/)[1]; let data = { key: "zoneCode", newValue: myTZ } console.log(`full date ${new Date()}`); console.log(`newValue ${data.newValue}`); sendVal(data); }; // Send data to device using Messaging API function sendVal(data) { if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) { messaging.peerSocket.send(data); } }
I get hundreds of the
Hundreds. As in, enough to bog down my mac's browser. Thoughts?
11-05-2018 16:33
11-05-2018 16:33
I notice you're not checking the data in the onmessage event in the companion.
messaging.peerSocket.onmessage = (evt) => { console.log(`peerSocket called: ${JSON.stringify(evt.data)}`); //sendTZ(); };
Are all of these containing your message "event"?
11-05-2018 16:51
11-05-2018 16:51
I only send the message so that the companion will send the time zone back (your recommendation). I never send any other information to the companion from the tracker. So, "event" is a random string I probably stole from the sample and has no value to me.
11-05-2018 16:54
11-05-2018 16:54
I'm just interested to see if you were perhaps receiving other messages from elsewhere in the code. Do you have the entire project online somewhere?
11-05-2018 16:57
11-05-2018 16:57
Ha. I literally just finished copying all my files to github. I think I did it correctly.
11-05-2018 19:07
11-05-2018 19:07
If I use
//Force a new timeZone from the companion if (Date.now() % 900000 < 100) { if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) { messaging.peerSocket.send("event"); } } else { console.log("don't do it"); }
I only get 3 returns. So, that it is better but it does not explain why it is executing more than once to begin with.
11-06-2018 00:15
11-06-2018 00:15
I've checked your project here using the simulator and I'm only seeing the tick event every minute.
If you're testing on a real device, try deleting the app from your phone/device and rebooting both. Then see if the tick is still happening every second.
In other feedback, I don't think you want to be doing this every minute: https://github.com/FlyFrosty/Traveler/blob/master/app/index.js#L417
You should read the options from the files only once.
11-06-2018 09:01
11-06-2018 09:01
I will assume that it is a glitch in the Fitbit OS Simulator then.
I also corrected the file call issue (insert face palm here). And the same error in my other clock face (insert double face palm here).
Thank you for your help.