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;
use GPUtils qw(GP_Import GP_Export);
# use GPUtils qw(GP_Import GP_Export); hast Du auch weiter unten stehen
use strict;
use warnings;
@ -69,7 +70,9 @@ eval {
# Import von Funktionen und/oder Variablen aus der FHEM main
# man kann ::Funktionaname wählen und sich so den Import schenken. Variablen sollten aber
# 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
#-- Run before package compilation
@ -104,7 +107,6 @@ GP_Export(
)
);
my %gets = (
update => "noArg",
health => "noArg",
@ -135,7 +137,6 @@ my %sets = (
deactivateTimer => "",
);
## Datapoint, all behind API URI
my %dpoints = (
getOAuthToken => 'accounts/login',
@ -160,8 +161,10 @@ my %dpoints = (
setPauseCharging => 'chargers/#ChargerID#/commands/pause_charging',
setResumeCharging => 'chargers/#ChargerID#/commands/resume_charging',
setToggleCharging => 'chargers/#ChargerID#/commands/toggle_charging',
setOverrideChargingSchedule => 'chargers/#ChargerID#/commands/override_schedule',
setPairRFIDTag => 'chargers/#ChargerID#/commands/set_rfid_pairing_mode_async',
setOverrideChargingSchedule =>
'chargers/#ChargerID#/commands/override_schedule',
setPairRFIDTag =>
'chargers/#ChargerID#/commands/set_rfid_pairing_mode_async',
changeChargerSettings => 'chargers/#ChargerID#/settings',
setChargingPrice => 'sites/#SiteID#/price',
);
@ -226,7 +229,6 @@ my %commandCodes = (
69 => "Set RFID Pairing Mode",
);
#Private function to evaluate command-lists
# private funktionen beginnen immer mit _
@ -238,14 +240,16 @@ sub _GetCmdList {
my $name = $hash->{NAME};
#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
my $retVal;
foreach my $mySet ( keys %cmdArray ) {
#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;
#get options
@ -259,12 +263,15 @@ sub _GetCmdList {
#Log3 ($name, 5, "parse cmd-table - Set:$mySet, Option:$myOpt, RetVal:$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 $retVal;
return "Unknown argument $cmd, choose one of " . $retVal;
# versuche wo wenig wie möglich if else zu verwenden.
# else {
# $retVal = "Unknown argument $cmd, choose one of " . $retVal;
# }
}
sub Initialize {
@ -309,13 +316,14 @@ sub Define {
}
#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];
$hash->{Username} = $username;
}
else {
$errmsg
= "specify valid email address within the field username. Format: define <name> EaseeWallbox <username> <password> [interval]";
$errmsg =
"specify valid email address within the field username. Format: define <name> EaseeWallbox <username> <password> [interval]";
Log3 $name, 1, "EaseeWallbox $name: " . $errmsg;
return $errmsg;
}
@ -342,12 +350,13 @@ sub Define {
#If not an integer abort with failure.
my $interval = 60;
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];
}
else {
$errmsg
= "Specify valid integer value for interval. Whole numbers > 5 only. Format: define <name> EaseeWallbox <username> <password> [interval]";
$errmsg =
"Specify valid integer value for interval. Whole numbers > 5 only. Format: define <name> EaseeWallbox <username> <password> [interval]";
Log3 $name, 1, "EaseeWallbox $name: " . $errmsg;
return $errmsg;
}
@ -361,16 +370,22 @@ sub Define {
#Initial load of data
WriteToCloudAPI( $hash, 'getChargers', 'GET' );
Log3 $name, 1, sprintf("EaseeWallbox_Define %s: Starting timer with interval %s", $name, InternalVal($name,'INTERVAL', undef));
InternalTimer(gettimeofday()+ InternalVal($name,'INTERVAL', undef), "FHEM::EaseeWallbox::UpdateDueToTimer", $hash) if (defined $hash);
return undef;
Log3 $name, 1,
sprintf( "EaseeWallbox_Define %s: Starting timer with interval %s",
$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 {
my ( $hash, $arg ) = @_;
RemoveInternalTimer($hash);
return undef;
return;
}
sub Get {
@ -389,7 +404,7 @@ sub Get {
WriteToCloudAPI( $hash, 'getChargers', 'GET' ) if $opt eq "charger";
RefreshData($hash) if $opt eq "update";
delete $hash->{LOCAL};
return undef;
return;
}
sub Set {
@ -399,29 +414,38 @@ sub Set {
my $opt = shift @param;
my $value = join( "", @param );
my %message;
#create response, if cmd is wrong or gui asks
my $cmdTemp = _GetCmdList( $hash, $opt, \%sets );
return $cmdTemp if ( defined($cmdTemp) );
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);
Log3 $name, 1,
"EaseeWallbox_Set $name: Stopped the timer to automatically update readings";
readingsSingleUpdate( $hash, 'state', 'Initialized', 0 );
return undef;
return;
}
elsif ( $opt eq "activateTimer" ) {
#Update once manually and then start the timer
RemoveInternalTimer($hash);
$hash->{LOCAL} = 1;
RefreshData($hash);
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 );
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;
$interval = 60 unless defined($interval);
@ -431,75 +455,85 @@ sub Set {
$hash->{INTERVAL} = $interval;
}
elsif ( $opt eq "cableLock" ) {
my %message;
$message{'state'} = $value;
WriteToCloudAPI($hash, 'setCableLockState', 'POST', \%message)
WriteToCloudAPI( $hash, 'setCableLockState', 'POST', \%message );
}
elsif ( $opt eq "pricePerKWH" ) {
my %message;
$message{'currencyId'} = "EUR";
$message{'vat'} = "19";
$message{'costPerKWh'} = shift @param;
WriteToCloudAPI($hash, 'setChargingPrice', 'POST', \%message)
WriteToCloudAPI( $hash, 'setChargingPrice', 'POST', \%message );
}
elsif ( $opt eq "pairRfidTag" ) {
my $timeout = shift @param;
#if (defined $timeout and /^\d+$/) { print "is a whole number\n" }
$timeout = '60' if not defined $timeout or $timeout = '';
my %message;
$message{'timeout'} = "60";
WriteToCloudAPI($hash, 'setPairRFIDTag', 'POST', \%message)
WriteToCloudAPI( $hash, 'setPairRFIDTag', 'POST', \%message );
}
elsif ( $opt eq "enableSmartCharging" ) {
my %message;
$message{'smartCharging'} = shift @param;
WriteToCloudAPI($hash, 'changeChargerSettings', 'POST', \%message)
WriteToCloudAPI( $hash, 'changeChargerSettings', 'POST', \%message );
}
elsif ( $opt eq "enabled" ) {
my %message;
$message{'enabled'} = "true";
WriteToCloudAPI($hash, 'changeChargerSettings', 'POST', \%message)
WriteToCloudAPI( $hash, 'changeChargerSettings', 'POST', \%message );
}
elsif ( $opt eq "disabled" ) {
my %message;
$message{'enabled'} = "false";
WriteToCloudAPI($hash, 'changeChargerSettings', 'POST', \%message)
WriteToCloudAPI( $hash, 'changeChargerSettings', 'POST', \%message );
}
elsif ( $opt eq "authorizationRequired" ) {
my %message;
$message{'authorizationRequired'} = shift @param;
WriteToCloudAPI($hash, 'changeChargerSettings', 'POST', \%message)
WriteToCloudAPI( $hash, 'changeChargerSettings', 'POST', \%message );
}
elsif ( $opt eq "enableSmartButton" ) {
my %message;
$message{'smartButtonEnabled'} = shift @param;
WriteToCloudAPI($hash, 'changeChargerSettings', 'POST', \%message)
WriteToCloudAPI( $hash, 'changeChargerSettings', 'POST', \%message );
}
elsif ( $opt eq "ledStripBrightness" ) {
my %message;
$message{'ledStripBrightness'} = shift @param;
WriteToCloudAPI($hash, 'changeChargerSettings', 'POST', \%message)
WriteToCloudAPI( $hash, 'changeChargerSettings', 'POST', \%message );
}
else
{
else {
$hash->{LOCAL} = 1;
WriteToCloudAPI($hash, 'setStartCharging', 'POST') if $opt eq "startCharging";
WriteToCloudAPI($hash, 'setStopCharging', 'POST') if $opt eq 'stopCharging';
WriteToCloudAPI($hash, 'setPauseCharging', 'POST') if $opt eq 'pauseCharging';
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, 'setStartCharging', 'POST' )
if $opt eq "startCharging";
WriteToCloudAPI( $hash, 'setStopCharging', 'POST' )
if $opt eq 'stopCharging';
WriteToCloudAPI( $hash, 'setPauseCharging', 'POST' )
if $opt eq 'pauseCharging';
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';
_loadToken($hash) if $opt eq 'refreshToken';
delete $hash->{LOCAL};
}
readingsSingleUpdate( $hash, 'state', 'Initialized', 0 );
return undef;
readingsSingleUpdate( $hash, 'state', 'Initialized', 0 )
; # 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 {
my ( $cmd, $name, $attrName, $attrVal ) = @_;
# hier kannst Du das setzen des Intervals umsetzen
return;
}
@ -512,9 +546,11 @@ sub RefreshData{
WriteToCloudAPI( $hash, 'getChargerConfiguration', 'GET' );
WriteToCloudAPI( $hash, 'getChargerSessionsMonthly', 'GET' );
WriteToCloudAPI( $hash, 'getChargerSessionsDaily', 'GET' );
return; # immer mit einem return eine funktion beenden
}
sub UpdateDueToTimer($) {
sub UpdateDueToTimer {
my ($hash) = @_;
my $name = $hash->{NAME};
@ -522,11 +558,13 @@ sub UpdateDueToTimer($) {
#must be set before call ($hash->{LOCAL} = 1) and removed after (delete $hash->{LOCAL};)
if ( !$hash->{LOCAL} ) {
RemoveInternalTimer($hash);
#Log3 "Test", 1, Dumper($hash);
InternalTimer(
gettimeofday() + InternalVal( $name, 'INTERVAL', undef ), "FHEM::EaseeWallbox::UpdateDueToTimer", $hash );
InternalTimer( gettimeofday() + InternalVal( $name, 'INTERVAL', undef ),
"FHEM::EaseeWallbox::UpdateDueToTimer", $hash );
}
RefreshData($hash);
return RefreshData($hash);
}
sub WriteToCloudAPI {