2014-10-01 22:03:18 +00:00
|
|
|
##############################################
|
|
|
|
#
|
|
|
|
# fhem bridge to mqtt (see http://mqtt.org)
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014 Norbert Truchsess
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
##############################################
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
my %sets = (
|
|
|
|
);
|
|
|
|
|
|
|
|
my %gets = (
|
|
|
|
"version" => "",
|
|
|
|
"readings" => ""
|
|
|
|
);
|
|
|
|
|
|
|
|
sub MQTT_BRIDGE_Initialize($) {
|
|
|
|
|
|
|
|
my $hash = shift @_;
|
|
|
|
|
|
|
|
# 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 ".
|
2014-10-02 11:59:37 +00:00
|
|
|
"qos:".join(",",keys %MQTT::qos)." ".
|
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
|
|
|
|
))
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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}");
|
2014-10-01 22:03:18 +00:00
|
|
|
foreach my $event (@{$dev->{CHANGED}}) {
|
|
|
|
$event =~ /^([^:]+)(: )?(.*)$/;
|
2014-10-02 11:59:37 +00:00
|
|
|
Log3($hash->{NAME},5,"$event, '".((defined $1) ? $1 : "-undef-")."', '".((defined $3) ? $3 : "-undef-")."'");
|
2014-10-01 22:03:18 +00:00
|
|
|
if (defined $3 and $3 ne "") {
|
|
|
|
if (defined $hash->{publishReadings}->{$1}) {
|
2014-10-02 11:59:37 +00:00
|
|
|
send_publish($hash->{IODev}, topic => $hash->{publishReadings}->{$1}, message => $3, qos => $hash->{qos});
|
2014-10-01 22:03:18 +00:00
|
|
|
readingsSingleUpdate($hash,"transmission-state","publish sent",1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (defined $hash->{publishState}) {
|
2014-10-02 11:59:37 +00:00
|
|
|
send_publish($hash->{IODev}, topic => $hash->{publishState}, message => $1, qos => $hash->{qos});
|
2014-10-01 22:03:18 +00:00
|
|
|
readingsSingleUpdate($hash,"transmission-state","publish sent",1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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") {
|
2014-10-08 13:07:07 +00:00
|
|
|
unless (defined $hash->{subscribeSets}->{$value} and $hash->{subscribeSets}->{$value} eq $2) {
|
|
|
|
unless (defined $hash->{subscribeSets}->{$value}) {
|
|
|
|
client_subscribe_topic($hash,$value);
|
2014-10-01 22:03:18 +00:00
|
|
|
}
|
2014-10-08 13:07:07 +00:00
|
|
|
$hash->{subscribeSets}->{$value} = $2;
|
2014-10-01 22:03:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach my $topic (keys %{$hash->{subscribeSets}}) {
|
2014-10-08 13:07:07 +00:00
|
|
|
if ($hash->{subscribeSets}->{$topic} eq $2) {
|
|
|
|
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;
|
|
|
|
};
|
2014-10-02 11:59:37 +00:00
|
|
|
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) = @_;
|
|
|
|
if (defined (my $command = $hash->{subscribeSets}->{$topic})) {
|
|
|
|
my @args = split ("[ \t]+",$message);
|
|
|
|
if ($command eq "") {
|
2014-10-02 11:59:37 +00:00
|
|
|
Log3($hash->{NAME},5,"calling DoSet($hash->{DEF}".(@args ? ",".join(",",@args) : ""));
|
|
|
|
DoSet($hash->{DEF},@args);
|
2014-10-01 22:03:18 +00:00
|
|
|
} else {
|
2014-10-02 11:59:37 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
=pod
|
|
|
|
=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>
|
|
|
|
acts as a bridge in between an fhem-device and <a href="http://mqtt.org">mqtt</a>-topics.
|
|
|
|
<br><br>
|
|
|
|
requires a <a href="#MQTT">MQTT</a>-device as IODev<br><br>
|
|
|
|
|
2014-10-06 10:43:46 +00:00
|
|
|
Note: this module is based on module <a href="https://metacpan.org/pod/distribution/Net-MQTT/lib/Net/MQTT.pod">Net::MQTT</a>.
|
2014-10-01 22:03:18 +00:00
|
|
|
<br><br>
|
|
|
|
|
|
|
|
<a name="MQTT_BRIDGEdefine"></a>
|
|
|
|
<b>Define</b><br>
|
|
|
|
<ul><br>
|
|
|
|
<code>define <name> MQTT_BRIDGE <fhem-device-name></code> <br>
|
|
|
|
Specifies the MQTT device.<br>
|
|
|
|
<fhem-device-name> is the fhem-device this MQTT_BRIDGE is linked to.<br>
|
|
|
|
<br>
|
|
|
|
<a name="MQTT_BRIDGEget"></a>
|
|
|
|
<b>Get</b>
|
|
|
|
<ul>
|
|
|
|
<li>
|
|
|
|
<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.
|
|
|
|
</li><br>
|
|
|
|
</ul>
|
|
|
|
<br><br>
|
|
|
|
|
|
|
|
<a name="MQTT_BRIDGEattr"></a>
|
|
|
|
<b>Attributes</b><br>
|
|
|
|
<ul>
|
|
|
|
<li>
|
2014-10-05 19:51:16 +00:00
|
|
|
<code>attr <name> subscribeSet <topic></code><br>
|
2014-10-01 22:03:18 +00:00
|
|
|
configures a topic that will issue a 'set <message> whenever a message is received<br>
|
|
|
|
</li>
|
|
|
|
<li>
|
2014-10-05 19:51:16 +00:00
|
|
|
<code>attr <name> subscribeSet_<reading> <topic></code><br>
|
2014-10-01 22:03:18 +00:00
|
|
|
configures a topic that will issue a 'set <reading> <message> whenever a message is received<br>
|
|
|
|
</li>
|
|
|
|
<li>
|
2014-10-05 19:51:16 +00:00
|
|
|
<code>attr <name> publishState <topic></code><br>
|
2014-10-01 22:03:18 +00:00
|
|
|
configures a topic such that a message is sent to topic whenever the device state changes.<br>
|
|
|
|
</li>
|
|
|
|
<li>
|
2014-10-05 19:51:16 +00:00
|
|
|
<code>attr <name> publishReading_<reading> <topic></code><br>
|
2014-10-01 22:03:18 +00:00
|
|
|
configures a topic such that a message is sent to topic whenever the device readings value changes.<br>
|
|
|
|
</li>
|
|
|
|
<li>
|
2014-10-05 19:51:16 +00:00
|
|
|
<code>attr <name> publish-topic-base <topic></code><br>
|
2014-10-01 22:03:18 +00:00
|
|
|
this is used as base path when issueing 'get <device> readings' to construct topics to publish to based on the devices existing readings<br>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</ul>
|
|
|
|
</ul>
|
|
|
|
<br>
|
|
|
|
|
|
|
|
=end html
|
|
|
|
=cut
|