mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-03-13 17:26:34 +00:00
fhem.pl: move getUniqueId from 99_Utils.pm to fhem.pl (Forum #31932)
git-svn-id: https://svn.fhem.de/fhem/trunk@7528 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
af6171d4f7
commit
57c6554c37
@ -12,58 +12,6 @@ Utils_Initialize($$)
|
|||||||
my ($hash) = @_;
|
my ($hash) = @_;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub
|
|
||||||
getUniqueId()
|
|
||||||
{
|
|
||||||
my ($err, $uniqueID) = getKeyValue("uniqueID");
|
|
||||||
return $uniqueID if(defined($uniqueID));
|
|
||||||
srand(time);
|
|
||||||
$uniqueID = join "",map { unpack "H*", chr(rand(256)) } 1..16;
|
|
||||||
setKeyValue("uniqueID", $uniqueID);
|
|
||||||
return $uniqueID;
|
|
||||||
}
|
|
||||||
|
|
||||||
sub
|
|
||||||
getKeyValue($)
|
|
||||||
{
|
|
||||||
my ($key) = @_;
|
|
||||||
my $fName = $attr{global}{modpath}."/FHEM/FhemUtils/uniqueID";
|
|
||||||
my ($err, @l) = FileRead($fName);
|
|
||||||
return ($err, undef) if($err);
|
|
||||||
for my $l (@l) {
|
|
||||||
return (undef, $1) if($l =~ m/^$key:(.*)/);
|
|
||||||
}
|
|
||||||
return (undef, undef);
|
|
||||||
}
|
|
||||||
|
|
||||||
sub
|
|
||||||
setKeyValue($$)
|
|
||||||
{
|
|
||||||
my ($key,$value) = @_;
|
|
||||||
my $fName = $attr{global}{modpath}."/FHEM/FhemUtils/uniqueID";
|
|
||||||
my ($err, @old) = FileRead($fName);
|
|
||||||
my @new;
|
|
||||||
if($err) {
|
|
||||||
push(@new, "# This file is auto generated.",
|
|
||||||
"# Please do not modify, move or delete it.",
|
|
||||||
"");
|
|
||||||
@old = ();
|
|
||||||
}
|
|
||||||
|
|
||||||
my $fnd;
|
|
||||||
foreach my $l (@old) {
|
|
||||||
if($l =~ m/^$key:/) {
|
|
||||||
$fnd = 1;
|
|
||||||
push @new, "$key:$value" if(defined($value));
|
|
||||||
} else {
|
|
||||||
push @new, $l;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
push @new, "$key:$value" if(!$fnd && defined($value));
|
|
||||||
|
|
||||||
return FileWrite($fName, @new);
|
|
||||||
}
|
|
||||||
|
|
||||||
sub
|
sub
|
||||||
time_str2num($)
|
time_str2num($)
|
||||||
{
|
{
|
||||||
|
53
fhem/fhem.pl
53
fhem/fhem.pl
@ -4004,4 +4004,57 @@ FileWrite($@)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub
|
||||||
|
getUniqueId()
|
||||||
|
{
|
||||||
|
my ($err, $uniqueID) = getKeyValue("uniqueID");
|
||||||
|
return $uniqueID if(defined($uniqueID));
|
||||||
|
srand(time);
|
||||||
|
$uniqueID = join "",map { unpack "H*", chr(rand(256)) } 1..16;
|
||||||
|
setKeyValue("uniqueID", $uniqueID);
|
||||||
|
return $uniqueID;
|
||||||
|
}
|
||||||
|
|
||||||
|
sub
|
||||||
|
getKeyValue($)
|
||||||
|
{
|
||||||
|
my ($key) = @_;
|
||||||
|
my $fName = $attr{global}{modpath}."/FHEM/FhemUtils/uniqueID";
|
||||||
|
my ($err, @l) = FileRead($fName);
|
||||||
|
return ($err, undef) if($err);
|
||||||
|
for my $l (@l) {
|
||||||
|
return (undef, $1) if($l =~ m/^$key:(.*)/);
|
||||||
|
}
|
||||||
|
return (undef, undef);
|
||||||
|
}
|
||||||
|
|
||||||
|
sub
|
||||||
|
setKeyValue($$)
|
||||||
|
{
|
||||||
|
my ($key,$value) = @_;
|
||||||
|
my $fName = $attr{global}{modpath}."/FHEM/FhemUtils/uniqueID";
|
||||||
|
my ($err, @old) = FileRead($fName);
|
||||||
|
my @new;
|
||||||
|
if($err) {
|
||||||
|
push(@new, "# This file is auto generated.",
|
||||||
|
"# Please do not modify, move or delete it.",
|
||||||
|
"");
|
||||||
|
@old = ();
|
||||||
|
}
|
||||||
|
|
||||||
|
my $fnd;
|
||||||
|
foreach my $l (@old) {
|
||||||
|
if($l =~ m/^$key:/) {
|
||||||
|
$fnd = 1;
|
||||||
|
push @new, "$key:$value" if(defined($value));
|
||||||
|
} else {
|
||||||
|
push @new, $l;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
push @new, "$key:$value" if(!$fnd && defined($value));
|
||||||
|
|
||||||
|
return FileWrite($fName, @new);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user