Unit.pm: scope support

git-svn-id: https://svn.fhem.de/fhem/trunk@12571 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
jpawlowski 2016-11-14 04:28:28 +00:00
parent 6f16177fd6
commit b720f9efaa

View File

@ -559,7 +559,7 @@ my $rtypes = {
nl => [ 'error', 'on' ], nl => [ 'error', 'on' ],
pl => [ 'error', 'on' ], pl => [ 'error', 'on' ],
}, },
scope => [ 'nok|error|0/nok', 'ok|1/ok' ], scope => [ '^(nok|error|0)$', '^(ok|1)$' ],
}, },
onoff => { onoff => {
@ -571,7 +571,7 @@ my $rtypes = {
nl => [ 'off', 'on' ], nl => [ 'off', 'on' ],
pl => [ 'off', 'on' ], pl => [ 'off', 'on' ],
}, },
scope => [ 'off|0', 'on|1' ], scope => [ '^(off|0)$', '^(on|1)$' ],
}, },
bool => { bool => {
@ -583,7 +583,7 @@ my $rtypes = {
nl => [ 'false', 'true' ], nl => [ 'false', 'true' ],
pl => [ 'false', 'true' ], pl => [ 'false', 'true' ],
}, },
scope => [ '0|false', '1|true' ], scope => [ '^(0|false)$', '^(1|true)$' ],
}, },
epoch => { epoch => {
@ -607,14 +607,14 @@ my $rtypes = {
nl => 'time hh:mm', nl => 'time hh:mm',
pl => 'time hh:mm', pl => 'time hh:mm',
}, },
scope => '(([0-1]?[0-9]|[0-2]?[0-3]):([0-5]?[0-9]))', scope => '^(([0-1]?[0-9]|[0-2]?[0-3]):([0-5]?[0-9]))$',
}, },
datetime => { datetime => {
ref_base => 900, ref_base => 900,
txt => 'YYYY-mm-dd hh:mm', txt => 'YYYY-mm-dd hh:mm',
scope => scope =>
'(([1-2][0-9]{3})-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2][0-9]|30|31) (0?[1-9]|1[0-9]|2[0-3]):(0?[1-9]|[1-5][0-9]))', '^(([1-2][0-9]{3})-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2][0-9]|30|31) (0?[1-9]|1[0-9]|2[0-3]):(0?[1-9]|[1-5][0-9]))$',
}, },
timesec => { timesec => {
@ -633,7 +633,7 @@ my $rtypes = {
ref_base => 900, ref_base => 900,
txt => 'YYYY-mm-dd hh:mm:ss', txt => 'YYYY-mm-dd hh:mm:ss',
scope => scope =>
'(([1-2][0-9]{3})-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2][0-9]|30|31) (0?[1-9]|1[0-9]|2[0-3]):(0?[1-9]|[1-5][0-9]):(0?[1-9]|[1-5][0-9]))', '^(([1-2][0-9]{3})-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2][0-9]|30|31) (0?[1-9]|1[0-9]|2[0-3]):(0?[1-9]|[1-5][0-9]):(0?[1-9]|[1-5][0-9]))$',
}, },
direction => { direction => {
@ -693,9 +693,10 @@ my $rtypes = {
], ],
}, },
scope => [ scope => [
'N|0', 'NNE|1', 'NE|2', 'ENE|3', 'E|4', 'ESE|5', '^(N|0)$', '^(NNE|1)$', '^(NE|2)$', '^(ENE|3)$',
'SE|6', 'SSE|7', 'S|8', 'SSW|9', 'SW|10', 'WSW|11', '^(E|4)$', '^(ESE|5)$', '^(SE|6)$', '^(SSE|7)$',
'W|12', 'WNW|13', 'NW|14', 'NNW|15' '^(S|8)$', '^(SSW|9)$', '^(SW|10)$', '^(WSW|11)$',
'^(W|12)$', '^(WNW|13)$', '^(NW|14)$', '^(NNW|15)$'
], ],
}, },
@ -708,7 +709,7 @@ my $rtypes = {
nl => 'open/closed/tilted', nl => 'open/closed/tilted',
pl => 'open/closed/tilted', pl => 'open/closed/tilted',
}, },
scope => [ 'closed|0', 'open|1', 'tilted|2' ], scope => [ '^(closed|0)$', '^(open|1)$', '^(tilted|2)$' ],
}, },
condition_hum => { condition_hum => {
@ -720,7 +721,10 @@ my $rtypes = {
nl => 'humidity condition', nl => 'humidity condition',
pl => 'humidity condition', pl => 'humidity condition',
}, },
scope => [ 'dry|0', 'low|1', 'optimal|2', 'high|3', 'wet|4' ], scope => [
'^(dry|0)$', '^(low|1)$', '^(optimal|2)$', '^(high|3)$',
'^(wet|4)$'
],
}, },
condition_uvi => { condition_uvi => {
@ -732,7 +736,11 @@ my $rtypes = {
nl => 'UV condition', nl => 'UV condition',
pl => 'UV condition', pl => 'UV condition',
}, },
scope => [ 'low|0', 'moderate|1', 'high|2', 'veryhigh|3', 'extreme|4' ], scope => [
'^(low|0)$', '^(moderate|1)$',
'^(high|2)$', '^(veryhigh|3)$',
'^(extreme|4)$'
],
}, },
pct => { pct => {
@ -2443,6 +2451,7 @@ sub replaceTemplate ($$$$;$) {
sub formatValue($$$;$$$$) { sub formatValue($$$;$$$$) {
my ( $device, $reading, $value, $desc, $format, $scope, $lang ) = @_; my ( $device, $reading, $value, $desc, $format, $scope, $lang ) = @_;
$lang = "en" if ( !$lang ); $lang = "en" if ( !$lang );
my $value_num;
return $value if ( !defined($value) || ref($value) ); return $value if ( !defined($value) || ref($value) );
@ -2462,17 +2471,29 @@ sub formatValue($$$;$$$$) {
# scope # scope
# #
if ( ref($scope) eq 'CODE' && &$scope ) { if ( ref($scope) eq 'CODE' && &$scope ) {
$value = $scope->($value); ( $value, $value_num ) = $scope->($value);
} }
elsif ( ref($scope) eq 'HASH' && looks_like_number($value) ) { elsif ( ref($scope) eq 'HASH' && looks_like_number($value) ) {
if ( $scope->{min} && $value < $scope->{min} ) { if ( $scope->{min} && $scope->{max} ) {
$value = $scope->{min} if ( $value < $scope->{min} );
$value = $scope->{max} if ( $value > $scope->{max} );
}
elsif ( $scope->{lt} && $scope->{gt} ) {
$value = $scope->{lt} if ( $value < $scope->{lt} );
$value = $scope->{gt} if ( $value > $scope->{gt} );
}
elsif ( $scope->{le} && $scope->{ge} ) {
$value = $scope->{le} if ( $value <= $scope->{le} );
$value = $scope->{ge} if ( $value >= $scope->{ge} );
}
elsif ( $scope->{min} && $value < $scope->{min} ) {
$value = $scope->{min}; $value = $scope->{min};
} }
elsif ( $scope->{lt} && $value < $scope->{lt} ) { elsif ( $scope->{lt} && $value < $scope->{lt} ) {
$value = $scope->{lt}; $value = $scope->{lt};
} }
elsif ( $scope->{max} && $value > $scope->{min} ) { elsif ( $scope->{max} && $value > $scope->{max} ) {
$value = $scope->{max}; $value = $scope->{max};
} }
elsif ( $scope->{gt} && $value > $scope->{gt} ) { elsif ( $scope->{gt} && $value > $scope->{gt} ) {
@ -2491,6 +2512,7 @@ sub formatValue($$$;$$$$) {
&& defined( $scope->[$value] ) && defined( $scope->[$value] )
&& $value =~ /$scope->[$value]/gmi ) && $value =~ /$scope->[$value]/gmi )
{ {
$value_num = $value;
if ( defined( $desc->{txt}{$lang}[$value] ) ) { if ( defined( $desc->{txt}{$lang}[$value] ) ) {
$value = $desc->{txt}{$lang}[$value]; $value = $desc->{txt}{$lang}[$value];
} }
@ -2501,18 +2523,26 @@ sub formatValue($$$;$$$$) {
} }
else { else {
foreach ($scope) { my $i = 0;
if ( $value =~ /$_/gmi ) { foreach ( @{$scope} ) {
$value = $1 if ($1); if ( $value =~ /^$_$/i ) {
$value = ${ $scope->{$_} } if ( !$1 ); if ( defined( $desc->{txt}{$lang}[$i] ) ) {
if ( defined( $desc->{txt}{$lang}[$_] ) ) { $value = $desc->{txt}{$lang}[$i];
$value = $desc->{txt}{$lang}[$_];
}
elsif ( defined( $desc->{txt}{$lang}[$value] ) ) {
$value = $desc->{txt}{$lang}[$value];
}
last; last;
} }
else {
$value = $1 if ($1);
$value = ${ $scope->[$_] } if ( !$1 );
}
if ( defined( $desc->{txt}{$lang}[$value] ) ) {
$value = $desc->{txt}{$lang}[$value];
}
$value_num = $i;
last;
}
$i++;
} }
} }
} }
@ -2554,6 +2584,7 @@ sub formatValue($$$;$$$$) {
} }
$desc->{value}{$lang} = $value; $desc->{value}{$lang} = $value;
$desc->{value_num} = $value_num if ( defined($value_num) );
my ( $txt, $txt_long ) = replaceTemplate( $device, $reading, $desc, $lang ); my ( $txt, $txt_long ) = replaceTemplate( $device, $reading, $desc, $lang );
return ( $txt, $txt_long ) if (wantarray); return ( $txt, $txt_long ) if (wantarray);