It is currently July 14th, 2024, 1:20 pm

"Refreshing Skin" every minute appearing in Logfile

Get help with creating, editing & fixing problems with skins
User avatar
SilverAzide
Rainmeter Sage
Posts: 2683
Joined: March 23rd, 2015, 5:26 pm

Re: "Refreshing Skin" every minute appearing in Logfile

Post by SilverAzide »

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!
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:

Code: Select all

[MeasureClientCurrentTime]
Measure=Time

[MeasureDelta]
Measure=Calc
Formula=([MeasureClientCurrentTime:Timestamp] - [MeasureGlucoseTime])
DynamicVariables=1
(Treat the above as pseudo-code, I did not try it and I don't know if you need DynamicVariables=1 or not, or the square brackets. If MeasureGlucoseTime is ever an empty string (like before the first webcall returns, the formula will fail with an error in the log. You can fix this later by using a Calc measure to change the glucose time to a real number. You also might need to divide the glucose time by 1000 to get seconds, as Timestamp is going to return the current time in seconds, not milliseconds.)

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"]
As you can see, your [MeasureSite] WebParser measure will be called when the delta exceeds 310 seconds. The reason for the "&& (MeasureDelta < 10000)" addition on the formula is in case the glucose time is zero (which it will be the first time the skin loads). If it is zero, then the delta value is going to be some huge number and cause the webparser to be called again, even though it is already running. So this is just a guard to prevent that.

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.
Gadgets Wiki GitHub More Gadgets...
emp00
Posts: 112
Joined: October 7th, 2022, 8:08 pm

Re: "Refreshing Skin" every minute appearing in Logfile

Post by emp00 »

Regarding the time difference (Client - Server) works in my case, needed to convert to Unix time and milliseconds to seconds and chose finally conversion to minutes with one single formula:

Code: Select all

[MeasureClientCurrentTime]
; Get Client Timestamp for comparison with Server BG Time, see [MeasureClientToGlucoseTimeDelta]
Measure=Time

[MeasureClientToGlucoseTimeDeltaMinutes]
Measure=Calc
Formula=(MeasureClientCurrentTime - 11644473600 - MeasureGlucoseTime/1000)/60
DynamicVariables=1
This time difference in minutes is ticking in my case always between 120 and 125 -> obviously because my client is running in UTC+2 time zone, correct? I could now proceed by subtracting 120 minutes, but this skin is supposed to work for people in "all" time zones and of course daylight savings time issues should be covered as well... Is there a rainmeter function which converts the client time to UTC?

Alternatively, I was thinking I'd just need to extract the first digit before and one digit behind the decimal point,

Examples UTC+2:
121.2353 ==> 1.2
124.5635 ==> 4.5

Example UTC+10:
600.9431 ==> 0.9

Example UTC-8
-481.048 ==> 1.0

But how would the Formula look like to do this job? Scratching my head ... Many thanks guys!!
User avatar
SilverAzide
Rainmeter Sage
Posts: 2683
Joined: March 23rd, 2015, 5:26 pm

Re: "Refreshing Skin" every minute appearing in Logfile

Post by SilverAzide »

emp00 wrote: October 9th, 2022, 9:03 pm Is there a rainmeter function which converts the client time to UTC?
Yes... per the manual, all you need to do is add the TimeZone option on your measure, like so:

Code: Select all

[MeasureClientCurrentTime]
; Get Client Timestamp for comparison with Server BG Time, see [MeasureClientToGlucoseTimeDelta]
Measure=Time
TimeZone=0
As far as that other idea... please don't, LOL. :vomit:
Gadgets Wiki GitHub More Gadgets...
emp00
Posts: 112
Joined: October 7th, 2022, 8:08 pm

Re: "Refreshing Skin" every minute appearing in Logfile

Post by emp00 »

SilverAzide wrote: October 9th, 2022, 11:27 pm

Code: Select all

[MeasureClientCurrentTime]
; Get Client Timestamp for comparison with Server BG Time, see [MeasureClientToGlucoseTimeDelta]
Measure=Time
TimeZone=0
Mmmmh, this changed the time delta from +120 min = +2 h (I'm in CET, UTC+2) to still 60 min = +1 h in my case. Still not "zero hours" as hoped for. Whatever this means, maybe either rainmeter or the server returns daylight savings time?

Step 1: The server is running the well-established "Nightscout" database and I looked this up in the respective documentation: "The Nightscout REST API normalizes all entered dates to UTC zone". This means that probably rainmeter is causing my "+1h problem"?

Step 2: "RTFM" ?!?! Hey, I found this option "DaylightSavingTime=0"

Now I have this - and this gives me the "zero hours" delta!! Yeah! :-)

Code: Select all

Measure=Time
TimeZone=0
DaylightSavingTime=0
@SilverAzide: Is this "safe code" meaning this should be working "all over the world" in whatever timezone the user is sitting? Any doubts? I'm asking because I cannot test this myself (I guess). Thanks so much again!!
User avatar
SilverAzide
Rainmeter Sage
Posts: 2683
Joined: March 23rd, 2015, 5:26 pm

Re: "Refreshing Skin" every minute appearing in Logfile

Post by SilverAzide »

emp00 wrote: October 10th, 2022, 8:41 pm @SilverAzide: Is this "safe code" meaning this should be working "all over the world" in whatever timezone the user is sitting? Any doubts? I'm asking because I cannot test this myself (I guess). Thanks so much again!!
Yes, that is "safe", and it is the definition of "UTC" time. I should have added this in the example, but I keep forgetting about DST. :oops:
Gadgets Wiki GitHub More Gadgets...