OpenHAB2
After reading about ‘reusable rules via functions’ and ‘Combining different items’ (more info https://github.com/openhab/openhab/wiki/Rule-example%3A-Combining-different-items)
I have some questions
A. in the function tempLogic, what is this code exactly doing?
var Map<String, Map<String, Object>> roomObj = fixtures.get(room)
if (roomObj != null) {
var Map<String, Object> valueObj = roomObj.get(value)
if (valueObj != null) {
var org.openhab.core.items.GenericItem inItem = valueObj.get("Item")
if (inItem != null) {
var doUpdate = false
doUpdate = doUpdate || value == "Actual" && inItem.state > 0.0
doUpdate = doUpdate || value == "Plan" && inItem.state > 0.0
doUpdate = doUpdate || value == "Valve" && inItem.state > 0
doUpdate = doUpdate || value == "Mode"
doUpdate = doUpdate || value == "Battery"
doUpdate = doUpdate && inItem.state != Uninitialized
if (doUpdate) {
fixtures.get(room).get(value).put("LastValue", inItem.state)
fixtures.get(room).get(value).put("LastUpdate",
DateTimeUtils::currentTimeMillis())
logDebug("Heating", String::format("Value updated! %s --> %s --> %s um %s",
room, value, valueObj.get("LastValue"), valueObj.get("LastUpdate"))
}
especially the part of the update
doUpdate = doUpdate || value == "Actual" && inItem.state > 0.0
define doUpdate = doUpdate
B. reusable functions
// lambda expression that can be used as a function (here: with 3 parameters)
// … used in update and createRoom
for (value : fixtures.get(room).keySet()) {
tempLogic.apply(room, value, fixtures)
}
tempOutput.apply(room, fixtures, outputItems)
is it possible to put in a separate function?
C. Can this be solved by lambda expression?
rule sysinit
when
System started
then
createRoom.apply("GF_WZ_WT", newArrayList(GF_WZ_WT_default, null, GF_WZ_WT_battery,
GF_WZ_WT_mode, GF_WZ_WT_actual, GF_WZ_WT_komplett), fixtures, outputItems)
createRoom.apply("GF_WZ_HT_Fenster", newArrayList(GF_WZ_HT_Fenster_default,
GF_WZ_HT_Fenster_valve, GF_WZ_HT_Fenster_battery, GF_WZ_HT_Fenster_mode,
GF_WZ_HT_Fenster_actual, GF_WZ_HT_Fenster_komplett), fixtures, outputItems)
...
for (value : fixtures.get(room).keySet()) {
tempLogic.apply(room, value, fixtures)
}
tempOutput.apply(room, fixtures, outputItems)
}
end
rule GF_WZ_WT_updated
when
Item GF_WZ_WT_default received update or
Item GF_WZ_WT_battery received update or
Item GF_WZ_WT_mode received update or
Item GF_WZ_WT_actual received update
then
var room = "GF_WZ_WT"
// same as in : rule sysinit
// createRoom.apply("GF_WZ_WT", newArrayList(GF_WZ_WT_default, null, GF_WZ_WT_battery,
// GF_WZ_WT_mode, GF_WZ_WT_actual, GF_WZ_WT_komplett), fixtures, outputItems)
createRoom.apply(room, newArrayList(GF_WZ_WT_default, null, GF_WZ_WT_battery,
GF_WZ_WT_mode, GF_WZ_WT_actual, GF_WZ_WT_komplett), fixtures, outputItems)
for (value : fixtures.get(room).keySet()) {
tempLogic.apply(room, value, fixtures)
}
tempOutput.apply(room, fixtures, outputItems)
end