bartbutenaers / node-red-contrib-onvif-nodes

Node Red nodes for communicating with OnVif compliant IP devices
Apache License 2.0
65 stars 25 forks source link

Config node does not connect if camera not accessible on boot, but becomes available #9

Closed colinl closed 1 year ago

colinl commented 4 years ago

To replicate disconnect the network connection to the camera (or power camera down) and re-deploy. The Onvif nodes show Disconnected as they should. Now reconnect the camera. The onvif nodes do not connect.

This is a particular nuisance on a PI 4 that I have. On reboot node-red starts up before the network is up and running so the onvif nodes do not connect. I have to re-deploy to get them to connect. A workaround is to get the node-red service to wait for the network before starting up, just need to work out how to do that.

bartbutenaers commented 4 years ago

Hey Colin (@colinl),

In the Device section on the readme page, there is an example flow. The last inject node in that flow allows to send a "reconnect" option. Could that perhaps be used to solve your problem?

Otherwise we could perhaps add an option to the Config node to reconnect automatically (with x time in between). But it seems to me that the first option is much more powerfull, because you can add some logic in your flow when you want to reconnect.

Please let me know if this is sufficient for you! Bart

colinl commented 4 years ago

Thanks, I had missed that. Of course it isn't a permanent connection like MQTT for example which is continually monitored. Unfortunately the reconnect doesn't work for me. The node passes on

msg : Object
object
xaddr: "camera.local"
action: "reconnect"
payload: ""
_msgid: "469543a.7391dbc"

but doesn't reconnect. There is nothing in the log. A full deploy does connect.

lorenzoconforti commented 2 years ago

Hi, is there any update on this? I'm having the same issue and the reconnect action doesn't work. From the node-red-log I get "Action reconnect is not supported".

If I restart the flows everything works again

bartbutenaers commented 2 years ago

@lorenzoconforti ,

Unfortunately I haven't been developing this node any further due to a lack of free time... To which node are you sending the "reconnect" action? Because the Device node should be able to recognize it:

image

lorenzoconforti commented 2 years ago

To which node are you sending the "reconnect" action? The events node

bartbutenaers commented 2 years ago

@lorenzoconforti, I have quickly added the "reconnect" action to the Event node. Could you please test this and give me feedback. Due to a technical issue with my private event stream, unfortunately I was not able to test this new code snippet myself...

lorenzoconforti commented 2 years ago

Hi, thank you for your reply. I've installed the new version (using the command "npm install bartbutenaers/node-red-contrib-onvif-nodes"); I've tried again but it does not work. The device is always in the "Disconnected" state

The events node does not give me an error in the logs anymore but still, it doesn't work. Could it be an issue with my camera (Dahua)?

As a workaround, I'm checking the status of the node and restart Node Red if I get any disconnect

bartbutenaers commented 2 years ago

@lorenzoconforti,

The problem with Onvif is that each brand has its own implementation. And I'm by far a camera expert, so don't know anything about Dahua. But it is a known brand, so I assume they have a decent implementation.

Secondly I use an Onvif library, so I cannot extra logging or whatever. So we will need to use some trial and error, and hopefully we can find what is causing your issue... But perhaps the problem is inside my node: e.g. it could be that the connection

Would be nice if I could add some own extra logging with your help, so we can avoid that other people have to go through the same troubles as you ...

Some basic questions to understand your setup.

  1. Do your camera's respond to the Broadcast node?
  2. I assume that you have entered all the info of your camera in the onvif config node screen (ip addres, ...)
  3. Do you have setup authentication on your camera's and entered your credentials in the onvif config node?
  4. Do you have already seen another status (e.g. connected) in another onvif node, e.g. the device node? Because the Event node was added recently, so perhaps it is less stable ...
  5. The device node (see here offers a "getSystemDateAndTime" action. I don't know for sure whether you need to be connected to use that... Could you check whether that action works? Because perhaps the nodes are connected, but I don't show the status correctly. Keep in mind that my Onvif nodes are experimental, which is why I haven't published them on NPM ...
  6. Other info that can help me?

At startup my Onvif nodes connect automatically to the camera, which fails in your case I assume. Perhaps I should add an "auto connect" checkbox in the config node screen, so you can deactivate that behaviour. Not sure if that would help ...

lorenzoconforti commented 2 years ago

Hi,

  1. Do you mean the discovery node? If that's the case, yes, both my cameras respond to that node and return the correct information
  2. Yes, and the other nodes/actions work without problems
  3. Yes, all working correctly
  4. I see "connected" in the other nodes I'm using (discovery, event and device)
  5. "getSystemDateAndTime" works fine when the node (in my case the device node) shows connected. When not connected it does not work
  6. The issue is on startup; the cameras take long to boot and the nodes timeout. My raspberry pi is under the same power source as the cameras; when I boot it up, the nodes are loaded but can't connect to the cameras (as they are still booting up); the nodes time out and show as disconnected

Thank you

bartbutenaers commented 2 years ago

Morning,

  1. I see "connected" in the other nodes I'm using (discovery, event and device)

I'm a bit confused now. When you say "other" nodes, which node does not show "connected" then? Or do all nodes show "connected" but they are not really connected? Can you please clarify this a bit in more detail please. A sequence would help me to understand your setup, for example:

  1. The issue is on startup; the cameras take long to boot and the nodes timeout.

So it might be useful that I add an "autoconnect" checkbox on the config screen, which you can deactivate (since you know in front that you will get a timeout). Or not?

Now off to work ;-(

lorenzoconforti commented 2 years ago

Hi, apologies for the confusion.

Sequence of events:

Will the autoconnect feature keep trying to connect to the cameras until they are "connected"? If that's what you have in mind, that would be perfect

Thank you

colinl commented 2 years ago

Is there a good reason why one would not want it to automatically attempt reconnection? For example the MQTT nodes don't have such an option, it is assumed that reconnection when possible is desirable.

bartbutenaers commented 2 years ago

Thanks for the extra clarification!!

So you cannot connect by sending a "reconnect" action, but you can connect by restarting Node-RED. Will "try" to see how the Onvif library differs in both scenarios...

About the autostart:

  1. Currently the Onvif library seems to autostart and quit after some timeout time.
  2. I could try to add my own automatic reconnect (at regular adjustable intervals). However I had added at the time being the "reconnect" action, to allow the users to implement this at their own (e.g. via an Inject node). But I can understand that this makes flows more complex as necessary.

@colin: should there be an adjustable reconnect interval?

colinl commented 2 years ago

About the reconnect interval, I suppose the question is why would anyone want to adjust it? Thinking of MQTT again one can't change the interval (at least not in node-red, I don't know whether the underlying library allows that).

bartbutenaers commented 2 years ago

question is why would anyone want to adjust it?

Well because when I hardcode the interval length, then I have to make up a value that fits every use case. Not sure if there is one single best value...

After node red restarts all nodes are connected

That is weird. I'm not at my computer, but could it be that - after sending a "reconnect" - the node is "connected" but shows "disconnected"? I mean can you get info from my node then (e.g. system date time)? Because in both cases the same 'connect' function of the Onvif library is being called: both at reconnect and restart (as I can see in the Onvif library). So it should react the same way in both scenarios...

Whether that causes your problem, I'm not sure. But I need to fix my "reconnect" callback handler anyway, to update the node status...

colinl commented 2 years ago

What use cases would require different reconnect intervals?

bartbutenaers commented 2 years ago

Not sure. Will tell you as soon as someone has asked me to make it configurable ;-)

Have you seen my question above? Could it be that the node incorrectly displays "unconnected", while it is in fact "connected" after the "reconnect" action? In that case it would respond e.g. to a getSystemDateTime action?

colinl commented 2 years ago

I presume the second part of the comment is addressed to @lorenzoconforti

lorenzoconforti commented 2 years ago

I've restarted the system today and the cameras are both showing "disconnected"

image

I've tried all the actions you see in the screenshot and none is responding (as you can see I have a debug node there); the only one that outputs something to the debug node is the reconnect action. This is what is giving in the debug window:

image

Even though it seems it is doing something, the cameras are still reported as disconnected and all the other action keep giving no result/response.

If I restart the flows, the cameras now show as connected:

image

The actions are now working as expected; see below the output in the debug window of the system datetime action:

image

bartbutenaers commented 2 years ago

Damn that is not what I expected/hoped. Will try to find some time next weekend to experiment with disconnecting one of my own camera's...

bartbutenaers commented 2 years ago

Hi guys (@colinl & @lorenzoconforti),

Sorry for the delay, but too much things to do in my limited free time.

I have been experimenting with one of my Onvif camera's, and I have pushed the following changes to this Github repository:

  1. When I send a "reconnect" action, the node status is now updated (to "connected" or "disconnected").

  2. In the config node it is now possible to adjust the timeout interval (i.e. the number of seconds that we wait for a response when trying to connect to the camera):

    image

  3. In the config node it is now also possible to set a connection check interval (i.e. the number of seconds between periodical checks of the connection):

    image

  4. When the connection check (see previous point) detects that a camera becomes connected, this node will now load all the Onvif configuration data from the camera. So hopefully now you don't need to restart Node-RED anymore to achieve the same ....

TODO: I will update the documenation on the readme file afterwards. And I should also add a validation to the config node screen: error if the timeout interval is greater than the connection check interval.

lorenzoconforti commented 2 years ago

@bartbutenaers thank you for the update. The system I've set up is now live (i.e. I want to avoid any risk of breaking it); do you think these changes you've implemented are stable enough?

bartbutenaers commented 2 years ago

@lorenzoconforti, I have done quite some tests, and for me it seemed to be working correctly. But your camera's might respond entirely different, since each Onvif implementation differs. There is no way I can guarantee that it won't be broken. Moreover since you have reported this issue, you are the only one that can confirm whether this fix solves your problems. Since this is open-source, I always keep hoping that people are prepared at least to do some tests after I have spend my free time to build a fix for their problems ...

colinl commented 2 years ago

Hi Bart, I can't test at the moment as I am away from home. Will give it a go when I get back.

lorenzoconforti commented 1 year ago

@bartbutenaers, apologies for the long delay. I've just finished updating my implementation and finally properly tested the new version and confirm it works as expected. After losing the connection to the camera, it is then reestablished and everything keeps working fine.

Again, apologies for the delay but I have only been able to update my system now.

Thank you very much for your support

colinl commented 1 year ago

I am sorry to say that it seems I did not feedback the results after upgrading. It is working perfectly. Thanks.

bartbutenaers commented 1 year ago

Hi guys, No problem at all! Thanks for confirming