I want to remove code duplication in a rule by using lambdas. The rule can be trigged by means of a channel Event trigger as well as by an Item change trigger.
One of the lambdas needs to check whether a channel event or an item triggered the rule.
The code without lambdas reads as follows:
var String eventSource = "Undefined Event or Item Source" // Initialize
if (receivedEvent !== null) { // A (channel) event triggered the rule
eventSource = "Event channel '" + receivedEvent.getChannel().asString + "', Event = '" + receivedEvent.getEvent() + "'"
} else if (triggeringItem !== null) { // An Item triggered the rule (used for debug purposes)
if (DBG_DebugMode == OFF) {
eventSource = "DEBUG MODE: Triggering Item '"
+ ( if (triggeringItem.label !== null) triggeringItem.label else triggeringItem.name ) + "', State = '"
+ triggeringItem.state.toString + "' - WILL NOT RUN: 'DBG_DebugMode' == 'OFF'"
// return;
}
eventSource = "DEBUG MODE: Triggering Item '"
+ ( if (triggeringItem.label !== null) triggeringItem.label else triggeringItem.name ) + "', State = '"
+ triggeringItem.state.toString + "'"
}
In order to move this code to a lambda, I need to pass it receivedEvent
and triggeringItem
. However, I need to type them if I want to properly use them.
Obviously, triggeringItem
is of type GenericItem
, but I can’t get hold of the type of triggeringItem
apart from the fact that it appears to be e.g. a ChannelTriggeredEvent
which I can’t use as type definition.
What could I use to get the following code work?
val getEventSourceStr = [
ChannelTriggeredEvent receivedEvent, // Does not work - please fix me
GenericItem triggeringItem
|
var String eventSource = "Undefined Event or Item Source"
if (receivedEvent !== null) {
eventSource = "Event channel '" + receivedEvent.getChannel().asString + "', Event = '" + receivedEvent.getEvent() + "'"
} else if (triggeringItem !== null) { // For debug purposes
if (DBG_DebugMode == OFF) {
eventSource = "DEBUG MODE: Triggering Item '"
+ ( if (triggeringItem.label !== null) triggeringItem.label else triggeringItem.name ) + "', State = '"
+ triggeringItem.state.toString + "' - WILL NOT RUN: 'DBG_DebugMode' == 'OFF'"
return;
}
eventSource = "DEBUG MODE: Triggering Item '"
+ ( if (triggeringItem.label !== null) triggeringItem.label else triggeringItem.name ) + "', State = '"
+ triggeringItem.state.toString + "'"
}
new String(eventSource)
]