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

98_DSBMobile.pm: Handle various cases with multiple pages for substitutions, new tableHTML function and small layout changes for weblinks

git-svn-id: https://svn.fhem.de/fhem/trunk@21340 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
KernSani 2020-03-02 20:54:54 +00:00
parent 05ae8ee8d9
commit 524d795888

View File

@ -172,7 +172,7 @@ sub DSBMobile_query($) {
"LastUpdate" => $date "LastUpdate" => $date
); );
my $json = encode_json \%arg; my $json = encode_json( \%arg );
Log3 $name, 5, "[$name] Arguments (in json) to encode" . Dumper($json); Log3 $name, 5, "[$name] Arguments (in json) to encode" . Dumper($json);
my $zip; my $zip;
IO::Compress::Gzip::gzip \$json => \$zip; IO::Compress::Gzip::gzip \$json => \$zip;
@ -232,6 +232,7 @@ sub DSBMobile_getDataCallback($) {
my $udate; my $udate;
my $test = $res->{ResultMenuItems}[0]->{Childs}; my $test = $res->{ResultMenuItems}[0]->{Childs};
my @aus; my @aus;
my %ttpages = (); # hash to get unique urls
foreach my $c (@$test) { foreach my $c (@$test) {
#if ($c->{Title} eq "Pläne") { #if ($c->{Title} eq "Pläne") {
@ -239,8 +240,16 @@ sub DSBMobile_getDataCallback($) {
#$ret .= Dumper($c->{root}{Childs}[0]->{Childs}[0]->{Detail}); #$ret .= Dumper($c->{root}{Childs}[0]->{Childs}[0]->{Detail});
foreach my $topic ($c) { foreach my $topic ($c) {
if ( $c->{MethodName} eq "timetable" ) { if ( $c->{MethodName} eq "timetable" ) {
$url = $topic->{Root}{Childs}[0]->{Childs}[0]->{Detail}; my $p = $topic->{Root}{Childs};
$udate = $topic->{Root}{Childs}[0]->{Childs}[0]->{Date}; for my $tt (@$p) {
my $subtt = $tt->{Childs};
for my $stt (@$subtt) {
$url = $stt->{Detail};
$udate = $stt->{Date};
$ttpages{$url} = 1;
Log3 $name, 5, "[$name] found url $url";
}
}
} }
if ( $c->{MethodName} eq "tiles" ) { if ( $c->{MethodName} eq "tiles" ) {
my $d = $topic->{Root}{Childs}; my $d = $topic->{Root}{Childs};
@ -295,22 +304,46 @@ sub DSBMobile_getDataCallback($) {
readingsBulkUpdate( $hash, ".lastAResult", encode_json( \@aus ) ); readingsBulkUpdate( $hash, ".lastAResult", encode_json( \@aus ) );
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
Log3 $name, 4, "[$name] Extracted the url: " . $url; # build an array from url hash
my @ttpages = keys %ttpages;
my $nparam = {
url => $url,
method => "GET",
hash => $hash,
callback => \&DSBMobile_getTTCallback
};
Log3 $name, 5, "[$name] 2nd nonblocking HTTP Call starting";
HttpUtils_NonblockingGet($nparam);
if ( @ttpages == 1 ) {
Log3 $name, 4, "[$name] Extracted the url: " . $url;
}
else {
Log3 $name, 4, "[$name] Extracted multiple urls: " . Dumper(@ttpages);
}
$hash->{helper}{tturl} = \@ttpages;
DSBMobile_processTTPages($hash);
return undef; return undef;
} }
##################################### #####################################
sub DSBMobile_getTTCallback($) { sub DSBMobile_processTTPages($) {
my ($hash) = @_;
my $name = $hash->{NAME};
my $ttpage = shift @{ $hash->{helper}{tturl} };
Log3 $name, 5, "[$name] processing page " . $ttpage;
if ($ttpage) {
my $nparam = {
url => $ttpage,
method => "GET",
hash => $hash,
callback => \&DSBMobile_TTpageCallback
};
Log3 $name, 5, "[$name] 2nd nonblocking HTTP Call starting for " . $ttpage;
HttpUtils_NonblockingGet($nparam);
}
else {
delete $hash->{helper}{tturl};
DSBMobile_getTTCallback($hash);
}
return undef;
}
#####################################
sub DSBMobile_TTpageCallback($) {
my ( $param, $err, $data ) = @_; my ( $param, $err, $data ) = @_;
my $hash = $param->{hash}; my $hash = $param->{hash};
my $name = $hash->{NAME}; my $name = $hash->{NAME};
@ -323,6 +356,18 @@ sub DSBMobile_getTTCallback($) {
return undef; return undef;
} }
$hash->{helper}{ttdata} .= $data;
DSBMobile_processTTPages($hash);
}
#####################################
sub DSBMobile_getTTCallback($) {
my ($hash) = @_;
my $name = $hash->{NAME};
my $data = $hash->{helper}{ttdata};
delete $hash->{helper}{ttdata};
$data =~ s/ /-/g; $data =~ s/ /-/g;
$data = latin1ToUtf8($data); $data = latin1ToUtf8($data);
Log3 $name, 4, "[$name] Starting extraction mon_list"; Log3 $name, 4, "[$name] Starting extraction mon_list";
@ -679,11 +724,15 @@ sub DSBMobile_tableHTML($;$) {
my $date = ""; my $date = "";
my $class; my $class;
my $out = AttrVal( $name, "dsb_outputFormat", undef ); my $out = AttrVal( $name, "dsb_outputFormat", undef );
my $cols = @cn;
foreach my $day (@days) { foreach my $day (@days) {
my $row = 0; my $row = 0;
my $cols = @cn;
my $class = "even"; my $class = "even";
$ret .= "</table><table class='block wide'><tr class='$class'><td><b>" . $day . "</b></td></tr>"; $ret
.= "</table><table class='block wide'><tr class='$class'><td colspan = '$cols'><b>"
. $day
. "</b></td></tr>";
$row++; $row++;
if ($infoDay) { if ($infoDay) {
foreach my $iline (@idata) { foreach my $iline (@idata) {
@ -697,7 +746,7 @@ sub DSBMobile_tableHTML($;$) {
if ( $iline->{sdate} eq $day ) { if ( $iline->{sdate} eq $day ) {
$ret $ret
.= "<tr class='$class'><td colspan ='@cn'>" .= "<tr class='$class'><td colspan ='$cols' align='middle'>"
. $iline->{topic} . ": " . $iline->{topic} . ": "
. $iline->{text} . $iline->{text}
. "</td></tr>"; . "</td></tr>";
@ -709,7 +758,7 @@ sub DSBMobile_tableHTML($;$) {
$ret .= "<td>" . $c . "</td>"; $ret .= "<td>" . $c . "</td>";
} }
$ret .= "</tr>"; $ret .= "</tr>";
$row++;
foreach my $line (@data) { foreach my $line (@data) {
if ( $line->{sdate} eq $day ) { if ( $line->{sdate} eq $day ) {
if ( $row % 2 == 0 ) { if ( $row % 2 == 0 ) {
@ -736,7 +785,7 @@ sub DSBMobile_tableHTML($;$) {
return $ret; return $ret;
} }
################################### #######################################
sub DSBMobile_infoHTML($) { sub DSBMobile_infoHTML($) {
my ( $name, $infoDay ) = @_; my ( $name, $infoDay ) = @_;
my $dat = ReadingsVal( $name, ".lastAResult", "" ); my $dat = ReadingsVal( $name, ".lastAResult", "" );