rroller / dahua

Dahua Camera and Doorbell Home Assistant Integration
MIT License
382 stars 73 forks source link

"Short term fix" in init.py is wrong - channel is not the same as index #310

Open pmichael opened 10 months ago

pmichael commented 10 months ago

In init.py you have a "Short term fix" on line 455 that filters out events if index not equal to channel. It's wrong assumption as index is not always equal to channel and causes alot of totally valid events, in the correct channel, to be filtered out.

stalniy commented 7 months ago

Why is it wrong? In that case channel is actually an index

Just check description on UI: https://github.com/rroller/dahua/blob/main/custom_components/dahua/translations/en.json#L15

it says “0 based index”

pmichael commented 7 months ago

Because if you debug it, you see that it's not the same order. events array and channels array are not related to each other and have different order.

I have several cameras connected with NVR. On runtime you can see for example that camera on channel #3 is not posting events on index #3. I was not getting any events from the camera, so had to comment that if - "if index != self._channel:" in order to get all the events.

stalniy commented 7 months ago

The thing is that it works as expected. I also have few cameras. In my setup there are 6 of them.

And I’m able to correctly receive IVS events and motion events.

I haven’t looked deep into the code yet but I fixed 2 important bugs in the code:

I think that you might have timeout errors on some cameras and lost events during testing (this is what happened to me, sometimes it worked sometimes not)

But at least from practical point of view that code with channel check works OK for me.

I connect all cameras through NVR channels

pmichael commented 7 months ago

Take this event for example: homeassistant | 2024-01-25T16:23:32.606165863Z 2024-01-25 18:23:32.604 DEBUG (MainThread) [custom_components.dahua] Events received from 192.168.1.98 on channel 3: [{'Code': 'CrossLineDetection', 'action': 'Stop', 'index': '2'}]

Camera on channel #3 fired this event. As you can see, the index is 2. This check makes it skip this event.

Do you use those events and it works for you?

stalniy commented 7 months ago

For me it works correctly. Somewhere in the code I saw that old firmware starts channels from 1 not from 0

That code is executed for every thread, in other words for every camera, comments says that response is processed by every channel. I can confirm it, one event is published and checked with every channel. That’s why you need a filter there. Otherwise all cameras will notify events from each other.