Kane610 / axis

Python library enabling easy communication with Axis devices and its metadatastream
MIT License
28 stars 11 forks source link

error handling event type with no MessageInstance #329

Closed jonoberheide closed 5 months ago

jonoberheide commented 7 months ago

In trying to debug an Axis HA issue (https://github.com/home-assistant/core/issues/109345), I used this library to query the A9188 device in question.

I get an exception when processing the various event types:


Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/jono/axis/axis/./__main__.py", line 114, in <module>
    asyncio.run(
  File "/usr/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/jono/axis/axis/./__main__.py", line 59, in main
    device = await axis_device(host, port, username, password)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jono/axis/axis/./__main__.py", line 35, in axis_device
    await device.vapix.initialize_event_instances()
  File "/home/jono/axis/axis/vapix/vapix.py", line 216, in initialize_event_instances
    await self.event_instances.do_update(skip_support_check=True)
  File "/home/jono/axis/axis/vapix/interfaces/api_handler.py", line 89, in do_update
    await self.update()
  File "/home/jono/axis/axis/vapix/interfaces/api_handler.py", line 113, in update
    self._items = await self._api_request()
                  ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jono/axis/axis/vapix/interfaces/event_instances.py", line 24, in _api_request
    return await self.get_event_instances()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jono/axis/axis/vapix/interfaces/event_instances.py", line 29, in get_event_instances
    response = ListEventInstancesResponse.decode(bytes_data)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jono/axis/axis/vapix/models/event_instance.py", line 190, in decode
    return cls(data=EventInstance.decode_from_list(events))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jono/axis/axis/vapix/models/event_instance.py", line 140, in decode_from_list
    events = [cls.decode(v) for v in data]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jono/axis/axis/vapix/models/event_instance.py", line 140, in <listcomp>
    events = [cls.decode(v) for v in data]
              ^^^^^^^^^^^^^
  File "/home/jono/axis/axis/vapix/models/event_instance.py", line 129, in decode
    stateful=data["data"]["MessageInstance"].get("@isProperty", None) == "true",
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'get'

It appears there is an event type returned that does not have a MessageInstance body:

 {'data': {'@topic': 'true', 'MessageInstance': None},
  'topic': 'tns1:EventBuffer/Begin'},

I'm not sure if this event type should just be skipped, or if some different parsing logic is required.

Kane610 commented 7 months ago

Thanks for reporting, would you mind enable debug for the library and share the raw data being parsed?

The way to mitigate the issue depends on what the raw data shows :)

jonoberheide commented 7 months ago

Here's the log file: log.txt

I'm able to skip that EventBuffer/Begin topic by tweaking some logic in decode()/decode_from_list() and then the event stream appears to start. However, I don't seem to get receive relay events from the A9188...which may be an independent issue.

Kane610 commented 7 months ago

What access rights does the user have? For port control it needs to be admin, not sure if that affects the event stream itself though

jonoberheide commented 7 months ago

Yep, it's the admin user.

Kane610 commented 7 months ago

Here's the log file: log.txt

I'm able to skip that EventBuffer/Begin topic by tweaking some logic in decode()/decode_from_list() and then the event stream appears to start. However, I don't seem to get receive relay events from the A9188...which may be an independent issue.

Main file does a few things extra and it seems to be broken, ignore the await device.vapix.initialize_event_instances() in the main.py and it should proceed

jonoberheide commented 7 months ago

Strangely, I can see the events coming from the device using the gstreamer rstpsrc client, but not the axis one. I'm not quite sure yet where the protocol sequence is differing though...

Kane610 commented 7 months ago

Strangely, I can see the events coming from the device using the gstreamer rstpsrc client, but not the axis one. I'm not quite sure yet where the protocol sequence is differing though...

I did skip out and didn't implement the rtcp channel which has worked great for a long time and I think I even tried it on a 9188 once.

I appreciate you are looking at it!

Kane610 commented 5 months ago

Here's the log file: log.txt

I'm able to skip that EventBuffer/Begin topic by tweaking some logic in decode()/decode_from_list() and then the event stream appears to start. However, I don't seem to get receive relay events from the A9188...which may be an independent issue.

Sorry didn't get to this until now, would you mind share the debug log output when you get that event over the rtsp stream

jonoberheide commented 5 months ago

I ended up going a different route for my use case, so I no longer need to consume the events from the A9188, so I'm ok to close this if it makes sense for you.

Also, maybe a separate issue (but again, no longer relevant), when I just tried re-adding my A9188 to the latest version of HA, it successfully set up the device, but did not create any entities for it, which I'd swear there were relay/IO switch entities set up in a past version (as I referenced in https://github.com/home-assistant/core/issues/109345 )

Kane610 commented 5 months ago

I ended up going a different route for my use case, so I no longer need to consume the events from the A9188, so I'm ok to close this if it makes sense for you.

No-one else has reported that issue so I guess if you're not concerned with it we can close it, even though I would like to see complete debug logs so I could solve it proactively :)

Also, maybe a separate issue (but again, no longer relevant), when I just tried re-adding my A9188 to the latest version of HA, it successfully set up the device, but did not create any entities for it, which I'd swear there were relay/IO switch entities set up in a past version (as I referenced in home-assistant/core#109345 )

Yes please create a new issue and we can look at it

jonoberheide commented 5 months ago

Ok, I suppose I will keep the issue open and we can revisit once I'm able to reproduce the issue.

I'll file a new issue...assuming the HA project is the right place for that as opposed to here.

jonoberheide commented 5 months ago

I would say this is safe to close now, given that the RTSP event streaming is working A-OK for the 9188 through HA.

Kane610 commented 5 months ago

Allright! Thanks for the swift actions!