From 44613a0c57dcd8079d4af356fb79f24ccaaa306a Mon Sep 17 00:00:00 2001 From: pahenning Date: Sun, 5 Jun 2016 18:46:23 +0000 Subject: [PATCH] 70_DoorPi.pm: Neue Version git-svn-id: https://svn.fhem.de/fhem/trunk@11622 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/contrib/DoorPi/70_DoorPi.pm | 101 ++++++++++++++++++------- fhem/contrib/DoorPi/FHEMHelper.sh.safe | 61 +++++++++++++++ fhem/contrib/DoorPi/doorpi.ini.safe | 86 ++++++++++++--------- 3 files changed, 185 insertions(+), 63 deletions(-) create mode 100644 fhem/contrib/DoorPi/FHEMHelper.sh.safe diff --git a/fhem/contrib/DoorPi/70_DoorPi.pm b/fhem/contrib/DoorPi/70_DoorPi.pm index 6fe135a3d..b54cce0c4 100644 --- a/fhem/contrib/DoorPi/70_DoorPi.pm +++ b/fhem/contrib/DoorPi/70_DoorPi.pm @@ -8,6 +8,7 @@ # # $Id: 70_DoorPi.pm 2016-05 - pahenning $ # +# ######################################################################################## # # This programm is free software; you can redistribute it and/or modify @@ -32,14 +33,15 @@ package main; use strict; use warnings; -use JSON; # imports encode_json, decode_json, to_json and from_json. +use JSON; # imports encode_json, decode_json, to_json and from_json. +use Test::JSON; use vars qw{%attr %defs}; sub Log($$); #-- globals on start -my $version = "1.0beta7"; +my $version = "1.0beta8"; #-- these we may get on request my %gets = ( @@ -225,9 +227,9 @@ sub DoorPi_Set ($@) { #-- commands my $door = AttrVal($name, "doorbutton", "door"); my $doorsubs = "open"; - $doorsubs .= ",lock" + $doorsubs .= ",locked" if(AttrVal($name, "doorlockcmd",undef)); - $doorsubs .= ",unlock" + $doorsubs .= ",unlocked" if(AttrVal($name, "doorunlockcmd",undef)); my @tsubs = (); @@ -260,6 +262,7 @@ sub DoorPi_Set ($@) { $key = shift @a; $value = shift @a; + #Log 1,"[DoorPi_Set] called with key ".$key." and value ".$value; return "[DoorPi_Set] With unknown argument $key, choose one of " . join(" ", @{$hash->{HELPER}->{CMDS}}) if ( !grep( /$key/, @{$hash->{HELPER}->{CMDS}} ) && ($key ne "call") && ($key ne "door") ); @@ -282,6 +285,8 @@ sub DoorPi_Set ($@) { DoorPi_GetHistory($hash); }elsif( $value eq "snapshot" ){ # TODO + }else{ + Log 1,"[DoorPi_Set] $value"; } #-- call target }elsif( $key eq "target" ){ @@ -299,17 +304,35 @@ sub DoorPi_Set ($@) { } #-- door opening - either from FHEM, or just as message from DoorPi }elsif( ($key eq "$door")||($key eq "door") ){ - if( $value eq "open" ){ - $v=DoorPi_Cmd($hash,"door"); + if( $value eq "opened" ){ + $v=DoorPi_Cmd($hash,"dooropen"); + Log 1,"[DoorPi_Set] sent dooropen command to DoorPi"; if(AttrVal($name, "dooropencmd",undef)){ fhem(AttrVal($name, "dooropencmd",undef)); } readingsSingleUpdate($hash,$door,"opened",1); - }elsif( $value eq "opened" ){ - if(AttrVal($name, "dooropencmd",undef)){ - fhem(AttrVal($name, "dooropencmd",undef)); + }elsif( $value eq "locked" ){ + $v=DoorPi_Cmd($hash,"doorlocked"); + #-- careful here - + # a third parameter indicates that the door is already unlocked + if( (AttrVal($name, "doorlockcmd",undef)) && (!$a[0]) ){ + fhem(AttrVal($name, "doorlockcmd",undef)); + Log 1,"[DoorPi_Set] sent doorlocked command to DoorPi and executed extra FHEM doorlock command"; + }else{ + Log 1,"[DoorPi_Set] sent doorlocked command to DoorPi and NOT executed extra FHEM doorlock command"; } - readingsSingleUpdate($hash,$door,"opened",1); + readingsSingleUpdate($hash,$door,"locked",1); + }elsif( $value eq "unlocked" ){ + $v=DoorPi_Cmd($hash,"doorunlocked"); + #-- careful here - + # a third parameter indicates that the door is already unlocked + if( (AttrVal($name, "doorunlockcmd",undef)) && (!$a[0]) ){ + fhem(AttrVal($name, "doorunlockcmd",undef)); + Log 1,"[DoorPi_Set] sent doorunlocked command to DoorPi and executed extra FHEM doorunlock command"; + }else{ + Log 1,"[DoorPi_Set] sent doorunlocked command to DoorPi and NOT executed extra FHEM doorunlock command"; + } + readingsSingleUpdate($hash,$door,"unlocked",1); } #-- snapshot }elsif( $key eq "$snapshot" ){ @@ -406,8 +429,8 @@ sub DoorPi_GetConfig { } #Log 1,"[DoorPi_GetConfig] has obtained data"; - #-- crude test if this is valid JSON or some HTML page - if( substr($status,0,1) eq "<" ){ + #-- test if this is valid JSON + if( !is_valid_json($status) ){ Log 1,"[DoorPi_GetConfig] but data is invalid"; readingsSingleUpdate($hash,"config","invalid data",0); readingsSingleUpdate($hash,"state","Error",1); @@ -431,6 +454,7 @@ sub DoorPi_GetConfig { $hash->{HELPER}->{vkeyboard}=$fskey; $fscmds = $jhash0->{"config"}->{$fskey."_InputPins"}; + my $door = AttrVal($name, "doorbutton", "door"); my $light = AttrVal($name, "lightbutton", "light"); my $dashlight = AttrVal($name, "dashlightbutton", "dashlight"); my $snapshot = AttrVal($name, "snapshotbutton", "snapshot"); @@ -441,8 +465,15 @@ sub DoorPi_GetConfig { foreach my $key (sort(keys $fscmds)) { + #-- check for door buttons + if($key =~ /dooropen/){ + push(@{ $hash->{HELPER}->{CMDS}},"$door"); + }elsif($key =~ /doorlocked/){ + #no need to get these + }elsif($key =~ /doorunlocked/){ + #no need to get these #-- check for stream buttons - if($key =~ /$stream(on)/){ + }elsif($key =~ /$stream(on)/){ push(@{ $hash->{HELPER}->{CMDS}},"$stream"); $son = 1; }elsif($key =~ /$stream(off)/){ @@ -567,14 +598,14 @@ sub DoorPi_GetHistory { } #Log 1,"[DoorPi_GetHistory] has obtained data in two calls"; - #-- crude test if this is valid JSON or some HTML page - if( substr($status1,0,1) eq "<" ){ + #-- test if this is valid JSON + if( !is_valid_json($status1) ){ Log 1,"[DoorPi_GetHistory] but data from first call is invalid"; readingsSingleUpdate($hash,"history","invalid data 1st call",0); readingsSingleUpdate($hash,"state","Error",1); return; } - if( substr($status2,0,1) eq "<" ){ + if( !is_valid_json($status2) ){ Log 1,"[DoorPi_GetHistory] but data from second call is invalid"; readingsSingleUpdate($hash,"history","invalid data 2nd call",0); readingsSingleUpdate($hash,"state","Error",1); @@ -658,8 +689,8 @@ sub DoorPi_GetHistory { my $record = $callrecord; $record =~ s/^.*records\///; #-- workaround for buggy DoorPi - $record = sprintf("%d-%02d-%02d_%02d-%02d-%02d.wav", $year,($month+1),$day,$hour, $min, $sec) - if( $callend eq "ok"); + $record = sprintf("%d-%02d-%02d_%02d-%02d-%02d.wav", $year,($month+1),$day,$hour, $min, $sec); + # if( $callend eq "ok"); #-- this is the snapshot file if taken at the same time my $snapshot = sprintf("%d-%02d-%02d_%02d-%02d-%02d.jpg", $year,($month+1),$day,$hour, $min, $sec); @@ -772,10 +803,10 @@ sub DoorPi_GetHistory { readingsSingleUpdate($hash,"state","Error",1); return; } - #Log 1,"[DoorPi_Cmd] has obtained data"; + #Log 1,"[DoorPi_Cmd] has obtained data $data"; - #-- crude test if this is valid JSON or some HTML page - if( substr($data,0,1) eq "<" ){ + #-- test if this is valid JSON + if( !is_valid_json($data) ){ Log 1,"[DoorPi_Cmd] invalid data"; readingsSingleUpdate($hash,"state","Error",1); return; @@ -992,16 +1023,24 @@ sub DoorPi_makeTable($$$$){ <IP address>
+ Note: The default configuration for the module assumes that opening the door is done by DoorPi + because it controls the local door opener, but locking and unlocking are handled by FHEM. Perl modules JSON and Test:JSON are needed.

Set