326 lines
8.0 KiB
Perl
326 lines
8.0 KiB
Perl
###############################################################################
|
|
#
|
|
# Developed with love
|
|
#
|
|
# (c) 2022-2022 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::Devices::GoE::GoECharger;
|
|
|
|
use strict;
|
|
use warnings;
|
|
#use POSIX;
|
|
|
|
use Data::Dumper;
|
|
|
|
|
|
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> GoECharger}
|
|
if ( scalar( @{$aArg} ) != 2 );
|
|
|
|
my $name = shift @$aArg;
|
|
$hash->{URL} = AttrVal( $name, 'LiberoIP', 'http://192.168.0.1:81');
|
|
$hash->{INTERVAL} = 60;
|
|
$hash->{NOTIFYDEV} = "global,$name";
|
|
$hash->{VERSION} = version->parse($VERSION)->normal;
|
|
|
|
CommandAttr( undef, $name . ' room GoEChargero' )
|
|
if ( AttrVal( $name, 'room', 'none' ) eq 'none' );
|
|
|
|
readingsSingleUpdate( $hash, 'state', 'initialized', 1 );
|
|
|
|
|
|
Log3($name, 3, qq{GoECharger ($name) - defined GoECharger});
|
|
|
|
### create password object to handle pass keystore
|
|
$hash->{helper}->{passObj} = FHEM::Core::Authentication::Passwords->new($hash->{TYPE});
|
|
|
|
return;
|
|
}
|
|
|
|
sub Undef {
|
|
my $hash = shift;
|
|
my $name = shift;
|
|
|
|
$hash->{helper}->{passObj}->setDeletePassword($name);
|
|
RemoveInternalTimer($hash);
|
|
|
|
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{GoECharger ($name) - disabled});
|
|
}
|
|
elsif ( $cmd eq 'del' ) {
|
|
readingsSingleUpdate( $hash, 'state', 'active', 1 );
|
|
Log3($name, 3, qq{GoECharger ($name) - enabled});
|
|
}
|
|
}
|
|
elsif ( $attrName eq 'interval' ) {
|
|
if ( $cmd eq 'set' ) {
|
|
return 'Interval must be greater than 0'
|
|
if ( $attrVal == 0 );
|
|
RemoveInternalTimer($hash);
|
|
$hash->{INTERVAL} = $attrVal;
|
|
Log3 $name, 3,
|
|
qq{GoECharger ($name) - set interval: $attrVal};
|
|
}
|
|
elsif ( $cmd eq 'del' ) {
|
|
RemoveInternalTimer($hash);
|
|
$hash->{INTERVAL} = 60;
|
|
Log3 $name, 3,
|
|
qq{GoECharger ($name) - delete User interval and set default: 60};
|
|
}
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
sub Notify {
|
|
my $hash = shift // return;
|
|
my $dev = shift // return;
|
|
|
|
my $name = $hash->{NAME};
|
|
return if ( IsDisabled($name) );
|
|
|
|
my $devname = $dev->{NAME};
|
|
my $devtype = $dev->{TYPE};
|
|
my $events = deviceEvents( $dev, 1 );
|
|
|
|
return if ( !$events );
|
|
|
|
// Notify stuff
|
|
|
|
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};
|
|
|
|
if ( lc $cmd eq 'password' ) {
|
|
return qq{error set foo bar first}
|
|
if ( AttrVal( $name, 'foobar', 'none' ) eq 'none' );
|
|
|
|
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) );
|
|
# do stuff
|
|
}
|
|
else {
|
|
my $list = 'password ';
|
|
# more setter if password and foobar available
|
|
$list .= 'foo:noArg bar:noArg'
|
|
if ( defined(ReadPassword( $hash, $name ))
|
|
&& AttrVal( $name, 'foobar', 'none' ) ne 'none'
|
|
);
|
|
|
|
return "Unknown argument $cmd, choose one of $list";
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
sub Delete {
|
|
my $hash = shift;
|
|
my $name = shift;
|
|
|
|
$hash->{helper}->{passObj}->setDeletePassword($name);
|
|
return;
|
|
}
|
|
|
|
sub setSettings {
|
|
my $hash = shift;
|
|
my $name = $hash->{NAME};
|
|
|
|
Write( $hash, 'settings', undef, undef );
|
|
|
|
}
|
|
|
|
sub getSettings {
|
|
my $hash = shift;
|
|
my $name = $hash->{NAME};
|
|
|
|
Write( $hash, undef, undef, undef );
|
|
}
|
|
|
|
sub Write {
|
|
my ( $hash, $payload ) = @_;
|
|
my $name = $hash->{NAME};
|
|
|
|
my ( $header, $uri, $method );
|
|
|
|
( $payload, $header, $uri, $method ) =
|
|
createHttpValueStrings( $hash, $payload );
|
|
|
|
HttpUtils_NonblockingGet(
|
|
{
|
|
url => $hash->{URL} . $uri,
|
|
timeout => 15,
|
|
hash => $hash,
|
|
data => $payload,
|
|
method => $method,
|
|
keepalive => 1,
|
|
header => $header,
|
|
ignoreredirects => 1,
|
|
doTrigger => 1,
|
|
callback => \&ErrorHandling
|
|
}
|
|
);
|
|
|
|
Log3 $name, 4,
|
|
"GoECharger ($name) - Send with URL: $hash->{URL}$uri, HEADER: secret!, DATA: secret!, METHOD: $method";
|
|
|
|
# Log3($name, 3,
|
|
# "GoECharger ($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;
|
|
|
|
Log3 $name, 4, "GoECharger ($name) - ErrorHandling ".$data;
|
|
# add internal timer
|
|
#InternalTimer( gettimeofday() + $hash->{INTERVAL}, "FHEM::Devices::GoE::GoECharger::sub_procudure", $hash )
|
|
|
|
return;
|
|
}
|
|
|
|
sub createHttpValueStrings {
|
|
my ( $hash, $payload ) = @_;
|
|
my $name = $hash->{NAME};
|
|
|
|
my $header = "Content-Type: application/x-www-form-urlencoded";
|
|
my $uri = '';
|
|
my $method = 'POST';
|
|
$payload = '' if (!defined ($payload));
|
|
|
|
return ( $payload, $header, $uri, $method );
|
|
}
|
|
|
|
####################################
|
|
####################################
|
|
#### my little helpers Sub's #######
|
|
|
|
sub ReadPassword {
|
|
my $hash = shift;
|
|
my $name = shift;
|
|
|
|
my $password;
|
|
|
|
Log3 $name, 4, "GoECharger ($name) - Read password from file";
|
|
$password = $hash->{helper}->{passObj}->getReadPassword($name);
|
|
|
|
if ( defined($password) ) {
|
|
return $password;
|
|
} else {
|
|
Log3 $name, 3, "GoECharger ($name) - No password in file";
|
|
return undef;
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
sub Rename {
|
|
my $new = shift;
|
|
my $old = shift;
|
|
|
|
my $hash = $defs{$new};
|
|
|
|
$hash->{helper}->{passObj}->getRename($new,$old);
|
|
return;
|
|
}
|
|
|
|
|
|
1;
|