I am creating a Roku channel. I need to fire a timer and check if device is being verified or not. It can be a complex logic, but you can see my code below. It successfully adding device, and triggers onAddDeviceResult methods. Then startPolling method is running but it doesn't trigger onPoll method so I can't verify the device. How can I solve this problem? Thank you.
sub init()
m.deviceActivatedLabel = m.top.findNode("deviceActivatedLabel")
m.qrPoster = m.top.findNode("qrPoster")
m.deviceSetting = new DeviceSettings()
m.deviceSetting.initialize()
m.deviceUUID = m.deviceSetting.getDeviceUUID()
m.deviceCode = m.deviceSetting.getDeviceCode()
qrUrl = "url"
m.qrPoster.uri = qrUrl
if (m.deviceSetting.readRegistry(m.deviceSetting.deviceActivatedKey) = "true")
m.deviceActivatedLabel.visible = true
print "Device already activated!"
navigateToNextScreen()
else
m.deviceActivatedLabel.visible = false
print "Starting activation process..."
addDeviceAndStartPolling()
end if
end sub
sub addDeviceAndStartPolling()
m.apiTask2 = createObject("roSGNode", "ApiTask2")
m.apiTask2.SetField("deviceUUID", m.deviceUUID)
m.apiTask2.SetField("deviceCode", m.deviceCode)
m.apiTask2.ObserveField("result", "onAddDeviceResult")
print "Observer registered for result field."
print "Adding device with UUID: " + m.deviceUUID + " and Code: " + m.deviceCode
m.apiTask2.control = "RUN"
end sub
sub onAddDeviceResult(event as Object)
print "Observed change in result field."
response = event.getData()
print "Add Device Response: "; response
if (response = invalid or response.id = invalid)
print "Failed to add device."
return
end if
m.deviceSetting.setDeviceId(response.id)
print "Device added successfully. Starting activation loop..."
startPolling()
end sub
sub startPolling()
m.attempts = 0
m.maxAttempts = 10
m.pollTimer = createObject("roSGNode", "Timer")
m.pollTimer.duration = 5000
m.pollTimer.repeat = true
if (m.pollTimer = invalid)
print "Failed to create the Timer node."
return
end if
m.pollTimer.observeField("fire", "onPoll")
print "Observer registered for Timer fire event."
m.top.appendChild(m.pollTimer)
m.pollTimer.control = "start"
print "Polling started with a 5-second interval."
end sub
sub onPoll()
print "Timer fired. Polling now..."
if (m.attempts >= m.maxAttempts)
print "Max attempts reached. Stopping polling."
m.pollTimer.control = "stop"
return
end if
m.attempts = m.attempts + 1
print "Polling attempt: " + m.attempts.toStr()
m.apiTask = createObject("roSGNode", "ApiTask")
m.apiTask.SetField("deviceUUID", m.deviceUUID)
m.apiTask.SetField("deviceCode", m.deviceCode)
m.apiTask.ObserveField("result", "onVerifyDeviceResult")
m.apiTask.control = "RUN"
end sub
sub onVerifyDeviceResult(event as Object)
response = event.getData()
print "Verify Device Response: "; response
if (response <> invalid and response.activated = true)
print "Device activated successfully!"
m.deviceSetting.writeRegistry(m.deviceSetting.deviceActivatedKey, "true")
m.deviceActivatedLabel.visible = true
m.pollTimer.control = "stop"
navigateToNextScreen()
else
print "Device not activated yet. Retrying..."
end if
end sub
sub navigateToNextScreen() as void
print "Navigating to the next screen..."
end sub
I tried adding Timer into xml file. But still it didn't work.
I believe your issue is this line:
m.pollTimer.duration = 5000
The Timer
component takes duration
in seconds, not milliseconds
. (see this docs page).
duration: Specifies the time in seconds before the Timer node fires after the control field value is set to start. To specify time values down to millisecond granularity, use a float type (0.001 equals one millisecond)