Scratching my head a bit further to write efficient code…
big picture:
To control the temperature in my house, I’m building some code.
detail question
I’m building some code to determine in which time-block I am of the day.
vTCD_HOUR_01_Number.postUpdate(6) // 6 = 06
vTCD_MINUTE_01_Number.postUpdate(0) // 0 = 00
vTCD_TEMP_01_Number.postUpdate(20)
//means: at 06 hours, 00 minutes, set temp to 20
vTCD_HOUR_02_Number.postUpdate(07)
vTCD_MINUTE_02_Number.postUpdate(00)
vTCD_TEMP_02_Number.postUpdate(21)
...
vTCD_HOUR_06_Number.postUpdate(22)
vTCD_MINUTE_06_Number.postUpdate(0)
vTCD_TEMP_06_Number.postUpdate(15)
This means:
I have in this example 6 markers in my day.
The first one (01) starts at 06:00 and sets the temp to 20.
The second (02) starts at 07:00 and sets the temp to 21.
…
The last one (06) starts at 22:00 and sets the temp to 15.
My current code consists of (pseudo code):
switch (true)
case (now >= hour_01:minute_01 && now < hour_02:minute_02):
logInfo("temp", "we are in block 1")
....
but in reality, this code is already 200 lines long and what frustrates the most, is that I’m repeating a lot of code and just changing the indexes from 1 to 2 and so on.
A problem I see is that I can not use something like (pseudo code):
val block_index=1
switch (true)
case (now >= hour_$block_index:minute_$block_index && now < hour_($block_index+1):minute_($block_index+1)):
logInfo("temp", "we are in block 1")
....
Then I considered using group_members as described in this design patterns.
But again, I see no way to use them in the case part of the switch.
I could rewrite the code completely to something like (pseudo-code)
for i in block_indexes
if (now >= hour_$block_index:minute_$block_index && now < hour_($block_index+1):minute_($block_index+1)):
then
logInfo("temp", "we are in block 1")
else
i++
Maybe I could find my way out using the above rewrite, but - and maybe this is my question - I feel like group_members usage might even be more reusable code after all, but I don’t see it how to do it.
I could use “findfirst”, but how to check on both conditions.
I could use “filter”, but again… I don’ see it…
So my question might boil down to:
anyone a hint how to (pseudo-code)
mygroup.members.find_first_hour_and_minute_combo_index( combo_index | where
hour(combo_index)*60 + minute(combo_index) >= now.getMinuteOfDay()
&&
hour(combo_index+1)*60 + minute(combo_index+1) < now.getMinuteOfDay()