2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-20 13:26:02 +00:00

fhem.pl/holiday/SVG: adding FileRead/FileWrite (Forum #22927)

git-svn-id: https://svn.fhem.de/fhem/trunk@5715 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2014-05-01 15:02:06 +00:00
parent 8c336f2644
commit ae30433082
3 changed files with 80 additions and 67 deletions

View File

@ -62,19 +62,10 @@ holiday_refresh($$)
} }
my $fname = $attr{global}{modpath} . "/FHEM/" . $hash->{NAME} . ".holiday"; my $fname = $attr{global}{modpath} . "/FHEM/" . $hash->{NAME} . ".holiday";
my @holidayfile; my ($err, @holidayfile) = FileRead($fname);
if(configDBUsed()) { return $err if($err);
my $hfile = _cfgDB_Readfile($fname);
return "Holiday file not found in database." unless defined $hfile;
@holidayfile = split("\n", $hfile);
} else {
return "Can't open $fname: $!" if(!open(FH, $fname));
@holidayfile = <FH>;
close(LAYOUT);
}
my @foundList; my @foundList;
foreach my $l (@holidayfile) { foreach my $l (@holidayfile) {
next if($l =~ m/^\s*#/); next if($l =~ m/^\s*#/);
next if($l =~ m/^\s*$/); next if($l =~ m/^\s*$/);

View File

@ -99,17 +99,10 @@ SVG_Set($@)
$hash->{GPLOTFILE} . ":". $hash->{GPLOTFILE} . ":".
$hash->{LOGFILE}; $hash->{LOGFILE};
if(configDBUsed()) { # copy template.gplot inside configDB my ($err,@rows) = FileRead($srcName);
_cfgDB_Writefile($dstName,_cfgDB_Readfile($srcName)); return $err if($err);
} else { $err = FileWrite($dstName, @rows);
open(SFH, $srcName) || return "Can't open $srcName: $!"; return $err;
open(DFH, ">$dstName") || return "Can't open $dstName: $!";
while(my $l = <SFH>) {
print DFH $l;
}
close(SFH); close(DFH);
}
return undef;
} }
################## ##################
@ -448,26 +441,24 @@ SVG_WriteGplot($)
my $fName = $FW_webArgs{gplotName}; my $fName = $FW_webArgs{gplotName};
return if(!$fName); return if(!$fName);
if(!open(FH, ">$fName")) {
FW_pO "SVG_WriteGplot: Can't write $fName"; my @rows;
return 0; push @rows, "# Created by FHEM/98_SVG.pm, ".TimeNow()."\n";
} push @rows, "set terminal png transparent size <SIZE> crop\n";
print FH "# Created by FHEM/98_SVG.pm, ".TimeNow()."\n"; push @rows, "set output '<OUT>.png'\n";
print FH "set terminal png transparent size <SIZE> crop\n"; push @rows, "set xdata time\n";
print FH "set output '<OUT>.png'\n"; push @rows, "set timefmt \"%Y-%m-%d_%H:%M:%S\"\n";
print FH "set xdata time\n"; push @rows, "set xlabel \" \"\n";
print FH "set timefmt \"%Y-%m-%d_%H:%M:%S\"\n"; push @rows, "set title '$FW_webArgs{title}'\n";
print FH "set xlabel \" \"\n"; push @rows, "set ytics ".$FW_webArgs{ytics}."\n";
print FH "set title '$FW_webArgs{title}'\n"; push @rows, "set y2tics ".$FW_webArgs{y2tics}."\n";
print FH "set ytics ".$FW_webArgs{ytics}."\n"; push @rows, "set grid".($FW_webArgs{gridy} ? " ytics" :"").
print FH "set y2tics ".$FW_webArgs{y2tics}."\n";
print FH "set grid".($FW_webArgs{gridy} ? " ytics" :"").
($FW_webArgs{gridy2} ? " y2tics":"")."\n"; ($FW_webArgs{gridy2} ? " y2tics":"")."\n";
print FH "set ylabel \"$FW_webArgs{ylabel}\"\n"; push @rows, "set ylabel \"$FW_webArgs{ylabel}\"\n";
print FH "set y2label \"$FW_webArgs{y2label}\"\n"; push @rows, "set y2label \"$FW_webArgs{y2label}\"\n";
print FH "set yrange $FW_webArgs{yrange}\n" if($FW_webArgs{yrange}); push @rows, "set yrange $FW_webArgs{yrange}\n" if($FW_webArgs{yrange});
print FH "set y2range $FW_webArgs{y2range}\n" if($FW_webArgs{y2range}); push @rows, "set y2range $FW_webArgs{y2range}\n" if($FW_webArgs{y2range});
print FH "\n"; push @rows, "\n";
my $ld = $FW_webArgs{logdevicetype}; my $ld = $FW_webArgs{logdevicetype};
my @plot; my @plot;
@ -480,7 +471,7 @@ SVG_WriteGplot($)
join(":", map { $v[$_] =~ s/:/\\x3a/g if($_<$#v); $v[$_] } 0..$#v) : join(":", map { $v[$_] =~ s/:/\\x3a/g if($_<$#v); $v[$_] } 0..$#v) :
$v[0]; $v[0];
print FH "#$ld $r\n"; push @rows, "#$ld $r\n";
push @plot, "\"<IN>\" using 1:2 axes ". push @plot, "\"<IN>\" using 1:2 axes ".
($FW_webArgs{"axes_$i"} eq "right" ? "x1y2" : "x1y1"). ($FW_webArgs{"axes_$i"} eq "right" ? "x1y2" : "x1y1").
($FW_webArgs{"title_$i"} eq "notitle" ? " notitle" : ($FW_webArgs{"title_$i"} eq "notitle" ? " notitle" :
@ -489,12 +480,11 @@ SVG_WriteGplot($)
" lw " .$FW_webArgs{"width_$i"} . " lw " .$FW_webArgs{"width_$i"} .
" with " .$FW_webArgs{"type_$i"}; " with " .$FW_webArgs{"type_$i"};
} }
print FH "\n"; push @rows, "\n";
print FH "plot ".join(",\\\n ", @plot)."\n"; push @rows, "plot ".join(",\\\n ", @plot)."\n";
close(FH);
# import the file into database and delete it. my $err = FileWrite($fName, @rows);
_cfgDB_Fileimport($fName,1) if(configDBUsed()); FW_pO "SVG_WriteGplot: $err" if($err);
return 0; return 0;
} }
@ -513,16 +503,8 @@ SVG_readgplotfile($$)
if($defs{$wl} && $defs{$wl}{LOGDEVICE} && $defs{$defs{$wl}{LOGDEVICE}}); if($defs{$wl} && $defs{$wl}{LOGDEVICE} && $defs{$defs{$wl}{LOGDEVICE}});
$ldType = $wl if(!$ldType); $ldType = $wl if(!$ldType);
my @svgplotfile; my ($err, @svgplotfile) = FileRead($gplot_pgm);
if(configDBUsed()) { return ("$err", undef) if($err);
my $hfile = _cfgDB_Readfile($gplot_pgm);
return (FW_fatal("$gplot_pgm: $!"), undef) unless defined $hfile;
@svgplotfile = split("\n", $hfile);
} else {
open(FH, $gplot_pgm) || return (FW_fatal("$gplot_pgm: $!"), undef);
@svgplotfile = <FH>;
close(FH);
}
foreach my $l (@svgplotfile) { foreach my $l (@svgplotfile) {
$l = "$l\n" unless $l =~ m/\n$/; $l = "$l\n" unless $l =~ m/\n$/;
@ -783,7 +765,8 @@ SVG_showLog($)
my $gplot_pgm = "$FW_gplotdir/$type.gplot"; my $gplot_pgm = "$FW_gplotdir/$type.gplot";
if(!-r $gplot_pgm && !configDBUsed()) { my ($err, $cfg, $plot, $flog) = SVG_readgplotfile($wl, $gplot_pgm);
if($err) {
my $msg = "Cannot read $gplot_pgm"; my $msg = "Cannot read $gplot_pgm";
Log3 $FW_wname, 1, $msg; Log3 $FW_wname, 1, $msg;
@ -811,9 +794,6 @@ SVG_showLog($)
my $f = 0; # From the beginning of time... my $f = 0; # From the beginning of time...
my $t = 9; # till the end my $t = 9; # till the end
my ($err, $cfg, $plot, $flog) = SVG_readgplotfile($wl, $gplot_pgm);
return ($FW_RETTYPE, $err) if($err);
# Read the data from the filelog # Read the data from the filelog
my $oll = $attr{global}{verbose}; my $oll = $attr{global}{verbose};
$attr{global}{verbose} = 0; # Else the filenames will be Log'ged $attr{global}{verbose} = 0; # Else the filenames will be Log'ged
@ -839,9 +819,6 @@ SVG_showLog($)
close(FH); close(FH);
} elsif($pm eq "gnuplot-scroll") { } elsif($pm eq "gnuplot-scroll") {
my ($err, $cfg, $plot, $flog) = SVG_readgplotfile($wl, $gplot_pgm);
return ($FW_RETTYPE, $err) if($err);
# Read the data from the filelog # Read the data from the filelog
my ($f,$t)=($SVG_devs{$d}{from}, $SVG_devs{$d}{to}); my ($f,$t)=($SVG_devs{$d}{from}, $SVG_devs{$d}{to});
my $oll = $attr{global}{verbose}; my $oll = $attr{global}{verbose};
@ -878,9 +855,6 @@ SVG_showLog($)
unlink("$tmpfile.png"); unlink("$tmpfile.png");
} elsif($pm eq "SVG") { } elsif($pm eq "SVG") {
my ($err, $cfg, $plot, $flog) = SVG_readgplotfile($wl, $gplot_pgm);
return ($FW_RETTYPE, $err) if($err);
my ($f,$t)=($SVG_devs{$d}{from}, $SVG_devs{$d}{to}); my ($f,$t)=($SVG_devs{$d}{from}, $SVG_devs{$d}{to});
$f = 0 if(!$f); # From the beginning of time... $f = 0 if(!$f); # From the beginning of time...
$t = 9 if(!$t); # till the end $t = 9 if(!$t); # till the end

View File

@ -54,6 +54,8 @@ sub DoSet(@);
sub Dispatch($$$); sub Dispatch($$$);
sub DoTrigger($$@); sub DoTrigger($$@);
sub EvalSpecials($%); sub EvalSpecials($%);
sub FileRead($);
sub FileWrite($@);
sub FmtDateTime($); sub FmtDateTime($);
sub FmtTime($); sub FmtTime($);
sub GetLogLevel(@); sub GetLogLevel(@);
@ -3776,4 +3778,50 @@ configDBUsed()
return ($attr{global}{configfile} eq 'configDB'); return ($attr{global}{configfile} eq 'configDB');
} }
sub
FileRead($)
{
my ($fname) = @_;
my ($err, @ret);
if(configDBUsed()) {
@ret = cfgDB_FileRead($fname);
$err = "$fname not found in the database." if(@ret==1 && !defined($ret[0]));
} else {
if(open(FH, $fname)) {
@ret = <FH>;
close(FH);
} else {
$err = "Can't open $fname: $!";
}
}
return ($err, @ret);
}
sub
FileWrite($@)
{
my ($fname, @rows) = @_;
my ($err, @ret);
if(configDBUsed()) {
return cfgDB_FileWrite($fname, @rows);
} else {
if(open(FH, ">$fname")) {
foreach my $l (@rows) {
print FH $l;
}
close(FH);
return undef;
} else {
return "Can't open $fname: $!";
}
}
}
1; 1;