code cleaning and new code style
change login method
This commit is contained in:
parent
d6254e937b
commit
f734d0e071
@ -2,7 +2,7 @@
|
|||||||
#
|
#
|
||||||
# Developed with Kate
|
# Developed with Kate
|
||||||
#
|
#
|
||||||
# (c) 2017-2019 Copyright: Marko Oldenburg (leongaultier at gmail dot com)
|
# (c) 2017-2021 Copyright: Marko Oldenburg (fhemdevelopment at cooltux dot net)
|
||||||
# All rights reserved
|
# All rights reserved
|
||||||
#
|
#
|
||||||
# This script is free software; you can redistribute it and/or modify
|
# This script is free software; you can redistribute it and/or modify
|
||||||
@ -170,28 +170,27 @@ GP_Export(
|
|||||||
);
|
);
|
||||||
|
|
||||||
my %paths = (
|
my %paths = (
|
||||||
'statussoe' => 'system_status/soe',
|
'statussoe' => 'system_status/soe',
|
||||||
'aggregates' => 'meters/aggregates',
|
'aggregates' => 'meters/aggregates',
|
||||||
'meterssite' => 'meters/site',
|
'meterssite' => 'meters/site',
|
||||||
'meterssolar' => 'meters/solar',
|
'meterssolar' => 'meters/solar',
|
||||||
'siteinfo' => 'site_info',
|
'siteinfo' => 'site_info',
|
||||||
'sitename' => 'site_info/site_name',
|
'sitename' => 'site_info/site_name',
|
||||||
'sitemaster' => 'sitemaster',
|
'sitemaster' => 'sitemaster',
|
||||||
'powerwalls' => 'powerwalls',
|
'powerwalls' => 'powerwalls',
|
||||||
'registration' => 'customer/registration',
|
'registration' => 'customer/registration',
|
||||||
'status' => 'status',
|
'status' => 'status',
|
||||||
'login' => 'login/Basic',
|
'gridstatus' => 'system_status/grid_status',
|
||||||
'gridstatus' => 'system_status/grid_status',
|
|
||||||
);
|
);
|
||||||
|
|
||||||
my %cmdPaths = (
|
my %cmdPaths = (
|
||||||
'powerwallsstop' => 'sitemaster/stop',
|
'powerwallsstop' => 'sitemaster/stop',
|
||||||
'powerwallsrun' => 'sitemaster/run',
|
'powerwallsrun' => 'sitemaster/run',
|
||||||
);
|
);
|
||||||
|
|
||||||
sub Initialize($) {
|
sub Initialize {
|
||||||
|
|
||||||
my ($hash) = @_;
|
my $hash = shift;
|
||||||
|
|
||||||
# Consumer
|
# Consumer
|
||||||
$hash->{GetFn} = 'FHEM::TeslaPowerwall2AC::Get';
|
$hash->{GetFn} = 'FHEM::TeslaPowerwall2AC::Get';
|
||||||
@ -207,9 +206,11 @@ sub Initialize($) {
|
|||||||
return FHEM::Meta::InitMod( __FILE__, $hash );
|
return FHEM::Meta::InitMod( __FILE__, $hash );
|
||||||
}
|
}
|
||||||
|
|
||||||
sub Define($$) {
|
sub Define {
|
||||||
my ( $hash, $def ) = @_;
|
my $hash = shift;
|
||||||
my @a = split( '[ \t][ \t]*', $def );
|
my $def = shift;
|
||||||
|
|
||||||
|
my @a = split( '[ \t][ \t]*', $def );
|
||||||
|
|
||||||
return $@ unless ( FHEM::Meta::SetInternals($hash) );
|
return $@ unless ( FHEM::Meta::SetInternals($hash) );
|
||||||
use version 0.60; our $VERSION = FHEM::Meta::Get( $hash, 'version' );
|
use version 0.60; our $VERSION = FHEM::Meta::Get( $hash, 'version' );
|
||||||
@ -234,9 +235,11 @@ sub Define($$) {
|
|||||||
return undef;
|
return undef;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub Undef($$) {
|
sub Undef {
|
||||||
my ( $hash, $arg ) = @_;
|
my $hash = shift;
|
||||||
my $name = $hash->{NAME};
|
my $arg = shift;
|
||||||
|
|
||||||
|
my $name = $hash->{NAME};
|
||||||
|
|
||||||
RemoveInternalTimer($hash);
|
RemoveInternalTimer($hash);
|
||||||
Log3 $name, 3, "TeslaPowerwall2AC ($name) - Device $name deleted";
|
Log3 $name, 3, "TeslaPowerwall2AC ($name) - Device $name deleted";
|
||||||
@ -244,7 +247,7 @@ sub Undef($$) {
|
|||||||
return undef;
|
return undef;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub Attr(@) {
|
sub Attr {
|
||||||
my ( $cmd, $name, $attrName, $attrVal ) = @_;
|
my ( $cmd, $name, $attrName, $attrVal ) = @_;
|
||||||
my $hash = $defs{$name};
|
my $hash = $defs{$name};
|
||||||
|
|
||||||
@ -304,9 +307,11 @@ sub Attr(@) {
|
|||||||
return undef;
|
return undef;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub Notify($$) {
|
sub Notify {
|
||||||
my ( $hash, $dev ) = @_;
|
my $hash = shift;
|
||||||
my $name = $hash->{NAME};
|
my $dev = shift;
|
||||||
|
|
||||||
|
my $name = $hash->{NAME};
|
||||||
return if ( IsDisabled($name) );
|
return if ( IsDisabled($name) );
|
||||||
|
|
||||||
my $devname = $dev->{NAME};
|
my $devname = $dev->{NAME};
|
||||||
@ -324,8 +329,10 @@ sub Notify($$) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub Get($@) {
|
sub Get {
|
||||||
my ( $hash, $name, $cmd ) = @_;
|
my $hash = shift;
|
||||||
|
my $name = shift;
|
||||||
|
my $cmd = shift;
|
||||||
my $arg;
|
my $arg;
|
||||||
|
|
||||||
if ( $cmd eq 'statusSOE' ) {
|
if ( $cmd eq 'statusSOE' ) {
|
||||||
@ -381,7 +388,7 @@ sub Get($@) {
|
|||||||
return undef;
|
return undef;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub Set($@) {
|
sub Set {
|
||||||
my ( $hash, $name, $cmd, @args ) = @_;
|
my ( $hash, $name, $cmd, @args ) = @_;
|
||||||
my $arg;
|
my $arg;
|
||||||
|
|
||||||
@ -403,7 +410,7 @@ sub Set($@) {
|
|||||||
return undef;
|
return undef;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub Timer_GetData($) {
|
sub Timer_GetData {
|
||||||
my $hash = shift;
|
my $hash = shift;
|
||||||
my $name = $hash->{NAME};
|
my $name = $hash->{NAME};
|
||||||
|
|
||||||
@ -413,12 +420,16 @@ sub Timer_GetData($) {
|
|||||||
and scalar( @{ $hash->{actionQueue} } ) == 0 )
|
and scalar( @{ $hash->{actionQueue} } ) == 0 )
|
||||||
{
|
{
|
||||||
if ( not IsDisabled($name) ) {
|
if ( not IsDisabled($name) ) {
|
||||||
while ( my $obj = each %paths ) {
|
if ( !defined( $hash->{TOKEN}) ) {
|
||||||
unshift( @{ $hash->{actionQueue} }, $obj );
|
unshift( @{ $hash->{actionQueue} }, 'login' );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
while ( my $obj = each %paths ) {
|
||||||
|
unshift( @{ $hash->{actionQueue} }, $obj );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Write($hash);
|
Write($hash);
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
readingsSingleUpdate( $hash, 'state', 'disabled', 1 );
|
readingsSingleUpdate( $hash, 'state', 'disabled', 1 );
|
||||||
@ -431,8 +442,8 @@ sub Timer_GetData($) {
|
|||||||
"TeslaPowerwall2AC ($name) - Call InternalTimer Timer_GetData";
|
"TeslaPowerwall2AC ($name) - Call InternalTimer Timer_GetData";
|
||||||
}
|
}
|
||||||
|
|
||||||
sub Write($) {
|
sub Write {
|
||||||
my ($hash) = @_;
|
my $hash = shift;
|
||||||
my $name = $hash->{NAME};
|
my $name = $hash->{NAME};
|
||||||
|
|
||||||
my ( $uri, $method, $header, $data, $path ) =
|
my ( $uri, $method, $header, $data, $path ) =
|
||||||
@ -464,10 +475,13 @@ sub Write($) {
|
|||||||
Log3 $name, 4, "TeslaPowerwall2AC ($name) - Send with URI: https://$uri";
|
Log3 $name, 4, "TeslaPowerwall2AC ($name) - Send with URI: https://$uri";
|
||||||
}
|
}
|
||||||
|
|
||||||
sub ErrorHandling($$$) {
|
sub ErrorHandling {
|
||||||
my ( $param, $err, $data ) = @_;
|
my $param = shift;
|
||||||
my $hash = $param->{hash};
|
my $err = shift;
|
||||||
my $name = $hash->{NAME};
|
my $data = shift;
|
||||||
|
|
||||||
|
my $hash = $param->{hash};
|
||||||
|
my $name = $hash->{NAME};
|
||||||
|
|
||||||
### Begin Error Handling
|
### Begin Error Handling
|
||||||
|
|
||||||
@ -538,9 +552,12 @@ sub ErrorHandling($$$) {
|
|||||||
ResponseProcessing( $hash, $param->{setCmd}, $data );
|
ResponseProcessing( $hash, $param->{setCmd}, $data );
|
||||||
}
|
}
|
||||||
|
|
||||||
sub ResponseProcessing($$$) {
|
sub ResponseProcessing {
|
||||||
my ( $hash, $path, $json ) = @_;
|
my $hash = shift;
|
||||||
my $name = $hash->{NAME};
|
my $path = shift;
|
||||||
|
my $json = shift;
|
||||||
|
|
||||||
|
my $name = $hash->{NAME};
|
||||||
my $decode_json;
|
my $decode_json;
|
||||||
my $readings;
|
my $readings;
|
||||||
|
|
||||||
@ -570,7 +587,8 @@ sub ResponseProcessing($$$) {
|
|||||||
$readings = ReadingsProcessing_Powerwalls( $hash, $decode_json );
|
$readings = ReadingsProcessing_Powerwalls( $hash, $decode_json );
|
||||||
}
|
}
|
||||||
elsif ( $path eq 'login' ) {
|
elsif ( $path eq 'login' ) {
|
||||||
return $hash->{TOKEN} = $decode_json->{token};
|
$hash->{TOKEN} = $decode_json->{token};
|
||||||
|
return Timer_GetData($hash);
|
||||||
}
|
}
|
||||||
elsif ( $path eq 'meterssite' ) {
|
elsif ( $path eq 'meterssite' ) {
|
||||||
$readings = ReadingsProcessing_Meters_Site( $hash, $decode_json );
|
$readings = ReadingsProcessing_Meters_Site( $hash, $decode_json );
|
||||||
@ -585,9 +603,12 @@ sub ResponseProcessing($$$) {
|
|||||||
WriteReadings( $hash, $path, $readings );
|
WriteReadings( $hash, $path, $readings );
|
||||||
}
|
}
|
||||||
|
|
||||||
sub WriteReadings($$$) {
|
sub WriteReadings {
|
||||||
my ( $hash, $path, $readings ) = @_;
|
my $hash = shift;
|
||||||
my $name = $hash->{NAME};
|
my $path = shift;
|
||||||
|
my $readings = shift;
|
||||||
|
|
||||||
|
my $name = $hash->{NAME};
|
||||||
|
|
||||||
Log3 $name, 4, "TeslaPowerwall2AC ($name) - Write Readings";
|
Log3 $name, 4, "TeslaPowerwall2AC ($name) - Write Readings";
|
||||||
|
|
||||||
@ -610,6 +631,7 @@ sub WriteReadings($$$) {
|
|||||||
) * ReadingsVal( $name, 'statussoe-percentage', 0 )
|
) * ReadingsVal( $name, 'statussoe-percentage', 0 )
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
readingsBulkUpdateIfChanged( $hash, 'actionQueue',
|
readingsBulkUpdateIfChanged( $hash, 'actionQueue',
|
||||||
scalar( @{ $hash->{actionQueue} } ) . ' entries in the Queue' );
|
scalar( @{ $hash->{actionQueue} } ) . ' entries in the Queue' );
|
||||||
readingsBulkUpdateIfChanged(
|
readingsBulkUpdateIfChanged(
|
||||||
@ -623,12 +645,15 @@ sub WriteReadings($$$) {
|
|||||||
. ' paths in actionQueue'
|
. ' paths in actionQueue'
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
readingsEndUpdate( $hash, 1 );
|
readingsEndUpdate( $hash, 1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
sub ReadingsProcessing_Aggregates($$) {
|
sub ReadingsProcessing_Aggregates {
|
||||||
my ( $hash, $decode_json ) = @_;
|
my $hash = shift;
|
||||||
my $name = $hash->{NAME};
|
my $decode_json = shift;
|
||||||
|
|
||||||
|
my $name = $hash->{NAME};
|
||||||
my %readings;
|
my %readings;
|
||||||
|
|
||||||
if ( ref($decode_json) eq 'HASH' ) {
|
if ( ref($decode_json) eq 'HASH' ) {
|
||||||
@ -645,9 +670,11 @@ sub ReadingsProcessing_Aggregates($$) {
|
|||||||
return \%readings;
|
return \%readings;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub ReadingsProcessing_Powerwalls($$) {
|
sub ReadingsProcessing_Powerwalls {
|
||||||
my ( $hash, $decode_json ) = @_;
|
my $hash = shift;
|
||||||
my $name = $hash->{NAME};
|
my $decode_json = shift;
|
||||||
|
|
||||||
|
my $name = $hash->{NAME};
|
||||||
my %readings;
|
my %readings;
|
||||||
|
|
||||||
if ( ref( $decode_json->{powerwalls} ) eq 'ARRAY'
|
if ( ref( $decode_json->{powerwalls} ) eq 'ARRAY'
|
||||||
@ -674,9 +701,11 @@ sub ReadingsProcessing_Powerwalls($$) {
|
|||||||
return \%readings;
|
return \%readings;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub ReadingsProcessing_Site_Info($$) {
|
sub ReadingsProcessing_Site_Info {
|
||||||
my ( $hash, $decode_json ) = @_;
|
my $hash = shift;
|
||||||
my $name = $hash->{NAME};
|
my $decode_json = shift;
|
||||||
|
|
||||||
|
my $name = $hash->{NAME};
|
||||||
my %readings;
|
my %readings;
|
||||||
|
|
||||||
if ( ref($decode_json) eq 'HASH' ) {
|
if ( ref($decode_json) eq 'HASH' ) {
|
||||||
@ -693,9 +722,11 @@ sub ReadingsProcessing_Site_Info($$) {
|
|||||||
return \%readings;
|
return \%readings;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub ReadingsProcessing_Meters_Site($$) {
|
sub ReadingsProcessing_Meters_Site {
|
||||||
my ( $hash, $decode_json ) = @_;
|
my $hash = shift;
|
||||||
my $name = $hash->{NAME};
|
my $decode_json = shift;
|
||||||
|
|
||||||
|
my $name = $hash->{NAME};
|
||||||
my %readings;
|
my %readings;
|
||||||
|
|
||||||
if ( ref($decode_json) eq 'ARRAY'
|
if ( ref($decode_json) eq 'ARRAY'
|
||||||
@ -704,7 +735,7 @@ sub ReadingsProcessing_Meters_Site($$) {
|
|||||||
if ( ref( $decode_json->[0] ) eq 'HASH' ) {
|
if ( ref( $decode_json->[0] ) eq 'HASH' ) {
|
||||||
while ( my $obj = each %{ $decode_json->[0] } ) {
|
while ( my $obj = each %{ $decode_json->[0] } ) {
|
||||||
if ( ref( $decode_json->[0]->{$obj} ) eq 'ARRAY'
|
if ( ref( $decode_json->[0]->{$obj} ) eq 'ARRAY'
|
||||||
or ref( $decode_json->[0]->{$obj} ) eq 'HASH' )
|
|| ref( $decode_json->[0]->{$obj} ) eq 'HASH' )
|
||||||
{
|
{
|
||||||
if ( ref( $decode_json->[0]->{$obj} ) eq 'HASH' ) {
|
if ( ref( $decode_json->[0]->{$obj} ) eq 'HASH' ) {
|
||||||
while ( my ( $r, $v ) =
|
while ( my ( $r, $v ) =
|
||||||
@ -740,9 +771,11 @@ sub ReadingsProcessing_Meters_Site($$) {
|
|||||||
return \%readings;
|
return \%readings;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub ReadingsProcessing_Meters_Solar($$) {
|
sub ReadingsProcessing_Meters_Solar {
|
||||||
my ( $hash, $decode_json ) = @_;
|
my $hash = shift;
|
||||||
my $name = $hash->{NAME};
|
my $decode_json = shift;
|
||||||
|
|
||||||
|
my $name = $hash->{NAME};
|
||||||
my %readings;
|
my %readings;
|
||||||
|
|
||||||
if ( ref($decode_json) eq 'ARRAY'
|
if ( ref($decode_json) eq 'ARRAY'
|
||||||
@ -751,7 +784,7 @@ sub ReadingsProcessing_Meters_Solar($$) {
|
|||||||
if ( ref( $decode_json->[0] ) eq 'HASH' ) {
|
if ( ref( $decode_json->[0] ) eq 'HASH' ) {
|
||||||
while ( my $obj = each %{ $decode_json->[0] } ) {
|
while ( my $obj = each %{ $decode_json->[0] } ) {
|
||||||
if ( ref( $decode_json->[0]->{$obj} ) eq 'ARRAY'
|
if ( ref( $decode_json->[0]->{$obj} ) eq 'ARRAY'
|
||||||
or ref( $decode_json->[0]->{$obj} ) eq 'HASH' )
|
|| ref( $decode_json->[0]->{$obj} ) eq 'HASH' )
|
||||||
{
|
{
|
||||||
if ( ref( $decode_json->[0]->{$obj} ) eq 'HASH' ) {
|
if ( ref( $decode_json->[0]->{$obj} ) eq 'HASH' ) {
|
||||||
while ( my ( $r, $v ) =
|
while ( my ( $r, $v ) =
|
||||||
@ -787,34 +820,30 @@ sub ReadingsProcessing_Meters_Solar($$) {
|
|||||||
return \%readings;
|
return \%readings;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub CreateUri($$) {
|
sub CreateUri {
|
||||||
my ( $hash, $path ) = @_;
|
my $hash = shift;
|
||||||
my $host = $hash->{HOST};
|
my $path = shift;
|
||||||
my $method = 'GET';
|
|
||||||
my $uri;
|
my $host = $hash->{HOST};
|
||||||
my $header;
|
my $header = ( defined($hash->{TOKEN}) ? 'Cookie: AuthCookie=' . $hash->{TOKEN} : undef );
|
||||||
|
my $method = 'GET';
|
||||||
|
my $uri = ( defined($paths{$path}) ? $host . '/api/' . $paths{$path} : undef );
|
||||||
my $data;
|
my $data;
|
||||||
|
|
||||||
if ( $path eq 'powerwallsstop'
|
|
||||||
or $path eq 'powerwallsruns' )
|
if ( $path eq 'login' ) {
|
||||||
|
$method = 'POST';
|
||||||
|
$header = 'Content-Type: application/json';
|
||||||
|
$uri = 'login/Basic',
|
||||||
|
$data = '{"username":"","password":"S'
|
||||||
|
. ReadingsVal( $hash->{NAME},
|
||||||
|
'powerwalls-wall_0_PackageSerialNumber', 0 )
|
||||||
|
. '","force_sm_off":false}';
|
||||||
|
}
|
||||||
|
elsif ( $path eq 'powerwallsstop'
|
||||||
|
|| $path eq 'powerwallsruns' )
|
||||||
{
|
{
|
||||||
$uri = $host . '/api/' . $cmdPaths{$path};
|
$uri = $host . '/api/' . $cmdPaths{$path};
|
||||||
}
|
|
||||||
else {
|
|
||||||
$uri = $host . '/api/' . $paths{$path};
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( $path eq 'sitemasterrun' ) {
|
|
||||||
$header = 'Authorization: Bearer' . $hash->{TOKEN};
|
|
||||||
|
|
||||||
}
|
|
||||||
elsif ( $path eq 'login' ) {
|
|
||||||
$method = 'POST';
|
|
||||||
$header = 'Content-Type: application/json';
|
|
||||||
$data = '{"username":"","password":"S'
|
|
||||||
. ReadingsVal( $hash->{NAME},
|
|
||||||
'powerwalls-wall_0_PackageSerialNumber', 0 )
|
|
||||||
. '","force_sm_off":false}';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ( $uri, $method, $header, $data, $path );
|
return ( $uri, $method, $header, $data, $path );
|
||||||
@ -911,7 +940,7 @@ sub CreateUri($$) {
|
|||||||
],
|
],
|
||||||
"release_status": "under develop",
|
"release_status": "under develop",
|
||||||
"license": "GPL_2",
|
"license": "GPL_2",
|
||||||
"version": "v0.7.3",
|
"version": "v0.7.99",
|
||||||
"author": [
|
"author": [
|
||||||
"Marko Oldenburg <leongaultier@gmail.com>"
|
"Marko Oldenburg <leongaultier@gmail.com>"
|
||||||
],
|
],
|
||||||
|
0
controls_TeslaPowerwall2AC.txt
Normal file
0
controls_TeslaPowerwall2AC.txt
Normal file
37
hooks/pre-commit
Executable file
37
hooks/pre-commit
Executable file
@ -0,0 +1,37 @@
|
|||||||
|
#!/usr/bin/perl -w
|
||||||
|
|
||||||
|
use File::Basename;
|
||||||
|
use POSIX qw(strftime);
|
||||||
|
use strict;
|
||||||
|
|
||||||
|
my @filenames = ( 'FHEM/46_TeslaPowerwall2AC.pm');
|
||||||
|
|
||||||
|
my $controlsfile = 'controls_TeslaPowerwall2AC.txt';
|
||||||
|
|
||||||
|
open(FH, ">$controlsfile") || return("Can't open $controlsfile: $!");
|
||||||
|
|
||||||
|
for my $filename (@filenames) {
|
||||||
|
my @statOutput = stat($filename);
|
||||||
|
|
||||||
|
if (scalar @statOutput != 13) {
|
||||||
|
printf 'error: stat has unexpected return value for ' . $filename . "\n";
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
|
||||||
|
my $mtime = $statOutput[9];
|
||||||
|
my $date = POSIX::strftime("%Y-%m-%d", localtime($mtime));
|
||||||
|
my $time = POSIX::strftime("%H:%M:%S", localtime($mtime));
|
||||||
|
my $filetime = $date."_".$time;
|
||||||
|
|
||||||
|
my $filesize = $statOutput[7];
|
||||||
|
|
||||||
|
printf FH 'UPD ' . $filetime . ' ' . $filesize . ' ' .$filename . "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
close(FH);
|
||||||
|
|
||||||
|
system("git add $controlsfile");
|
||||||
|
|
||||||
|
print 'Create controls File succesfully' . "\n";
|
||||||
|
|
||||||
|
exit 0;
|
Loading…
x
Reference in New Issue
Block a user