applied patch up to WriteToCloud

This commit is contained in:
Matthias Sandmann 2022-03-26 22:48:10 +01:00
parent ff79d0c662
commit fc6e74dd3c

View File

@ -1,5 +1,6 @@
package FHEM::EaseeWallbox; package FHEM::EaseeWallbox;
use GPUtils qw(GP_Import GP_Export);
# use GPUtils qw(GP_Import GP_Export); hast Du auch weiter unten stehen
use strict; use strict;
use warnings; use warnings;
@ -69,7 +70,9 @@ eval {
# Import von Funktionen und/oder Variablen aus der FHEM main # Import von Funktionen und/oder Variablen aus der FHEM main
# man kann ::Funktionaname wählen und sich so den Import schenken. Variablen sollten aber # man kann ::Funktionaname wählen und sich so den Import schenken. Variablen sollten aber
# sauber importiert werden # sauber importiert werden
use GPUtils qw(GP_Import); # use GPUtils qw(GP_Import);
use GPUtils qw(GP_Import GP_Export)
; # da Du beide Funktionen aus dem package verwendest
## Import der FHEM Funktionen ## Import der FHEM Funktionen
#-- Run before package compilation #-- Run before package compilation
@ -104,7 +107,6 @@ GP_Export(
) )
); );
my %gets = ( my %gets = (
update => "noArg", update => "noArg",
health => "noArg", health => "noArg",
@ -135,7 +137,6 @@ my %sets = (
deactivateTimer => "", deactivateTimer => "",
); );
## Datapoint, all behind API URI ## Datapoint, all behind API URI
my %dpoints = ( my %dpoints = (
getOAuthToken => 'accounts/login', getOAuthToken => 'accounts/login',
@ -160,8 +161,10 @@ my %dpoints = (
setPauseCharging => 'chargers/#ChargerID#/commands/pause_charging', setPauseCharging => 'chargers/#ChargerID#/commands/pause_charging',
setResumeCharging => 'chargers/#ChargerID#/commands/resume_charging', setResumeCharging => 'chargers/#ChargerID#/commands/resume_charging',
setToggleCharging => 'chargers/#ChargerID#/commands/toggle_charging', setToggleCharging => 'chargers/#ChargerID#/commands/toggle_charging',
setOverrideChargingSchedule => 'chargers/#ChargerID#/commands/override_schedule', setOverrideChargingSchedule =>
setPairRFIDTag => 'chargers/#ChargerID#/commands/set_rfid_pairing_mode_async', 'chargers/#ChargerID#/commands/override_schedule',
setPairRFIDTag =>
'chargers/#ChargerID#/commands/set_rfid_pairing_mode_async',
changeChargerSettings => 'chargers/#ChargerID#/settings', changeChargerSettings => 'chargers/#ChargerID#/settings',
setChargingPrice => 'sites/#SiteID#/price', setChargingPrice => 'sites/#SiteID#/price',
); );
@ -226,7 +229,6 @@ my %commandCodes = (
69 => "Set RFID Pairing Mode", 69 => "Set RFID Pairing Mode",
); );
#Private function to evaluate command-lists #Private function to evaluate command-lists
# private funktionen beginnen immer mit _ # private funktionen beginnen immer mit _
@ -238,14 +240,16 @@ sub _GetCmdList {
my $name = $hash->{NAME}; my $name = $hash->{NAME};
#return, if cmd is valid #return, if cmd is valid
return undef if ( defined($cmd) and defined( $cmdArray{$cmd} ) ); return if ( defined($cmd) and defined( $cmdArray{$cmd} ) );
#response for gui or the user, if command is invalid #response for gui or the user, if command is invalid
my $retVal; my $retVal;
foreach my $mySet ( keys %cmdArray ) { foreach my $mySet ( keys %cmdArray ) {
#append set-command #append set-command
$retVal = $retVal . " " if ( defined($retVal) ); $retVal = $retVal . " "
if ( defined($retVal) )
; # Macht denke ich keinen Sinn da durch my $retVal bereits $retVal definiert ist
$retVal = $retVal . $mySet; $retVal = $retVal . $mySet;
#get options #get options
@ -259,12 +263,15 @@ sub _GetCmdList {
#Log3 ($name, 5, "parse cmd-table - Set:$mySet, Option:$myOpt, RetVal:$retVal"); #Log3 ($name, 5, "parse cmd-table - Set:$mySet, Option:$myOpt, RetVal:$retVal");
} }
if ( !defined($retVal) ) { if ( !defined($retVal) ) {
$retVal = "error while parsing set-table"; return "error while parsing set-table";
} }
else {
$retVal = "Unknown argument $cmd, choose one of " . $retVal; return "Unknown argument $cmd, choose one of " . $retVal;
}
return $retVal; # versuche wo wenig wie möglich if else zu verwenden.
# else {
# $retVal = "Unknown argument $cmd, choose one of " . $retVal;
# }
} }
sub Initialize { sub Initialize {
@ -309,13 +316,14 @@ sub Define {
} }
#Check if the username is an email address #Check if the username is an email address
if ( $param[2] =~ /^.+@.+$/ ) { if ( $param[2] =~ /^.+@.+$/x )
{ # Regular expression without "/x" flag. See page 236 of PBP (RegularExpressions::RequireExtendedFormatting)
my $username = $param[2]; my $username = $param[2];
$hash->{Username} = $username; $hash->{Username} = $username;
} }
else { else {
$errmsg $errmsg =
= "specify valid email address within the field username. Format: define <name> EaseeWallbox <username> <password> [interval]"; "specify valid email address within the field username. Format: define <name> EaseeWallbox <username> <password> [interval]";
Log3 $name, 1, "EaseeWallbox $name: " . $errmsg; Log3 $name, 1, "EaseeWallbox $name: " . $errmsg;
return $errmsg; return $errmsg;
} }
@ -342,12 +350,13 @@ sub Define {
#If not an integer abort with failure. #If not an integer abort with failure.
my $interval = 60; my $interval = 60;
if ( defined $param[4] ) { if ( defined $param[4] ) {
if ( $param[4] =~ /^\d+$/ ) { if ( $param[4] =~ /^\d+$/x )
{ # Regular expression without "/x" flag. See page 236 of PBP (RegularExpressions::RequireExtendedFormatting)
$interval = $param[4]; $interval = $param[4];
} }
else { else {
$errmsg $errmsg =
= "Specify valid integer value for interval. Whole numbers > 5 only. Format: define <name> EaseeWallbox <username> <password> [interval]"; "Specify valid integer value for interval. Whole numbers > 5 only. Format: define <name> EaseeWallbox <username> <password> [interval]";
Log3 $name, 1, "EaseeWallbox $name: " . $errmsg; Log3 $name, 1, "EaseeWallbox $name: " . $errmsg;
return $errmsg; return $errmsg;
} }
@ -361,16 +370,22 @@ sub Define {
#Initial load of data #Initial load of data
WriteToCloudAPI( $hash, 'getChargers', 'GET' ); WriteToCloudAPI( $hash, 'getChargers', 'GET' );
Log3 $name, 1, sprintf("EaseeWallbox_Define %s: Starting timer with interval %s", $name, InternalVal($name,'INTERVAL', undef)); Log3 $name, 1,
InternalTimer(gettimeofday()+ InternalVal($name,'INTERVAL', undef), "FHEM::EaseeWallbox::UpdateDueToTimer", $hash) if (defined $hash); sprintf( "EaseeWallbox_Define %s: Starting timer with interval %s",
return undef; $name, InternalVal( $name, 'INTERVAL', undef ) );
InternalTimer( gettimeofday() + InternalVal( $name, 'INTERVAL', undef ),
"FHEM::EaseeWallbox::UpdateDueToTimer", $hash )
if ( defined $hash );
## return; sollte es nicht geben, ein return; ist per see mit Rückgabe undef
return;
} }
sub Undef { sub Undef {
my ( $hash, $arg ) = @_; my ( $hash, $arg ) = @_;
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
return undef; return;
} }
sub Get { sub Get {
@ -389,7 +404,7 @@ sub Get {
WriteToCloudAPI( $hash, 'getChargers', 'GET' ) if $opt eq "charger"; WriteToCloudAPI( $hash, 'getChargers', 'GET' ) if $opt eq "charger";
RefreshData($hash) if $opt eq "update"; RefreshData($hash) if $opt eq "update";
delete $hash->{LOCAL}; delete $hash->{LOCAL};
return undef; return;
} }
sub Set { sub Set {
@ -399,29 +414,38 @@ sub Set {
my $opt = shift @param; my $opt = shift @param;
my $value = join( "", @param ); my $value = join( "", @param );
my %message;
#create response, if cmd is wrong or gui asks #create response, if cmd is wrong or gui asks
my $cmdTemp = _GetCmdList( $hash, $opt, \%sets ); my $cmdTemp = _GetCmdList( $hash, $opt, \%sets );
return $cmdTemp if ( defined($cmdTemp) ); return $cmdTemp if ( defined($cmdTemp) );
if ( $opt eq "deactivateTimer" ) { if ( $opt eq "deactivateTimer" ) {
# Cascading if-elsif chain. See pages 117,118 of PBP (ControlStructures::ProhibitCascadingIfElse) kann man anders machen. Später machen wir das
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
Log3 $name, 1, Log3 $name, 1,
"EaseeWallbox_Set $name: Stopped the timer to automatically update readings"; "EaseeWallbox_Set $name: Stopped the timer to automatically update readings";
readingsSingleUpdate( $hash, 'state', 'Initialized', 0 ); readingsSingleUpdate( $hash, 'state', 'Initialized', 0 );
return undef; return;
} }
elsif ( $opt eq "activateTimer" ) { elsif ( $opt eq "activateTimer" ) {
#Update once manually and then start the timer #Update once manually and then start the timer
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
$hash->{LOCAL} = 1; $hash->{LOCAL} = 1;
RefreshData($hash); RefreshData($hash);
delete $hash->{LOCAL}; delete $hash->{LOCAL};
InternalTimer(gettimeofday()+ InternalVal($name,'INTERVAL', undef), "FHEM::EaseeWallbox::UpdateDueToTimer", $hash); InternalTimer( gettimeofday() + InternalVal( $name, 'INTERVAL', undef ),
"FHEM::EaseeWallbox::UpdateDueToTimer", $hash );
readingsSingleUpdate( $hash, 'state', 'Started', 0 ); readingsSingleUpdate( $hash, 'state', 'Started', 0 );
Log3 $name, 1, sprintf("EaseeWallbox_Set %s: Updated readings and started timer to automatically update readings with interval %s", $name, InternalVal($name,'INTERVAL', undef)); Log3 $name, 1,
sprintf(
"EaseeWallbox_Set %s: Updated readings and started timer to automatically update readings with interval %s",
$name, InternalVal( $name, 'INTERVAL', undef ) );
} }
elsif ( $opt eq "interval" ) { elsif ( $opt eq "interval" )
{ # interval wird immer über Attribut gesetzt. Also in die Funktion AttrFn aus Initialize
my $interval = shift @param; my $interval = shift @param;
$interval = 60 unless defined($interval); $interval = 60 unless defined($interval);
@ -431,75 +455,85 @@ sub Set {
$hash->{INTERVAL} = $interval; $hash->{INTERVAL} = $interval;
} }
elsif ( $opt eq "cableLock" ) { elsif ( $opt eq "cableLock" ) {
my %message;
$message{'state'} = $value; $message{'state'} = $value;
WriteToCloudAPI($hash, 'setCableLockState', 'POST', \%message) WriteToCloudAPI( $hash, 'setCableLockState', 'POST', \%message );
} }
elsif ( $opt eq "pricePerKWH" ) { elsif ( $opt eq "pricePerKWH" ) {
my %message;
$message{'currencyId'} = "EUR"; $message{'currencyId'} = "EUR";
$message{'vat'} = "19"; $message{'vat'} = "19";
$message{'costPerKWh'} = shift @param; $message{'costPerKWh'} = shift @param;
WriteToCloudAPI($hash, 'setChargingPrice', 'POST', \%message) WriteToCloudAPI( $hash, 'setChargingPrice', 'POST', \%message );
} }
elsif ( $opt eq "pairRfidTag" ) { elsif ( $opt eq "pairRfidTag" ) {
my $timeout = shift @param; my $timeout = shift @param;
#if (defined $timeout and /^\d+$/) { print "is a whole number\n" } #if (defined $timeout and /^\d+$/) { print "is a whole number\n" }
$timeout = '60' if not defined $timeout or $timeout = ''; $timeout = '60' if not defined $timeout or $timeout = '';
my %message;
$message{'timeout'} = "60"; $message{'timeout'} = "60";
WriteToCloudAPI($hash, 'setPairRFIDTag', 'POST', \%message) WriteToCloudAPI( $hash, 'setPairRFIDTag', 'POST', \%message );
} }
elsif ( $opt eq "enableSmartCharging" ) { elsif ( $opt eq "enableSmartCharging" ) {
my %message;
$message{'smartCharging'} = shift @param; $message{'smartCharging'} = shift @param;
WriteToCloudAPI($hash, 'changeChargerSettings', 'POST', \%message) WriteToCloudAPI( $hash, 'changeChargerSettings', 'POST', \%message );
} }
elsif ( $opt eq "enabled" ) { elsif ( $opt eq "enabled" ) {
my %message;
$message{'enabled'} = "true"; $message{'enabled'} = "true";
WriteToCloudAPI($hash, 'changeChargerSettings', 'POST', \%message) WriteToCloudAPI( $hash, 'changeChargerSettings', 'POST', \%message );
} }
elsif ( $opt eq "disabled" ) { elsif ( $opt eq "disabled" ) {
my %message;
$message{'enabled'} = "false"; $message{'enabled'} = "false";
WriteToCloudAPI($hash, 'changeChargerSettings', 'POST', \%message) WriteToCloudAPI( $hash, 'changeChargerSettings', 'POST', \%message );
} }
elsif ( $opt eq "authorizationRequired" ) { elsif ( $opt eq "authorizationRequired" ) {
my %message;
$message{'authorizationRequired'} = shift @param; $message{'authorizationRequired'} = shift @param;
WriteToCloudAPI($hash, 'changeChargerSettings', 'POST', \%message) WriteToCloudAPI( $hash, 'changeChargerSettings', 'POST', \%message );
} }
elsif ( $opt eq "enableSmartButton" ) { elsif ( $opt eq "enableSmartButton" ) {
my %message;
$message{'smartButtonEnabled'} = shift @param; $message{'smartButtonEnabled'} = shift @param;
WriteToCloudAPI($hash, 'changeChargerSettings', 'POST', \%message) WriteToCloudAPI( $hash, 'changeChargerSettings', 'POST', \%message );
} }
elsif ( $opt eq "ledStripBrightness" ) { elsif ( $opt eq "ledStripBrightness" ) {
my %message;
$message{'ledStripBrightness'} = shift @param; $message{'ledStripBrightness'} = shift @param;
WriteToCloudAPI($hash, 'changeChargerSettings', 'POST', \%message) WriteToCloudAPI( $hash, 'changeChargerSettings', 'POST', \%message );
} }
else else {
{
$hash->{LOCAL} = 1; $hash->{LOCAL} = 1;
WriteToCloudAPI($hash, 'setStartCharging', 'POST') if $opt eq "startCharging"; WriteToCloudAPI( $hash, 'setStartCharging', 'POST' )
WriteToCloudAPI($hash, 'setStopCharging', 'POST') if $opt eq 'stopCharging'; if $opt eq "startCharging";
WriteToCloudAPI($hash, 'setPauseCharging', 'POST') if $opt eq 'pauseCharging'; WriteToCloudAPI( $hash, 'setStopCharging', 'POST' )
WriteToCloudAPI($hash, 'setResumeCharging', 'POST') if $opt eq 'resumeCharging'; if $opt eq 'stopCharging';
WriteToCloudAPI($hash, 'setToggleCharging', 'POST') if $opt eq 'toggleCharging'; WriteToCloudAPI( $hash, 'setPauseCharging', 'POST' )
WriteToCloudAPI($hash, 'setUpdateFirmware', 'POST') if $opt eq 'updateFirmware'; if $opt eq 'pauseCharging';
WriteToCloudAPI($hash, 'setOverrideChargingSchedule', 'POST') if $opt eq 'overrideChargingSchedule'; WriteToCloudAPI( $hash, 'setResumeCharging', 'POST' )
if $opt eq 'resumeCharging';
WriteToCloudAPI( $hash, 'setToggleCharging', 'POST' )
if $opt eq 'toggleCharging';
WriteToCloudAPI( $hash, 'setUpdateFirmware', 'POST' )
if $opt eq 'updateFirmware';
WriteToCloudAPI( $hash, 'setOverrideChargingSchedule', 'POST' )
if $opt eq 'overrideChargingSchedule';
WriteToCloudAPI( $hash, 'setReboot', 'POST' ) if $opt eq 'reboot'; WriteToCloudAPI( $hash, 'setReboot', 'POST' ) if $opt eq 'reboot';
_loadToken($hash) if $opt eq 'refreshToken'; _loadToken($hash) if $opt eq 'refreshToken';
delete $hash->{LOCAL}; delete $hash->{LOCAL};
} }
readingsSingleUpdate( $hash, 'state', 'Initialized', 0 ); readingsSingleUpdate( $hash, 'state', 'Initialized', 0 )
return undef; ; # Die Modulinstanz ist doch nicht erst bei einem set Initialized, das ist doch schon nach dem define. Wenn dann ist hier ein status ala "processing setter" oder so.
return;
} }
sub Attr { sub Attr {
my ( $cmd, $name, $attrName, $attrVal ) = @_; my ( $cmd, $name, $attrName, $attrVal ) = @_;
# hier kannst Du das setzen des Intervals umsetzen
return; return;
} }
@ -512,9 +546,11 @@ sub RefreshData{
WriteToCloudAPI( $hash, 'getChargerConfiguration', 'GET' ); WriteToCloudAPI( $hash, 'getChargerConfiguration', 'GET' );
WriteToCloudAPI( $hash, 'getChargerSessionsMonthly', 'GET' ); WriteToCloudAPI( $hash, 'getChargerSessionsMonthly', 'GET' );
WriteToCloudAPI( $hash, 'getChargerSessionsDaily', 'GET' ); WriteToCloudAPI( $hash, 'getChargerSessionsDaily', 'GET' );
return; # immer mit einem return eine funktion beenden
} }
sub UpdateDueToTimer($) { sub UpdateDueToTimer {
my ($hash) = @_; my ($hash) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
@ -522,11 +558,13 @@ sub UpdateDueToTimer($) {
#must be set before call ($hash->{LOCAL} = 1) and removed after (delete $hash->{LOCAL};) #must be set before call ($hash->{LOCAL} = 1) and removed after (delete $hash->{LOCAL};)
if ( !$hash->{LOCAL} ) { if ( !$hash->{LOCAL} ) {
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
#Log3 "Test", 1, Dumper($hash); #Log3 "Test", 1, Dumper($hash);
InternalTimer( InternalTimer( gettimeofday() + InternalVal( $name, 'INTERVAL', undef ),
gettimeofday() + InternalVal( $name, 'INTERVAL', undef ), "FHEM::EaseeWallbox::UpdateDueToTimer", $hash ); "FHEM::EaseeWallbox::UpdateDueToTimer", $hash );
} }
RefreshData($hash);
return RefreshData($hash);
} }
sub WriteToCloudAPI { sub WriteToCloudAPI {