[SOLVED] - How do I replace thread::sleep in a while loop?

Hello, how can I convert the thread :: sleep into a timer in the following lambda?

val Functions$Function4<SwitchItem , Integer, Integer, Integer, Boolean> repeater= [
	m_switch, 
	m_delay, 
	m_repeat,
	m_sleep |

	var Timer DelayTimer=null 
	DelayTimer = createTimer(now.plusMillis(m_delay)) [ |
		var count = m_repeat + 1
    	var i=0
    	while ((i=i+1) < count ) 
		{
			logInfo ("RULES", "AUTO-Licht -- " + m_switch.name + ": Wiederholung " + i + " - Command " + m_switch.state)
			if (m_switch.state == ON) 
			{
				m_switch.sendCommand(ON)
			}
			else 
			{
				m_switch.sendCommand(OFF)
			}
			Thread::sleep(m_sleep)
		}
    	logInfo("RULES", "AUTO-Licht -- "+ m_switch.name + " ist " + m_switch.state.toString)
       	DelayTimer.cancel
		DelayTimer=null
	]
    true
]

The lambda should repeat a command to RFXCOM outlets. The number of repetitions (m_repeat), the delay at the beginning (m_delay) and the delay between the commands (m_sleep) are parameters of the function.

thanks
Heiko

Haven’t tested it, but I believe something like this should work:

val Functions$Function4<SwitchItem , Integer, Integer, Integer, Boolean> repeater= [
	m_switch, 
	m_delay, 
	m_repeat,
	m_sleep |

	var count = 1	
 	var Timer DelayTimer=null 
	DelayTimer = createTimer(now.plusMillis(m_delay)) [ |
		logInfo ("RULES", "AUTO-Licht -- " + m_switch.name + ": Wiederholung " + count + " - Command " + m_switch.state)
		if (m_switch.state == ON) 
		{
			m_switch.sendCommand(ON)
		}
		else 
		{
			m_switch.sendCommand(OFF)
		}
		if (count < m_repeat) {
			DelayTimer.rescedule(now.plusMillis(m_sleep))
		} else {
			logInfo("RULES", "AUTO-Licht -- "+ m_switch.name + " ist " + m_switch.state.toString)
			DelayTimer.cancel
			DelayTimer=null
	]
	true
]

Edit: Not entirely sure if all variables in a lambda gets passed to the timer. After the timer is created, the lambda returns, and the variables might disappear at that point.

thanks, but it’s don’t work:

I corrected the typos in your script.

thanks

Yes, I’ve got it.

val Functions$Function4<SwitchItem , Integer, Integer, Integer, Boolean> repeater= [
	m_switch, 
	m_delay, 
	m_repeat,
	m_sleep |
	
	var count = 1
	var Timer DelayTimer=null 
	DelayTimer = createTimer(now.plusMillis(m_delay)) [ |
		logInfo ("RULES", "AUTO-Licht -- " + m_switch.name + ": Wiederholung " + count + " - Command " + m_switch.state)
		if (m_switch.state == ON) 
		{
			m_switch.sendCommand(ON)
		}
		else 
		{
			m_switch.sendCommand(OFF)
		}
		if (count < m_repeat) 
		{
			DelayTimer.reschedule(now.plusMillis(m_sleep))
			count = count + 1
		} 
		else 
		{
			logInfo("RULES", "AUTO-Licht -- "+ m_switch.name + " ist " + m_switch.state.toString)
			DelayTimer.cancel
			DelayTimer=null
		}
	]
    true
]

That’s how it should work, I’ll watch.
Thank you for the approach.