CUL_HM:implement common parsing for command definitions

git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@22648 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
martinp876 2020-08-22 14:34:45 +00:00
parent ae99cc19d3
commit be17a7ed9e

View File

@ -4269,7 +4269,7 @@ sub CUL_HM_SetList($$) {#+++++++++++++++++ get command basic list+++++++++++++++
.":".InternalVal($name,"peerList","") .":".InternalVal($name,"peerList","")
;# update cmds entry in case ;# update cmds entry in case
} }
if( $hash->{helper}{cmds}{cmdKey} ne $cmdKey){ if( $hash->{helper}{cmds}{cmdKey} ne $cmdKey || 1){
my ($roleC,$roleD,$roleV,$fkt,$devName,$mId,$chn,$peerLst) = split(":", $cmdKey); my ($roleC,$roleD,$roleV,$fkt,$devName,$mId,$chn,$peerLst) = split(":", $cmdKey);
my $st = $mId ne "" ? $culHmModel->{$mId}{st} : AttrVal($devName, "subType", ""); my $st = $mId ne "" ? $culHmModel->{$mId}{st} : AttrVal($devName, "subType", "");
my $md = $mId ne "" ? $culHmModel->{$mId}{name} : AttrVal($devName, "model" , ""); my $md = $mId ne "" ? $culHmModel->{$mId}{name} : AttrVal($devName, "model" , "");
@ -4312,8 +4312,8 @@ sub CUL_HM_SetList($$) {#+++++++++++++++++ get command basic list+++++++++++++++
$hash->{helper}{cmds}{lst}{peer} = join",",grep/./,split",",InternalVal($name,"peerList",""); $hash->{helper}{cmds}{lst}{peer} = join",",grep/./,split",",InternalVal($name,"peerList","");
if ($peerLst ne ""){ if ($peerLst ne ""){
if (grep /^press:/,@arr1){ if (grep /^press:/,@arr1){
push @arr1,"pressS:-peer-"; push @arr1,"pressS:[(-peer-|{self})]";
push @arr1,"pressL:-peer-"; push @arr1,"pressL:[(-peer-|{self})]";
} }
} }
else{#remove command else{#remove command
@ -4412,134 +4412,136 @@ sub CUL_HM_Set($@) {#+++++++++++++++++ set command+++++++++++++++++++++++++++++
my @cmdPrep = (); my @cmdPrep = ();
foreach my $cmdS (keys%{$hash->{helper}{cmds}{cmdLst}}){ foreach my $cmdS (keys%{$hash->{helper}{cmds}{cmdLst}}){
my $val = $hash->{helper}{cmds}{cmdLst}{$cmdS}; my $val = $hash->{helper}{cmds}{cmdLst}{$cmdS};
if($val eq "noArg"){
$val = ":$val"; if($cmdS =~ m/^(pct|pctSlat)$/){
$val = ":slider,0,1,100";
} }
elsif($val =~ m/^(\[?)-([a-zA-Z]*?)-\]? *$/){#add relacements if available elsif($val !~ m/ /){#no space - this is a single param command (or less)
my ($null,$repl) = ($1,$2); if ($val eq "noArg"){
if (defined $hash->{helper}{cmds}{lst}{$repl}){ $val = ":$val";
$null = $null ? "noArg," : ""; # if "optional" add "noArg" to optionList
$val =~ s/\[?-$repl-\]?/:$null$hash->{helper}{cmds}{lst}{$repl}/;
next if ($hash->{helper}{cmds}{lst}{$repl} eq "");# no options - no command
} }
else{ elsif($val eq "-tempTmpl-"){
if(!defined $modules{CUL_HM}{tempListTmplLst} ){
$val = "";
}
else{
$val = ":$modules{CUL_HM}{tempListTmplLst}";
}
}
elsif($val =~ m/^(\[?)-([a-zA-Z]*?)-\]? *$/){#add relacements if available
my ($null,$repl) = ($1,$2);
if (defined $hash->{helper}{cmds}{lst}{$repl}){
$null = $null ? "noArg," : ""; # if "optional" add "noArg" to optionList
$val =~ s/\[?-$repl-\]?/:$null$hash->{helper}{cmds}{lst}{$repl}/;
next if ($hash->{helper}{cmds}{lst}{$repl} eq "");# no options - no command
}
else{
$val = "";
}
}
elsif($val =~ m/^(\[?)\(-([a-zA-Z]*?)-\|\{(.*)\}\)\]? *$/){#add relacements if available plus default
my ($null,$repl,$def) = ($1,$2,$3);
$def = "" if (!defined $def);
if (defined $hash->{helper}{cmds}{lst}{$repl}){
$null = $null ? "noArg," : ""; # if "optional" add "noArg" to optionList
# $val =~ s/\[?(?-$repl-\)?\]?/:$null$hash->{helper}{cmds}{lst}{$repl}$def/;
$val = ":$null$hash->{helper}{cmds}{lst}{$repl},$def";
next if ($hash->{helper}{cmds}{lst}{$repl} eq "");# no options - no command
}
else{
$val = $def;
}
}
elsif($val =~ m/^[\(\[]*([a-zA-Z0-9\;_-\|\.\{\}]*)[\]\)]*$/){#(xxx|yyy) as optionlist - new
my $v1 = $1;
$v1 =~ s/[\{\}]//g;#remove default marking
my @lst1;
foreach(split('\|',$v1)){
if ($_ =~ m/(.*)\.\.(.*)/ ){
my ($min,$max,$step) = ($1,$2,0.5);
if ($max =~ m/(.*);(.*)/){
($max,$step) = ($1,$2);
}
my @list = ();
for(my $i = $min;$i <= $max; $i += $step){
push @list,$i;
}
push @lst1,@list;
}
else{
push @lst1,$_;
}
}
$val = ":".join(",",@lst1);
}
elsif($cmdS eq "color"){
$val = ":colorpicker,HUE,0,0.5,100";
}
else {# no shortcut
$val = ""; $val = "";
} }
} }
elsif($val =~ m/^(\[?)\(-([a-zA-Z]*?)-\|\{(.*)\}\)\]? *$/){#add relacements if available plus default else {# multi-parameter - no quick select
my ($null,$repl,$def) = ($1,$2,$3);
$def = "" if (!defined $def);
if (defined $hash->{helper}{cmds}{lst}{$repl}){
$null = $null ? "noArg," : ""; # if "optional" add "noArg" to optionList
# $val =~ s/\[?(?-$repl-\)?\]?/:$null$hash->{helper}{cmds}{lst}{$repl}$def/;
$val = ":$null$hash->{helper}{cmds}{lst}{$repl},$def";
next if ($hash->{helper}{cmds}{lst}{$repl} eq "");# no options - no command
}
else{
$val = $def;
}
}
elsif($val =~ m/^[\(\[]*([a-zA-Z0-9_-|\.\{\}]*)[\]\)]*$/){#(xxx|yyy) as optionlist - new
my $v1 = $1;
$v1 =~ s/[\{\}]//g;#remove default marking
my @lst1;
foreach(split('\|',$v1)){
if ($_ =~ m/(.*)\.\.(.*)/ ){
my @list = map { ($_.".0", $_+0.5) } (($1+0)..($2+0));
pop @list;
push @lst1,@list;
}
else{
push @lst1,$_;
}
}
$val = ":".join(",",@lst1);
}
elsif($cmdS eq "virtual"){
$val = ":slider,1,1,50";
}
elsif($cmdS eq "color"){
$val = ":colorpicker,HUE,0,0.5,100";
}
elsif($cmdS =~ m/^(pct|pctSlat)$/){
$val = ":slider,0,1,100";
}
else {
$val = ""; $val = "";
} }
push @cmdPrep,"$cmdS$val"; push @cmdPrep,"$cmdS$val";
} }
@cmdPrep = ("--") if (!scalar @cmdPrep); @cmdPrep = ("--") if (!scalar @cmdPrep);
my $usg = "Unknown argument $cmd, choose one of ".join(" ",sort @cmdPrep)." "; return "Unknown argument $cmd, choose one of ".join(" ",@cmdPrep)." ";
if ($usg =~ m/ tempTmplSet/){
my $tl = $modules{CUL_HM}{AttrList}."";
my $ok = ($tl =~ s/.* (tempListTmpl)(\:.*? ).*/$2/);
$tl = $ok ? $tl : "";
$usg =~ s/ tempTmplSet/ tempTmplSet$tl/;
}
return $usg;
} }
###------------------- commands parameter parsing -------------------### ###------------------- commands parameter parsing -------------------###
my $paraOpts = $hash->{helper}{cmds}{cmdLst}{$cmd}; if (1){
if($paraOpts eq "noArg"){ # no argument allowed
return "$cmd no params required" if(@a != 2);
}
elsif(@a == 2){# no arguments - is this ok?
if($paraOpts !~ m/\.\.\./ && $paraOpts !~ m/^\[.*?\] *$/ ){# argument required
return "$cmd parameter required:$paraOpts";
}
else{
push @a,"noArg";
}
}
if (0){#not active by now
my @parIn = grep!/noArg/,@a[2..$#a]; my @parIn = grep!/noArg/,@a[2..$#a];
my $in = scalar(@parIn);
$paraOpts =~ s/(\.\.\.|noArg|\'.*?\')//;#remove comment, ... and noArg my $paraOpts = $hash->{helper}{cmds}{cmdLst}{$cmd};
my $min = scalar(split(" ",$paraOpts)); $paraOpts =~ s/(\.\.\.|noArg|\'.*?\')//;#remove comment, "..." and noArg
# my @optLst = grep!/\[\]/,split(" ",$paraOpts);#[...] would leave an empty list
my @optLst = split(" ",$paraOpts);#[...] would leave an empty list my @optLst = split(" ",$paraOpts);#[...] would leave an empty list
#my $max = $hash->{helper}{cmds}{cmdLst}{$cmd} =~ m/\.\.\./ ? 99 : scalar(@optLst);
$paraOpts =~ s/(\[.*?\])//g; # remove optionals
my $pCnt = 0; my $pCnt = 0;
foreach my $param (@optLst){ my $paraFail = "";
my $optional = $param =~ m/^\[(.*)\]$/ ? 1:0; foreach my $param (@optLst){ # check each parameter
$param =~ s/^\[(.*)\]$/$1/; # remove optional my $optional = $param =~ m/^\[(.*)\]$/ ? 1:0; # is parameter optional?
if($param =~ m/^\((.*)\)$/ ){# list? $param =~ s/^\[(.*)\]$/$1/; # remove optional brackets
my @parLst = split("|",$1); if($param =~ m/^\((.*)\)$/ ){ # list of optionals?
if( defined $parIn[$pCnt]){ my @parLst = split('\|',$1);
if( grep/$parIn[$pCnt]/,@parLst){ if( defined $parIn[$pCnt]){ # user param provided
if( grep/$parIn[$pCnt]/,@parLst){ # parameter matched
} }
else{ else{ # user param no match
if($param =~ m/-.*-/){#free value possible if($param =~ m/-.*-/){ # but not distinct
} }
elsif($optional && $param =~ m/\{(.*?)\}/){#found default elsif($optional && $param =~ m/\{(.*?)\}/){# no match, distinct but optional with default
my $default = $1; my $default = $1;
splice @parIn, $pCnt, 0,$default;#insert the default splice @parIn, $pCnt, 0,$default;#insert the default
} }
else{ else{ # no match, distinct, not optional or no default => fail
return "$parIn[$pCnt] does not match parameter $pCnt: $optLst[$pCnt]"; $paraFail = "$parIn[$pCnt] does not match options";
} }
} }
} }
else{ else{ # no user param
if($optional && $param =~ m/\{(.*)\}/){#defaut if($optional && $param =~ m/\{(.*)\}/){ # defaut available, use it
my $default = $1; my $default = $1;
splice @parIn, $pCnt, 0,$default;#insert the default splice @parIn, $pCnt, 0,$default;#insert the default
} }
else{ else{ # no user param, no default => fail
return "param $pCnt: $optLst[$pCnt] is not optional or no dafault identifies"; $paraFail = "is not optional. No dafault identifies";
} }
} }
} }
return "parameter missing for $pCnt: $optLst[$pCnt]" if(!defined $parIn[$pCnt] && !$optional); $paraFail = "is required but missing" if(!defined $parIn[$pCnt] && !$optional);
return "param $pCnt:($optLst[$pCnt]) => $paraFail"
."\n$cmd: $hash->{helper}{cmds}{cmdLst}{$cmd}" if($paraFail);
$pCnt++; $pCnt++;
} }
$paraOpts =~ s/(\[.*?\])//g;
my $max = scalar(split(" ",$paraOpts));
splice @parIn, $pCnt, 0,"nArg" if(scalar(@parIn) == 0); splice @parIn, $pCnt, 0,"nArg" if(scalar(@parIn) == 0);
return; @a = ($a[0],$cmd,@parIn);
Log3 $name,3,"CUL_HM set $name " . join(" ", @a[1..$#a]);
} }
my @postCmds=(); #Commands to be appended after regSet (ugly...) my @postCmds=(); #Commands to be appended after regSet (ugly...)