98_weekprofile: fix statefile and configfile

git-svn-id: https://svn.fhem.de/fhem/trunk@10351 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
risiko79 2016-01-03 16:13:39 +00:00
parent d7c0b66087
commit 79eec35e6e

View File

@ -3,11 +3,7 @@
# #
# Usage # Usage
# #
# define <name> weekprofile <device> # define <name> weekprofile [device]
#
# Changelog
#
# V 0.01 2015-12-23 - first version
############################################## ##############################################
package main; package main;
@ -313,6 +309,7 @@ sub weekprofile_Initialize($)
$hash->{SetFn} = "weekprofile_Set"; $hash->{SetFn} = "weekprofile_Set";
$hash->{StateFn} = "weekprofile_State"; $hash->{StateFn} = "weekprofile_State";
$hash->{NotifyFn} = "weekprofile_Notify"; $hash->{NotifyFn} = "weekprofile_Notify";
$hash->{AttrFn} = "weekprofile_Attr";
$hash->{AttrList} = "widgetWeekdays configFile ".$readingFnAttributes; $hash->{AttrList} = "widgetWeekdays configFile ".$readingFnAttributes;
$hash->{FW_summaryFn} = "weekprofile_SummaryFn"; $hash->{FW_summaryFn} = "weekprofile_SummaryFn";
@ -529,6 +526,8 @@ sub weekprofile_State($$$$)
{ {
my ($hash, $time, $name, $val) = @_; my ($hash, $time, $name, $val) = @_;
my $me = $hash->{NAME}; my $me = $hash->{NAME};
#do nothing we do not restore readings from statefile
return undef;
} }
############################################## ##############################################
sub weekprofile_Notify($$) sub weekprofile_Notify($$)
@ -556,24 +555,40 @@ sub weekprofile_Notify($$)
return undef; return undef;
} }
############################################## ##############################################
sub weekprofile_Attr($$$)
{
my ($cmd, $me, $attrName, $attrVal) = @_;
my $hash = $defs{$me};
Log3 $me, 5, "$me(weekprofile_Attr): $cmd, $attrName, $attrVal";
$attr{$me}{$attrName} = $attrVal;
weekprofile_writeProfilesToFile($hash) if ($attrName eq 'configFile');
return undef;
}
##############################################
sub weekprofile_writeProfilesToFile(@) sub weekprofile_writeProfilesToFile(@)
{ {
my ($hash) = @_; my ($hash) = @_;
my $me = $hash->{NAME}; my $me = $hash->{NAME};
my $start = (defined($hash->{MASTERDEV}->{NAME})) ? 1:0;
my $prfCnt = scalar(@{$hash->{PROFILES}});
return if ($prfCnt <= $start);
my $filename = "./log/weekprofile-$me.cfg"; my $filename = "./log/weekprofile-$me.cfg";
$filename = AttrVal($me,"configFile",$filename); $filename = AttrVal($me,"configFile",$filename);
Log3 $me, 5, "$me(writeProfileToFile): write profiles to $filename";
my $ret = open(my $fh, '>', $filename); my $ret = open(my $fh, '>', $filename);
if (!$ret){ if (!$ret){
Log3 $me, 1, "Could not open file '$filename' $!"; Log3 $me, 1, "$me(writeProfileToFile): Could not open file '$filename' $!";
return; return;
} }
my $json = JSON->new; Log3 $me, 5, "$me(writeProfileToFile): write profiles to $filename";
my $start = (defined($hash->{MASTERDEV}->{NAME})) ? 1:0; my $json = JSON->new;
my $prfCnt = scalar(@{$hash->{PROFILES}});
for (my $i = $start; $i < $prfCnt; $i++) { for (my $i = $start; $i < $prfCnt; $i++) {
print $fh $hash->{PROFILES}[$i]->{NAME}."=".$json->encode($hash->{PROFILES}[$i]->{DATA})."\n"; print $fh $hash->{PROFILES}[$i]->{NAME}."=".$json->encode($hash->{PROFILES}[$i]->{DATA})."\n";
} }
@ -588,8 +603,6 @@ sub weekprofile_readProfilesFromFile(@)
my $filename = "./log/weekprofile-$me.cfg"; my $filename = "./log/weekprofile-$me.cfg";
$filename = AttrVal($me,"configFile",$filename); $filename = AttrVal($me,"configFile",$filename);
Log3 $me, 5, "$me(readProfilesFromFile): read profiles from $filename";
unless (-e $filename) { unless (-e $filename) {
Log3 $me, 5, "$me(readProfilesFromFile): file do not exist '$filename'"; Log3 $me, 5, "$me(readProfilesFromFile): file do not exist '$filename'";
return; return;
@ -600,6 +613,9 @@ sub weekprofile_readProfilesFromFile(@)
Log3 $me, 1, "$me(readProfilesFromFile): Could not open file '$filename' $!"; Log3 $me, 1, "$me(readProfilesFromFile): Could not open file '$filename' $!";
return; return;
} }
Log3 $me, 5, "$me(readProfilesFromFile): read profiles from $filename";
my $json = JSON->new; my $json = JSON->new;
my $rowCnt = 0; my $rowCnt = 0;
while (my $row = <$fh>) { while (my $row = <$fh>) {