2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-10 09:16:53 +00:00

00_FBAHAHTTP.pm: fix "set template" (Forum #98913)

git-svn-id: https://svn.fhem.de/fhem/trunk@19019 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2019-03-24 17:39:43 +00:00
parent 1af3883ad0
commit e29526ba90

View File

@ -216,20 +216,59 @@ FBAHAHTTP_Set($@)
setKeyValue("FBAHAHTTP_PASSWORD_$name", $a[0]); setKeyValue("FBAHAHTTP_PASSWORD_$name", $a[0]);
delete($hash->{".SID"}); delete($hash->{".SID"});
FBAHAHTTP_Poll($hash); FBAHAHTTP_Poll($hash);
return;
} } elsif($type eq "refreshstate") {
if($type eq "refreshstate") {
FBAHAHTTP_Poll($hash); FBAHAHTTP_Poll($hash);
return;
} elsif($type eq "template") {
my $cl = $hash->{CL};
my $doRet = sub($)
{
if($cl) {
asyncOutput($cl, $_[0]);
} else {
Log3 $hash, 4, "$_";
} }
if($type eq "template") { };
FBAHAHTTP_Write($hash, $a[0], "applytemplate"); FBAHAHTTP_GetTemplateList($hash, sub($$){
return; my ($err, $r) = @_;
return $doRet->($err) if($err);
return $doRet->("Unknown template $a[0]") if(!defined($r->{$a[0]}));
FBAHAHTTP_Write($hash, $r->{$a[0]}, "applytemplate");
});
} }
return undef; return undef;
} }
sub
FBAHAHTTP_GetTemplateList($$)
{
my ($hash, $callbackFn) = @_;
my $host = ($hash->{DEF} =~ m/^http/i ? $hash->{DEF}:"http://$hash->{DEF}");
my $sid = $hash->{".SID"};
my $name = $hash->{NAME};
return "No SID found" if(!$sid);
HttpUtils_NonblockingGet({
url=>"$host/webservices/homeautoswitch.lua?".
"sid=$sid&switchcmd=gettemplatelistinfos",
loglevel => AttrVal($name, "verbose", 4),
timeout => AttrVal($name, "fbTimeout", 4),
callback => sub {
if($_[1]) {
delete $hash->{".SID"};
return $callbackFn->("$name: $_[1]");
}
my $ret = (defined($_[2]) ? $_[2] : "") ;
my %r;
$ret =~ s:<template identifier="([^"]*)".*?<name>([^<]+)</name>:
$r{$2}=$1:ge;
$callbackFn->(undef, \%r);
}
});
}
##################################### #####################################
sub sub
FBAHAHTTP_Get($@) FBAHAHTTP_Get($@)
@ -241,6 +280,11 @@ FBAHAHTTP_Get($@)
my $type = shift @a; my $type = shift @a;
my $cl = $hash->{CL}; my $cl = $hash->{CL};
return "Unknown argument $type, choose one of templatelist:noArg"
if(!defined($gets{$type}));
return "Missing argument for $type" if(int(@a) < $gets{$type}-1);
if($type eq "templatelist") {
my $doRet = sub($) my $doRet = sub($)
{ {
if($cl) { if($cl) {
@ -249,31 +293,12 @@ FBAHAHTTP_Get($@)
Log3 $hash, 4, "$_"; Log3 $hash, 4, "$_";
} }
}; };
FBAHAHTTP_GetTemplateList($hash, sub($$){
return "Unknown argument $type, choose one of templatelist:noArg" my ($err, $r) = @_;
if(!defined($gets{$type})); return $doRet->($err) if($err);
return "Missing argument for $type" if(int(@a) < $gets{$type}-1); my $str = join(",",sort keys %{$r});
readingsSingleUpdate($hash, "templateList", $str, 1);
if($type eq "templatelist") { $doRet->($str);
my $host = ($hash->{DEF} =~ m/^http/i ? $hash->{DEF}:"http://$hash->{DEF}");
my $sid = $hash->{".SID"};
return "No SID found" if(!$sid);
HttpUtils_NonblockingGet({
url=>"$host/webservices/homeautoswitch.lua?".
"sid=$sid&switchcmd=gettemplatelistinfos",
loglevel => AttrVal($name, "verbose", 4),
timeout => AttrVal($name, "fbTimeout", 4),
callback => sub {
if($_[1]) {
delete $hash->{".SID"};
return $doRet->("$name: $_[1]");
}
my $ret = (defined($_[2]) ? $_[2] : "") ;
my @r;
$ret =~ s:<name>([^<]+)</name>:push(@r,$1):ge;
readingsSingleUpdate($hash, "templateList", join(",",@r), $1);
$doRet->(join(",",@r));
}
}); });
} }
return undef; return undef;