11-16-2018 10:32
11-16-2018 10:32
Hi!
I'm trying to design a clock face with "steps per hour"
for this I need to write today.adjusted.steps into a file (json) (if another app is used - the step-Offset would not be reset during the hour)
it works on Simulator but not on the Versa
I really need your help how to solve this problem
thank you
_me
Answered! Go to the Best Answer.
11-17-2018 06:07
11-17-2018 06:07
You are right - the clock tick event doesn't fire unless the display is on. So you need to run code like to check for hour changes inside an interval routine, which you can create using code like this:
let intervalId = setInterval(periodicActions, 5000);
The code in the function (periodicActions in this case which runs every 5 seconds) can handle all of the repetitive work.
And note that this won't run unless the clock is active, which it won't be if another app is running (e.g. the Today app or Weather). As a result you have to save various values to a file and restore them on startup of your clock.
As a result of this, I don't recommend using a check for minutes = 0 since that won't fire if the clock isn't active. I use (in my Move It!) app a check for the current hour != the previous hour. I detect a change of hour this way and save the new hour and step offset to the file, then reload it on startup. This handles most of the cases, but it can't deal with the clock being inactive for multiple hours, although I haven't seen this to be an issue.
You should also write the current values to your save file during the unload event.
Hope this helps.
John
11-16-2018 10:35
11-16-2018 10:35
do you have any code to share?
11-16-2018 10:40
11-16-2018 10:40
clock.granularity = "seconds";
clock.ontick = (evt) => {
let jetzt = evt.date;
let minutes = util.zeroPad(jetzt.getMinutes());
if (minutes == 0){ let json_data = { "stepOffset": today.adjusted.steps, }; fs.writeFileSync("cbor.txt", json_data, "cbor"); }
let json_object = fs.readFileSync("cbor.txt", "cbor"); hstep.text = today.adjusted.steps - json_object.stepOffset;
}
11-16-2018 10:51
11-16-2018 10:51
If you zeroPad minutes, you'll get a string back "00", so you might want to change that first "if" statement.
I also don't think you want to read that file every second, just get the offset into a global variable on startup, and update that variable on 0 minutes. Then use global value in your tick event to update the hstep.text
11-16-2018 12:08
11-16-2018 12:08
Permission to access today.adjusted.steps?
11-16-2018 23:28
11-16-2018 23:28
it was just for testing to read it that often.
i changed the if Statement but it is still not working on the watch
are there any other Settings to do? SDK Version 1 or 2?
@Gondwanayes I have permission
11-17-2018 05:17
11-17-2018 05:17
What isn't working? What does the code do that it should not do?
John
11-17-2018 05:57
11-17-2018 05:57
@Drifty26writing to a file when minutes = 0
I found out that it is working if the Display is on - is there a way to use a function (only if minutes = 0) when Display is off?
11-17-2018 06:07
11-17-2018 06:07
You are right - the clock tick event doesn't fire unless the display is on. So you need to run code like to check for hour changes inside an interval routine, which you can create using code like this:
let intervalId = setInterval(periodicActions, 5000);
The code in the function (periodicActions in this case which runs every 5 seconds) can handle all of the repetitive work.
And note that this won't run unless the clock is active, which it won't be if another app is running (e.g. the Today app or Weather). As a result you have to save various values to a file and restore them on startup of your clock.
As a result of this, I don't recommend using a check for minutes = 0 since that won't fire if the clock isn't active. I use (in my Move It!) app a check for the current hour != the previous hour. I detect a change of hour this way and save the new hour and step offset to the file, then reload it on startup. This handles most of the cases, but it can't deal with the clock being inactive for multiple hours, although I haven't seen this to be an issue.
You should also write the current values to your save file during the unload event.
Hope this helps.
John
11-23-2018 09:22
11-23-2018 09:22
I'm now using setInterval for my case and it is working - thanks for the input
Maybe not the best solution but OK for my first try