2021-05-17 20:21:28 +02:00

260 lines
6.3 KiB
Perl

###############################################################################
#
# Developed with love
#
# (c) 2021-2021 Copyright: Sebastian Schwarz
#
# This script is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
#
# The GNU General Public License can be found at
# http://www.gnu.org/copyleft/gpl.html.
# A copy is found in the textfile GPL.txt and important notices to the license
# from the author is found in LICENSE.txt distributed with these scripts.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#
# $Id$
#
###############################################################################
package FHEM::Hailo::Libero;
use strict;
use warnings;
#use POSIX;
use FHEM::Meta;
use GPUtils qw(GP_Import);
use HttpUtils;
use FHEM::Core::Authentication::Passwords qw(:ALL);
my $missingModul = '';
## Import der FHEM Funktionen
#-- Run before package compilation
BEGIN {
# Import from main context
GP_Import(
qw(readingsSingleUpdate
readingsBulkUpdate
readingsBulkUpdateIfChanged
readingsBeginUpdate
readingsEndUpdate
Log3
CommandAttr
AttrVal
ReadingsVal
CommandDefMod
modules
setKeyValue
getKeyValue
getUniqueId
RemoveInternalTimer
InternalTimer
defs
init_done
IsDisabled
deviceEvents
HttpUtils_NonblockingGet
gettimeofday
Dispatch)
);
}
sub Define {
my $hash = shift // return;
my $aArg = shift // return;
return $@ unless ( FHEM::Meta::SetInternals($hash) );
use version 0.60; our $VERSION = FHEM::Meta::Get( $hash, 'version' );
return q{too few parameters: define <NAME> HailoLibero}
if ( scalar( @{$aArg} ) != 2 );
my $name = shift @$aArg;
$hash->{VERSION} = version->parse($VERSION)->normal;
CommandAttr( undef, $name . ' room Hailo' )
if ( AttrVal( $name, 'room', 'none' ) eq 'none' );
readingsSingleUpdate( $hash, 'state', 'initialized', 1 );
Log3($name, 3, qq{HailoLibero ($name) - defined HailoLibero});
### create password object to handle pass keystore
$hash->{helper}->{passObj} = FHEM::Core::Authentication::Passwords->new();
return;
}
sub Undef {
my $hash = shift;
my $name = shift;
$hash->{helper}->{passObj}->setDeletePassword($name);
return;
}
sub Attr {
my ( $cmd, $name, $attrName, $attrVal ) = @_;
my $hash = $defs{$name};
if ( $attrName eq 'disable' ) {
if ( $cmd eq 'set' && $attrVal eq '1' ) {
RemoveInternalTimer($hash);
readingsSingleUpdate( $hash, 'state', 'inactive', 1 );
Log3($name, 3, qq{HailoLibero ($name) - disabled});
}
elsif ( $cmd eq 'del' ) {
readingsSingleUpdate( $hash, 'state', 'active', 1 );
Log3($name, 3, qq{HailoLibero ($name) - enabled});
}
}
return;
}
sub Set {
my $hash = shift // return;
my $aArg = shift // return;
my $name = shift @$aArg // return;
my $cmd = shift @$aArg // return qq{"set $name" needs at least one argument};
# Das Argument für das Passwort, also das Passwort an sich darf keine = enthalten!!!
if ( lc $cmd eq 'hailopwd' ) {
my ($passResp,$passErr);
($passResp,$passErr) = $hash->{helper}->{passObj}->setStorePassword($name,$aArg->[0]);
return qq{error while saving the password - $passErr}
if ( !defined($passResp)
and defined($passErr) );
return q{password successfully saved}
if ( defined($passResp)
and !defined($passErr) );
}
else {
my $list = "hailopwd ";
return "Unknown argument $cmd, choose one of $list";
}
return;
}
sub Delete {
my $hash = shift;
my $name = shift;
$hash->{helper}->{passObj}->setDeletePassword($name);
return;
}
sub Write {
my ( $hash, $payload ) = @_;
my $name = $hash->{NAME};
my ( $session_id, $header, $uri, $method );
( $payload, $session_id, $header, $uri, $method ) =
createHttpValueStrings( $hash, $payload );
HttpUtils_NonblockingGet(
{
url => $hash->{URL} . $uri,
timeout => 15,
hash => $hash,
data => $payload,
method => $method,
header => $header,
doTrigger => 1,
callback => \&ErrorHandling
}
);
Log3( $name, 4,
"HailoLibero ($name) - Send with URL: $hash->{URL}$uri, HEADER: secret!, DATA: secret!, METHOD: $method"
);
# Log3($name, 3,
# "HailoLibero ($name) - Send with URL: $hash->{URL}$uri, HEADER: $header, DATA: $payload, METHOD: $method");
return;
}
sub ErrorHandling {
my $param = shift;
my $err = shift;
my $data = shift;
my $hash = $param->{hash};
my $name = $hash->{NAME};
my $dhash = $hash;
readingsSingleUpdate( $hash, 'state', 'Connected', 1 );
ResponseProcessing( $hash, $data );
return;
}
sub ResponseProcessing {
my $hash = shift;
my $json = shift;
my $name = $hash->{NAME};
Log3 $name, 3, "HailoLibero ($name) - no Match for processing data";
return;
}
sub WriteReadings {
my $hash = shift;
my $decode_json = shift;
my $name = $hash->{NAME};
Log3 $name, 4, "HailoLibero ($name) - readings would be written";
return;
}
####################################
####################################
#### my little helpers Sub's #######
sub Rename {
my $new = shift;
my $old = shift;
return;
}
sub createHttpValueStrings {
my ( $hash, $payload ) = @_;
my $session_id = $hash->{helper}{session_id};
my $header = "Content-Type: application/json";
my $uri = '';
my $method = 'POST';
$payload = '{}' if ( !defined($payload) );
$uri .= '?parms';
return ( $payload, $session_id, $header, $uri, $method );
}
1;