My problem is after a few years I have no clue on how to write a rule. I’ve seen so many design patterns and seen people recommend so many different approaches I don’t know what is I’m suppose to do.
Here is just a small part of one file that relates to just turning lights on and off. You can see where I tried to use a timer instead of sleep but it just broke it so I reverted back. Just be warned you’ll probably don’t want to see the code, it’ll make you cry.
val Functions$Function4<GenericItem, HSBType, PercentType,Functions.Function2<void>, HSBType> colorSmooth = [
inColor,
passColor,
inBrightness,
Functions.Function2<void> sendColor
|
if(db) logInfo ("Lambda Smooth", "Lambda in color [{}] and brightness [{}]", inColor, inBrightness)
var PercentType outBrightness = passColor.brightness
var HSBType transitionColor=new HSBType(passColor.hue, passColor.saturation, inBrightness)
var HSBType oldColor=inColor.state as HSBType
if((outBrightness as DecimalType) == 0){
transitionColor=new HSBType(oldColor.hue, oldColor.saturation, inBrightness)
}
var HSBType lambdaColor=transitionColor
var int step=10
var DecimalType stepSize = new DecimalType(step)
var PercentType brightJump= new PercentType(step)
var PercentType dimmingJump= new PercentType (0)
var PercentType lambdaBrightness = inBrightness
var PercentType startBrightness = inBrightness
var int speed = 15
var DecimalType diffBrightness = new DecimalType(outBrightness - inBrightness)
var PercentType satStep = new PercentType(0)
var DecimalType steps = new DecimalType(diffBrightness / stepSize as DecimalType)
var DecimalType hueStep = new DecimalType(0)
if(steps==0){
steps=new DecimalType(10)
step=0;
}
if(steps > 0){
hueStep = new DecimalType((passColor.hue - oldColor.hue)/steps)
satStep = new DecimalType((passColor.saturation - oldColor.saturation)/steps)
}
else{
}
if ( (inBrightness as DecimalType) < (outBrightness as DecimalType) )
{
//logInfo( "Lambda Smooth", "Brighten" )
brightJump=new PercentType(step)
dimmingJump=new PercentType(0)
lambdaBrightness=new PercentType(inBrightness + 1)
}
if ( (inBrightness as DecimalType) > (outBrightness as DecimalType) )
{
//logInfo( "Lambda Smooth", "Dim values out [{}], pass [{}]", inBrightness, outBrightness )
startBrightness=outBrightness
outBrightness=inBrightness
brightJump=new PercentType(0)
dimmingJump=new PercentType(step)
//logInfo( "Lambda Smooth", "Before start [{}], out [{}] and in [{}]", startBrightness, outBrightness, inBrightness )
lambdaBrightness=new PercentType(outBrightness - 1)
}
if(db) logInfo( "Lambda Smooth", "inBrightness is [{}] to out [{}] and current lambda is [{}]", startBrightness, outBrightness, lambdaBrightness )
//sendCommand(inColor, lambdaColor)
if(db) logInfo( "Lambda Smooth", "oldColor {} {}", oldColor, oldColor.hue)
//var DecimalType dimValue=((dimEnd as DecimalType) - (dimStart as DecimalType)).intValue
//while(inBrightness < lambdaBrightness && lambdaBrightness < outBrightness)
var DecimalType newHue=oldColor.hue as DecimalType
var PercentType newSat=oldColor.saturation as PercentType
if((oldColor.brightness as DecimalType )==0){
newHue=passColor.hue as DecimalType
newSat=passColor.saturation as PercentType
hueStep=new DecimalType(0)
satStep=new PercentType(0)
}
if(db) logInfo( "Lambda Smooth", "pre steps {}", steps )
/*
timer = createTimer(now, [ |
// some code
steps = steps=new DecimalType(steps -1)
if(db) logInfo( "Lambda Smooth", "Brighten" )
Thread::sleep(speed)
lambdaColor=new HSBType(newHue, newSat, lambdaBrightness)
if(db) logInfo("lambda smooth", "in trans {}, lambda {}", transitionColor, lambdaColor)
if(lambdaColor.brightness as DecimalType ==0){
var HSBType preOff=new HSBType(setColor.hue,setColor.saturation,new PercentType(1))
sendColor.apply(item, preOff.toString)
Thread::sleep(10)
}
sendColor.apply(inColor, lambdaColor)
//inBrightness=inBrightness + brightJump
//outBrightness=outBrightness - dimJump
if((lambdaBrightness+brightJump as DecimalType) > 100)
{
brightJump= new PercentType(100 - lambdaBrightness)
}
if((lambdaBrightness - dimmingJump as DecimalType) < 0)
{
//logInfo( "Lambda Smooth", "lesse than 0 [{}] to out [{}] and start is [{}]", lambdaBrightness, dimmingJump, startBrightness )
dimmingJump= lambdaBrightness
}
lambdaBrightness=new PercentType(lambdaBrightness + brightJump - dimmingJump)
newHue=new DecimalType(newHue + hueStep)
newSat=new PercentType(newSat + satStep)
if((startBrightness as DecimalType) < (lambdaBrightness as DecimalType) && (lambdaBrightness as DecimalType) < (outBrightness as DecimalType)) timer.reschedule(now.plusMillis(speed))
else timer = null
])
*/
while((startBrightness as DecimalType) < (lambdaBrightness as DecimalType) && (lambdaBrightness as DecimalType) < (outBrightness as DecimalType))
{
steps = steps=new DecimalType(steps -1)
if(db) logInfo( "Lambda Smooth", "Brighten" )
Thread::sleep(speed)
lambdaColor=new HSBType(newHue, newSat, lambdaBrightness)
if(db) logInfo("lambda smooth", "in trans {}, lambda {}", transitionColor, lambdaColor)
if(lambdaColor.brightness as DecimalType ==0){
var HSBType preOff=new HSBType(setColor.hue,setColor.saturation,new PercentType(1))
sendColor.apply(item, preOff.toString)
Thread::sleep(10)
}
sendColor.apply(inColor, lambdaColor)
//inBrightness=inBrightness + brightJump
//outBrightness=outBrightness - dimJump
if((lambdaBrightness+brightJump as DecimalType) > 100)
{
brightJump= new PercentType(100 - lambdaBrightness)
}
if((lambdaBrightness - dimmingJump as DecimalType) < 0)
{
//logInfo( "Lambda Smooth", "lesse than 0 [{}] to out [{}] and start is [{}]", lambdaBrightness, dimmingJump, startBrightness )
dimmingJump= lambdaBrightness
}
lambdaBrightness=new PercentType(lambdaBrightness + brightJump - dimmingJump)
newHue=new DecimalType(newHue + hueStep)
newSat=new PercentType(newSat + satStep)
Thread::sleep(speed)
//logInfo( "Lambda Smooth", "LOOP LOOP inBrightness is [{}] to out [{}] and current lambda is [{}]", startBrightness, outBrightness, lambdaBrightness )
}
if(db) logInfo("Lambda Smooth", "debgug after loop")
//sendColor.apply(inColor, passColor)
return lambdaColor
]
rule "Generic light"
when
Item lightDetails received command
then
logInfo("Generic Light", "start {}", receivedCommand)
var stringCommand= receivedCommand.toString
var itemName=stringCommand.split("-").get(0)
var itemNewColor=stringCommand.split("-").get(1)
var itemOldColor=stringCommand.split("-").get(2)
if(db) logInfo("Generic Light", "real thing is {} {} {}", itemName, itemNewColor, itemOldColor)
var updatedLight=gLightColor.members.filter[s|s.lastUpdate() != null].sortBy[lastUpdate()].last
gLightColor.members.forEach[ light|
if(light.name.contains(itemName)){
updatedLight=light
//logInfo("Generic light", "updating light {}", light)
}
]
if(db) logInfo("Generic Light", "updated item is {}", updatedLight)
var prevLight=updatedLight
var altLight=updatedLight
var hue=false
updatedLight.getGroupNames.forEach[room |
if(room.contains("gHue")){
if(db) logInfo("GEneric light", "hue")
hue=true
}
if(!room.contains("gLightColor")){
gColor.members.forEach [ light |
light.getGroupNames.forEach[ group |
if(group.contains(room)){
prevLight=light
}
]
]
gAltColor.members.forEach [ light |
light.getGroupNames.forEach[ group |
if(group.contains(room)){
altLight=light
}
]
]
}
]
if(!hue){
if(db) logInfo("Generic Light", "updated item is {}", updatedLight)
var receivedCommand=updatedLight.state
var HSBType nextColor
if(receivedCommand.toString.matches("\\d+"))
{
logInfo("Generic Light", "match digit {}", receivedCommand)
nextColor=new HSBType((updatedLight.state as HSBType).hue,(updatedLight.state as HSBType).saturation, receivedCommand as PercentType)
if(db) logInfo( "Generic Light", "on to Scene [{}]", newColor )
}
else{
nextColor = receivedCommand as HSBType
}
var HSBType prevColor = prevLight.state as HSBType
logInfo( "Generic Light", "sending color [{}] to [{}] from [{}]", updatedLight, receivedCommand, prevColor)
//perBrightness=(HueLivingRoomColor.state as HSBType).brightness
var newBrightness = nextColor.brightness
if((prevColor.brightness as DecimalType) == 0 && Integer::parseInt(newBrightness.toString) > 30){
if(db) logInfo("Transition", "TIF")
prevColor=new HSBType(prevColor.hue, prevColor.saturation, new PercentType(30))
//HueLivingRoom.sendCommand(prevColor)
}
if(prevColor.brightness > 0 || Integer::parseInt(newBrightness.toString) > 0)
{
logInfo( "Generic Light", "sending perBrightness [{}] to [{}]", nextColor, updatedLight)
newColor=colorSmooth.apply(prevLight, nextColor, prevColor.brightness, sendColor)
if(db) logInfo( "Generic Light", "sending newColor [{}] to [{}]", newColor, updatedLight)
}
sendColor.apply(altLight, nextColor)
Thread::sleep(100)
sendColor.apply(prevLight, nextColor)
}
end
rule "Lamba test"
when
Channel "amazondashbutton:dashbutton:88-71-e5-74-0a-6a:press" triggered
then
oldColor=new HSBType(new DecimalType(0),new PercentType(0),new PercentType(0))
perBrightness=new PercentType(0)
decHue=new DecimalType(0)
Thread::sleep(500)
end
rule "All rooms rule"
when
Item ProxyAllColor received command
then
sendCommand(ProxyLivingRoomColor, receivedCommand)
sendCommand(ProxyHallwayColor, receivedCommand)
sendCommand(ProxyBathroomColor, receivedCommand)
sendCommand(HueBedroom, receivedCommand)
sendCommand(HueStriplight, receivedCommand)
Thread::sleep(500)
sendCommand(HueAllColor, receivedCommand)
end
Anyway I’ve got the jython up and running pretty well, I’ve even integrated a spotify api that can be loaded as a module which is much better than doing some kind of exec command. I’m pretty sure I’ll be much happier there, and spent 1/10 of time and write 10* better code.