2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 06:39:11 +00:00

10_MQTT2_DEVICE.pm: more elaborate zigbee2mqtt image retrieval (Forum #139205)

git-svn-id: https://svn.fhem.de/fhem/trunk@29153 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2024-09-19 17:58:21 +00:00
parent 7396517564
commit 5964dc9a1e

View File

@ -739,8 +739,6 @@ MQTT2_DEVICE_nlData($)
my (%img,%h,%n2n);
my $fo="";
#my $pref = "https://koenkk.github.io/zigbee2mqtt/images/devices/";
my $pref = "https://www.zigbee2mqtt.io/images/devices/";
# Needed for the image links
my $dv = ReadingsVal($d, ".devices", ReadingsVal($d, "devices", ""));
@ -756,14 +754,13 @@ MQTT2_DEVICE_nlData($)
my $h = json2nameValue($dv);
my $dm;
foreach my $key (sort keys %{$h}) {
$dm = urlEncode($h->{$key}).".jpg"
$dm = $h->{$key}
if($key =~ m/^\d+_definition_model$/);
if($key =~ m/^\d+_ieee_address$/ && $dm && $dm !~ m,%2F,) {
if($key =~ m/^\d+_ieee_address$/ && $dm) {
$img{$h->{$key}} = $dm;
$dm = "";
}
}
}
# Name translation
@ -778,6 +775,30 @@ MQTT2_DEVICE_nlData($)
}
}
sub
getImg($$$)
{
my ($imgName, $suffix, $fileName) = @_;
#my $pref = "https://koenkk.github.io/zigbee2mqtt/images/devices";
my $pref = "https://www.zigbee2mqtt.io/images/devices/";
my $url = $pref . urlEncode($imgName) . $suffix;
Log 3, "MQTT2_DEVICE: trying $url";
my $data = GetFileFromURL($url);
if($data && $data !~ m/<html/ && open(FH, ">$fileName")) {
Log 3, "Got data, writing $fileName, length: ".length($data);
binmode(FH);
print FH $data;
close(FH);
return 1;
} else {
Log 3, "No result";
return 0;
}
}
my $fPref = "$attr{global}{modpath}/www/deviceimages/mqtt2";
my $div = ($FW_userAgent =~ m/WebKit/ ? "&#xA;" : " ");
my $gv = ReadingsVal($d, ".graphviz", ReadingsVal($d, "graphviz", ""));
$gv =~ s/\\n/\n/g; #126970
@ -793,25 +814,33 @@ MQTT2_DEVICE_nlData($)
if($v =~ m/{(.*)\|(.*)\|(.*)\|(.*)}/) {
my ($x1,$x2,$x3,$x4) = ($1,$2,$3,$4);
$nv = $n2n{$x1} if($n2n{$x1});
if($img{$n}) {
my $fn = $attr{global}{modpath}."/www/deviceimages/mqtt2/"
. urlDecode($img{$n});
if(!-f $fn) { # Cache the picture
my $url = "$pref/$img{$n}";
Log 3, "MQTT2_DEVICE: downloading $url to $fn";
my $data = GetFileFromURL($url);
if($data && open(FH,">$fn")) {
binmode(FH);
$data = "" if($data =~ m/<html/); # error page is html
print FH $data;
close(FH)
}
} elsif(-z $fn) { # got error page for the request
$img{$n} = 0;
}
$h{$n}{img} = "$FW_ME/deviceimages/mqtt2/$img{$n}" if($img{$n});
if($img{$n}) {
$img{$n} =~ s,[ /],-,g;
my $fJpg = "$fPref/$img{$n}.jpg";
my $fPng = "$fPref/$img{$n}.png";
my $fn;
if(-e $fJpg) {
$fn = (-z $fJpg ? "" : "$img{$n}.jpg");
} elsif (-e $fPng) {
$fn = "$img{$n}.png";
} elsif (getImg($img{$n}, ".jpg", $fJpg)) {
$fn = "$img{$n}.jpg";
} elsif (getImg($img{$n}, ".png", $fPng)) {
$fn = "$img{$n}.png";
} else {
if(open(FH, ">$fJpg")) { # empty file: dont try it again
close(FH);
}
}
$h{$n}{img} = "$FW_ME/deviceimages/mqtt2/$fn" if($fn);
}
if($img{$n} && $n2n{$x1} && !AttrVal($n2n{$x1}, "imageLink", "")) {
CommandAttr(undef, "$nv imageLink $h{$n}{img}");
}