mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-04-22 14:16:42 +00:00
00_MQTT2_SERVER.pm: save the retain values
git-svn-id: https://svn.fhem.de/fhem/trunk@17180 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
d2791ab58f
commit
c27b63383a
@ -2,7 +2,7 @@
|
||||
# $Id$
|
||||
package main;
|
||||
|
||||
# TODO: save retain, test SSL
|
||||
# TODO: test SSL
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
@ -31,6 +31,7 @@ MQTT2_SERVER_Initialize($)
|
||||
$hash->{SetFn} = "MQTT2_SERVER_Set";
|
||||
$hash->{UndefFn} = "MQTT2_SERVER_Undef";
|
||||
$hash->{WriteFn} = "MQTT2_SERVER_Write";
|
||||
$hash->{StateFn} = "MQTT2_SERVER_State";
|
||||
$hash->{CanAuthenticate} = 1;
|
||||
|
||||
no warnings 'qw';
|
||||
@ -142,6 +143,21 @@ MQTT2_SERVER_Set($@)
|
||||
}
|
||||
}
|
||||
|
||||
sub
|
||||
MQTT2_SERVER_State()
|
||||
{
|
||||
my ($hash, $ts, $name, $val) = @_;
|
||||
|
||||
if($name eq "RETAIN") {
|
||||
my $now = gettimeofday;
|
||||
my $ret = json2nameValue($val);
|
||||
for my $k (keys %{$ret}) {
|
||||
my %h = ( ts=>$now, val=>$ret->{$k} );
|
||||
$hash->{retain}{$k} = \%h;
|
||||
}
|
||||
}
|
||||
return undef;
|
||||
}
|
||||
|
||||
my %cptype = (
|
||||
0 => "RESERVED_0",
|
||||
@ -358,6 +374,10 @@ MQTT2_SERVER_doPublish($$$$;$)
|
||||
my $now = gettimeofday();
|
||||
my %h = ( ts=>$now, val=>$val );
|
||||
$tgt->{retain}{$tp} = \%h;
|
||||
|
||||
# Save it
|
||||
my %nots = map { $_ => $tgt->{retain}{$_}{val} } keys %{$tgt->{retain}};
|
||||
setReadingsVal($tgt, "RETAIN", toJSON(\%nots), FmtDateTime(gettimeofday()));
|
||||
}
|
||||
|
||||
foreach my $clName (keys %{$tgt->{clients}}) {
|
||||
@ -407,7 +427,12 @@ sub
|
||||
MQTT2_SERVER_Write($$$)
|
||||
{
|
||||
my ($hash,$topic,$msg) = @_;
|
||||
MQTT2_SERVER_doPublish($hash, $hash, $topic, $msg);
|
||||
my $retain;
|
||||
if($topic =~ m/^(.*):r$/) {
|
||||
$topic = $1;
|
||||
$retain = 1;
|
||||
}
|
||||
MQTT2_SERVER_doPublish($hash, $hash, $topic, $msg, $retain);
|
||||
}
|
||||
|
||||
sub
|
||||
|
@ -423,12 +423,13 @@ MQTT2_DEVICE_Undef($$)
|
||||
the same topic, but with different messages (arguments).<br>
|
||||
Notes:
|
||||
<ul>
|
||||
<li>Arguments to the set command will be appended to the message
|
||||
<li>arguments to the set command will be appended to the message
|
||||
published (not for the perl expression)</li>
|
||||
<li>If using a perl expressions, the command arguments are available as
|
||||
<li>if using a perl expressions, the command arguments are available as
|
||||
$EVENT, $EVTPART0, etc. The perl expression must return a string
|
||||
containing the topic and the message separated by a space.</li>
|
||||
<li>SetExtensions is activated</li>
|
||||
<li>if the topic name ends with :r, then the retain flag is set</li>
|
||||
</ul>
|
||||
</li><br>
|
||||
|
||||
|
@ -4939,6 +4939,7 @@ json2nameValue($;$)
|
||||
|
||||
if($val =~ m/^"/) {
|
||||
($val, $in) = lquote($val);
|
||||
$val =~ s/\\u([0-9A-F]{4})/chr(hex($1))/gsie; # toJSON reverse
|
||||
$ret{"$prefix$name"} = $val;
|
||||
|
||||
} elsif($val =~ m/^{/) { # }
|
||||
|
Loading…
x
Reference in New Issue
Block a user