From 15d6909c430ed4d10cb41a146764dd24a56485a2 Mon Sep 17 00:00:00 2001
From: nasseeder1 <>
Date: Wed, 23 Dec 2015 17:14:54 +0000
Subject: [PATCH] 49_SSCam: function "enable" and "disable" for SS-Cams added,
changed timout of Http-calls to a higher value
git-svn-id: https://svn.fhem.de/fhem/trunk@10240 2b470e98-0d58-463d-a4d8-8e2adae1ed80
---
fhem/CHANGED | 3 +
fhem/FHEM/49_SSCam.pm | 414 +++++++++++++++++++++++++++++++++++-------
2 files changed, 349 insertions(+), 68 deletions(-)
diff --git a/fhem/CHANGED b/fhem/CHANGED
index afdfac64b..2072b05bd 100644
--- a/fhem/CHANGED
+++ b/fhem/CHANGED
@@ -1,5 +1,8 @@
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide.
# Do not insert empty lines here, update check depends on it.
+ - feature: 49_SSCam: function "enable" and "disable" for SS-Cams added
+ - change: 49_SSCam: changed timout of Http-calls to a higher value,
+ commandref enhanced
- feature: 57_CALVIEW.pm: added new function for running terms
- change: 57_CALVIEW.pm: improved commandref
- feature: 98_update.pm: support multiple repositories (Forum #45121)
diff --git a/fhem/FHEM/49_SSCam.pm b/fhem/FHEM/49_SSCam.pm
index e40865775..630fdb63c 100644
--- a/fhem/FHEM/49_SSCam.pm
+++ b/fhem/FHEM/49_SSCam.pm
@@ -1,10 +1,12 @@
#####################################################################################################
+# $Id $
+#####################################################################################################
# 49_SSCam.pm
#
-# Copyright by Heiko Maaz
+# written by Heiko Maaz
# e-mail: Heiko dot Maaz at t-online dot de
#
-# This Modul is used to operate Cameras defined in Synology Surveillance Station 7.0 or higher
+# This Modul can be used to operate Cameras defined in Synology Surveillance Station 7.0 or higher.
# It's based on Synology Surveillance Station API Guide 2.0
#
# This file is part of fhem.
@@ -24,7 +26,8 @@
#
######################################################################################################
# Versionshistorie:
-#
+# 1.4 23.12.2015 function "enable" and "disable" for SS-Cams added,
+# changed timout of Http-calls to a higher value
# 1.3 19.12.2015 function "snap" for taking snapshots added,
# fixed a bug that functions may impact each other
# 1.2 14.12.2015 improve usage of verbose-modes
@@ -44,7 +47,7 @@
# $username = ""; # User für login auf DS
# $password = ""; # Passwort für User login
# $camname = ""; # Name der Kamera
-# $rectime = ""; # Dauer der Aufnahme in Sekunden
+# $rectime = ""; # Dauer der Aufnahme in Sekunden
package main;
@@ -114,6 +117,7 @@ sub SSCam_Define {
# Anfangswerte setzen
$hash->{HELPER}{ACTIVE} = "off";
readingsSingleUpdate($hash,"Record","Stop",0);
+ readingsSingleUpdate($hash, "Availability", "", 0);
return undef;
}
@@ -136,7 +140,9 @@ sub SSCam_Set {
my %SSCam_sets = (
on => "on",
off => "off",
- snap => "snap"
+ snap => "snap",
+ enable => "enable",
+ disable => "disable"
);
my $camname = $hash->{CAMNAME};
@@ -153,23 +159,32 @@ sub SSCam_Set {
{
if ($opt eq "on")
{
- &camstartrec($hash);
+ &camstartrec($hash);
}
- elsif ($opt eq "off")
- {
+ elsif ($opt eq "off")
+ {
&camstoprec($hash);
- }
- elsif ($opt eq "snap")
- {
+ }
+ elsif ($opt eq "snap")
+ {
&camsnap($hash);
- }
+ }
+ elsif ($opt eq "enable")
+ {
+ &camenable($hash);
+ }
+ elsif ($opt eq "disable")
+ {
+ &camdisable($hash);
+ }
+
}
}
#############################################################################################################################
######### OpMode-Startroutinen #############
-######### #############
+######### $hash->{HELPER}{ACTIVE} = Funktionstoken #############
######### $hash->{HELPER}{ACTIVE} = "on" -> eine Routine läuft, Start anderer Routine erst wenn "off". #############
######### $hash->{HELPER}{ACTIVE} = "off" -> keine andere Routine läuft, sofortiger Start möglich #############
#############################################################################################################################
@@ -182,6 +197,26 @@ sub camstartrec ($) {
my $camname = $hash->{CAMNAME};
my $name = $hash->{NAME};
my $logstr;
+ my $errorcode;
+ my $error;
+
+ if (ReadingsVal("$name", "Availability", "enabled") eq "disabled") {
+ # wenn Kamera disabled ist ....
+ $errorcode = "402";
+
+ # Fehlertext zum Errorcode ermitteln
+ $error = &experror($hash,$errorcode);
+
+ # Setreading
+ readingsBeginUpdate($hash);
+ readingsBulkUpdate($hash,"Errorcode",$errorcode);
+ readingsBulkUpdate($hash,"Error",$error);
+ readingsEndUpdate($hash, 1);
+
+ $logstr = "ERROR - Start Recording of Camera $camname can't be executed - $error" ;
+ &printlog($hash,$logstr,"1");
+ return;
+ }
if ($hash->{HELPER}{ACTIVE} ne "on" and ReadingsVal("$name", "Record", "Start") ne "Start") {
# Aufnahme starten
@@ -207,6 +242,26 @@ sub camstoprec ($) {
my $camname = $hash->{CAMNAME};
my $name = $hash->{NAME};
my $logstr;
+ my $errorcode;
+ my $error;
+
+ if (ReadingsVal("$name", "Availability", "enabled") eq "disabled") {
+ # wenn Kamera disabled ist ....
+ $errorcode = "402";
+
+ # Fehlertext zum Errorcode ermitteln
+ $error = &experror($hash,$errorcode);
+
+ # Setreading
+ readingsBeginUpdate($hash);
+ readingsBulkUpdate($hash,"Errorcode",$errorcode);
+ readingsBulkUpdate($hash,"Error",$error);
+ readingsEndUpdate($hash, 1);
+
+ $logstr = "ERROR - Stop Recording of Camera $camname can't be executed - $error" ;
+ &printlog($hash,$logstr,"1");
+ return;
+ }
if ($hash->{HELPER}{ACTIVE} ne "on" and ReadingsVal("$name", "Record", "Stop") ne "Stop") {
# Aufnahme stoppen
@@ -232,6 +287,26 @@ sub camsnap ($) {
my $camname = $hash->{CAMNAME};
my $name = $hash->{NAME};
my $logstr;
+ my $errorcode;
+ my $error;
+
+ if (ReadingsVal("$name", "Availability", "enabled") eq "disabled") {
+ # wenn Kamera disabled ist ....
+ $errorcode = "402";
+
+ # Fehlertext zum Errorcode ermitteln
+ $error = &experror($hash,$errorcode);
+
+ # Setreading
+ readingsBeginUpdate($hash);
+ readingsBulkUpdate($hash,"Errorcode",$errorcode);
+ readingsBulkUpdate($hash,"Error",$error);
+ readingsEndUpdate($hash, 1);
+
+ $logstr = "ERROR - Snapshot of Camera $camname can't be executed - $error" ;
+ &printlog($hash,$logstr,"1");
+ return;
+ }
if ($hash->{HELPER}{ACTIVE} ne "on") {
# einen Schnappschuß aufnehmen
@@ -249,6 +324,59 @@ sub camsnap ($) {
}
}
+###############################################################################
+### Kamera aktivieren
+
+sub camenable ($) {
+ my ($hash) = @_;
+ my $camname = $hash->{CAMNAME};
+ my $name = $hash->{NAME};
+ my $logstr;
+
+ if (ReadingsVal("$name", "Availability", "disabled") eq "enabled") {return;} # Kamera ist bereits enabled
+
+ if ($hash->{HELPER}{ACTIVE} ne "on") {
+ # eine Kamera aktivieren
+ $logstr = "Enable Camera $camname";
+ &printlog($hash,$logstr,"4");
+
+ $hash->{OPMODE} = "Enable";
+ $hash->{HELPER}{ACTIVE} = "on";
+
+ &getapisites_nonbl($hash);
+ }
+ else
+ {
+ InternalTimer(gettimeofday()+0.1, "camenable", $hash, 0);
+ }
+}
+
+###############################################################################
+### Kamera deaktivieren
+
+sub camdisable ($) {
+ my ($hash) = @_;
+ my $camname = $hash->{CAMNAME};
+ my $name = $hash->{NAME};
+ my $logstr;
+
+ if (ReadingsVal("$name", "Availability", "enabled") eq "disabled") {return;} # Kamera ist bereits disabled
+
+ if ($hash->{HELPER}{ACTIVE} ne "on" and ReadingsVal("$name", "Record", "Start") ne "Start") {
+ # eine Kamera deaktivieren
+ $logstr = "Disable Camera $camname";
+ &printlog($hash,$logstr,"4");
+
+ $hash->{OPMODE} = "Disable";
+ $hash->{HELPER}{ACTIVE} = "on";
+
+ &getapisites_nonbl($hash);
+ }
+ else
+ {
+ InternalTimer(gettimeofday()+0.2, "camdisable", $hash, 0);
+ }
+}
#############################################################################################################################
@@ -283,12 +411,12 @@ sub getapisites_nonbl {
$logstr = "--- Begin Function getapisites nonblocking ---";
&printlog($hash,$logstr,"4");
- # URL zur Abfrage der Eigenschaften von API SYNO.SurveillanceStation.ExternalRecording,$apicam
+ # URL zur Abfrage der Eigenschaften der API's
$url = "http://$servername:$serverport/webapi/query.cgi?api=$apiinfo&method=Query&version=1&query=$apiauth,$apiextrec,$apicam,$apitakesnap";
$param = {
url => $url,
- timeout => 5,
+ timeout => 10,
hash => $hash,
method => "GET",
header => "Accept: application/json",
@@ -337,7 +465,12 @@ sub login_nonbl ($) {
&printlog($hash,$logstr,"1");
$logstr = "--- End Function getapisites nonblocking with error ---";
&printlog($hash,$logstr,"4");
+
readingsSingleUpdate($hash, "Error", $err, 1); # Readings erzeugen
+
+ # ausgeführte Funktion ist abgebrochen, Freigabe Funktionstoken
+ $hash->{HELPER}{ACTIVE} = "off";
+
return;
}
@@ -436,9 +569,6 @@ sub login_nonbl ($) {
}
else
{
- # ausgeführte Funktion ist erledigt
- $hash->{HELPER}{ACTIVE} = "off";
-
# Fehlertext setzen
$error = "couldn't call API-Infosite";
@@ -454,6 +584,10 @@ sub login_nonbl ($) {
$logstr = "--- End Function getapisites nonblocking with error ---";
&printlog($hash,$logstr,"4");
+
+ # ausgeführte Funktion ist abgebrochen, Freigabe Funktionstoken
+ $hash->{HELPER}{ACTIVE} = "off";
+
return;
}
}
@@ -468,7 +602,7 @@ sub login_nonbl ($) {
$param = {
url => $url,
- timeout => 5,
+ timeout => 10,
hash => $hash,
method => "GET",
header => "Accept: application/json",
@@ -512,7 +646,12 @@ sub getcamid_nonbl ($) {
&printlog($hash,$logstr,"1"); # Eintrag fürs Log
$logstr = "--- End Function serverlogin nonblocking with error ---";
&printlog($hash,$logstr,"4");
+
readingsSingleUpdate($hash, "Error", $err, 1); # Readings erzeugen
+
+ # ausgeführte Funktion ist abgebrochen, Freigabe Funktionstoken
+ $hash->{HELPER}{ACTIVE} = "off";
+
return;
}
elsif ($myjson ne "") # wenn die Abfrage erfolgreich war ($data enthält die Ergebnisdaten des HTTP Aufrufes)
@@ -556,9 +695,6 @@ sub getcamid_nonbl ($) {
}
else
{
- # ausgeführte Funktion ist erledigt
- $hash->{HELPER}{ACTIVE} = "off";
-
# Errorcode aus JSON ermitteln
$errorcode = $data->{'error'}->{'code'};
@@ -577,6 +713,10 @@ sub getcamid_nonbl ($) {
$logstr = "--- End Function serverlogin nonblocking with error ---";
&printlog($hash,$logstr,"4");
+
+ # ausgeführte Funktion ist abgebrochen, Freigabe Funktionstoken
+ $hash->{HELPER}{ACTIVE} = "off";
+
return;
}
}
@@ -592,7 +732,7 @@ sub getcamid_nonbl ($) {
$param = {
url => $url,
- timeout => 5,
+ timeout => 10,
hash => $hash,
method => "GET",
header => "Accept: application/json",
@@ -615,6 +755,9 @@ sub camop_nonbl ($) {
my $servername = $hash->{SERVERNAME};
my $serverport = $hash->{SERVERPORT};
my $camname = $hash->{CAMNAME};
+ my $apicam = $hash->{HELPER}{APICAM};
+ my $apicampath = $hash->{HELPER}{APICAMPATH};
+ my $apicammaxver = $hash->{HELPER}{APICAMMAXVER};
my $apiextrec = $hash->{HELPER}{APIEXTREC};
my $apiextrecpath = $hash->{HELPER}{APIEXTRECPATH};
my $apiextrecmaxver = $hash->{HELPER}{APIEXTRECMAXVER};
@@ -644,6 +787,10 @@ sub camop_nonbl ($) {
$logstr = "--- End Function getcamid nonblocking with error ---";
&printlog($hash,$logstr,"4");
readingsSingleUpdate($hash, "Error", $err, 1); # Readings erzeugen
+
+ # ausgeführte Funktion ist abgebrochen, Freigabe Funktionstoken
+ $hash->{HELPER}{ACTIVE} = "off";
+
return;
}
elsif ($myjson ne "") # wenn die Abfrage erfolgreich war ($data enthält die Ergebnisdaten des HTTP Aufrufes)
@@ -708,15 +855,15 @@ sub camop_nonbl ($) {
&printlog($hash,$logstr,"1");
$logstr = "--- End Function getcamid nonblocking with error ---";
&printlog($hash,$logstr,"4");
+
+ # ausgeführte Funktion ist abgebrochen, Freigabe Funktionstoken
+ $hash->{HELPER}{ACTIVE} = "off";
+
return;
}
}
else
{
- # die Abfrage konnte nicht ausgeführt werden
- # ausgeführte Funktion ist erledigt
- $hash->{HELPER}{ACTIVE} = "off";
-
# Errorcode aus JSON ermitteln
$errorcode = $data->{'error'}->{'code'};
@@ -734,6 +881,10 @@ sub camop_nonbl ($) {
&printlog($hash,$logstr,"1");
$logstr = "--- End Function getcamid nonblocking with error ---";
&printlog($hash,$logstr,"4");
+
+ # ausgeführte Funktion ist abgebrochen, Freigabe Funktionstoken
+ $hash->{HELPER}{ACTIVE} = "off";
+
return;
}
@@ -753,13 +904,25 @@ sub camop_nonbl ($) {
}
elsif ($OpMode eq "Snap")
{
- # ein Schnappschuß wird gemacht und in SS gespeichert, Rückkehr wird mit "camret_nonbl" verarbeitet
+ # ein Schnappschuß wird ausgelöst und in SS gespeichert, Rückkehr wird mit "camret_nonbl" verarbeitet
$url = "http://$servername:$serverport/webapi/$apitakesnappath?api=\"$apitakesnap\"&dsId=0&method=\"TakeSnapshot\"&version=\"$apitakesnapmaxver\"&camId=$camid&blSave=true&_sid=\"$sid\"";
- }
+ $hash->{STATE} = "snap";
+ readingsSingleUpdate($hash, "LastSnapId", "", 1);
+ }
+ elsif ($OpMode eq "Enable")
+ {
+ # eine Kamera wird aktiviert, Rückkehr wird mit "camret_nonbl" verarbeitet
+ $url = "http://$servername:$serverport/webapi/$apicampath?api=$apicam&version=$apicammaxver&method=Enable&cameraIds=$camid&_sid=\"$sid\"";
+ }
+ elsif ($OpMode eq "Disable")
+ {
+ # eine Kamera wird aktiviert, Rückkehr wird mit "camret_nonbl" verarbeitet
+ $url = "http://$servername:$serverport/webapi/$apicampath?api=$apicam&version=$apicammaxver&method=Disable&cameraIds=$camid&_sid=\"$sid\"";
+ }
$param = {
url => $url,
- timeout => 5,
+ timeout => 10,
hash => $hash,
method => "GET",
header => "Accept: application/json",
@@ -780,7 +943,7 @@ sub camop_nonbl ($) {
sub camret_nonbl ($) {
my ($param, $err, $myjson) = @_;
my $hash = $param->{hash};
- my $device = $hash->{NAME};
+ my $name = $hash->{NAME};
my $servername = $hash->{SERVERNAME};
my $serverport = $hash->{SERVERPORT};
my $camname = $hash->{CAMNAME};
@@ -805,7 +968,12 @@ sub camret_nonbl ($) {
&printlog($hash,$logstr,"1"); # Eintrag fürs Log
$logstr = "--- End Function cam: $OpMode nonblocking with error ---";
&printlog($hash,$logstr,"4");
+
readingsSingleUpdate($hash, "Error", $err, 1); # Readings erzeugen
+
+ # ausgeführte Funktion ist abgebrochen, Freigabe Funktionstoken
+ $hash->{HELPER}{ACTIVE} = "off";
+
return;
}
elsif ($myjson ne "") # wenn die Abfrage erfolgreich war ($data enthält die Ergebnisdaten des HTTP Aufrufes)
@@ -832,10 +1000,10 @@ sub camret_nonbl ($) {
&printlog($hash,$logstr,"4");
if ($OpMode eq "Start")
- {
+ {
# bedingt Browseraktualisierung und Status der "Lampen"
$hash->{STATE} = "on";
-
+
# Setreading
readingsBeginUpdate($hash);
readingsBulkUpdate($hash,"Record","Start");
@@ -870,10 +1038,7 @@ sub camret_nonbl ($) {
readingsBulkUpdate($hash,"Error","none");
readingsEndUpdate($hash, 1);
- # Generiert das Ereignis "off", bedingt Browseraktualisierung und Status der "Lampen" wenn kein longpoll=1
- # { fhem "trigger $device off" }
-
- RemoveInternalTimer($hash);
+ # RemoveInternalTimer($hash);
# Logausgabe
$logstr = "Camera $camname Recording stopped";
@@ -883,7 +1048,15 @@ sub camret_nonbl ($) {
}
elsif ($OpMode eq "Snap")
{
- # ein Schnapschuß wurde aufgenommen
+ # ein Schnapschuß wurde aufgenommen
+ # falls Aufnahme noch läuft -> STATE = on setzen
+ if (ReadingsVal("$name", "Record", "Stop") eq "Start") {
+ $hash->{STATE} = "on";
+ }
+ else
+ {
+ $hash->{STATE} = "off";
+ }
$snapid = $data->{data}{'id'};
@@ -893,13 +1066,49 @@ sub camret_nonbl ($) {
readingsBulkUpdate($hash,"Error","none");
readingsBulkUpdate($hash,"LastSnapId",$snapid);
readingsEndUpdate($hash, 1);
-
+
# Logausgabe
$logstr = "Snapshot of Camera $camname has been done successfully";
&printlog($hash,$logstr,"3");
$logstr = "--- End Function cam: $OpMode nonblocking ---";
&printlog($hash,$logstr,"4");
}
+ elsif ($OpMode eq "Enable")
+ {
+ # Kamera wurde aktiviert
+ $hash->{STATE} = "enable";
+
+ # Setreading
+ readingsBeginUpdate($hash);
+ readingsBulkUpdate($hash,"Availability","enabled");
+ readingsBulkUpdate($hash,"Errorcode","none");
+ readingsBulkUpdate($hash,"Error","none");
+ readingsEndUpdate($hash, 1);
+
+ # Logausgabe
+ $logstr = "Camera $camname has been enabled successfully";
+ &printlog($hash,$logstr,"3");
+ $logstr = "--- End Function cam: $OpMode nonblocking ---";
+ &printlog($hash,$logstr,"4");
+ }
+ elsif ($OpMode eq "Disable")
+ {
+ # Kamera wurde deaktiviert
+ $hash->{STATE} = "disable";
+
+ # Setreading
+ readingsBeginUpdate($hash);
+ readingsBulkUpdate($hash,"Availability","disabled");
+ readingsBulkUpdate($hash,"Errorcode","none");
+ readingsBulkUpdate($hash,"Error","none");
+ readingsEndUpdate($hash, 1);
+
+ # Logausgabe
+ $logstr = "Camera $camname has been disabled successfully";
+ &printlog($hash,$logstr,"3");
+ $logstr = "--- End Function cam: $OpMode nonblocking ---";
+ &printlog($hash,$logstr,"4");
+ }
}
else
{
@@ -917,14 +1126,14 @@ sub camret_nonbl ($) {
readingsEndUpdate($hash, 1);
# Logausgabe
- $logstr = "ERROR - Operationmode $OpMode of Camera $camname was not successful. Errorcode: $errorcode - $error";
+ $logstr = "ERROR - Operation $OpMode of Camera $camname was not successful. Errorcode: $errorcode - $error";
&printlog($hash,$logstr,"1");
$logstr = "--- End Function cam: $OpMode nonblocking with error ---";
&printlog($hash,$logstr,"4");
- # ausgeführte Funktion ist erledigt
+ # ausgeführte Funktion ist abgebrochen, Freigabe Funktionstoken
$hash->{HELPER}{ACTIVE} = "off";
-
+
return;
}
@@ -937,7 +1146,7 @@ sub camret_nonbl ($) {
$param = {
url => $url,
- timeout => 5,
+ timeout => 10,
hash => $hash,
method => "GET",
header => "Accept: application/json",
@@ -964,7 +1173,7 @@ sub logout_nonbl ($) {
my $error;
my $errorcode;
- # ausgeführte Funktion ist erledigt
+ # ausgeführte Funktion ist erledigt (auch wenn logout nicht erfolgreich), Freigabe Funktionstoken
$hash->{HELPER}{ACTIVE} = "off";
if($err ne "") # wenn ein Fehler bei der HTTP Abfrage aufgetreten ist
@@ -973,7 +1182,9 @@ sub logout_nonbl ($) {
&printlog($hash,$logstr,"1"); # Eintrag fürs Log
$logstr = "--- End Function logout nonblocking with error ---";
&printlog($hash,$logstr,"4");
- readingsSingleUpdate($hash, "Error", $err, 1); # Readings erzeugen
+
+ readingsSingleUpdate($hash, "Error", $err, 1); # Readings erzeugen
+
return;
}
elsif($myjson ne "") # wenn die Abfrage erfolgreich war ($data enthält die Ergebnisdaten des HTTP Aufrufes)
@@ -1156,18 +1367,25 @@ return;
Using this Module you are able to operate with cameras which are defined in Synology Surveillance Station.
At present the following functions are available:
-
-
"on": | startet eine Aufnahme. Die Aufnahme wird automatisch nach Ablauf der Zeit <RecordTime> gestoppt. |
"off" : | stoppt eine laufende Aufnahme manuell oder durch die Nutzung anderer Events (z.B. über at, notify) |
"snap": | löst einen Schnappschuß der entsprechenden Kamera aus und speichert ihn in der Synology Surveillance Station |
"disable": | deaktiviert eine Kamera in der Synology Surveillance Station |
"enable": | aktiviert eine Kamera in der Synology Surveillance Station |