You don't really need to care about the server's current time. You need the client's current time. That's super-easy. Once you have that, get the difference between the glucose time and "client now", which is what you need:emp00 wrote: ↑October 8th, 2022, 9:15 pm Banning is not a problem since it's my own server (running in the cloud) and I'm using this skin already for a few years calling every 1 minute without issues. However, still I'd like to improve the code to save some network traffic, just for the sake of improvement and avoiding the five times too frequent = unnecessary api calls.
Actually the skin already has the following measures implemented:
Both [MeasureCurrentTime] and and [MeasureGlucoseTime] are server timestamps and [MeasureTimeDifference] is the calculated time difference in milliseconds. However, this is all "server time".
How could I use the [MeasureGlucoseTime] which is the server timestamp of the latest value updated on server-side to accordingly synchronize the skin update interval of the Webparser? Of course I would add e.g. +10 extra seconds in order to be sure to call slightly after the next update. This could enable the skin calling the api every 5 instead of every 1 minute... Many thanks for your concrete coding ideas or reference code examples!
Code: Select all
[MeasureClientCurrentTime]
Measure=Time
[MeasureDelta]
Measure=Calc
Formula=([MeasureClientCurrentTime:Timestamp] - [MeasureGlucoseTime])
DynamicVariables=1
The next thing to do is change the basic logic of the skin from being timer-driven to being event-driven. In other words, instead of just firing off the web call at some fixed interval, fire off the web call when the delta between the last fetched glucose time and "now" hits 5 minutes (or better yet, 5 minutes and 10 seconds like you suggest).
To do that, first change your WebParser's UpdateRate to -1. That means it will fire one time only, the first time the skin loads, then never again.
Next, add an IfCondition to the [MeasureDelta] measure to trigger a call to the web server when the delta is greater than 310 seconds. So, add to the MeasureDelta measure, like this:
Code: Select all
[MeasureDelta]
Measure=Calc
Formula=([MeasureClientCurrentTime:Timestamp] - [MeasureGlucoseTime])
DynamicVariables=1
IfCondition=((MeasureDelta > 310) && (MeasureDelta < 10000))
IfTrueAction=[!MeasureCommand MeasureSite "Update"]
You might need to also guard against the webparser being called twice if the web call takes longer than 1 second or the call doesn't reset the glucose time, or it fails. Worry about that bit later, but this is the general tack you might want to take to turn this into an event-driven skin.