From c5f6854eedac29085b2d9f4dd5fb5a304c308f9e Mon Sep 17 00:00:00 2001 From: betateilchen <> Date: Thu, 4 Feb 2021 19:41:41 +0000 Subject: [PATCH] 55_InfoPanel.pm: code rework to use own package git-svn-id: https://svn.fhem.de/fhem/trunk@23675 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/55_InfoPanel.pm | 150 ++++++++++++++++++++++++-------------- 1 file changed, 94 insertions(+), 56 deletions(-) diff --git a/fhem/FHEM/55_InfoPanel.pm b/fhem/FHEM/55_InfoPanel.pm index 32d068874..f3ed3c3a1 100644 --- a/fhem/FHEM/55_InfoPanel.pm +++ b/fhem/FHEM/55_InfoPanel.pm @@ -61,34 +61,66 @@ # 2016-09-04 - 12114 - added: movecalculated # # 2018-05-06 - 16695 - changed: check plotName exists -# 2018-05-28 - $Rev$ - changed: remove misleading link in commandref +# 2018-05-28 - 21562 - changed: remove misleading link in commandref +# +# 2021-02-04 - - changed: use own package # ############################################## =cut -package main; +package FHEM::InfoPanel; ## no critic + use strict; use warnings; -use Data::Dumper; +#use Data::Dumper; +#use HttpUtils; +############################################## + +use GPUtils qw(GP_Import GP_Export); + +BEGIN { + # Import from main context + GP_Import( + qw( AttrVal + Debug + Log3 + data + defs + FileRead + getKeyValue + readingFnAttributes + readingsSingleUpdate + setKeyValue + FW_userAgent + FW_wname + FW_RET + FW_webArgs + FW_pos + FW_plotsize + FW_ME + FW_httpheader + FW_CSRF ) + ); + + #-- Export to main context with different name + GP_Export( + qw( Initialize ) + ); +} + +############################################## + +no if $] >= 5.017011, warnings => 'experimental'; use feature qw/switch/; use vars qw(%data); -use HttpUtils; my @valid_valign = qw(auto baseline middle center hanging); my @valid_halign = qw(start middle end); - my $useImgTools = 1; -no if $] >= 5.017011, warnings => 'experimental'; - -sub btIP_Define; -sub btIP_Undef; -sub btIP_Set; -sub btIP_Get; -sub btIP_Notify; -sub btIP_readLayout; +############################################## sub btIP_itemArea; sub btIP_itemButton; @@ -130,10 +162,9 @@ sub btIP_HTMLTail; sub btIP_Overview; sub btIP_getURL; - ###################################### -sub InfoPanel_Initialize { +sub Initialize { my ($hash) = @_; ## no critic @@ -145,11 +176,12 @@ sub InfoPanel_Initialize { Log3(undef,4,"InfoPanel: Image::Info missing.") unless $useImgTools; ## use critic - $hash->{DefFn} = "btIP_Define"; - $hash->{UndefFn} = "btIP_Undef"; - $hash->{SetFn} = "btIP_Set"; - $hash->{GetFn} = "btIP_Get"; - $hash->{NotifyFn} = "btIP_Notify"; + $hash->{DefFn} = \&Define; + $hash->{UndefFn} = \&Undef; + $hash->{SetFn} = \&Set; + $hash->{GetFn} = \&Get; + $hash->{NotifyFn} = \&Notify; + $hash->{AttrList} = "autoreread:1,0 useViewPort:1,0 bgcolor refresh size "; $hash->{AttrList} .= "mobileApp:none,apple,other "; $hash->{AttrList} .= "title noscript showTime:1,0 "; @@ -158,7 +190,7 @@ sub InfoPanel_Initialize { return; } -sub btIP_Define { +sub Define { my ($hash, $def) = @_; my @a = split("[ \t]+", $def); return "Usage: define InfoPanel filename" if(int(@a) != 3); @@ -169,14 +201,14 @@ sub btIP_Define { $hash->{fhem}{div} = ''; $hash->{LAYOUTFILE} = $filename; - btIP_addExtension("btIP_CGI","btip","InfoPanel"); - btIP_readLayout($hash); + btIP_addExtension("FHEM::InfoPanel::btIP_CGI","btip","InfoPanel"); + readLayout($hash); readingsSingleUpdate($hash,'state','defined',1); return; } -sub btIP_Undef { +sub Undef { my ($hash, $arg) = @_; # check if last device my $url = '/btip'; @@ -184,7 +216,7 @@ sub btIP_Undef { return; } -sub btIP_Set { +sub Set { my ($hash, @a) = @_; my $name = $a[0]; @@ -208,7 +240,7 @@ sub btIP_Set { $defs{$name}{fhem}{override}{$a[2]} = 1; } when ("reread") { - btIP_readLayout($hash); + readLayout($hash); } default { $ret = $usage; @@ -217,7 +249,7 @@ sub btIP_Set { return $ret; } -sub btIP_Get { +sub Get { my ($hash, @a) = @_; my $name = $a[0]; @@ -245,7 +277,7 @@ sub btIP_Get { return $ret; } -sub btIP_Notify { +sub Notify { my ($hash,$dev) = @_; return unless AttrVal($hash->{NAME},'autoreload',1); @@ -253,11 +285,11 @@ sub btIP_Notify { return if(!grep(m/^FILEWRITE $hash->{LAYOUTFILE}$/, @{$dev->{CHANGED}})); Log3(undef, 4, "InfoPanel: $hash->{NAME} reread layout after edit."); - undef = btIP_readLayout($hash); + undef = readLayout($hash); return; } -sub btIP_readLayout { +sub readLayout { my ($hash)= @_; my $filename= $hash->{LAYOUTFILE}; my $name= $hash->{NAME}; @@ -267,7 +299,7 @@ sub btIP_readLayout { if($err) { # Log 1, "InfoPanel $name: $err"; # $hash->{fhem}{layout} = "text ERROR 50 50 \"Error on reading layout!\""; - Log 1, "InfoPanel $name: $err"; + Log3(undef, 1, "InfoPanel $name: $err"); $hash->{fhem}{layout} = "text ERROR 50 50 \"Error on reading layout!\""; my ($e,@layout) = FileRead('./FHEM/template.layout'); unless ($e){ @@ -1350,31 +1382,6 @@ sub btIP_addExtension { $data{FWEXT}{jqueryvticker}{SCRIPT} = "/pgm2/jquery.vticker.min.js" unless $data{FWEXT}{jqueryvticker}{SCRIPT}; } -sub btIP_CGI{ - - my ($request) = @_; - - my ($name,$ext)= btIP_splitRequest($request); - - if(defined($name)) { - if($ext eq "") { - return("text/plain; charset=utf-8", "Illegal extension."); - } - if(!defined($defs{$name})) { - return("text/plain; charset=utf-8", "Unknown InfoPanel device: $name"); - } - if($ext eq "png") { - return btIP_returnPNG($name); - } - if($ext eq "info" || $ext eq "html") { - return btIP_returnHTML($name); - } - } else { - return btIP_Overview(); - } - -} - sub btIP_splitRequest { my ($request) = @_; @@ -1487,6 +1494,37 @@ sub btIP_getURL { return $proto."://$FW_httpheader{Host}$FW_ME"; } + +#################### +# +#package main; + +sub btIP_CGI{ + + my ($request) = @_; + + my ($name,$ext)= btIP_splitRequest($request); + + if(defined($name)) { + if($ext eq "") { + return("text/plain; charset=utf-8", "Illegal extension."); + } + if(!defined($defs{$name})) { + return("text/plain; charset=utf-8", "Unknown InfoPanel device: $name"); + } + if($ext eq "png") { + return btIP_returnPNG($name); + } + if($ext eq "info" || $ext eq "html") { + return btIP_returnHTML($name); + } + } else { + return btIP_Overview(); + } + +} + + 1; #