Closed ldeora closed 1 week ago
We could also do something like this:
local function retry_get_request(url, headers, socket_builder, max_retries, delay)
local retries = 0
local response, error, status
repeat
response, error, status = RestClient.one_shot_get(url, headers, socket_builder)
if error == "wantread" then
log.warn("Retrying due to 'wantread' error")
socket.sleep(delay)
retries = retries + 1
elseif error then
log.error(string.format("Error: %s", error))
break
end
until not error or retries >= max_retries
return response, error, status
end
function fp2_api.get_credential(device_ip, socket_builder)
local url = get_base_url(device_ip) .. "/authcode"
local response, error, status = retry_get_request(url, ADDITIONAL_HEADERS, socket_builder, 5, 2) -- Retry 5 times with a 2-second delay
if not error and status == 200 then
return response
else
log.error(string.format("get_credential: ip = %s, failed to get token, error = %s", device_ip, error))
end
end
This way, we could use the new function retry_get_request()
in all cases where RestClient.one_shot_get()
is used. The name one_shot_get implies that it's really only one try.
The retry logic is in the lunchbox...
This is how the log looks like when the device isn't ready yet to answer REST API requests:
We don't know how the firmware works but maybe it just needs a bit more time? Here's how the driver gets the token:
https://github.com/SmartThingsCommunity/SmartThingsEdgeDrivers/blob/ea0625ddf6adceb26799dfc5cf05aa2ad4b9a5ec/drivers/Aqara/aqara-presence-sensor/src/fp2/api.lua#L70-L79
Can we change this to retry a few times? Maybe like this?
With this change the driver should be more resilient against timing issues while retrieving the auth token from the device.
What do you think, @seojune79 ?