- Platform information:
- Hardware: Raspberry Pi 3b
- OS: Openhabian
- Java Runtime Environment: OpenJDK Runtime Environment (Zulu8.36.0.152-CA-linux_aarch32hf) (build 1.8.0_202-b152)
- openHAB version: 2.4
Hi all,
I’m trying to itterate through all items of a group. If the current item is a DimmerItem I want to set a Level and if it is a ColorItem I want to set a Color.
But is the current Item is a Sub-Group, I have to dive into this sub-group and Analyse those member again.
Usually this is a perfect Situation to use recursion.
I was able to design a Lambda Expression to Analyse the members of a Group, but I’m struggling when recursively calling the Lambda Expression insode itself.
import org.joda.time.*
// *********** LAMBDAS ***********
val Functions$Function3<GroupItem, GenericItem, GenericItem, Result> applySettings = [ GroupItem s, GenericItem b, GenericItem c |
var group = s
var color = c
var brightness = b
var applySettings = true
val boolean logLambda = true
val String logPrefix = 'Example LAMBDA - '
logInfo('rules', logPrefix + '--------------------- Lambda Begin ---------------------')
logInfo('rules', logPrefix + ' >>> ' + group + ' --> Class: ' + group.getClass().getSimpleName())
group.members.forEach[member|
if (member !== null) {
logInfo('rules', logPrefix + ' >>> ' + member + ' Class:' + member.getClass().getSimpleName())
switch (member.getClass().getSimpleName())
{
case "DimmerItem" : {
logInfo('rules', logPrefix + 'Applying Brightness: ' + brightness)
sendCommand(member, brightness)
}
case "ColorItem" : {
logInfo('rules', logPrefix + 'Applying Color: ' + color)
sendCommand(member, color.state)
}
case "GroupItem" : {
logInfo('rules', logPrefix + 'Recursion starts here')
applySettings.apply(member, b, c) // <-- **Here shall the Lambda call itself recursively**
}
default:
{
logInfo('rules', logPrefix + 'default: ' + member + ' Class:' + member.getClass().getSimpleName())
}
}
}
else
logInfo('rules', logPrefix + member + ' = null')
]
logInfo('rules', logPrefix + '--------------------- Lambda END ---------------------')
]
rule "Testrule"
when
Time cron "0 0/1 * ? * * *"
then
val String logPrefix = 'Example - '
var group = G_LightOut
logInfo('rules', logPrefix + '************************ Rule Begin ************************')
applySettings.apply(group, 80, BeleuchtungStrasseObenColor)
logInfo('rules', logPrefix + '************************ Rule End ************************')
end
Any Idea how to get the recursion working?
Thanks a lot,
Thorsten