psyciknz / CameraEvents

Dahua NVR/Camera Alerting to MQTT. Fairly specific to my needs, but also an attempt at starting to use travis.
Eclipse Public License 2.0
58 stars 19 forks source link

No issue: Learn Python; how does this script actually work? #21

Closed ChillXXL closed 3 years ago

ChillXXL commented 3 years ago

Hello psyciknz,

I am newbie to Python and try to read and understand your script (especially the connection part) for learning purpose. Can you explain me how it is coded and what is does?

Looking at: https://github.com/psyciknz/CameraEvents/blob/ed54c3e890376fc3fa0d75d5413fc4d275bbd64e/CameraEvents.py#L863

Does this script make a HTTP request (by Pycurl) every second (polling) to the Dahua NVR connected to the Dahua eventhandler service and translates the JSON output of the HTPP request (if responsecode = 200) to MQTT events?

If yes, doesn't this frequent traffic request stresses out the NVR cpu?

psyciknz commented 3 years ago

Hi.

No there's a call in there (a curl command) to an API on the camera/nvr that is a stream of events. So when an event is triggered it writes to that stream (the code is in the OnReceive method at this point), it then looks for some info in the received event and then calls for a snapshot image and post out an mqtt message.

ChillXXL commented 3 years ago

OnReceive

So This line? https://github.com/psyciknz/CameraEvents/blob/ed54c3e890376fc3fa0d75d5413fc4d275bbd64e/CameraEvents.py#L841

I am new to cURL but familiar with HTTP requests. Can I see a cURL as a sort of 'hotline' (read: direct connection) and refreshes automatically the content on the line as frequent as possible and doesn't close the connection like a HTTP request does?

Sort of like a website and F5 refresh is pushed as much as possible so the content gets updated every time?

psyciknz commented 3 years ago

thats setting an option for pycurl -to use the onreceive method for any data written by pycurl.

Pycurl, is a python implemention of the cUrl binary. So instead of a single call, which exits, the dahua api, keeps the stream open, and when events occur they write to that open curl stream. Eg:

--myboundary
Content-Type: text/plain
Content-Length:37
Code=VideoMotion;action=Start;index=5
--myboundary
Content-Type: text/plain
Content-Length:37
Code=VideoMotion;action=Start;index=5

Is what is received for a videomotion event. So when I see that, I can see the camera (index 5) and that it's a start. A stop event is also triggered which is the end of the event.

Most of the pycurl stuff came from another source. THen I added the MQTT and snapshot extraction.

ChillXXL commented 3 years ago

Thanks for the info. So there is only one initial connection (continuously) made to the Dahua device from your python script and the Dahua NVR or CAM PUSHes data, like your example above, towards the stream when a event occurs. Then in your code this is followed by a function like IF data is received AND you find string-data like 'VideoMotion' and 'action=Start' THEN to do some MQTT handling ELSE do x. Like this?

psyciknz commented 3 years ago

yep pretty much

ChillXXL commented 3 years ago

Thanks! cURL and open stream are the keywords :-) I do understand now. Thumps up for the app. I shall close the issue.