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

writing to file not working in clock.ontick = (evt) => {

ANSWERED

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

iPhone 8 - Versa
Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

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

View best answer in original post

Best Answer
0 Votes
9 REPLIES 9

do you have any code to share?

Best Answer
0 Votes
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;
}
iPhone 8 - Versa
Best Answer
0 Votes

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

Best Answer
0 Votes

Permission to access today.adjusted.steps?

Peter McLennan
Gondwana Software
Best Answer
0 Votes

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

iPhone 8 - Versa
Best Answer
0 Votes

What isn't working?  What does the code do that it should not do?

 

John

Best Answer
0 Votes

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

iPhone 8 - Versa
Best Answer
0 Votes

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

Best Answer
0 Votes

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

iPhone 8 - Versa
Best Answer
0 Votes