tropicoo / hikvision-camera-bot

Hikvision Telegram Camera Bot 🇺🇦
MIT License
105 stars 24 forks source link

Alert stream endless loop #50

Open k3it opened 2 years ago

k3it commented 2 years ago

Доброго вечора, Тарас!

I have a doorbell hikvision camera and the bot is having some problem with initializing the alert stream. Is there something I can look at to fix this:

2022-09-14 17:12:24,345 - [INFO] - [ServiceAlarmMonitoringTask:27] - Starting alert pusher task for camera: "doorbell" 2022-09-14 17:12:24,347 - [DEBUG] - [asyncio:808] - <_SelectorSocketTransport fd=12 read=polling write=<idle, bufsize=0>> resumes reading 2022-09-14 17:12:24,872 - [DEBUG] - [asyncio:800] - <_SelectorSocketTransport fd=12 read=idle write=<idle, bufsize=0>> pauses reading 2022-09-14 17:12:24,872 - [DEBUG] - [httpx._client:1706] - HTTP Request: GET http://192.168.123.80/ISAPI/Event/notification/alertStream "HTTP/1.1 200 OK" 2022-09-14 17:12:24,873 - [ERROR] - [ServiceAlarmMonitoringTask:37] - Unexpectedly exited from stream chunk processing loop for cam_1. Retrying in 0.5 seconds... 2022-09-14 17:12:25,374 - [INFO] - [ServiceAlarmMonitoringTask:27] - Starting alert pusher task for camera: "doorbell"

the camera is: HNDB302-W Firmware V1.4.62 build 180809

The rest of the functions work quite welll.

дякую за гарний софт!

tropicoo commented 2 years ago

Доброго дня!

Unfortunately, I don't have a Hikvision doorbell, and not sure whether it's possible to investigate what's going on with that alert stream. That error means we connect to the alert stream on the doorbell and just exit. This is not the case on Hikvision cameras because we wait and read the data from the stream and exiting is unexpected behavior.

-Taras

k3it commented 2 years ago

Taras

Apparently the doorbell's http response headers do not have the flag to indicate to the client that the connection is a stream. I suspect it's a firmware bug, however i was not able to find a newer fw.

I was able to connect to the alert stream url with a basic telnet test and receive the continuous heartbeat message (once per second) as follows:


$ telnet 192.168.123.80 80
Trying 192.168.123.80...
Connected to 192.168.123.80.
Escape character is '^]'.
GET /ISAPI/Event/notification/alertStream HTTP/1.1
Authorization: Basic <XXXXbase64encoded credential==>

HTTP/1.1 200 OK
Connection: keep-alive
Keep-Alive: timeout=60, max=5
Pragma: no-cache
Cache-Control: no-cache
Content-Type: application/xml; charset="UTF-8"
Content-Length: 532

<EventNotificationAlert version="1.0" xmlns="urn:psialliance-org"><ipAddress>192.168.123.80</ipAddress><portNo>80</portNo><protocol>HTTP</protocol><macAddress>18:68:cb:f7:d3:ba</macAddress><dateTime>2022-09-29T11:49:06-05:00</dateTime><activePostCount>1</activePostCount><eventType>other</eventType><eventDescription>Heartbeat</eventDescription><eventState>inactive</eventState><Extensions version="1.0" xmlns="urn:psialliance-org"><serialNumber>HNDB302-W0120180319WR202749083CLU</serialNumber></Extensions></EventNotificationAlert>HTTP/1.1 200 OK
Connection: keep-alive
Keep-Alive: timeout=60, max=5
Pragma: no-cache
Cache-Control: no-cache
Content-Type: application/xml; charset="UTF-8"
Content-Length: 532

<EventNotificationAlert version="1.0" xmlns="urn:psialliance-org"><ipAddress>192.168.123.80</ipAddress><portNo>80</portNo><protocol>HTTP</protocol><macAddress>18:68:cb:f7:d3:ba</macAddress><dateTime>2022-09-29T11:49:07-05:00</dateTime><activePostCount>1</activePostCount><eventType>other</eventType><eventDescription>Heartbeat</eventDescription><eventState>inactive</eventState><Extensions version="1.0" xmlns="urn:psialliance-org"><serialNumber>HNDB302-W0120180319WR202749083CLU</serialNumber></Extensions></EventNotificationAlert>HTTP/1.1 200 OK
Connection: keep-alive
Keep-Alive: timeout=60, max=5
Pragma: no-cache
Cache-Control: no-cache
Content-Type: application/xml; charset="UTF-8"

... and so on

The python requests library, curl, and other http clients seem to terminate the connection after receiving the first chunk of data. I'm curious if there is a way to force the socket connection to remain open despite the missing http stream header?

tropicoo commented 2 years ago

Let's try to log what we receive in the bot here in the file. Maybe we will see something interesting.https://github.com/tropicoo/hikvision-camera-bot/blob/6b873ac6cf9eeabe0f222cfb36466d279b883d91/hikcamerabot/clients/hikvision/endpoints/endpoints.py#L145

            async for chunk in response.aiter_text():
                self._log.debug('\n----- CHUNK START -----\n\n%s\n\n----- CHUNK END -----', chunk)
                self._log.debug('Response Headers: %s', response.headers)
                self._log.debug('Request Headers: %s', response.request.headers)
                self._log.debug('Length of the chunk: %d', len(chunk))
                yield chunk
k3it commented 2 years ago

here is the log. I think the camera is not sending Transfer-Encoding: chunked and httpx disconnects because it does not realize that this is a chunked stream. Do other hikvision cameras have this header?

hikvision_camera_bot    | 2022-09-30 20:04:08,317 - [DEBUG] - [httpx._client:1706] - HTTP Request: GET http://192.168.123.80/ISAPI/Event/notification/alertStream "HTTP/1.1 200 OK"
hikvision_camera_bot    | 2022-09-30 20:04:08,317 - [DEBUG] - [AlertStreamEndpoint:146] -
hikvision_camera_bot    | ----- CHUNK START -----
hikvision_camera_bot    |
hikvision_camera_bot    | <EventNotificationAlert version="1.0" xmlns="urn:psialliance-org"><ipAddress>192.168.123.80</ipAddress><portNo>80</portNo><protocol>HTTP</protocol><macAddress>18:68:cb:f7:d3:ba</macAddress><dateTime>2022-09-30T13:04:07-05:00</dateTime><activePostCount>1</activePostCount><eventType>other</eventType><eventDescription>Heartbeat</eventDescription><eventState>inactive</eventState><Extensions version="1.0" xmlns="urn:psialliance-org"><serialNumber>HNDB302-W0120180319WR202749083CLU</serialNumber></Extensions></EventNotificationAlert>
hikvision_camera_bot    |
hikvision_camera_bot    | ----- CHUNK END -----
hikvision_camera_bot    | 2022-09-30 20:04:08,317 - [DEBUG] - [AlertStreamEndpoint:147] - Response Headers: Headers({'connection': 'keep-alive', 'keep-alive': 'timeout=60, max=5', 'pragma': 'no-cache', 'cache-control': 'no-cache', 'content-type': 'application/xml; charset="UTF-8"', 'content-length': '532'})
hikvision_camera_bot    | 2022-09-30 20:04:08,317 - [DEBUG] - [AlertStreamEndpoint:148] - Request Headers: Headers({'host': '192.168.123.80', 'accept': '*/*', 'accept-encoding': 'gzip, deflate', 'connection': 'keep-alive', 'user-agent': 'python-httpx/0.22.0', 'authorization': '[secure]'})
hikvision_camera_bot    | 2022-09-30 20:04:08,317 - [DEBUG] - [AlertStreamEndpoint:149] - Length of the chunk: 532
hikvision_camera_bot    | 2022-09-30 20:04:08,319 - [DEBUG] - [AlertStreamEndpoint:146] -
hikvision_camera_bot    | ----- CHUNK START -----
hikvision_camera_bot    |
hikvision_camera_bot    |
hikvision_camera_bot    |
hikvision_camera_bot    | ----- CHUNK END -----
hikvision_camera_bot    | 2022-09-30 20:04:08,319 - [DEBUG] - [AlertStreamEndpoint:147] - Response Headers: Headers({'connection': 'keep-alive', 'keep-alive': 'timeout=60, max=5', 'pragma': 'no-cache', 'cache-control': 'no-cache', 'content-type': 'application/xml; charset="UTF-8"', 'content-length': '532'})
hikvision_camera_bot    | 2022-09-30 20:04:08,319 - [DEBUG] - [AlertStreamEndpoint:148] - Request Headers: Headers({'host': '192.168.123.80', 'accept': '*/*', 'accept-encoding': 'gzip, deflate', 'connection': 'keep-alive', 'user-agent': 'python-httpx/0.22.0', 'authorization': '[secure]'})
hikvision_camera_bot    | 2022-09-30 20:04:08,319 - [DEBUG] - [AlertStreamEndpoint:149] - Length of the chunk: 0
hikvision_camera_bot    | 2022-09-30 20:04:08,319 - [DEBUG] - [AlertStreamEndpoint:146] -
hikvision_camera_bot    | ----- CHUNK START -----
hikvision_camera_bot    |
hikvision_camera_bot    |
hikvision_camera_bot    |
hikvision_camera_bot    | ----- CHUNK END -----
hikvision_camera_bot    | 2022-09-30 20:04:08,319 - [DEBUG] - [AlertStreamEndpoint:147] - Response Headers: Headers({'connection': 'keep-alive', 'keep-alive': 'timeout=60, max=5', 'pragma': 'no-cache', 'cache-control': 'no-cache', 'content-type': 'application/xml; charset="UTF-8"', 'content-length': '532'})
hikvision_camera_bot    | 2022-09-30 20:04:08,319 - [DEBUG] - [AlertStreamEndpoint:148] - Request Headers: Headers({'host': '192.168.123.80', 'accept': '*/*', 'accept-encoding': 'gzip, deflate', 'connection': 'keep-alive', 'user-agent': 'python-httpx/0.22.0', 'authorization': '[secure]'})
hikvision_camera_bot    | 2022-09-30 20:04:08,319 - [DEBUG] - [AlertStreamEndpoint:149] - Length of the chunk: 0
hikvision_camera_bot    | 2022-09-30 20:04:08,320 - [ERROR] - [ServiceAlarmMonitoringTask:37] - Unexpectedly exited from stream chunk processing loop for cam_1. Retrying in 0.5 seconds...
hikvision_camera_bot    | 2022-09-30 20:04:08,820 - [INFO] - [ServiceAlarmMonitoringTask:27] - Starting alert pusher task for camera: "doorbell"
hikvision_camera_bot    | 2022-09-30 20:04:08,823 - [DEBUG] - [asyncio:808] - <_SelectorSocketTransport fd=12 read=polling write=<idle, bufsize=0>> resumes reading
hikvision_camera_bot    | 2022-09-30 20:04:09,335 - [DEBUG] - [asyncio:800] - <_SelectorSocketTransport fd=12 read=idle write=<idle, bufsize=0>> pauses reading
hikvision_camera_bot    | 2022-09-30 20:04:09,336 - [DEBUG] - [httpx._client:1706] - HTTP Request: GET http://192.168.123.80/ISAPI/Event/notification/alertStream "HTTP/1.1 200 OK"
hikvision_camera_bot    | 2022-09-30 20:04:09,336 - [DEBUG] - [AlertStreamEndpoint:146] -
hikvision_camera_bot    | ----- CHUNK START -----
hikvision_camera_bot    |
hikvision_camera_bot    | <EventNotificationAlert version="1.0" xmlns="urn:psialliance-org"><ipAddress>192.168.123.80</ipAddress><portNo>80</portNo><protocol>HTTP</protocol><macAddress>18:68:cb:f7:d3:ba</macAddress><dateTime>2022-09-30T13:04:08-05:00</dateTime><activePostCount>1</activePostCount><eventType>other</eventType><eventDescription>Heartbeat</eventDescription><eventState>inactive</eventState><Extensions version="1.0" xmlns="urn:psialliance-org"><serialNumber>HNDB302-W0120180319WR202749083CLU</serialNumber></Extensions></EventNotificationAlert>
best-tech commented 2 years ago

i have Hikvision DS-2CD2523G0-IS V5.6.6 build 210827

hikvision_camera_bot  | 2022-10-07 14:39:53,372 - [DEBUG] - [asyncio:800] - <_SelectorSocketTransport fd=12 read=idle write=<idle, bufsize=0>> pauses reading
hikvision_camera_bot  | 2022-10-07 14:39:53,373 - [DEBUG] - [httpx._client:1728] - HTTP Request: GET http://192.168.1.1/ISAPI/Event/notification/alertStream "HTTP/1.1 403 Forbidden"
hikvision_camera_bot  | 2022-10-07 14:39:53,377 - [ERROR] - [ServiceAlarmMonitoringTask:37] - Unexpectedly exited from stream chunk processing loop for cam_1. Retrying in 0.5 seconds...
hikvision_camera_bot  | 2022-10-07 14:39:53,879 - [INFO] - [ServiceAlarmMonitoringTask:27] - Starting alert pusher task for camera: "First"
hikvision_camera_bot  | 2022-10-07 14:39:53,884 - [DEBUG] - [asyncio:808] - <_SelectorSocketTransport fd=12 read=polling write=<idle, bufsize=0>> resumes reading
hikvision_camera_bot  | 2022-10-07 14:39:53,892 - [DEBUG] - [asyncio:800] - <_SelectorSocketTransport fd=12 read=idle write=<idle, bufsize=0>> pauses reading
hikvision_camera_bot  | 2022-10-07 14:39:53,893 - [DEBUG] - [httpx._client:1728] - HTTP Request: GET http://192.168.1.1/ISAPI/Event/notification/alertStream "HTTP/1.1 403 Forbidden"
hikvision_camera_bot  | 2022-10-07 14:39:53,896 - [ERROR] - [ServiceAlarmMonitoringTask:37] - Unexpectedly exited from stream chunk processing loop for cam_1. Retrying in 0.5 seconds...
hikvision_camera_bot  | 2022-10-07 14:39:54,398 - [INFO] - [ServiceAlarmMonitoringTask:27] - Starting alert pusher task for camera: "First"
hikvision_camera_bot  | 2022-10-07 14:39:54,404 - [DEBUG] - [asyncio:808] - <_SelectorSocketTransport fd=12 read=polling write=<idle, bufsize=0>> resumes reading
hikvision_camera_bot  | 2022-10-07 14:39:54,422 - [DEBUG] - [asyncio:800] - <_SelectorSocketTransport fd=12 read=idle write=<idle, bufsize=0>> pauses reading
hikvision_camera_bot  | 2022-10-07 14:39:54,423 - [DEBUG] - [httpx._client:1728] - HTTP Request: GET http://192.168.1.1/ISAPI/Event/notification/alertStream "HTTP/1.1 403 Forbidden"
hikvision_camera_bot  | 2022-10-07 14:39:54,425 - [ERROR] - [ServiceAlarmMonitoringTask:37] - Unexpectedly exited from stream chunk processing loop for cam_1. Retrying in 0.5 seconds...
Blogoslov commented 1 year ago

yes, the same issue for me with camera IPC-B682-G2/ZS. Any advise how to avoid this error?

thanks!

tropicoo commented 1 year ago

@best-tech Your error is a different kind, you have 403 Forbidden error. Probably related to the configuration on the camera side. @Blogoslov Do you have the same 403 Forbidden error?