Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Initial attempt at naive debounce for login api #7

Merged
merged 4 commits into from
Feb 8, 2024

Conversation

hgoscenski
Copy link
Collaborator

@hgoscenski hgoscenski commented Feb 8, 2024

Update maybeLogin to check last login time and perform exponential backoff, up to 5 minutes. Debounce will reset back to 1s after 12 hours has passed, to prevent users from being stuck with a 5 minute polling interval in the case of a login.

In the event a user's password/api key do expire or are changed naturally, then during the config update the bridge/homebridge will be prompted to restart, resetting the debounce and allowing them to login promptly.

Should ameliorate issues in #6

Update example to have parameters provided via env var.
Added .env.sample
Update sleep function to have correctly named parameter

Example can be run via node --env-file=.env example

Example run with loginCheck(4) and persisted token temporarily disabled.

[22:24:21] ~/wyze-homebridge/wyze-api [debounce_login] λ node --env-file=.env example
 | DEBUG           2024-02-07 22:26:37   Last login 0 debounce 1000 now 1707366397148
 | DEBUG           2024-02-07 22:26:37   Performing request: api/user/login
 | DEBUG           2024-02-07 22:26:37   Request config: <SNIP>
 | DEBUG           2024-02-07 22:26:37   API response PerformRequest: <SNIP>
 | DEBUG           2024-02-07 22:26:37   scratch/wyze-63773091-e5d0-554a-8ba2-5a69db9191ee.json
 | DEBUG           2024-02-07 22:26:37   Successfully logged into Wyze API
 | DEBUG           2024-02-07 22:26:37   Last login 1707366397148 debounce 2000 now 1707366397684
 | WARNING         2024-02-07 22:26:37   Attempting to login before debounce has cleared, waiting 2 seconds
 | DEBUG           2024-02-07 22:26:39   Performing request: api/user/login
 | DEBUG           2024-02-07 22:26:39   Request config: <SNIP>
 | DEBUG           2024-02-07 22:26:40   API response PerformRequest: <SNIP>
 | DEBUG           2024-02-07 22:26:40   scratch/wyze-63773091-e5d0-554a-8ba2-5a69db9191ee.json
 | DEBUG           2024-02-07 22:26:40   Successfully logged into Wyze API
 | DEBUG           2024-02-07 22:26:40   Last login 1707366397684 debounce 4000 now 1707366400236
 | WARNING         2024-02-07 22:26:40   Attempting to login before debounce has cleared, waiting 4 seconds
 | DEBUG           2024-02-07 22:26:44   Performing request: api/user/login
 | DEBUG           2024-02-07 22:26:44   Request config: <SNIP>
 | DEBUG           2024-02-07 22:26:44   API response PerformRequest: <SNIP>
 | DEBUG           2024-02-07 22:26:44   scratch/wyze-63773091-e5d0-554a-8ba2-5a69db9191ee.json
 | DEBUG           2024-02-07 22:26:44   Successfully logged into Wyze API
 | DEBUG           2024-02-07 22:26:44   Last login 1707366400236 debounce 8000 now 1707366404582
 | WARNING         2024-02-07 22:26:44   Attempting to login before debounce has cleared, waiting 8 seconds
 | DEBUG           2024-02-07 22:26:52   Performing request: api/user/login
 | DEBUG           2024-02-07 22:26:52   Request config: <SNIP>
 | DEBUG           2024-02-07 22:26:54   API response PerformRequest: <SNIP>
 | DEBUG           2024-02-07 22:26:54   scratch/wyze-63773091-e5d0-554a-8ba2-5a69db9191ee.json
 | DEBUG           2024-02-07 22:26:54   Successfully logged into Wyze API
[22:26:54] ~/wyze-homebridge/wyze-api [debounce_login] λ 

@jfarmer08
Copy link
Owner

I like that changes. I will also clean up the MFA code.

@jfarmer08 jfarmer08 merged commit 2a56055 into jfarmer08:main Feb 8, 2024
1 check passed
@jfarmer08 jfarmer08 linked an issue Feb 8, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

To many request to wyze server.
2 participants