From 38fe672ccd38955ce8b42ab8f8b7fea8dd81a5a5 Mon Sep 17 00:00:00 2001 From: Adimarantis <> Date: Sat, 29 Jan 2022 19:47:01 +0000 Subject: [PATCH] 50_Signalbot: native SVG plot and DOIF uiTable widget handling git-svn-id: https://svn.fhem.de/fhem/trunk@25587 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/50_Signalbot.pm | 163 +++++++++++++++++++++++--- fhem/contrib/signal/signal_install.sh | 26 ++-- 3 files changed, 162 insertions(+), 28 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index e4031e280..5c4879a68 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,6 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. + - feature: 50_Signalbot: native SVG plot and DOIF uiTable widget handling - change: 32_withings: updated oauth endpoint - feature: 31_HUEDevice: added v2effect command - bugfix: 14_Hideki.pm: fix match regex - forum:#125679 diff --git a/fhem/FHEM/50_Signalbot.pm b/fhem/FHEM/50_Signalbot.pm index 9ad570820..ca3dea5ba 100755 --- a/fhem/FHEM/50_Signalbot.pm +++ b/fhem/FHEM/50_Signalbot.pm @@ -1,6 +1,6 @@ ############################################## #$Id$ -my $Signalbot_VERSION="3.4"; +my $Signalbot_VERSION="3.5"; # Simple Interface to Signal CLI running as Dbus service # Author: Adimarantis # License: GPL @@ -401,7 +401,7 @@ sub Signalbot_Set($@) { # my @newatt; foreach my $file (@attachments) { if ( -e $file ) { - if ($file =~ /[tmp\/signalbot]/) { + if (! $file =~ /tmp\/signalbot/) { $file =~ /^.*?\.([^.]*)?$/; my $type = $1; my $tmpfilename="/tmp/signalbot".gettimeofday().".".$type; @@ -411,7 +411,9 @@ sub Signalbot_Set($@) { # push @newatt, $file; } } else { - return "File not found: $file"; + my $png=Signalbot_getPNG($hash,$file); + return "File not found: $file" if !defined $png; + push @newatt, $png; } } @attachments=@newatt; @@ -1911,21 +1913,10 @@ sub Signalbot_replaceCommands(@) { my ( $isMediaStream, $type ) = Signalbot_IdentifyStream( $hash, $msg ); if ($isMediaStream<0) { Log3 $hash->{NAME}, 5, $hash->{NAME}.": Media stream found $isMediaStream $type"; - my $tmpfilename="/tmp/signalbot".gettimeofday().".".$type; - my $fh; - #tempfile() would be the better way, but is not readable by signal-cli (how to set world-readable?) - #could be changed with "chmod 0666, $tmpfilename;" which should even work on Windows, but what's the point - dbus/signal-cli works on Linux only anyways - #my ($fh, $tmpfilename) = tempfile(); - if(!open($fh, ">", $tmpfilename,)) { - #if (!defined $fh) { - Log3 $hash->{NAME}, 3, $hash->{NAME}.": Can't write $tmpfilename"; - #return undef since this is a fatal error - return ("Can't write $tmpfilename",@processed); - } - print $fh $msg; - close($fh); + my $ret=Signalbot_copyToFile($msg,$type); + return ("Can't write to temporary file",@processed) if !defined $ret; #If we created a file return the filename instead - push @processed, $tmpfilename; + push @processed, $ret; } else { Log3 $hash->{NAME}, 5, $hash->{NAME}.": normal text found:$msg"; #No mediastream - return what it was @@ -1941,6 +1932,144 @@ sub Signalbot_replaceCommands(@) { return (undef,@processed); } +sub Signalbot_copyToFile(@) { + my ($msg,$type) = @_; + my $tmpfilename="/tmp/signalbot".gettimeofday().".".$type; + my $fh; + if(!open($fh, ">", $tmpfilename,)) { + #return undef since this is a fatal error + return undef; + } + print $fh $msg; + close($fh); + return $tmpfilename; +} + +sub Signalbot_getPNG(@) { + my ($hash, $file) = @_; + + my @special=split(",",$file); + #does the first part equal to a FHEM device? + my $sname = shift @special; + my $shash = $defs{$sname}; + if (defined $shash) { + Log3 $hash->{NAME}, 4 , $hash->{NAME}.": Attachment matches device $sname of type $shash->{TYPE}"; + my $svg; + if ($shash->{TYPE} eq "SVG") { + $svg=plotAsPng($sname,@special); + } + if ($shash->{TYPE} eq "DOIF") { + $svg=Signalbot_DOIFAsPng($shash,@special); + } + return undef if !defined $svg; + return Signalbot_copyToFile($svg,"png"); + } + return; +} + +sub +Signalbot_DOIFAsPng($@) +{ + my ($hash,@params) = @_; + my (@webs, $mimetype, $svgdata, $rsvg, $pngImg); + + my $matchid=-1; + my $sizex=-1; + my $sizey=-1; + my $zoom=1.0; + my $matchdev=""; + my $matchread=""; + my $target="uiTable"; + + #If no further information given, take the first entry of uitables + if (@params == 0) { + $matchid=1; + } else { + foreach my $p (@params) { + my @par=split("=",$p); + my $cm=shift @par; + my $val=shift @par; + $matchid=$p if looks_like_number($p); + next if (!defined $cm || !defined $val); + if ($cm eq "id") { + $matchid=$val if looks_like_number($val); + } elsif ($cm eq "zoom") { + $zoom=$val if looks_like_number($val); + } elsif ($cm eq "dev") { + $matchdev=$val; + } elsif ($cm eq "val") { + $matchread=$val; + } elsif ($cm eq "sizex") { + $sizex=$val if looks_like_number($val); + } elsif ($cm eq "sizey") { + $sizey=$val if looks_like_number($val); + } elsif ($cm eq "state") { + $target="uiState"; + } + } + } + $target="uiState" if (!defined $hash->{$target}{table}); + my $table=$hash->{$target}{table}; + + # "Error: uiTable/uiState not defined" + return undef if (!defined $table); + + my $id=0; + my $cmd; + OUTER: + for (my $i=0;$i < scalar keys %{$table};$i++){ + for (my $k=0;$k < scalar keys %{$table->{$i}};$k++){ + for (my $l=0;$l < scalar keys %{$table->{$i}{$k}};$l++){ + for (my $m=0;$m < scalar keys %{$table->{$i}{$k}{$l}};$m++) { + $id++; + my $tab=$table->{$i}{$k}{$l}{$m}; + $tab =~ /.*DOIF_Widget\(.*?,.*?,.*?,(.*),.*\)/; + $cmd=$1; + next if !defined $cmd; + if ($matchid==$id) { + last OUTER; + } elsif ($matchid==-1) { + $cmd =~ /.*ReadingValDoIf\(\$hash,\'(.*?)\',\'(.*?)\'.*\)/; + next if ($matchdev ne "" && $matchdev ne $1); + next if ($matchread ne "" && $matchread ne $2); + last OUTER; + } + } + } + } + #Nothing found, since if something found we skip out with "last" + $cmd=""; + } + + if (defined $cmd && $cmd ne "") { + $cmd="package ui_Table;"."$cmd".";"; + $svgdata=eval($cmd); + print $@; + } else { + #print "Error: uiTable format error (no card)"; + return undef; + } + + # "Error: getting data from card" + return undef if (! defined $svgdata); + $svgdata=' '.$svgdata.''; + + eval { + require Image::LibRSVG; + $rsvg = new Image::LibRSVG(); + if ($sizex != -1 && $sizey !=-1) { + $rsvg->loadFromStringAtZoomWithMax($svgdata, $zoom, $zoom, $sizex, $sizey ); + } else { + $rsvg->loadFromStringAtZoom($svgdata, $zoom, $zoom); + } + $pngImg = $rsvg->getImageBitmap(); + }; + if (defined $pngImg && $pngImg ne "") { + return $pngImg if $pngImg; + } + return; +} + #Get OSRelease Version sub Signalbot_OSRel() { my $fh; diff --git a/fhem/contrib/signal/signal_install.sh b/fhem/contrib/signal/signal_install.sh index 4c93d1989..39a110f22 100755 --- a/fhem/contrib/signal/signal_install.sh +++ b/fhem/contrib/signal/signal_install.sh @@ -1,6 +1,6 @@ #!/bin/bash #$Id:$ -SCRIPTVERSION="3.3" +SCRIPTVERSION="3.5" # Author: Adimarantis # License: GPL #Install script for signal-cli @@ -20,7 +20,7 @@ OPERATION=$1 JAVA_VERSION=11.0 if [ $OPERATION = "experimental" ]; then - SIGNALVERSION="0.10.1" + SIGNALVERSION="0.10.2" JAVA_VERSION=17.0 OPERATION= fi @@ -157,13 +157,22 @@ fi GLIBC=`ldd --version | grep -m1 -o '[0-9]\.[0-9][0-9]' | head -n 1` IDENTSTR=$ARCH-glibc$GLIBC-$SIGNALVERSION -KNOWN=("amd64-glibc2.27-0.9.0" "amd64-glibc2.28-0.9.0" "amd64-glibc2.31-0.9.0" "armhf-glibc2.28-0.9.0" "amd64-glibc2.27-0.9.2" "amd64-glibc2.28-0.9.2" "amd64-glibc2.31-0.9.2" "armhf-glibc2.28-0.9.2" "armhf-glibc2.31-0.9.2" "armhf-glibc2.31-0.10.1") +KNOWN=("amd64-glibc2.27-0.9.2" "amd64-glibc2.28-0.9.2" "amd64-glibc2.31-0.9.2" "armhf-glibc2.28-0.9.2" "armhf-glibc2.31-0.9.2" "armhf-glibc2.31-0.10.2" "armhf-glibc2.28-0.10.2" "amd64-glibc2.27-0.10.2" "amd64-glibc2.31-0.10.2") GETLIBS=1 if [[ ! " ${KNOWN[*]} " =~ " ${IDENTSTR} " ]]; then echo "$IDENTSTR is an unsupported combination - signal-cli binary libraries might not work" - GETLIBS=0 + if [ $ARCH = "amd64" && $GLIBC < "2.31" ]; then + GLIBC=2.28 + echo "Fallback to GLIBC $GLIBC"; + elif [ $ARCH = "armhf" && $GLIBC < "2.31" ]; then + GLIBC=2.27 + echo "Fallback to GLIBC $GLIBC"; + else + GETLIBS=0 + fi fi +IDENTSTR=$ARCH-glibc$GLIBC-$SIGNALVERSION if [ $OSNAME != "Linux" ]; then echo "Only Linux systems are supported (you: $OSNAME), quitting" @@ -284,7 +293,7 @@ if ! [ "$JAVA_VERSION" = "$JVER" ]; then rm /tmp/$JAVA_ARC echo "done" fi - JAVA_HOME=/opt/java + export JAVA_HOME=/opt/java fi } @@ -352,12 +361,6 @@ if [ $NEEDINSTALL = 1 ]; then fi echo "done" rm -f /tmp/signal-cli-$SIGNALVERSION.tar.gz - cd /opt/signal/bin - mv signal-cli signal-cli.org - echo "#!/bin/sh" >signal-cli - echo "JAVA_HOME=$JAVA_HOME" >>signal-cli - cat signal-cli.org >>signal-cli - chmod a+x signal-cli fi fi @@ -411,6 +414,7 @@ After=network-online.target [Service] Type=dbus Environment="SIGNAL_CLI_OPTS=-Xms2m" +Environment="JAVA_HOME=$JAVA_HOME" ExecStart=$SIGNALPATH/signal/bin/signal-cli --config $SIGNALVAR daemon --system User=$SIGNALUSER BusName=org.asamk.Signal