Refactor language handling and cleanup in 77_UWZ.pm

This commit includes several improvements and refactoring to the
77_UWZ.pm file. The primary changes involve unifying language
handling by replacing direct comparisons of the country code with
a call to a new utility function, `contains_string`, which checks
if the `CountryCode` exists within an array. This change enhances
the readability and maintainability of the code.

Additionally, certain code cleanup has been performed, including
minor formatting adjustments for better alignment and spacing,
removing deprecated warning pragmas, and correcting the 'Mäy'
month name spelling in the English months array.

These changes do not introduce any breaking changes, but they
improve the overall structure and consistency of the code, making
future modifications and updates easier.
This commit is contained in:
2025-10-14 04:50:00 +02:00
parent e25ded22b9
commit a469315070

166
77_UWZ.pm
View File

@@ -35,7 +35,7 @@
#
#
#
# $Id$
# $Id: 77_UWZ.pm 25306 2021-12-06 05:27:48Z CoolTux $
#
####################################################################################################
# also a thanks goes to hexenmeister
@@ -55,8 +55,7 @@ use Encode qw(encode_utf8);
no
if $] >= 5.017011,
warnings => 'experimental::lexical_subs',
'experimental::smartmatch';
warnings => 'experimental::lexical_subs';
my $missingModul;
eval 'use LWP::UserAgent;1' or $missingModul .= 'LWP::UserAgent ';
@@ -170,7 +169,8 @@ BEGIN {
init_done
FW_httpheader
HttpUtils_BlockingGet
deviceEvents)
deviceEvents
contains_string)
);
}
@@ -558,13 +558,13 @@ sub Define {
## URL by CountryCode
my $URL_language = 'en';
if ( $hash->{CountryCode} ~~ [ 'DE', 'AT', 'CH' ] ) {
if ( contains_string( $hash->{CountryCode}, ( 'DE', 'AT', 'CH' ) ) ) {
$URL_language = 'de';
}
if ( $hash->{CountryCode} ~~ ['NL'] ) {
if ( $hash->{CountryCode} eq 'NL' ) {
$URL_language = 'nl';
}
if ( $hash->{CountryCode} ~~ ['FR'] ) {
if ( $hash->{CountryCode} eq 'FR' ) {
$URL_language = 'fr';
}
@@ -626,29 +626,23 @@ sub Set {
my $aArg = shift // return;
my $name = shift @$aArg // return;
my $cmd = shift @$aArg // return qq{"set $name" needs at least one argument};
my $cmd = shift @$aArg
// return qq{"set $name" needs at least one argument};
my $usage = "Unknown argument $cmd, choose one of update:noArg "
if ( ( lc $hash->{CountryCode} ) ne 'search' );
return $usage if ( scalar( @{$aArg} ) != 0 );
given ($cmd) {
when ("?") {
return $usage;
}
when ('update') {
if ( $cmd eq 'update' ) {
Log $hash, 4, 'set command: ' . $cmd;
$hash->{fhem}{LOCAL} = 1;
Start($hash);
$hash->{fhem}{LOCAL} = 0;
}
default {
else { # including $cmd eq '?'
return $usage;
}
}
return;
}
@@ -702,9 +696,10 @@ sub Get {
my $aArg = shift // return;
my $name = shift @$aArg // return;
my $cmd = shift @$aArg // return qq{"get $name" needs at least one argument};
my $cmd = shift @$aArg
// return qq{"get $name" needs at least one argument};
if ( $hash->{CountryCode} ~~ [ 'DE', 'AT', 'CH' ] ) {
if ( contains_string( $hash->{CountryCode}, ( 'DE', 'AT', 'CH' ) ) ) {
my $usage =
"Unknown argument $cmd, choose one of Sturm:noArg Schneefall:noArg Regen:noArg Extremfrost:noArg Waldbrand:noArg Gewitter:noArg Glaette:noArg Hitze:noArg Glatteisregen:noArg Bodenfrost:noArg Hagel:noArg ";
@@ -725,7 +720,7 @@ sub Get {
: $usage;
}
elsif ( $hash->{CountryCode} ~~ ['NL'] ) {
elsif ( $hash->{CountryCode} eq 'NL' ) {
my $usage =
"Unknown argument $cmd, choose one of storm:noArg sneeuw:noArg regen:noArg strenge-vorst:noArg bosbrand:noArg onweer:noArg gladheid:noArg hitte:noArg ijzel:noArg grondvorst:noArg hagel:noArg ";
@@ -746,7 +741,7 @@ sub Get {
: $usage;
}
elsif ( $hash->{CountryCode} ~~ ['FR'] ) {
elsif ( $hash->{CountryCode} eq 'FR' ) {
my $usage =
"Unknown argument $cmd, choose one of tempete:noArg neige:noArg pluie:noArg strenge-vorst:noArg incendie-de-foret:noArg orage:noArg glissange:noArg canicule:noArg verglas:noArg grondvorst:noArg grele:noArg ";
@@ -772,7 +767,9 @@ sub Get {
return $usage if ( scalar( @{$aArg} ) != 1 );
if ( $cmd =~ m{\ASearchAreaID}xms ) { UWZSearchLatLon( $name, $aArg->[0] ); }
if ( $cmd =~ m{\ASearchAreaID}xms ) {
UWZSearchLatLon( $name, $aArg->[0] );
}
elsif ( $cmd =~ m{\AAreaID}xms ) {
my @splitparam = split( /,/, $aArg->[0] );
UWZSearchAreaID( $splitparam[0], $splitparam[1] );
@@ -932,13 +929,13 @@ sub Start {
$URL_language = AttrVal( $hash->{NAME}, 'lang', '' );
}
else {
if ( $hash->{CountryCode} ~~ [ 'DE', 'AT', 'CH' ] ) {
if ( contains_string( $hash->{CountryCode}, ( 'DE', 'AT', 'CH' ) ) ) {
$URL_language = 'de';
}
elsif ( $hash->{CountryCode} ~~ ['NL'] ) {
elsif ( $hash->{CountryCode} eq 'NL' ) {
$URL_language = 'nl';
}
elsif ( $hash->{CountryCode} ~~ ['FR'] ) {
elsif ( $hash->{CountryCode} eq 'FR' ) {
$URL_language = 'fr';
}
}
@@ -1022,12 +1019,17 @@ sub Done {
# Message by CountryCode
$newState = 'Warnings: ' . $values{WarnCount};
$newState = 'Warnungen: ' . $values{WarnCount}
if ( $hash->{CountryCode} ~~ [ 'DE', 'AT', 'CH' ] );
$newState = 'Warnungen: '
. $values{WarnCount}
if (
contains_string(
$hash->{CountryCode}, ( 'DE', 'AT', 'CH' )
)
);
$newState = 'Aantal waarschuwingen: ' . $values{WarnCount}
if ( $hash->{CountryCode} ~~ ['NL'] );
if ( $hash->{CountryCode} eq 'NL' );
$newState = 'Avertissements: ' . $values{WarnCount}
if ( $hash->{CountryCode} ~~ ['FR'] );
if ( $hash->{CountryCode} eq 'FR' );
# end Message by CountryCode
}
@@ -1335,8 +1337,7 @@ sub Run {
. $i
. '_Start_Date|'
. strftime( "%d.%m.%Y",
localtime( $single_warning->{'dtgStart'} ) )
. '|';
localtime( $single_warning->{'dtgStart'} ) ) . '|';
Log $hash, 4,
'Warn_'
@@ -1348,8 +1349,7 @@ sub Run {
. $i
. '_Start_Time|'
. strftime( "%H:%M",
localtime( $single_warning->{'dtgStart'} ) )
. '|';
localtime( $single_warning->{'dtgStart'} ) ) . '|';
Log $hash, 4,
'Warn_'
@@ -1361,8 +1361,7 @@ sub Run {
. $i
. '_End_Date|'
. strftime( "%d.%m.%Y",
localtime( $single_warning->{'dtgEnd'} ) )
. '|';
localtime( $single_warning->{'dtgEnd'} ) ) . '|';
Log $hash, 4,
'Warn_'
@@ -1374,8 +1373,7 @@ sub Run {
. $i
. '_End_Time|'
. strftime( "%H:%M",
localtime( $single_warning->{'dtgEnd'} ) )
. '|';
localtime( $single_warning->{'dtgEnd'} ) ) . '|';
Log $hash, 4,
'Warn_'
@@ -1398,7 +1396,12 @@ sub Run {
. strftime( "%H:%M", localtime($chopcreation) ) . '|';
# Begin Language by AttrVal
if ( $hash->{CountryCode} ~~ [ 'DE', 'AT', 'CH' ] ) {
if (
contains_string(
$hash->{CountryCode}, ( 'DE', 'AT', 'CH' )
)
)
{
Log $hash, 4,
'Warn_'
. $i
@@ -1430,11 +1433,10 @@ sub Run {
. $uwzlevelname{
GetUWZLevel( $hash,
$single_warning->{'payload'}{'levelName'} )
}
. '|';
} . '|';
}
elsif ( $hash->{CountryCode} ~~ ['NL'] ) {
elsif ( $hash->{CountryCode} eq 'NL' ) {
Log $hash, 4,
'Warn_'
. $i
@@ -1469,11 +1471,10 @@ sub Run {
. $uwzlevelname{
GetUWZLevel( $hash,
$single_warning->{'payload'}{'levelName'} )
}
. '|';
} . '|';
}
elsif ( $hash->{CountryCode} ~~ ['FR'] ) {
elsif ( $hash->{CountryCode} eq 'FR' ) {
Log $hash, 4,
'Warn_'
. $i
@@ -1505,8 +1506,7 @@ sub Run {
. $uwzlevelname{
GetUWZLevel( $hash,
$single_warning->{'payload'}{'levelName'} )
}
. '|';
} . '|';
}
else {
@@ -1541,8 +1541,7 @@ sub Run {
. $uwzlevelname{
GetUWZLevel( $hash,
$single_warning->{'payload'}{'levelName'} )
}
. '|';
} . '|';
}
@@ -1591,13 +1590,18 @@ sub Run {
}
else {
# Begin Language by AttrVal
if ( $hash->{CountryCode} ~~ [ 'DE', 'AT', 'CH' ] ) {
if (
contains_string(
$hash->{CountryCode}, ( 'DE', 'AT', 'CH' )
)
)
{
$uclang = 'DE';
}
elsif ( $hash->{CountryCode} ~~ ['NL'] ) {
elsif ( $hash->{CountryCode} eq 'NL' ) {
$uclang = 'NL';
}
elsif ( $hash->{CountryCode} ~~ ['FR'] ) {
elsif ( $hash->{CountryCode} eq 'FR' ) {
$uclang = 'FR';
}
else {
@@ -1690,21 +1694,22 @@ sub Run {
# Begin Language by AttrVal
if ( $hash->{CountryCode} ~~ [ 'DE', 'AT', 'CH' ] ) {
if ( contains_string( $hash->{CountryCode}, ( 'DE', 'AT', 'CH' ) ) )
{
$hagelcount = my @hagelmatch =
$single_warning->{'payload'}{'translationsLongText'}{'DE'} =~
/Hagel/g;
}
elsif ( $hash->{CountryCode} ~~ ['NL'] ) {
elsif ( $hash->{CountryCode} eq 'NL' ) {
$hagelcount = my @hagelmatch =
$single_warning->{'payload'}{'translationsLongText'}{'NL'} =~
/hagel/g;
}
elsif ( $hash->{CountryCode} ~~ ['FR'] ) {
elsif ( $hash->{CountryCode} eq 'FR' ) {
$hagelcount = my @hagelmatch =
$single_warning->{'payload'}{'translationsLongText'}{'FR'} =~
@@ -1749,7 +1754,8 @@ sub Run {
## Begin of redundant Reading
if ( $UWZ_humanreadable eq 1 ) {
if ( $hash->{CountryCode} ~~ [ 'DE', 'AT', 'CH' ] ) {
if ( contains_string( $hash->{CountryCode}, ( 'DE', 'AT', 'CH' ) ) )
{
my %uwzlevelname = (
'0' => 'Stufe Grün (keine Warnung)',
'1' => 'Stufe Dunkelgrün (Wetterhinweise)',
@@ -1762,7 +1768,7 @@ sub Run {
$message .= 'WarnUWZLevel_Str|' . $uwzlevelname{$max} . '|';
}
elsif ( $hash->{CountryCode} ~~ ['NL'] ) {
elsif ( $hash->{CountryCode} eq 'NL' ) {
my %uwzlevelname = (
'0' => 'niveau groen (geen waarschuwingen)',
'1' => 'niveau donkergroen (voorwaarschuwing)',
@@ -1778,7 +1784,7 @@ sub Run {
$message .= 'WarnUWZLevel_Str|' . $uwzlevelname{$max} . '|';
}
elsif ( $hash->{CountryCode} ~~ ['FR'] ) {
elsif ( $hash->{CountryCode} eq 'FR' ) {
my %uwzlevelname = (
'0' => 'niveau vert (aucune alerte)',
'1' => 'niveau vert foncé (indication météo)',
@@ -1863,7 +1869,11 @@ sub UWZAsHtml {
}
}
else {
for ( my $i = 0 ; $i < ReadingsVal( $name, 'WarnCount', 0 ) ; $i++ )
for (
my $i = 0 ;
$i < ReadingsVal( $name, 'WarnCount', 0 ) ;
$i++
)
{
$ret .= UWZHtmlFrame( $hash, 'Warn_' . $i, $attr, 1 );
@@ -1887,13 +1897,13 @@ sub UWZAsHtml {
$ret .= '<tr><td class="uwzIcon" style="vertical-align:top;">';
# language by AttrVal
if ( $hash->{CountryCode} ~~ [ 'DE', 'AT', 'CH' ] ) {
if ( contains_string( $hash->{CountryCode}, ( 'DE', 'AT', 'CH' ) ) ) {
$ret .= '<b>Keine Warnungen</b>';
}
elsif ( $hash->{CountryCode} ~~ ['NL'] ) {
elsif ( $hash->{CountryCode} eq 'NL' ) {
$ret .= '<b>Geen waarschuwingen</b>';
}
elsif ( $hash->{CountryCode} ~~ ['FR'] ) {
elsif ( $hash->{CountryCode} eq 'FR' ) {
$ret .= '<b>Aucune alerte</b>';
}
else {
@@ -1951,8 +1961,11 @@ sub UWZAsHtmlLite {
}
}
else {
for ( my $i = 0 ;
$i < ReadingsVal( $name, 'WarnCount', '' ) ; $i++ )
for (
my $i = 0 ;
$i < ReadingsVal( $name, 'WarnCount', '' ) ;
$i++
)
{
$ret .= UWZHtmlFrame( $hash, 'Warn_' . $i, $attr, 0 );
}
@@ -1974,13 +1987,13 @@ sub UWZAsHtmlLite {
$ret .= '<tr><td class="uwzIcon" style="vertical-align:top;">';
# language by AttrVal
if ( $hash->{CountryCode} ~~ [ 'DE', 'AT', 'CH' ] ) {
if ( contains_string( $hash->{CountryCode}, ( 'DE', 'AT', 'CH' ) ) ) {
$ret .= '<b>Keine Warnungen</b>';
}
elsif ( $hash->{CountryCode} ~~ ['NL'] ) {
elsif ( $hash->{CountryCode} eq 'NL' ) {
$ret .= '<b>Geen waarschuwingen</b>';
}
elsif ( $hash->{CountryCode} ~~ ['FR'] ) {
elsif ( $hash->{CountryCode} eq 'FR' ) {
$ret .= '<b>Aucune alerte</b>';
}
else {
@@ -2066,13 +2079,13 @@ sub UWZAsHtmlMovie {
}
else {
# language by AttrVal
if ( $hash->{CountryCode} ~~ [ 'DE', 'AT', 'CH' ] ) {
if ( contains_string( $hash->{CountryCode}, ( 'DE', 'AT', 'CH' ) ) ) {
$ret .= 'unbekannte Landbezeichnung';
}
elsif ( $hash->{CountryCode} ~~ ['NL'] ) {
elsif ( $hash->{CountryCode} eq 'NL' ) {
$ret .= 'Onbekende landcode';
}
elsif ( $hash->{CountryCode} ~~ ['FR'] ) {
elsif ( $hash->{CountryCode} eq 'FR' ) {
$ret .= 'code de pays inconnu';
}
else {
@@ -2107,13 +2120,13 @@ sub UWZAsHtmlKarteLand {
}
else {
# language by AttrVal
if ( $hash->{CountryCode} ~~ [ 'DE', 'AT', 'CH' ] ) {
if ( contains_string( $hash->{CountryCode}, ( 'DE', 'AT', 'CH' ) ) ) {
$ret .= 'unbekannte Landbezeichnung';
}
elsif ( $hash->{CountryCode} ~~ ['NL'] ) {
elsif ( $hash->{CountryCode} eq 'NL' ) {
$ret .= 'onbekende landcode';
}
elsif ( $hash->{CountryCode} ~~ ['FR'] ) {
elsif ( $hash->{CountryCode} eq 'FR' ) {
$ret .= 'code de pays inconnu';
}
else {
@@ -2183,7 +2196,7 @@ sub UWZHtmlTimestamp {
if ( length($min) == 1 ) { $min = "0$min"; }
# language by AttrVal
if ( $hash->{CountryCode} ~~ [ 'DE', 'AT', 'CH' ] ) {
if ( contains_string( $hash->{CountryCode}, ( 'DE', 'AT', 'CH' ) ) ) {
$ret .=
"<td><b>$DEText[$StartEnd]</b></td><td>"
. "$DEweekdays[$wday], $mday $DEmonths[$mon] "
@@ -2191,7 +2204,7 @@ sub UWZHtmlTimestamp {
. " $hour:$min "
. "$DEText[2]</td>";
}
elsif ( $hash->{CountryCode} ~~ ['NL'] ) {
elsif ( $hash->{CountryCode} eq 'NL' ) {
$ret .=
"<td><b>$NLText[$StartEnd]</b></td><td>"
. "$NLweekdays[$wday], $mday $NLmonths[$mon] "
@@ -2199,7 +2212,7 @@ sub UWZHtmlTimestamp {
. " $hour:$min "
. "$NLText[2]</td>";
}
elsif ( $hash->{CountryCode} ~~ ['FR'] ) {
elsif ( $hash->{CountryCode} eq 'FR' ) {
$ret .=
"<td><b>$FRText[$StartEnd]</b></td><td>"
. "$FRweekdays[$wday], $mday $FRmonths[$mon] "
@@ -2378,8 +2391,7 @@ sub UWZSearchLatLon {
'<a href="/fhem?cmd=get%20'
. $name
. '%20AreaID%20'
. $value->{'latitude'}
. ','
. $value->{'latitude'} . ','
. $value->{'longitude'}
. $::FW_CSRF
. '">Get AreaID</a>';