Isaksson / node-red-contrib-unifi

MIT License
50 stars 19 forks source link

Error Bad Request #115

Closed sascha108 closed 10 months ago

sascha108 commented 11 months ago

Since Update to 0.3.x action "WLanSettings" or "ClientDevices" randomly failed with ERR_BAD_REQUEST in UnifiNode. At fail, msg.payload is set to Timestamp.

System: RPi4 Controller: 7.3.83 NodeRed: 3.0.1

Sascha

Isaksson commented 11 months ago

I have verified with an inject node with interval every 5 seconds and it has been running over 1 hour now with no failed requests. So there is no general issue with the node, must be something special with your environment. How often does it fail and is it only those two commands that fail?

sascha108 commented 11 months ago

Failure only at rarely called nodes. For a client list i call "ClientDevices" every 30 seconds - no failure To turn off the wlan over night, the node called once every 10 minutes (from11pm) to wait until no clients connected, and one call at morning to switch on the wlan. In this situation, the error almost always occurs. I'll try it again with other requests.

add: At fail, no login entry found in controller log. Nodes that are polled regularly do not seem to re-login each time. So there seems to be a problem when first login, so that this only works after the 2nd call.

Sascha

Isaksson commented 11 months ago

Thanks for the information. Yes, I think it's issue witch first login, I have build in logic to automatically retry one time if the login fail. Seems to work well first time on a Node but not in the case same node needs to login again. I will have to check the logic.

Isaksson commented 11 months ago

Could you verify that you run one of the latest version?

sascha108 commented 11 months ago

i used 0.3.5 actual - have not seen that 0.3.6 online sice 1d. I'll do the update tomorrow and test it again.

sascha108 commented 11 months ago

At first test @0.3.6 no failure occured. Let's see for the next 24 hours.

sascha108 commented 10 months ago

Hello, i must break the test, every run of unifi node produced the following output in syslog so my 30s call to list clients floods log with a lot of stuff. syslog_output_unifi-node.txt This is the output in syslog of one call "wlansettings" and "clientdevices".

Sascha

Isaksson commented 10 months ago

Hello. I know of that log output, I could fix and publish a new version so that you could continue your test, I will update you in this post when Iam done.

Isaksson commented 10 months ago

0.3.7 solves the log issue.

sascha108 commented 10 months ago

I test over night with following setup: every 21 minutes a call of "ClientDevs", "Health", "Alarms", "SiteStates" with a intermediate delay of 5 seconds. Exactly every second call, all 4 nodes fails with error. I test now with 34 and 41 minutes. Confusing.

Isaksson commented 10 months ago

One of the latest changes is the login logic, the node will keep the login session alive, this change was due to a change in the latest version of Unifi that blocks all the calls if you try to login multiple times during a time intervall. I do not know the number of logins or the time frame but it was easy to reach the limit.

It's only possible to keep the login session in the same node, so if you have 5 nodes of Unifi then there will be 5 login sessions.

So i recommend to use as few nodes as possible and create the calls dynamic.

I don't think that is the issue right now but if it possible, try to reduce the number of nodes and see if that makes any difference.

sascha108 commented 10 months ago

At the test over last night i see in the controller log 4 logins if the nodes runs without error and no entry if the nodes fails.

sascha108 commented 10 months ago

The test with the longer time between the calls (34 and 41 minutes) has the same result as before - every second call fails. It looks like the node is logged out and then the next run fails. In the next execution, the node logs in again and the retrieval works. The error did not occur until the last version in 0.2.x - i've been using the functions for a number of years.

Isaksson commented 10 months ago

Ahh okey, that is kind of expected, eventuell the calls will fail because the login token has expired, but for this I have build in logic that will make a new login if there is one call that in unauthorized, I have tired to simulate this and then it works every time. But just to make sure, if you send one command and then wait 34 minutes and makes another call to the same node then it will fail?

sascha108 commented 10 months ago

But just to make sure, if you send one command and then wait 34 minutes and makes another call to the same node then it will fail? Yes, it is.

Isaksson commented 10 months ago

I just tested like you see on the images, waited 40 minutes between the calls to the same node, and it worked, you do it some other way? image image

sascha108 commented 10 months ago

I use the same setup. Which controller version do you use?

Isaksson commented 10 months ago

This test was on network controller 7.4.162 and Unifi OS v3.1.14

sascha108 commented 10 months ago

ok, i'm useing 7.3.83 because there are problems with v7.4.x on the 32-Bit RasPi-OS. If there is no solution, then I will probably have to use the last 0.2.x from a backup again.

Isaksson commented 10 months ago

But I guess you could build some logic in Node-RED with a function node and if you receive ERR_BAD_REQUEST then output a signal back that will initiate the flow again? then this second call will work?

I guess there must be some other error code for that version when you try to make an request when not logged in, it should be Unauthorized but I guess its something else in your case.

What I could to is to add some kind of debug to the node that will print out the error codes and then you could send that code to me and maybe I could add that as well in the logic that the node needs to login again.

Isaksson commented 10 months ago

Or, if you have some experience in firewall and ports then you could open up a connection from my development environment to your Unifi Controller. Then I could debug this in real time.

Then you have to let me have an account to your Controller and open ports from my IP Address.

sascha108 commented 10 months ago

I don't want to give them direct access. I took a quick look at the unifi_helper.js of v0.2.14 and v0.3.7. In 0.2.14 there was still the option to log out, which is now commented out in 0.3.7. In the contoller log I can also see that with 0.2.14 the node logged in 2 times per minute because I query the devices every 30 seconds. This problem obviously did not arise in the previous version.

Nokomis449 commented 10 months ago

Although I understand your reluctance, "them" is only Isaksson who is the sole developer of this tool (it's not provided nor sanctioned by Ubiquiti). I have given him access to my controller several times in the past so he could test devices he doesn't have, and will happily do it again. He gave me his home IP address and I set up a firewall rule that only allowed that IP address to be directed to my controller, and I set up a user for him to use. He let me know when he was ready to test, and I turned on the port re-direct and user. When he was done, I turned them back off. Again I understand your reluctance, but just wanted you to know it wouldn't be the first time someone gave him access as requested. Since you're the only one requesting this change, it may be the only way for him to help you short of mailing him an RGB device to test with.

Isaksson commented 10 months ago

Nokomis are absolutely right, I have connected to his environment to solve issues that you guys have. That's is of course the fastest and simples solution, especially when I could not reproduce the error in my environment.

In the older version the Node logged out after every single call. So the Node produced a lot of logins/logouts. In later version of Unifi OS they blocks all the communication if there is to many logins during a timeframe. That's the reason I had to change the login/logout procedure.

Isaksson commented 10 months ago

In version 0.3.8 there is a new option named debug that you could enable on the node that does not work. enable debug send command (ignore if there is debug info in your log file) wait 34 minutes and make the call send the output in your log file to me

sascha108 commented 10 months ago

ok, there is the debug output if node fails ... unifi_debug.log

Isaksson commented 10 months ago

Please try with version 0.3.9 and verify if it solves your issue

sascha108 commented 10 months ago

I tested - and wath i say - it works proberly The debug output is the same as before, but the relogin works as aspected so that the node output allway correct. Grate work, thanks!

Isaksson commented 10 months ago

That's nice. Yeah, we have to try the command and if it failed because you need to login again then the Node should do it automatically.