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

Logic and clock.granularity help

ANSWERED

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?  

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

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");
}
}

 

 

View best answer in original post

Best Answer
0 Votes
12 REPLIES 12

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.

Best Answer
0 Votes

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.

Best Answer
0 Votes

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.

Best Answer
0 Votes

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");
}
}

 

 

Best Answer
0 Votes

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 

 

[7:15:20 PM]full date Mon Nov 05 2018 19:15:20 GMT-0500 (EST)
[7:15:20 PM]newValue EST
[7:15:20 PM]peerSocket called
 
console logs.

 

Hundreds.  As in, enough to bog down my mac's browser.  Thoughts?

 

Best Answer
0 Votes

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"?

Best Answer
0 Votes

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.

Best Answer
0 Votes

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?

Best Answer
0 Votes

Ha.  I literally just finished copying all my files to github.  I think I did it correctly.

 

https://github.com/FlyFrosty/Traveler

Best Answer
0 Votes

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.

Best Answer
0 Votes

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.

Best Answer
0 Votes

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.

Best Answer