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

[enhancement] Patch to add auth support for MQTT #4

Closed ghost closed 4 years ago

ghost commented 5 years ago

This should add support for broker authentication. Make sure 'user' and 'password' are present in the right section for the config.ini file.

No error checking added, err=5 or so when auth fails. Quick hack that works for me.

diff --git a/CameraEvents.py b/CameraEvents.py
index f5f632e..21adc39 100644
--- a/CameraEvents.py
+++ b/CameraEvents.py
@@ -312,6 +312,7 @@ class DahuaEventThread(threading.Thread):
         self.basetopic = mqtt["basetopic"]

         self.client = paho.Client("CameraEvents-" + socket.gethostname(), clean_session=True)
+        self.client.username_pw_set(mqtt["user"], mqtt["password"])
         self.client.on_connect = self.mqtt_on_connect
         self.client.on_disconnect = self.mqtt_on_disconnect
         self.client.message_callback_add(self.basetopic +"/+/picture",self.mqtt_on_picture_message)
@@ -529,6 +530,8 @@ if __name__ == '__main__':
         mqtt["IP"] = cp.get("MQTT Broker","IP")
         mqtt["port"] = cp.get("MQTT Broker","port")
         mqtt["basetopic"] = cp.get("MQTT Broker","BaseTopic")
+        mqtt["user"] = cp.get("MQTT Broker","user")
+        mqtt["password"] = cp.get("MQTT Broker","password")
         dahua_event = DahuaEventThread(mqtt,cameras)

         dahua_event.start()
psyciknz commented 5 years ago

Ok, I can look at adding that snippet

oblogic7 commented 4 years ago

Any plans to merge this into master? My MQTT broker requires authentication. I'll be happy to fork and open a PR if you can approve it.

psyciknz commented 4 years ago

I've added this. It's in the image-playback branch, Any chance @oblogic7 can try it? Though this branch did jump to python 3.7.2 as well....so unsure if that will impact - I don't know how you run it.

oblogic7 commented 4 years ago

I was trying to run via docker image. I'll manually checkout that branch and give it a test. Do you have an ETA on when that branch may be merged and available on the docker image?

psyciknz commented 4 years ago

What architecture of an image? Arm32, arm64 or amd64? I can push a dev one for you you try (there's some other code in there as well, which is why I don't want to merge to master yet).

oblogic7 commented 4 years ago

amd64

psyciknz commented 4 years ago

docker pull psyciknz/cameraevents:0.2.0-dev. @oblogic7

oblogic7 commented 4 years ago

Pulled the image, but am running into the following error: 2020-01-09 17:45:10,161 - __main__ - ERROR - Error starting:Failed to open/find all files

Any ideas on that? Volume is bound according to the screenshot below and config.ini file exists with full permissions. Not sure why it isn't working.

image

psyciknz commented 4 years ago

Does it bomb out entirely, or does the container stay running? If it stays running, exec into it and check the volumes. But I don't think I played with anything there (though the python version was increased).

Did you note the change to the user name in the config file? cp.get("MQTT Broker","user",fallback=None) user - not username

oblogic7 commented 4 years ago

I did notice that key changed, but I don't think it is a format issue. It seems that it isn't finding the file at all. I don't see any reason in the code why it wouldn't be working, so has to be a permission or binding issue.

psyciknz commented 4 years ago

There are exceptions when it doens't like the format of the .ini....but if it was that I think you'd have a bigger crash.

I think you migh right on permissions/binding. Execing into the container should be able to point you in the right direction.

oblogic7 commented 4 years ago

I've been able to get further along by removing the user specific lines from the dockerfile and rebuilding the image. Still poking at it on the image-playback branch. Will report back once I have it fully working with the changes that I made.

oblogic7 commented 4 years ago

I was finally able to get things running on the image-playback branch with the greatly simplified dockerfile below. I also needed to run the container with host network mode to allow connection to my broker. Not sure if this dockerfile is compatible with all architectures you build for, but feel free to use it if it does.

FROM amd64/python:2.7.15-jessie

WORKDIR /opt/cameraevents

COPY . /opt/cameraevents

RUN pip install -r requirements.txt

# run process
CMD python CameraEvents.py
psyciknz commented 4 years ago

And that works? I thought I’d updated the code for python 3.7 which means it would fail in 2.7

On Fri, 10 Jan 2020 at 16:41, Matt Snyder notifications@github.com wrote:

I was finally able to get things running on the image-playback branch with the greatly simplified dockerfile below. I also needed to run the container with host network mode to allow connection to my broker. Not sure if this dockerfile is compatible with all architectures you build for, but feel free to use it if it does.

FROM amd64/python:2.7.15-jessie

WORKDIR /opt/cameraevents

COPY . /opt/cameraevents

RUN pip install -r requirements.txt

run process

CMD python CameraEvents.py

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/psyciknz/CameraEvents/issues/4?email_source=notifications&email_token=ABI2I4YGHWAUNYHK73WELLDQ47U6FA5CNFSM4HD4VSB2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEISSI3I#issuecomment-572859501, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABI2I455MDEWAT5UA4IGSS3Q47U6FANCNFSM4HD4VSBQ .

oblogic7 commented 4 years ago

I haven't seen any issues yet. Just setup my IVS rules last night, so planning to test and start using the events today. Will let you know if I run into any issues, but there weren't any complaints about syntax. The container has been running for 11 hours now without issue.

valepe commented 4 years ago

Hi, I'd like to use this script as docker image on a Synology Nas with DSM 6.x. Because my MQTT broker requires authentication can I use this image as it is?

psyciknz commented 4 years ago

This dev one yes. Then if you confirm it’s ok, I’ll pr the code to master

valepe commented 4 years ago

Sorry for the delay but I'm busy with my family. In next days I will try it. Only one question: since my cameras are in a different subnet, they aren't accessible from your docker image but NVR it is (its IP is in the main lan). Does it report tripwire and intrusion alerts from cameras?

psyciknz commented 4 years ago

Sorry for the delay but I'm busy with my family. In next days I will try it. Only one question: since my cameras are in a different subnet, they aren't accessible from your docker image but NVR it is (its IP is in the main lan). Does it report tripwire and intrusion alerts from cameras?

Yes, you point camera events at your NVR, and tell it it's an nvr (in the config).

[NVR]
host=192.168.1.108
protocol=http
name=NVR
**isNVR=True**
port=80
user=<nvruser>
pass=<nvrpassword>
auth=digest
events=VideoMotion,CrossLineDetection,AlarmLocal,VideoLoss,VideoBlind,CrossRegionDetection,FaceDetection
#channels=0:Garage|1:Backdoor|2:Backyard|3:Frontdoor|4:Ludlam
snapshotoffset=1

And there's an optional (I think it still works) channels, to name and number channels. But the default is it will figure out all the attached cameras.

psyciknz commented 4 years ago

Pushed to master #10