darnfish / homebridge-unifi-wap-light

Control the light rings on your UniFi Wireless Access Point(s) with HomeKit
Apache License 2.0
2 stars 1 forks source link

Refactored Code, API Session Manager, and Support For New API URL Endpoints #3

Open pizzalorddex opened 5 months ago

pizzalorddex commented 5 months ago

Overview: This pull request introduces significant improvements to the homebridge-unifi-wap-light plugin. The updates include a comprehensive refactor of the codebase, the integration of a session manager to handle API sessions more robustly, and extended support for both UniFi hosted and self-hosted UniFi Controller API endpoints.

Changes:

  1. Code Refactoring: Enhanced error handling mechanisms to provide clearer error messages and to handle API connectivity issues more gracefully.
  2. API Session Management: Introduced a new SessionManager class responsible for managing API sessions. This includes handling authentication, session renewal, and request retries after session expirations.
  3. Enhanced API Support: Added functionality to communicate with both UniFi hosted and self-hosted UniFi Controller API endpoints. Implemented logic to dynamically select API endpoints based on the availability and response of the UniFi Controller.

Testing:

pizzalorddex commented 5 months ago

I was only able to test using my current setup a self-hosted UniFi controller (running on RaspberryOS) and U7 Pro WAPs. The refactored code should still work for the older/other API and its endpoints, but it is not tested. This solves the bug I raised a few days ago.

Based on other feedback from other Homebridge plugins and from my testing a session management class was implemented to prevent the API calls from working only for a short time before the cookies/token gets refreshed and is no longer valid for making calls.

In my environment this plugin has now been running for 2 days with no issues observed.

nielsth commented 4 months ago

Great to see so much progress on this. Looking forward to its release!

darnfish commented 4 months ago

Thank you so much for the work here @pizzalorddex—I just moved across the states to California so I haven't had a chance to take a look at this, but I hope to approve and merge soon!

pizzalorddex commented 4 months ago

Very nice!!

I have recently been working together with daywalkeraut to hopefully support setting the LEDs of the UDR. I wasn't able to test my code before with the UniFi hosted endpoints and discovered that my changes didn't 100% cover for that. The main problem was authentication, among others.

I have since fixed that in my repo, but am still working on support for UDRs (and hopefully as a side effect UDMs).

I have also changed a number of things in my repo, so definitely please check over everything.

I am really new to this all still, APIs, GitHub, NPM, even coding, etc etc etc. So I hope the changes in my repo will be helpful and follow some good practices. I am sure I am also doing some strange stuff, so please let me know what I can improve!