Simulate Relais in Rule and code reduction

hi,

used version 1.8.3

i want to simulate a relais-control in openhab.

background: i do have my own homecontrol with some µCs and i2c MCP-devices controled via mqtt.
before, i hard-coded every gpio-output to the correspondending ports.
now, i want the same more dynamicaly and send the µCs commands to set gpio to input or output.
for security reasons i use two relais for one rolloshutter. one for on/off, and one for direction.

so i coded a rule, which do this stuff …
but now i want to know, if there is another more compact way to do this. what i did not want is a highend java-like crypto code where a java-noob like me do see nothing more than a kind of ascii-table …

i also want to have some security, that nobody could do the same stuff, during execution of rule.
but, if i pressed i.e. UP but i wanted to press DOWN, than i should have to be able to do this …

are there any advices out there?

String			ardu02_20_out_10_06_stat	"Status (ardu02-20-10-06) [%s]"	<info>	{ mqtt="<[mosquitto_cubie:i2c/stat/pinout/20/13/6:state:default]" }
Rollershutter	ardu02_20_out_10_07			"Schalten (ardu02-20-10-07)"	<light>
String			ardu02_20_out_10_07_stat	"Status (ardu02-20-10-07) [%s]"	<info>	{ mqtt="<[mosquitto_cubie:i2c/stat/pinout/20/13/7:state:default]" }

Switch item=ardu02_20_out_10_07
Text item=ardu02_20_out_10_06_stat
Text item=ardu02_20_out_10_07_stat

this code is working, but it seemed to me as a little bit to long, especially if i want to add a lot of them …

var int lockUDS_ardu02_20_out_10_07 = 0

rule "rule_ardu02_20_out_10_07_receive_update_UP"
when
    Item ardu02_20_out_10_07 received command UP
then
	logInfo("rule_ardu02_20_out_10_07_updated.rule", "UP: START: rule_ardu02_20_out_10_07_receive " + lockUDS_ardu02_20_out_10_07)
	
	while (lockUDS_ardu02_20_out_10_07 != 0)
	{
		Thread::sleep(5);
	}
	lockUDS_ardu02_20_out_10_07 = 3
	
	publish("mosquitto_cubie", "i2c", "pinout:20:13:6:0")

	while (ardu02_20_out_10_06_stat.state == "1")
	{
		Thread::sleep(5)
	}
	
	if (ardu02_20_out_10_06_stat.state == "0" && lockUDS_ardu02_20_out_10_07 == 3)
	{
		Thread::sleep(250)
		publish("mosquitto_cubie", "i2c", "pinout:20:13:7:1")
		Thread::sleep(250)
		publish("mosquitto_cubie", "i2c", "pinout:20:13:6:1")
	}
	else if (ardu02_20_out_10_06_stat.state != "0")
	{
		logInfo("rule_ardu02_20_out_10_07_updated.rule", "UP: Pin 6 bleibt 1, Frage, warum")	
		publish("mosquitto_cubie", "i2c", "pinout:20:13:6:0")
	}
	else
	{
		logInfo("rule_ardu02_20_out_10_07_updated.rule", "UP: Lock hat nicht funktioniert")
	}

	lockUDS_ardu02_20_out_10_07 = 0
	logInfo("rule_ardu02_20_out_10_07_updated.rule", "UP: ENDE: rule_ardu02_20_out_10_07_receive " + lockUDS_ardu02_20_out_10_07)
end


rule "rule_ardu02_20_out_10_07_receive_update_DOWN"
when
    Item ardu02_20_out_10_07 received command DOWN
then
	logInfo("rule_ardu02_20_out_10_07_updated.rule", "DOWN: START: rule_ardu02_20_out_10_07_receive: " + lockUDS_ardu02_20_out_10_07)
	
	while (lockUDS_ardu02_20_out_10_07 != 0)
	{
		Thread::sleep(5);
	}
	lockUDS_ardu02_20_out_10_07 = 2
	
	publish("mosquitto_cubie", "i2c", "pinout:20:13:6:0")

	while (ardu02_20_out_10_06_stat.state == "1")
	{
		Thread::sleep(5)
	}
	
	if (ardu02_20_out_10_06_stat.state == "0" && lockUDS_ardu02_20_out_10_07 == 2)
	{
		Thread::sleep(250)
		publish("mosquitto_cubie", "i2c", "pinout:20:13:7:0")
		Thread::sleep(250)
		publish("mosquitto_cubie", "i2c", "pinout:20:13:6:1")
	}
	else if (ardu02_20_out_10_06_stat.state != "0")
	{
		logInfo("rule_ardu02_20_out_10_07_updated.rule", "DOWN: Pin 6 bleibt 1, Frage, warum")	
		publish("mosquitto_cubie", "i2c", "pinout:20:13:6:0")
	}
	else
	{
		logInfo("rule_ardu02_20_out_10_07_updated.rule", "DOWN: Lock hat nicht funktioniert")
	}

	lockUDS_ardu02_20_out_10_07 = 0
	logInfo("rule_ardu02_20_out_10_07_updated.rule", "DOWN: ENDE: rule_ardu02_20_out_10_07_receive: " + lockUDS_ardu02_20_out_10_07)
end


rule "rule_ardu02_20_out_10_07_receive_update_STOP"
when
    Item ardu02_20_out_10_07 received command STOP
then
	logInfo("rule_ardu02_20_out_10_07_updated.rule", "STOP: START: rule_ardu02_20_out_10_07_receive; " + lockUDS_ardu02_20_out_10_07)

	publish("mosquitto_cubie", "i2c", "pinout:20:13:6:0")
	Thread::sleep(250)
	publish("mosquitto_cubie", "i2c", "pinout:20:13:7:0")

	lockUDS_ardu02_20_out_10_07 = 0
	logInfo("rule_ardu02_20_out_10_07_updated.rule", "STOP: ENDE: rule_ardu02_20_out_10_07_receive: " + lockUDS_ardu02_20_out_10_07)
end

if i only use a switch-item, this works like expected, but i do not get it to work with rollershutter-item.

rule "rule_ardu02_20_out_10_07_receive_update"
when
    Item ardu02_20_out_10_07 received command UP or
    Item ardu02_20_out_10_07 received command DOWN or
    Item ardu02_20_out_10_07 received command STOP
then
	logInfo("rule_ardu02_20_out_10_07_updated.rule", "rule_ardu02_20_out_10_07_receive_update: " + ardu02_20_out_10_07.state.toString())

	publish("mosquitto_cubie", "BB55/hctl/v1/ardu02/sub/i2c", "pinout:20:13:6:0")

	logInfo("rule_ardu02_20_out_10_07_updated.rule", "Warten auf 6 = 0")
	while (ardu02_20_out_10_06_stat.state == "1")
	{
		Thread::sleep(5)
	}
	
	if (ardu02_20_out_10_06_stat.state == "0")
	{
		logInfo("rule_ardu02_20_out_10_07_updated.rule", "Wir dürfen schalten")
		if (ardu02_20_out_10_07.state == UP)
		{
			logInfo("rule_ardu02_20_out_10_07_updated.rule", "und schalten 7 auf 1 UP")
			Thread::sleep(500)
			publish("mosquitto_cubie", "i2c", "pinout:20:13:7:1")
			Thread::sleep(500)
			publish("mosquitto_cubie", "i2c", "pinout:20:13:6:1")
		}
		else if (ardu02_20_out_10_07.state == DOWN)
		{
			logInfo("rule_ardu02_20_out_10_07_updated.rule", "und schalten 7 auf 0 DOWN")
			Thread::sleep(500)
			publish("mosquitto_cubie", "i2c", "pinout:20:13:7:0")
			Thread::sleep(500)
			publish("mosquitto_cubie", "i2c", "pinout:20:13:6:1")
		}
		else
		{
			logInfo("rule_ardu02_20_out_10_07_updated.rule", "und schalten 7 auf 0")
			Thread::sleep(500)
			publish("mosquitto_cubie", "i2c", "pinout:20:13:7:0")
		}
	}
	else
	{
		logInfo("rule_ardu02_20_out_10_07_updated.rule", "Pin 6 bleibt 1, Frage, warum")	
		publish("mosquitto_cubie", "i2c", "pinout:20:13:6:0")
	}
end

at last, i need an advice how to lock them against re-start again. so if the shutter is set to UP, only STOP and DOWN have to work. (for now i only “lock” against UP/DOWN simultaneously)
but keep in mind, i need a lot of this rules.

this moring i got an idea, i send off to port 6, and i have to wait, that this port is down and the µC replies this, if not, nothing could be changed. i came up with the idea to do this in a for-loop, for maybe 10 times with 100ms wait between. is it good idea?