CameraEvents Service - Dahua Events to mqtt

Discussion in 'Home Automation' started by psycik, Feb 25, 2019.

Share This Page

  1. psycik

    psycik Getting the hang of it

    Joined:
    Dec 9, 2015
    Messages:
    149
    Likes Received:
    18
    Location:
    Wellington, New Zealand
    As per: NodeJS Module: node-dahua-api

    psyciknz/CameraEvents

    This is a python service I run in docker. One of the first I wrote from scratch, so the documentation is probably a little light.

    I've updated the readme for some run information, (git clone, python CameraEvents.py). And will figure out making the docker image automatically (this is all learning to me).

    The main things it can do are to convert all VideoMotion events and IVS events into switches that can then be read by OpenHAB and HomeAssistant (or anything else that uses MQTT). IT also has the ability to pull a snapshot from the camera and post that as binary data to mqtt.
     
    johnnyletrois and aristobrat like this.
  2. johnnyletrois

    johnnyletrois n3wb

    Joined:
    Feb 13, 2017
    Messages:
    29
    Likes Received:
    5
    This is great. Full circle to ipcamtalk. The script I modified to work with MQTT originally came from the forum here!
     
    aristobrat likes this.
  3. psycik

    psycik Getting the hang of it

    Joined:
    Dec 9, 2015
    Messages:
    149
    Likes Received:
    18
    Location:
    Wellington, New Zealand
    Yeah there was a base I'd read, (and I believe I've kept the original credit). And adapted it. I had a similar service that was windows (c#), but most of my gear had moved to docker/python so I was keen to re-write it as I learnt python more.
     
    johnnyletrois likes this.
  4. trollmar

    trollmar n3wb

    Joined:
    Feb 9, 2017
    Messages:
    20
    Likes Received:
    0
    Thx for the info.

    So..

    I have to install node.js etc. as described here:
    NodeJS Module: node-dahua-api

    after that gitclone your python script and run it?
     
  5. psycik

    psycik Getting the hang of it

    Joined:
    Dec 9, 2015
    Messages:
    149
    Likes Received:
    18
    Location:
    Wellington, New Zealand
    No mine is completely separate to the node-js.

    Just git clone to a folder, and run.

    I only added a comment there as I want working on a similar service in that it talked to the dahua api and that was the only thread that seemed to discuss the api.
     
  6. trollmar

    trollmar n3wb

    Joined:
    Feb 9, 2017
    Messages:
    20
    Likes Received:
    0
    Got it

    The requirements in requiremnts.txt are needed?

    paho-mqtt
    pycurl
    ConfigParser
    requests

    Thx again :highfive:
     
  7. psycik

    psycik Getting the hang of it

    Joined:
    Dec 9, 2015
    Messages:
    149
    Likes Received:
    18
    Location:
    Wellington, New Zealand
    yes, this is almost my first attempt at automated testing. Something else new for me to play with.
     
  8. trollmar

    trollmar n3wb

    Joined:
    Feb 9, 2017
    Messages:
    20
    Likes Received:
    0
    Hi,

    some problems to run the script:

    Code:
    me@ubuntu18:~/CameraEvents$ python3 CameraEvents.py
    Traceback (most recent call last):
    File "CameraEvents.py", line 15, in <module>
    import ConfigParser
    ModuleNotFoundError: No module named 'ConfigParser'
    
    me@ubuntu18:~/CameraEvents$ pip search ConfigParser
    configparser (3.7.3)              - Updated configparser from Python 3.7 for Python 2.6+.
    INSTALLED: 3.7.3 (latest)

    Some Hint?
    Best regards
     
    Last edited: Feb 26, 2019
  9. psycik

    psycik Getting the hang of it

    Joined:
    Dec 9, 2015
    Messages:
    149
    Likes Received:
    18
    Location:
    Wellington, New Zealand
    p
    You're running in python (not docker?)

    pip install ConfigParser
    Same with everything else in that requriements.txt. I believe there's a command that will take the requirements.txt and go get everything it needed.
     
  10. trollmar

    trollmar n3wb

    Joined:
    Feb 9, 2017
    Messages:
    20
    Likes Received:
    0
    Thx.
    I already intstalled ConfigParser with pip under ubuntu18.x
    I tried various hints to solve ... No luck.

    Is there a Docker Image available?
     
  11. trollmar

    trollmar n3wb

    Joined:
    Feb 9, 2017
    Messages:
    20
    Likes Received:
    0
  12. psycik

    psycik Getting the hang of it

    Joined:
    Dec 9, 2015
    Messages:
    149
    Likes Received:
    18
    Location:
    Wellington, New Zealand
  13. trollmar

    trollmar n3wb

    Joined:
    Feb 9, 2017
    Messages:
    20
    Likes Received:
    0
    I think i get it to work.
    Changed ConfigParser to configparser.

    Edit my config.ini

    Here my terminal output:

    Code:
    me@ubuntu18:~/CameraEvents$ python3  CameraEvents.py
    
    
    
    2019-02-26 21:13:23,523 - __main__ - INFO - Loading config
    
    2019-02-26 21:13:23,524 - __main__ - ERROR - Error Reading channel list:No option 'channels' in section: 'Garten'
    
    2019-02-26 21:13:23,599 - __main__ - DEBUG - Added Dahua device at: http://myIP/cgi-bin/eventManager.cgi?action=attach&codes=[VideoMotion,CrossLineDetection,AlarmLocal,VideoLoss,VideoBlind]
    
    2019-02-26 21:13:23,601 - __main__ - INFO - Connected to MQTT OK Returned code=0
    
    2019-02-26 21:13:23,806 - __main__ - DEBUG - [IPC]: HTTP/1.1 200 OK
    
    Cache-Control: no-cache
    
    Pragma: no-cache
    
    Expires: Thu, 01 Dec 2099 16:00:00 GMT
    
    Connection: Keep-Alive
    
    Content-Type: multipart/x-mixed-replace; boundary=myboundary
    
    
    
    2019-02-26 21:13:23,807 - __main__ - DEBUG - [IPC] OnConnect()
    
    heartbeat
    
    2019-02-26 21:14:13,798 - __main__ - DEBUG - Heartbeat
    
    heartbeat
    
    2019-02-26 21:15:03,997 - __main__ - DEBUG - Heartbeat
    
    2019-02-26 21:15:44,746 - __main__ - DEBUG - [IPC]: -- myboundary
    
    Content-Type: text/plain
    
    Content-Length:44
    
    Code=CrossLineDetection;action=Start;index=0
    
    
    
    Traceback (most recent call last):
    
      File "CameraEvents.py", line 261, in OnReceive
    
        crossData = json.loads(Alarm["data"])
    
    KeyError: 'data'
    
    2019-02-26 21:15:44,798 - __main__ - DEBUG - [IPC] OnDisconnect(Failed writing body (0 != 108) (23))
    
    2019-02-26 21:15:50,019 - __main__ - DEBUG - [IPC] OnDisconnect(Success)
    
    heartbeat
    
    2019-02-26 21:15:54,234 - __main__ - DEBUG - Heartbeat
    
    2019-02-26 21:15:55,188 - __main__ - DEBUG - [IPC]: HTTP/1.1 200 OK
    
    Cache-Control: no-cache
    
    Pragma: no-cache
    
    Expires: Thu, 01 Dec 2099 16:00:00 GMT
    
    Connection: Keep-Alive
    
    Content-Type: multipart/x-mixed-replace; boundary=myboundary
    
    
    
    2019-02-26 21:15:55,188 - __main__ - DEBUG - [IPC] OnConnect()
    
    2019-02-26 21:16:30,976 - __main__ - DEBUG - [IPC]: -- myboundary
    
    Content-Type: text/plain
    
    Content-Length:44
    
    Code=CrossLineDetection;action=Start;index=0
    
    
    
    Traceback (most recent call last):
    
      File "CameraEvents.py", line 261, in OnReceive
    
        crossData = json.loads(Alarm["data"])
    
    KeyError: 'data'
    
    2019-02-26 21:16:30,977 - __main__ - DEBUG - [IPC] OnDisconnect(Failed writing body (0 != 108) (23))
    
    2019-02-26 21:16:36,248 - __main__ - DEBUG - [IPC] OnDisconnect(Success)
    
    2019-02-26 21:16:41,468 - __main__ - DEBUG - [IPC]: HTTP/1.1 200 OK
    
    Cache-Control: no-cache
    
    Pragma: no-cache
    
    Expires: Thu, 01 Dec 2099 16:00:00 GMT
    
    Connection: Keep-Alive
    
    Content-Type: multipart/x-mixed-replace; boundary=myboundary
    
    
    
    2019-02-26 21:16:41,468 - __main__ - DEBUG - [IPC] OnConnect()
    
    heartbeat
    
    2019-02-26 21:16:44,430 - __main__ - DEBUG - Heartbeat
    
    heartbeat
    
    2019-02-26 21:17:34,626 - __main__ - DEBUG - Heartbeat
    
    heartbeat
    
    2019-02-26 21:18:24,817 - __main__ - DEBUG - Heartbeat
    
    heartbeat
    
    2019-02-26 21:19:15,008 - __main__ - DEBUG - Heartbeat
    


    I get Heartbeats topics. But no Topics for IPC Events.
    mmh
     
    Last edited: Feb 26, 2019
  14. psycik

    psycik Getting the hang of it

    Joined:
    Dec 9, 2015
    Messages:
    149
    Likes Received:
    18
    Location:
    Wellington, New Zealand
    Hmm You don't get a json string with the CrossLineDetection?

    Code:
    Content-Type: text/plain
    Content-Length:905
    
    Code=CrossLineDetection;action=Stop;index=0;data={
       "Class" : "Normal",
       "CountInGroup" : 1,
       "DetectLine" : [
          [ 3843, 5677 ],
          [ 6512, 5136 ]
       ],
       "Direction" : "LeftToRight",
       "EventSeq" : 11,
       "FrameSequence" : 41852,
       "GroupID" : 11,
       "IndexInGroup" : 0,
       "LocaleTime" : "2019-02-27 10:25:21",
       "Mark" : 0,
       "Name" : "Gate",
       "Object" : {
          "Action" : "Appear",
          "BoundingBox" : [ 3536, 5504, 4560, 8192 ],
          "Center" : [ 4048, 6848 ],
          "Confidence" : 0,
          "FrameSequence" : 0,
          "MainColor" : [ 0, 0, 0, 0 ],
          "ObjectID" : 1100,
          "ObjectType" : "Vehicle",
          "RelativeID" : 0,
          "Source" : 0.0,
          "Speed" : 0,
          "SpeedTypeInternal" : 0,
          "Text" : ""
       },
       "PTS" : 42956392890.0,
       "RuleId" : 1,
       "Sequence" : 0,
       "Source" : 36137280.0,
       "Track" : null,
       "UTC" : 1551219919.0,
       "UTCMS" : 708
    }
    
    So mine does this...and this is what it's based on. I might have to add a check for that, and just trigger an event with no detail (just like a videomotion event)
     
  15. trollmar

    trollmar n3wb

    Joined:
    Feb 9, 2017
    Messages:
    20
    Likes Received:
    0
    Yeah ..looks like.
    Code:
    2019-02-26 21:59:50,727 - __main__ - DEBUG - Heartbeat
    2019-02-26 22:00:21,396 - __main__ - DEBUG - [IPC]: -- myboundary
    Content-Type: text/plain
    Content-Length:44
    Code=CrossLineDetection;action=Start;index=0
    
    
    Traceback (most recent call last):
      File "CameraEvents.py", line 261, in OnReceive
        crossData = json.loads(Alarm["data"])
    KeyError: 'data'
    2019-02-26 22:00:21,447 - __main__ - DEBUG - [IPC] OnDisconnect(Failed writing body (0 != 108) (23))
    I also didnt get a mqtt message that crossline detection happend.

    what could I do to figure it out.
     
  16. psycik

    psycik Getting the hang of it

    Joined:
    Dec 9, 2015
    Messages:
    149
    Likes Received:
    18
    Location:
    Wellington, New Zealand
    So this section
    Code:
    crossData = json.loads(Alarm["data"])
    _LOGGER.info(Alarm["Code"] + " received: " + Alarm["data"] )
    if "Direction" not in crossData:
    direction = "unknown"
    else:
    direction = crossData["Direction"]
    region = crossData["Name"]
    object = crossData["Object"]["ObjectType"]
    regionText = "{} With {} in {} direction for {} region".format(Alarm["Code"],object,direction,region)
    self.client.publish(self.basetopic +"/IVS/" + Alarm["channel"] ,regionText)
    
    you'll need to refactor....In fact if it was removed and bundled in to the VideoMotion code: (ie change line 245)
    Code:
    if Alarm["Code"] == "VideoMotion" or Alarm["Code"] ==  "CrossRegionDetection" or Alarm["Code"] ==  "CrossLineDetection":
    
    then for your case you'd bypass the code causing the problem. But all MQTT events would say "motion detected" - but it should get you going.
     
  17. trollmar

    trollmar n3wb

    Joined:
    Feb 9, 2017
    Messages:
    20
    Likes Received:
    0
    Hi psycik.

    This looks good :)

    Code:
    2019-02-26 22:24:05,505 - __main__ - DEBUG - Heartbeat
    2019-02-26 22:24:08,064 - __main__ - DEBUG - [IPC]: -- myboundary
    Content-Type: text/plain
    Content-Length:44
    Code=CrossLineDetection;action=Start;index=0
    
    
    2019-02-26 22:24:08,065 - __main__ - INFO - Video Motion received: IPC Index: Garten Code: CrossLineDetection
    2019-02-26 22:24:08,065 - __main__ - INFO - Snapshot Url: http://myIP/cgi-bin/snapshot.cgi?channel=1
    2019-02-26 22:24:08,456 - __main__ - ERROR - Error sending image: Object of type 'bytes' is not JSON serializable
    2019-02-26 22:24:10,425 - __main__ - DEBUG - [IPC]: -- myboundary
    Content-Type: text/plain
    Content-Length:43
    Code=CrossLineDetection;action=Stop;index=0
    
    

    I change the code in Line 245 as you described.
    Now first a play with this in my homeautomation. :love:
     
  18. psycik

    psycik Getting the hang of it

    Joined:
    Dec 9, 2015
    Messages:
    149
    Likes Received:
    18
    Location:
    Wellington, New Zealand
    it seems to not be getting an image. I assume this might be related to that last change I had to make (listed in the readme, that for my specific NVR I have to put channel (index)+1. So for a snapshot if it's index=0 (as per your example) I have to ask the NVR for channel=1. Yours is probably channel=index not index+1

    So if you look at the snapshot request (new thread snapshot), you should see it asking for index+1 you should be good to remove the +1
     
  19. trollmar

    trollmar n3wb

    Joined:
    Feb 9, 2017
    Messages:
    20
    Likes Received:
    0
    Your right. But i look at that later.

    First I have to Bridge my "CameraEvents MQTT Broker" (local Broker just for your CameraEvents Script) to my main Broker.
    My main Broker needs user credentials :wave:

    Best regards and have a nice day!
     
  20. psycik

    psycik Getting the hang of it

    Joined:
    Dec 9, 2015
    Messages:
    149
    Likes Received:
    18
    Location:
    Wellington, New Zealand
    DO
    Do you want to post your version to me, and I'll look at putting some of those changes in and dealing with the change in API (the 1/0 thing)
    I should also make "do you want an image" configurable for if you're not using that function, they're pretty large to be chucking around mqtt for no reason.