nibi79 / synologysurveillancestation

Synology Surveillancestation Binding
45 stars 5 forks source link

Adding Motion detect type switch (enable/disable motion detection for a given camera) #36

Closed b-r-y closed 5 years ago

b-r-y commented 5 years ago

Hi @Pavion ,

I was wondering if you could help me add a motion detection type switch where the type are according to SS: -1:disable, 0: by camera and 1: by ss. I see this as a extra camera channel.

This should be possible via this API: https://192.168.20.120:5001/webapi/entry.cgi?api=SYNO.SurveillanceStation.Camera.Event&method=MDParamSave&version=1&_sid="blahblahb auth token"&camId=2&source=1&keep=true

I was able to fetch the motion detect params via: https://192.168.20.120:5001/webapi/entry.cgi?api=SYNO.SurveillanceStation.Camera.Event&method=MotionEnum&version=1&_sid="blahblahb auth token"&camId=2

but i struggle to get the MDParamSave going... i keep getting error 401

Anyway the idea behind it to make an openhab button that say Arm/Disarm for the motion detection/alarm etc. when i get home or leave.

I checked and the API for this is the same from V2.0 onwards.

Thanks as always! Boyan

Pavion commented 5 years ago

Hello again! Please consider using Home Mode for this. This switch can be used in Surveillance Station rules and is a Bridge Channel. If this is insufficient, I can look into it. As far as I can remember, there were issues with MDParamSave....

Pavion commented 5 years ago

By the way, this works for me and disables event detection.

OFF

http://192.168.1.1:5000/webapi/entry.cgi?camId=2&version="1"&api="SYNO.SurveillanceStation.Camera.Event"&method="MDParamSave"&keep=true&_sid=omaOkZMY6ujlE14A0MQN043107&source=-1

ON

http://192.168.1.1:5000/webapi/entry.cgi?camId=2&version="1"&api="SYNO.SurveillanceStation.Camera.Event"&method="MDParamSave"&keep=true&_sid=omaOkZMY6ujlE14A0MQN043107&source=1
Pavion commented 5 years ago

I've fixed some errors in the post above. Please check, whether this commands work for you and whether you think an internal implementation would be a good enhancement.

b-r-y commented 5 years ago

Thanks for teh hints @Pavion .

I actually figured it out - it was about permissions - i had set up the openhab user as a Surveillance Station Spectator only. Once i granted manager privilege all went ok.

I cannot use home mode as this is new in SS 8.0+ with API v2.8+...

What i have now is constantly recording camera on rotation archive schedule that sends motion detect mails within certain hours where i am supposed to be out of the house. This is of course not accurate and many days when i get home early i get tens of mails. I wanted to stop the motion detection or stop the notifications. So far i figured that i need SS manager privilege to manipulate the motion detection but to enable/disable the notification i need DS admin it seems.

Either way i'd appreciate help getting an extra channel. For the motion detection it should really be a number with value -1,0 and 1. For the mail notification switch it can also be binary switch.

Let me know which you think is easier and i will dig to see if I can figure out those permissions...

Thanks! Boyan

b-r-y commented 5 years ago

Hi again,

So the easiest way to do this is to simply grant admin privileges to the openhab user. I don't like this too much especially since the password sits in plain text in the things file but for my local application is fine. I don't see other ways to control the notifications settings...

That being said i can get to surgically disable (0)/enable (1) the email notification for motion detection only via: https://192.168.20.120:5001/webapi/entry.cgi?api=SYNO.SurveillanceStation.Notification.Filter&version=1&method=Set&_sid={{token}}&5=1

At first i though i can just do the java transformation but now i am not sure how to do that and expose a switch... I can make a rule and use the SID channel i guess but that's like building a new binding over the http binding...

What would you suggest? I would prefer i try to do a new channel and you help me fix the bugs :)

Cheers Boyan

Pavion commented 5 years ago

You should create an dedicated Surveillance Admin for the binding. Your password is kept locally and should be safe there. If someone evil can access your openHAB installation, you've done something wrong. By the way, you can also use automatic discovery in PaperUI, which obfuscates password further.

As for your channel question:

Afaik you will not be able to create an Switch using JS transformation. It could be done with a JS/PHP script or HTTP binding, but as you've said, it's kind of wrong.

The right way as I see it is to introduce all MD channels (on/off, sensitivity, threshold, object size and percentage) and allow them to be set and read. This also means creating even more threads and polling Disk Station more often. This is a rather large issue and I could look into it if I find some time but I can't make any promises.

Pavion commented 5 years ago

Please check the latest release for an implementation. Your feedback is appreciated! https://github.com/nibi79/synologysurveillancestation/releases/tag/0.37

b-r-y commented 5 years ago

Hi @Pavion ,

Thanks a lot for this implementation! I tried the enable / disable function and as far as i can tell it works. I checked also that the rest of the parameters are preserved so no need to set again thresholds and such after a disable.

I see no other errors or warning in the log either. As far as I can tell it's a solid build.

Perhaps one comment you can add to the guide is that the user should have manager privileges in SS or did you perhaps find a way to do it with viewer only?

P.S. i was reading the code thinking you would not have the time for this and i was planning to try to do it. Now that's mute but i think i will try to play around with the Notifications. I feel bad at this point asking you to do stuff so i will try myself first and ask for review only :).

b-r-y commented 5 years ago

By the way did you use older version of the repo to build the binding? I pulled openhab from git few days back and had to change all header strings with the copyright to fit some new change. I see the mdparam branch still carries the old headers. Did you simply not update or do you have a trick up your sleeve? :)

Pavion commented 5 years ago

Hi! I'm glad it works!

By the way, Readme says: "For security reasons it's always a better practice to create a separate user". I always thought it clear, that this user have to have all rights necessary :)

As for openHAB and headers: I actually don't update my workspace too often to avoid bugs, mostly between major releases. As this binding is no part of openHAB distribution, I follow the guidelines - but loosely. The headers contain current year and copyright information and this have to suffice for now.

As for the rest, you are welcome to produce new ideas and contibute in any way you like!

Best regards Pav

Pavion commented 5 years ago

I see now, EPL 2.0 took effect a month ago and I haven't implemented it yet. On the other hand, neither style check nor the headers are mandatory for this binding so you may ignore any related errors on compile.

If you're satisfied with the latest release for now, please close this issue - but feel free to open new ones 👍

b-r-y commented 5 years ago

Hi @Pavion ,

You have to show me how you saw those things and what is EPL 2.0 :) I just tired mvn clean install and got a bunch of errors. I fixed the compile by replacing the header with one from the other projects. I don't know how to ignore these without doing what the compiler wants...

Anyway, this issue is done. Thanks again.

Pavion commented 5 years ago

Perhaps we've talked about different things all along. If you're cloning this binding, you should compile it using mvn install from the binding's folder. It's not necessary to clean/build entire project each time and you can safely ignore any style check errors as soon as you've got a compiled jar in your /target folder.

As per style check every java file should contain a pre-defined header, found in src/etc. There you'd find the header template mentioning EPL 1.0 (Eclipse Public Licence). About a month ago, openHAB core (found in its own repository https://github.com/openhab/openhab-core https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=2ahUKEwiJne251dvgAhXjMewKHWWNAjAQFjAAegQIBBAC&url=https%3A%2F%2Fgithub.com%2Fopenhab%2Fopenhab-core&usg=AOvVaw2FqTmQIMLPqUHdORfmHMzM) switched their headers (also in src/etc) to using EPL 2.0. It's worth mentioning, that even the main openhab-addons repository is still using old 1.0 headers, so I'm not that slow. But anyway you're right and I'll implement new headers somewhen.