But in OH 3 there is absolutely no need to use a Reentrant lock. Any individual rule can only have one instance running at a given time. If the rule is triggered while it’s already running, that the subsequent triggers are queued up and processed in sequence.
Therefore, even if you have a reentrant lock, it won’t do anything because it will never be the case where two separate threads need to view for the lock.
Thank you, I was trying to run the following script to get the status of my washing machine. This ran well in 2.5 for quite some time:
// import
import java.util.concurrent.locks.ReentrantLock
// variables and values
val Number MODE_OFF = 0
val Number MODE_STANDBY = 1
val Number MODE_ACTIVE = 2
val Number MODE_FINISHED = 3
val ReentrantLock Waschmaschine_lock = new ReentrantLock()
val telegramAction = getActions("telegram","telegram:telegramBot:xxx")
// rule
if (Wash_power.state < 0.5) Washingmachine_OpState.postUpdate(MODE_OFF)
else if (Wash_power.state > 100) Washingmachine_OpState.postUpdate(MODE_ACTIVE)
else if (Wash_power.state < 6.5) {
if (Washingmachine_OpState.state == MODE_OFF) Washingmachine_OpState.postUpdate(MODE_STANDBY)
else if (Washingmachine_OpState.state == MODE_ACTIVE) {
Waschmaschine_lock.lock()
try {
Thread::sleep(100000) // Debounce for 100 seconds
if (Wash_power.state < 6.5) {
Washingmachine_OpState.postUpdate(MODE_FINISHED)
telegramAction.sendTelegram("Washing Machine Finished")
}
} finally {
Waschmaschine_lock.unlock()
}
}
}
So in OH 3 i can scratch that lock-function completely?
What about other java imports like hash maps?
This I am using for a DSL scripts that gives me the gas station prices neatly listed on telegram request:
import java.util.HashMap
if (telegram_last_message_text.state.toString == "Diesel")
{
var reply = "Dieselpreise:"
val HashMap<String, DecimalType> DieselMap = newHashMap
if (TankstelleShellRissenerLandstr1a_Diesel.state != NULL && TankstelleShellRissenerLandstr1a_Diesel.state != UNDEF && TankstelleShellRissenerLandstr1a_Openingstate.state == OPEN) {
DieselMap.put("SHELL Rissener Landstrasse 1a", (TankstelleShellRissenerLandstr1a_Diesel.state as DecimalType))
}
if (Tankstelle_Suelldorfer_Landstrasse_Diesel.state != NULL && Tankstelle_Suelldorfer_Landstrasse_Diesel.state != UNDEF && Tankstelle_Suelldorfer_Landstrasse.state == OPEN) {
DieselMap.put("ARAL Suelldorfer Landstrasse 91", (Tankstelle_Suelldorfer_Landstrasse_Diesel.state as DecimalType))
}
if (TankstelleJETOsdorferLandstr170_Diesel.state != NULL && TankstelleJETOsdorferLandstr170_Diesel.state != UNDEF && TankstelleJETOsdorferLandstr170_Openingstate.state == OPEN) {
DieselMap.put("JET Osdorfer Landstrasse 170", (TankstelleJETOsdorferLandstr170_Diesel.state as DecimalType))
}
for (Price : DieselMap.entrySet.sortBy[value]) {
var String key = Price.getKey().toString
var String value = Price.getValue().toString
reply = reply + String::format("\n%s: %s €", key, value)
}
logInfo("Dieselpreise", reply)
val telegramAction = getActions("telegram","telegram:telegramBot:xxx")
telegramAction.sendTelegramAnswer(telegram_reply_id.state.toString, reply)
}
else
{
var reply = "Preise Super:\n"
val HashMap<String, DecimalType> E5Map = newHashMap
if (TankstelleShellRissenerLandstr1a_E5.state != NULL && TankstelleShellRissenerLandstr1a_E5.state != UNDEF && TankstelleShellRissenerLandstr1a_Openingstate.state == OPEN) {
E5Map.put("SHELL Rissener Landstrasse 1a", (TankstelleShellRissenerLandstr1a_E5.state as DecimalType))
}
if (Tankstelle_Suelldorfer_Landstrasse_E5.state != NULL && Tankstelle_Suelldorfer_Landstrasse_E5.state != UNDEF && Tankstelle_Suelldorfer_Landstrasse.state == OPEN) {
E5Map.put("ARAL Suelldorfer Landstrasse 91", (Tankstelle_Suelldorfer_Landstrasse_E5.state as DecimalType))
}
if (TankstelleJETOsdorferLandstr170_E5.state != NULL && TankstelleJETOsdorferLandstr170_E5.state != UNDEF && TankstelleJETOsdorferLandstr170_Openingstate.state == OPEN) {
E5Map.put("JET Osdorfer Landstrasse 170", (TankstelleJETOsdorferLandstr170_E5.state as DecimalType))
}
for (Price : E5Map.entrySet.sortBy[value]) {
var String key = Price.getKey().toString
var String value = Price.getValue().toString
reply = reply + String::format("\n%s: %s €", key, value)
}
logInfo("Preise Super", reply)
val telegramAction = getActions("telegram","telegram:telegramBot:xxx")
telegramAction.sendTelegramAnswer(telegram_reply_id.state.toString, reply)
}
I am not a big code person therefore I did not even dare to think about moving to other rule languages than DSL…