Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alert stream endless loop #50

Open
k3it opened this issue Sep 14, 2022 · 7 comments
Open

Alert stream endless loop #50

k3it opened this issue Sep 14, 2022 · 7 comments
Assignees
Labels
question Further information is requested

Comments

@k3it
Copy link

k3it commented Sep 14, 2022

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

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
Copy link
Owner

Доброго дня!

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
Copy link
Author

k3it commented Sep 29, 2022

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
Copy link
Owner

Let's try to log what we receive in the bot here in the file. Maybe we will see something interesting.

async for chunk in response.aiter_text():

            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
Copy link
Author

k3it commented Sep 30, 2022

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
Copy link

best-tech commented Oct 7, 2022

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
Copy link

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

thanks!

@tropicoo
Copy link
Owner

@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?

@tropicoo tropicoo self-assigned this Jun 17, 2023
@tropicoo tropicoo added the question Further information is requested label Jun 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants