我正在创建一个 Roku 频道。我需要启动一个计时器并检查设备是否正在验证。这可能是一个复杂的逻辑,但您可以在下面看到我的代码。它成功添加设备并触发 onAddDeviceResult 方法。然后 startPolling 方法正在运行,但它不会触发 onPoll 方法,所以我无法验证设备。我该如何解决这个问题?谢谢。
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
我尝试将 Timer 添加到 xml 文件中。但仍然没有效果。
代码应该可以正常工作。问题在于计时器持续时间。持续时间 5000 实际上是秒而不是毫秒。因此您的计时器将在 5000 秒后触发(即 1 小时 23 分钟 20 秒后)。如果您想在 5 秒后触发它,则仅将持续时间设置为 5。
我相信你的问题是这一行:
该
Timer
组件需要几秒duration
的时间,而不是。(请参阅此文档页面)。milliseconds