Filter events on state True and add listening address

This commit is contained in:
Steven B 2024-12-19 15:44:43 +00:00
parent d201e59096
commit f4857ef76d
No known key found for this signature in database
GPG Key ID: 6D5B46B3679F2A43

View File

@ -41,6 +41,16 @@ class Listen(SmartCamModule):
site: web.TCPSite site: web.TCPSite
runner: web.AppRunner runner: web.AppRunner
instance_id: str instance_id: str
path: str
_listening_address: str | None = None
@property
def listening_address(self) -> str | None:
"""Address the listener is receiving onvif notifications on.
Or None if not listening.
"""
return self._listening_address
async def _invoke_callback(self, event: EventType) -> None: async def _invoke_callback(self, event: EventType) -> None:
self.callback(event) self.callback(event)
@ -49,8 +59,15 @@ class Listen(SmartCamModule):
content = await request.read() content = await request.read()
result = self.manager.process(content) result = self.manager.process(content)
for msg in result.NotificationMessage: for msg in result.NotificationMessage:
_LOGGER.debug(
"Received notification message for %s: %s",
self._device.host,
msg,
)
if (event := TOPIC_EVENT_TYPE.get(msg.Topic._value_1)) and ( if (event := TOPIC_EVENT_TYPE.get(msg.Topic._value_1)) and (
not self.event_types or event in self.event_types (not self.event_types or event in self.event_types)
and (simple_items := msg.Message._value_1.Data.SimpleItem)
and simple_items[0].Value == "true"
): ):
asyncio.create_task(self._invoke_callback(event)) asyncio.create_task(self._invoke_callback(event))
return web.Response() return web.Response()
@ -68,6 +85,7 @@ class Listen(SmartCamModule):
self.callback = callback self.callback = callback
self.event_types = event_types self.event_types = event_types
self.instance_id = str(uuid.uuid4()) self.instance_id = str(uuid.uuid4())
self.path = f"/{self._device.host}/{self.instance_id}/"
if listen_port is None: if listen_port is None:
listen_port = DEFAULT_LISTEN_PORT listen_port = DEFAULT_LISTEN_PORT
@ -87,14 +105,15 @@ class Listen(SmartCamModule):
) )
await mycam.update_xaddrs() await mycam.update_xaddrs()
address = await self._start_server(listen_ip, listen_port) host_port = await self._start_server(listen_ip, listen_port)
self.manager = await mycam.create_notification_manager( self.manager = await mycam.create_notification_manager(
address=address, address=host_port + self.path,
interval=timedelta(minutes=10), interval=timedelta(minutes=10),
subscription_lost_callback=subscription_lost, subscription_lost_callback=subscription_lost,
) )
self._listening_address = host_port
self.listening = True self.listening = True
_LOGGER.debug("Listener started for %s", self._device.host) _LOGGER.debug("Listener started for %s", self._device.host)
@ -106,6 +125,7 @@ class Listen(SmartCamModule):
_LOGGER.debug("Stopping listener for %s", self._device.host) _LOGGER.debug("Stopping listener for %s", self._device.host)
self.listening = False self.listening = False
self._listening_address = None
await self.site.stop() await self.site.stop()
await self.runner.shutdown() await self.runner.shutdown()
@ -127,9 +147,7 @@ class Listen(SmartCamModule):
async def _start_server(self, listen_ip: str | None, listen_port: int) -> str: async def _start_server(self, listen_ip: str | None, listen_port: int) -> str:
app = web.Application() app = web.Application()
app.add_routes( app.add_routes([web.post(self.path, self._handle_event)])
[web.post(f"/{self._device.host}/{self.instance_id}/", self._handle_event)]
)
self.runner = web.AppRunner(app) self.runner = web.AppRunner(app)
await self.runner.setup() await self.runner.setup()
@ -159,6 +177,4 @@ class Listen(SmartCamModule):
listen_port, listen_port,
) )
return ( return f"http://{listen_ip}:{listen_port}"
f"http://{listen_ip}:{listen_port}/{self._device.host}/{self.instance_id}/"
)