2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 18:59:33 +00:00

MQTT: implement retain

git-svn-id: https://svn.fhem.de/fhem/trunk@6710 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
ntruchsess 2014-10-08 15:51:10 +00:00
parent de665f1995
commit 32003c87bf
3 changed files with 15 additions and 4 deletions

View File

@ -433,6 +433,7 @@ sub Client_Define($$) {
$client->{NOTIFYDEV} = $client->{DEF} if $client->{DEF}; $client->{NOTIFYDEV} = $client->{DEF} if $client->{DEF};
$client->{qos} = MQTT_QOS_AT_MOST_ONCE; $client->{qos} = MQTT_QOS_AT_MOST_ONCE;
$client->{retain} = 0;
$client->{subscribe} = []; $client->{subscribe} = [];
$client->{subscribeExpr} = []; $client->{subscribeExpr} = [];
@ -459,6 +460,14 @@ sub client_attr($$$$$) {
} }
last; last;
}; };
$attribute eq "retain" and do {
if ($command eq "set") {
$client->{retain} = $value;
} else {
$client->{retain} = 0;
}
last;
};
$attribute eq "IODev" and do { $attribute eq "IODev" and do {
if ($main::init_done) { if ($main::init_done) {
if ($command eq "set") { if ($command eq "set") {

View File

@ -48,6 +48,7 @@ sub MQTT_BRIDGE_Initialize($) {
$hash->{AttrList} = $hash->{AttrList} =
"IODev ". "IODev ".
"qos:".join(",",keys %MQTT::qos)." ". "qos:".join(",",keys %MQTT::qos)." ".
"retain:0,1 ".
"publish-topic-base ". "publish-topic-base ".
"publishState ". "publishState ".
"publishReading_.* ". "publishReading_.* ".
@ -108,12 +109,12 @@ sub Notify() {
my $msgid; my $msgid;
if (defined $3 and $3 ne "") { if (defined $3 and $3 ne "") {
if (defined $hash->{publishReadings}->{$1}) { if (defined $hash->{publishReadings}->{$1}) {
$msgid = send_publish($hash->{IODev}, topic => $hash->{publishReadings}->{$1}, message => $3, qos => $hash->{qos}); $msgid = send_publish($hash->{IODev}, topic => $hash->{publishReadings}->{$1}, message => $3, qos => $hash->{qos}, retain => $hash->{retain});
readingsSingleUpdate($hash,"transmission-state","outgoing publish sent",1); readingsSingleUpdate($hash,"transmission-state","outgoing publish sent",1);
} }
} else { } else {
if (defined $hash->{publishState}) { if (defined $hash->{publishState}) {
$msgid = send_publish($hash->{IODev}, topic => $hash->{publishState}, message => $1, qos => $hash->{qos}); $msgid = send_publish($hash->{IODev}, topic => $hash->{publishState}, message => $1, qos => $hash->{qos}, retain => $hash->{retain});
readingsSingleUpdate($hash,"transmission-state","outgoing publish sent",1); readingsSingleUpdate($hash,"transmission-state","outgoing publish sent",1);
} }
} }

View File

@ -46,6 +46,7 @@ sub MQTT_DEVICE_Initialize($) {
$hash->{AttrList} = $hash->{AttrList} =
"IODev ". "IODev ".
"qos:".join(",",keys %MQTT::qos)." ". "qos:".join(",",keys %MQTT::qos)." ".
"retain:0,1 ".
"publishSet ". "publishSet ".
"publishSet_.* ". "publishSet_.* ".
"subscribeReading_.* ". "subscribeReading_.* ".
@ -83,10 +84,10 @@ sub Set($@) {
my $value = $a[2]; my $value = $a[2];
my $msgid; my $msgid;
if (defined $value) { if (defined $value) {
$msgid = send_publish($hash->{IODev}, topic => $hash->{publishSets}->{$command}->{topic}, message => $value, qos => $hash->{qos}); $msgid = send_publish($hash->{IODev}, topic => $hash->{publishSets}->{$command}->{topic}, message => $value, qos => $hash->{qos}, retain => $hash->{retain});
readingsSingleUpdate($hash,$command,$value,1); readingsSingleUpdate($hash,$command,$value,1);
} else { } else {
$msgid = send_publish($hash->{IODev}, topic => $hash->{publishSets}->{""}->{topic}, message => $command, qos => $hash->{qos}); $msgid = send_publish($hash->{IODev}, topic => $hash->{publishSets}->{""}->{topic}, message => $command, qos => $hash->{qos}, retain => $hash->{retain});
readingsSingleUpdate($hash,"state",$command,1); readingsSingleUpdate($hash,"state",$command,1);
} }
$hash->{message_ids}->{$msgid}++ if defined $msgid; $hash->{message_ids}->{$msgid}++ if defined $msgid;