2014-10-01 22:03:18 +00:00
|
|
|
##############################################
|
|
|
|
#
|
|
|
|
# fhem bridge to mqtt (see http://mqtt.org)
|
|
|
|
#
|
2018-09-17 12:57:29 +00:00
|
|
|
# Copyright (C) 2018 Alexander Schulz
|
2017-02-03 09:42:52 +00:00
|
|
|
# Copyright (C) 2017 Stephan Eisler
|
|
|
|
# Copyright (C) 2014 - 2016 Norbert Truchsess
|
2014-10-01 22:03:18 +00:00
|
|
|
#
|
|
|
|
# This file is part of fhem.
|
|
|
|
#
|
|
|
|
# Fhem is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Fhem is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with fhem. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
2021-09-22 20:19:28 +00:00
|
|
|
# $Id: 10_MQTT_BRIDGE.pm 23658 2021-02-01 20:58:53Z hexenmeister $
|
2014-10-01 22:03:18 +00:00
|
|
|
#
|
|
|
|
##############################################
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
my %sets = (
|
|
|
|
);
|
|
|
|
|
|
|
|
my %gets = (
|
|
|
|
"version" => "",
|
|
|
|
"readings" => ""
|
|
|
|
);
|
|
|
|
|
|
|
|
sub MQTT_BRIDGE_Initialize($) {
|
|
|
|
|
|
|
|
my $hash = shift @_;
|
|
|
|
|
2018-04-29 12:53:05 +00:00
|
|
|
require "$main::attr{global}{modpath}/FHEM/00_MQTT.pm";
|
|
|
|
|
2014-10-01 22:03:18 +00:00
|
|
|
# Consumer
|
2014-10-02 11:59:37 +00:00
|
|
|
$hash->{DefFn} = "MQTT::Client_Define";
|
|
|
|
$hash->{UndefFn} = "MQTT::Client_Undefine";
|
|
|
|
$hash->{GetFn} = "MQTT::BRIDGE::Get";
|
|
|
|
$hash->{NotifyFn} = "MQTT::BRIDGE::Notify";
|
|
|
|
$hash->{AttrFn} = "MQTT::BRIDGE::Attr";
|
2014-10-01 22:03:18 +00:00
|
|
|
|
|
|
|
$hash->{AttrList} =
|
|
|
|
"IODev ".
|
2017-08-26 22:15:46 +00:00
|
|
|
#"qos:".join(",",keys %MQTT::qos)." ".
|
|
|
|
"qos ".
|
|
|
|
"retain ".
|
2014-10-01 22:03:18 +00:00
|
|
|
"publish-topic-base ".
|
|
|
|
"publishState ".
|
|
|
|
"publishReading_.* ".
|
|
|
|
"subscribeSet ".
|
|
|
|
"subscribeSet_.* ".
|
|
|
|
$main::readingFnAttributes;
|
2014-10-02 11:59:37 +00:00
|
|
|
|
|
|
|
main::LoadModule("MQTT");
|
2014-10-01 22:03:18 +00:00
|
|
|
}
|
|
|
|
|
2014-10-02 11:59:37 +00:00
|
|
|
package MQTT::BRIDGE;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use GPUtils qw(:all);
|
|
|
|
|
|
|
|
use Net::MQTT::Constants;
|
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
MQTT->import(qw(:all));
|
|
|
|
|
|
|
|
GP_Import(qw(
|
|
|
|
AttrVal
|
|
|
|
CommandAttr
|
|
|
|
readingsSingleUpdate
|
|
|
|
Log3
|
|
|
|
DoSet
|
2017-08-26 22:15:46 +00:00
|
|
|
fhem
|
|
|
|
defs
|
|
|
|
AttrVal
|
|
|
|
ReadingsVal
|
2014-10-02 11:59:37 +00:00
|
|
|
))
|
|
|
|
};
|
|
|
|
|
|
|
|
sub Get($$@) {
|
2014-10-01 22:03:18 +00:00
|
|
|
my ($hash, $name, $command) = @_;
|
|
|
|
return "Need at least one parameters" unless (defined $command);
|
|
|
|
return "Unknown argument $command, choose one of " . join(" ", sort keys %gets)
|
|
|
|
unless (defined($gets{$command}));
|
|
|
|
|
|
|
|
COMMAND_HANDLER: {
|
|
|
|
# populate dynamically from keys %{$defs{$sdev}{READINGS}}
|
|
|
|
$command eq "readings" and do {
|
|
|
|
my $base = AttrVal($name,"publish-topic-base","/$hash->{DEF}/");
|
|
|
|
foreach my $reading (keys %{$main::defs{$hash->{DEF}}{READINGS}}) {
|
2014-10-02 11:59:37 +00:00
|
|
|
unless (defined AttrVal($name,"publishReading_$reading",undef)) {
|
|
|
|
CommandAttr($hash,"$name publishReading_$reading $base$reading");
|
2014-10-01 22:03:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
last;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
2017-08-26 22:15:46 +00:00
|
|
|
#use Data::Dumper;
|
2014-10-02 11:59:37 +00:00
|
|
|
sub Notify() {
|
2014-10-01 22:03:18 +00:00
|
|
|
my ($hash,$dev) = @_;
|
|
|
|
|
2014-10-02 11:59:37 +00:00
|
|
|
Log3($hash->{NAME},5,"Notify for $dev->{NAME}");
|
2017-08-26 22:15:46 +00:00
|
|
|
#Log3($hash->{NAME},5,">>>>=====".Dumper($dev->{CHANGED}));
|
2014-10-01 22:03:18 +00:00
|
|
|
foreach my $event (@{$dev->{CHANGED}}) {
|
|
|
|
$event =~ /^([^:]+)(: )?(.*)$/;
|
2017-08-26 22:15:46 +00:00
|
|
|
#Log3($hash->{NAME},5,">>>>>>>>>>>>>>>>>>");
|
2014-10-02 11:59:37 +00:00
|
|
|
Log3($hash->{NAME},5,"$event, '".((defined $1) ? $1 : "-undef-")."', '".((defined $3) ? $3 : "-undef-")."'");
|
2014-10-08 15:31:39 +00:00
|
|
|
my $msgid;
|
2014-10-01 22:03:18 +00:00
|
|
|
if (defined $3 and $3 ne "") {
|
|
|
|
if (defined $hash->{publishReadings}->{$1}) {
|
2017-08-26 22:15:46 +00:00
|
|
|
my $retain = $hash->{".retain"}->{$1};
|
|
|
|
$retain = $hash->{".retain"}->{'*'} unless defined($retain);
|
|
|
|
my $qos = $hash->{".qos"}->{$1};
|
|
|
|
$qos = $hash->{".qos"}->{'*'} unless defined($qos);
|
|
|
|
#Log3($hash->{NAME},1,">>>>>>>>>>>>>>>>>> RETAIN: ".$retain); $retain=0; ### TEST
|
|
|
|
$msgid = send_publish($hash->{IODev}, topic => $hash->{publishReadings}->{$1}, message => $3, qos => $qos, retain => $retain);
|
2014-10-08 15:31:39 +00:00
|
|
|
readingsSingleUpdate($hash,"transmission-state","outgoing publish sent",1);
|
2014-10-01 22:03:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (defined $hash->{publishState}) {
|
2017-08-26 22:15:46 +00:00
|
|
|
my $retain = $hash->{".retain"}->{""};
|
|
|
|
$retain = $hash->{".retain"}->{'*'} unless defined($retain);
|
|
|
|
my $qos = $hash->{".qos"}->{""};
|
|
|
|
$qos = $hash->{".qos"}->{'*'} unless defined($qos);
|
|
|
|
#Log3($hash->{NAME},1,">>>>>>>>>>>>>>>>>> RETAIN: ".$retain); $retain=0; ### TEST
|
|
|
|
$msgid = send_publish($hash->{IODev}, topic => $hash->{publishState}, message => $1, qos => $qos, retain => $retain);
|
2014-10-08 15:31:39 +00:00
|
|
|
readingsSingleUpdate($hash,"transmission-state","outgoing publish sent",1);
|
2014-10-01 22:03:18 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-08 15:31:39 +00:00
|
|
|
$hash->{message_ids}->{$msgid}++ if defined $msgid;
|
2014-10-01 22:03:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-02 11:59:37 +00:00
|
|
|
sub Attr($$$$) {
|
2014-10-01 22:03:18 +00:00
|
|
|
my ($command,$name,$attribute,$value) = @_;
|
|
|
|
|
|
|
|
my $hash = $main::defs{$name};
|
|
|
|
ATTRIBUTE_HANDLER: {
|
|
|
|
$attribute =~ /^subscribeSet(_?)(.*)/ and do {
|
|
|
|
if ($command eq "set") {
|
2017-08-26 22:15:46 +00:00
|
|
|
my ($mqos, $mretain,$mtopic, $mvalue, $mcmd)=MQTT::parsePublishCmdStr($value);
|
|
|
|
if(!defined($mtopic)) { return "topic may not be empty";}
|
|
|
|
unless (defined $hash->{subscribeSets}->{$mtopic}->{name} and $hash->{subscribeSets}->{$mtopic}->{name} eq $2) {
|
|
|
|
unless (defined $hash->{subscribeSets}->{$mtopic}->{name}) {
|
|
|
|
client_subscribe_topic($hash,$mtopic,$mqos,$mretain);
|
2014-10-01 22:03:18 +00:00
|
|
|
}
|
2017-08-26 22:15:46 +00:00
|
|
|
$hash->{subscribeSets}->{$mtopic}->{name} = $2;
|
|
|
|
$hash->{subscribeSets}->{$mtopic}->{cmd} = $mcmd;
|
2014-10-01 22:03:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach my $topic (keys %{$hash->{subscribeSets}}) {
|
2017-08-26 22:15:46 +00:00
|
|
|
if ($hash->{subscribeSets}->{$topic}->{name} eq $2) {
|
2014-10-08 13:07:07 +00:00
|
|
|
client_unsubscribe_topic($hash,$topic);
|
2014-10-01 22:03:18 +00:00
|
|
|
delete $hash->{subscribeSets}->{$topic};
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last;
|
|
|
|
};
|
|
|
|
$attribute eq "publishState" and do {
|
|
|
|
if ($command eq "set") {
|
|
|
|
$hash->{publishState} = $value;
|
|
|
|
} else {
|
|
|
|
delete $hash->{publishState};
|
|
|
|
}
|
|
|
|
last;
|
|
|
|
};
|
|
|
|
$attribute =~ /^publishReading_(.+)$/ and do {
|
|
|
|
if ($command eq "set") {
|
|
|
|
$hash->{publishReadings}->{$1} = $value;
|
|
|
|
} else {
|
|
|
|
delete $hash->{publishReadings}->{$1};
|
|
|
|
}
|
|
|
|
last;
|
|
|
|
};
|
2017-08-26 22:15:46 +00:00
|
|
|
return client_attr($hash,$command,$name,$attribute,$value);
|
2014-10-01 22:03:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-02 11:59:37 +00:00
|
|
|
sub onmessage($$$) {
|
2014-10-01 22:03:18 +00:00
|
|
|
my ($hash,$topic,$message) = @_;
|
2017-08-26 22:15:46 +00:00
|
|
|
if (defined (my $command = $hash->{subscribeSets}->{$topic}->{name})) {
|
|
|
|
my $do=1;
|
|
|
|
if(defined (my $cmd = $hash->{subscribeSets}->{$topic}->{cmd})) {
|
|
|
|
Log3($hash->{NAME},5,"evaluating cmd: $cmd");
|
|
|
|
my $name = $hash->{NAME};
|
|
|
|
my $device = $hash->{DEF};
|
|
|
|
$do=eval($cmd);
|
|
|
|
Log3($hash->{NAME},1,"ERROR evaluating $cmd: $@") if($@);
|
|
|
|
$do=1 if (!defined($do));
|
|
|
|
}
|
|
|
|
if($do) {
|
|
|
|
my @args = split ("[ \t]+",$message);
|
|
|
|
if ($command eq "") {
|
|
|
|
Log3($hash->{NAME},5,"calling DoSet($hash->{DEF}".(@args ? ",".join(",",@args) : ""));
|
|
|
|
DoSet($hash->{DEF},@args);
|
|
|
|
} else {
|
|
|
|
Log3($hash->{NAME},5,"calling DoSet($hash->{DEF},$command".(@args ? ",".join(",",@args) : ""));
|
|
|
|
DoSet($hash->{DEF},$command,@args);
|
|
|
|
}
|
2014-10-01 22:03:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
1;
|
2021-02-01 20:58:53 +00:00
|
|
|
__END__
|
2014-10-01 22:03:18 +00:00
|
|
|
=pod
|
2017-02-03 09:42:52 +00:00
|
|
|
=item [device]
|
2021-02-01 20:58:53 +00:00
|
|
|
=item summary MQTT_BRIDGE is outdated, consider using MQTT_GENERIC_BRIDGE instead!
|
2014-10-01 22:03:18 +00:00
|
|
|
=begin html
|
|
|
|
|
|
|
|
<a name="MQTT_BRIDGE"></a>
|
2014-10-05 19:51:16 +00:00
|
|
|
<h3>MQTT_BRIDGE</h3>
|
2014-10-01 22:03:18 +00:00
|
|
|
<ul>
|
2021-02-01 20:58:53 +00:00
|
|
|
<p>acts as a bridge in between a single fhem-device and <a href="http://mqtt.org/">mqtt</a>-topics.</p>
|
|
|
|
<p><b>As newer module <a href="#MQTT_GENERIC_BRIDGE">MQTT_GENERIC_BRIDGE</a> offers options to bridge multiple fhem-device to <a href="http://mqtt.org/">mqtt</a>-topics,
|
|
|
|
it's highly recommended to use the generic variant!</b><br>
|
|
|
|
Additionally, MQTT_GENERIC_BRIDGE is also compatible with MQTT2_.*-TYPE of interfaces and thus requires less prerequisites on the Perl side!</p>
|
2014-10-09 08:49:59 +00:00
|
|
|
<p>requires a <a href="#MQTT">MQTT</a>-device as IODev<br/>
|
2014-11-09 20:35:34 +00:00
|
|
|
Note: this module is based on <a href="https://metacpan.org/pod/distribution/Net-MQTT/lib/Net/MQTT.pod">Net::MQTT</a> which needs to be installed from CPAN first.</p>
|
2014-10-01 22:03:18 +00:00
|
|
|
<a name="MQTT_BRIDGEdefine"></a>
|
2014-10-09 08:49:59 +00:00
|
|
|
<p><b>Define</b></p>
|
|
|
|
<ul>
|
|
|
|
<p><code>define <name> MQTT_BRIDGE <fhem-device-name></code></p>
|
|
|
|
<p>Specifies the MQTT device.<br/>
|
|
|
|
<fhem-device-name> is the fhem-device this MQTT_BRIDGE is linked to.</p>
|
|
|
|
</ul>
|
|
|
|
<a name="MQTT_BRIDGEget"></a>
|
|
|
|
<p><b>Get</b></p>
|
|
|
|
<ul>
|
|
|
|
<li>
|
|
|
|
<p><code>get <name> readings</code><br/>
|
|
|
|
retrieves all existing readings from fhem-device and configures (default-)topics for them.<br/>
|
|
|
|
attribute 'publish-topic-base' is prepended if set.</p>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
<a name="MQTT_BRIDGEattr"></a>
|
|
|
|
<p><b>Attributes</b></p>
|
|
|
|
<ul>
|
|
|
|
<li>
|
2017-08-26 22:15:46 +00:00
|
|
|
<p><code>attr <name> subscribeSet [{Perl-expression}] [qos:?] [retain:?] <topic></code><br/>
|
|
|
|
configures a topic that will issue a 'set <message> whenever a message is received<br/>
|
|
|
|
QOS and ratain can be optionally defined for this topic. <br/>
|
2017-09-28 20:39:00 +00:00
|
|
|
Furthermore, a Perl statement can be provided which is executed when the message is received. The following variables are available for the expression: $hash, $name, $topic, $message, $device (linked device). Return value decides whether reading is set (true (e.g., 1) or undef) or discarded (false (e.g., 0)).
|
2017-08-26 22:15:46 +00:00
|
|
|
</p>
|
|
|
|
<p>Example:<br/>
|
|
|
|
<code>attr mqttest subscribeSet {fhem("do somethin")} /topic/cmd</code>
|
|
|
|
</p>
|
2014-10-09 08:49:59 +00:00
|
|
|
</li>
|
|
|
|
<li>
|
2017-08-26 22:15:46 +00:00
|
|
|
<p><code>attr <name> subscribeSet_<reading> [{Perl-expression}] [qos:?] [retain:?] <topic></code><br/>
|
|
|
|
configures a topic that will issue a 'set <reading> <message> whenever a message is received. see above
|
|
|
|
for Perl-Expression/QOS/retain</p>
|
|
|
|
<p>Example:<br/>
|
2017-09-28 20:39:00 +00:00
|
|
|
<code>attr mqttest subscribeSet_cmd {if ($message eq "config") fhem("set $device getconfig");; 0} /topic/cmd</code>
|
2017-08-26 22:15:46 +00:00
|
|
|
</p>
|
2014-10-09 08:49:59 +00:00
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<p><code>attr <name> publishState <topic></code><br/>
|
|
|
|
configures a topic such that a message is sent to topic whenever the device state changes.</p>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<p><code>attr <name> publishReading_<reading> <topic></code><br/>
|
|
|
|
configures a topic such that a message is sent to topic whenever the device readings value changes.</p>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<p><code>attr <name> publish-topic-base <topic></code><br/>
|
|
|
|
this is used as base path when issueing 'get <device> readings' to construct topics to publish to based on the devices existing readings</p>
|
|
|
|
</li>
|
2017-08-26 22:15:46 +00:00
|
|
|
<li>
|
|
|
|
<p><code>attr <name> retain <flags> ...</code><br/>
|
|
|
|
Specifies the retain flag for all or specific readings. Possible values are 0, 1</p>
|
|
|
|
<p>Examples:<br/>
|
|
|
|
<code>attr mqttest retain 0</code><br/>
|
|
|
|
defines retain 0 for all readings/topics (due to downward compatibility)<br>
|
|
|
|
<code> retain *:0 1 test:1</code><br/>
|
|
|
|
defines retain 0 for all readings/topics except the reading 'test'. Retain for 'test' is 1<br>
|
|
|
|
</p>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<p><code>attr <name> qos <flags> ...</code><br/>
|
|
|
|
Specifies the QOS flag for all or specific readings. Possible values are 0, 1 or 2. Constants may be also used: at-most-once = 0, at-least-once = 1, exactly-once = 2</p>
|
|
|
|
<p>Examples:<br/>
|
|
|
|
<code>attr mqttest qos 0</code><br/>
|
|
|
|
defines QOS 0 for all readings/topics (due to downward compatibility)<br>
|
|
|
|
<code> retain *:0 1 test:1</code><br/>
|
|
|
|
defines QOS 0 for all readings/topics except the reading 'test'. Retain for 'test' is 1<br>
|
|
|
|
</p>
|
|
|
|
</li>
|
2014-10-01 22:03:18 +00:00
|
|
|
</ul>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
=end html
|
|
|
|
=cut
|