folks,
When using a lambda, are variables passed to it a reference or a copy?
I have the following code - which works - but wondering if it’s wrong.
import java.util.concurrent.locks.ReentrantLock
import org.eclipse.xtext.xbase.lib.Functions
import java.util.concurrent.TimeUnit
val ReentrantLock fronRoomBlindLock = new ReentrantLock
val Functions$Function3 setPosition = [
GenericItem itemBlind,
String mac,
ReentrantLock fronRoomBlindLock
|
if (fronRoomBlindLock.getHoldCount() > 0){logWarn("Blinds - Front room", "setPosition - Waiting on other locks. Count:" + fronRoomBlindLock.getHoldCount().toString)}
try{
fronRoomBlindLock.tryLock(5, TimeUnit.SECONDS)
val req = itemBlind.state
sendHttpGetRequest("http://rpiblinds01:8080/setposition/" + mac + "/" + req, 1000)
}
catch(Throwable t) {
logError("Blinds - setPosition", t.getMessage())
}
finally {
fronRoomBlindLock.unlock()
}
]
val Functions$Function3 getPosition = [
GenericItem itemBlind,
String mac,
ReentrantLock fronRoomBlindLock
|
try{
if (fronRoomBlindLock.getHoldCount() > 0){logWarn("Blinds - Front room", "getPosition - Waiting on other locks. Count:" + fronRoomBlindLock.getHoldCount().toString)}
fronRoomBlindLock.tryLock(5, TimeUnit.SECONDS)
var String value = sendHttpGetRequest("http://rpiblinds01:8080/getposition/" + mac, 1000)
val int pos = Integer.parseInt(transform("JS", "blindsExtractValue.js", value))
if (pos < 0){
logWarn("Blinds - Check Position", "Position for blind " + itemBlind.name + " reports less than zero")
return;
}
var iPOS = itemBlind.name.toString + "_Position"
sendCommand(itemBlind.name + "_Position", pos)
postUpdate(itemBlind.name + "_PC", pos)
}
catch(Throwable t) {
logError("Blinds - getPosition", t.getMessage())
}
finally {
fronRoomBlindLock.unlock()
}
]
rule "Blinds - Front Room - Right - PC"
when
Item FrontRoomBlindsRight_PC received command
then
setPosition.apply(FrontRoomBlindsRight_PC, "C1:03:74:91:05:6C", fronRoomBlindLock)
end
rule "Check Blind Position"
when
Time cron "0/30 * * ? * * *"
then
// timer = Timer.createTimer(now.plusSeconds(3) [|
// sendCommand(EnergiseGD, OFF)
// timer = null // reset the timer
// ])
getPosition.apply(FrontRoomBlindsLeft, "F3:95:30:3E:DD:4A", fronRoomBlindLock)
Thread::sleep(500)
getPosition.apply(FrontRoomBlindsRight, "C1:03:74:91:05:6C", fronRoomBlindLock)
end
I have to use the lock because the call is ultimately made by a BT device. This throws an error if there are two simultaneous calls. With multiple items in a group, this happens a lot.
What I am seeing through is from another call - getPosition, which uses the same lock but is printing warnings that it is waiting on the lock. What is odd is that the last operation was a while ago so not use why it is.
I do have some exceptions taking place - still trying to understand - but I think it’s them which are tripping up and not doing the unlock.
So, correct use or not? Better way?
thanks.