| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- ##############################################
- #
- # fhem bridge to mqtt (see http://mqtt.org)
- #
- # Copyright (C) 2017 Stephan Eisler
- # Copyright (C) 2014 - 2016 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: 10_MQTT_DEVICE.pm 13318 2017-02-03 09:42:52Z eisler $
- #
- ##############################################
- use strict;
- use warnings;
- my %gets = (
- "version" => "",
- );
- sub MQTT_DEVICE_Initialize($) {
- my $hash = shift @_;
- # Consumer
- $hash->{DefFn} = "MQTT::DEVICE::Define";
- $hash->{UndefFn} = "MQTT::Client_Undefine";
- $hash->{SetFn} = "MQTT::DEVICE::Set";
- $hash->{AttrFn} = "MQTT::DEVICE::Attr";
-
- $hash->{AttrList} =
- "IODev ".
- "qos:".join(",",keys %MQTT::qos)." ".
- "retain:0,1 ".
- "publishSet ".
- "publishSet_.* ".
- "subscribeReading_.* ".
- "autoSubscribeReadings ".
- $main::readingFnAttributes;
-
- main::LoadModule("MQTT");
- }
- package MQTT::DEVICE;
- use strict;
- use warnings;
- use GPUtils qw(:all);
- use Net::MQTT::Constants;
- BEGIN {
- MQTT->import(qw(:all));
- GP_Import(qw(
- CommandDeleteReading
- CommandAttr
- readingsSingleUpdate
- Log3
- ))
- };
- sub Define() {
- my ( $hash, $def ) = @_;
- $hash->{sets} = {};
- return MQTT::Client_Define($hash,$def);
- };
- sub Set($$$@) {
- my ($hash,$name,$command,@values) = @_;
- return "Need at least one parameters" unless defined $command;
- return "Unknown argument $command, choose one of " . join(" ", map {$hash->{sets}->{$_} eq "" ? $_ : "$_:".$hash->{sets}->{$_}} sort keys %{$hash->{sets}})
- if(!defined($hash->{sets}->{$command}));
- my $msgid;
- if (@values) {
- my $value = join " ",@values;
- $msgid = send_publish($hash->{IODev}, topic => $hash->{publishSets}->{$command}->{topic}, message => $value, qos => $hash->{qos}, retain => $hash->{retain});
- readingsSingleUpdate($hash,$command,$value,1);
- } else {
- $msgid = send_publish($hash->{IODev}, topic => $hash->{publishSets}->{""}->{topic}, message => $command, qos => $hash->{qos}, retain => $hash->{retain});
- readingsSingleUpdate($hash,"state",$command,1);
- }
- $hash->{message_ids}->{$msgid}++ if defined $msgid;
- readingsSingleUpdate($hash,"transmission-state","outgoing publish sent",1);
- return undef;
- }
- sub Attr($$$$) {
- my ($command,$name,$attribute,$value) = @_;
- my $hash = $main::defs{$name};
- ATTRIBUTE_HANDLER: {
- $attribute =~ /^subscribeReading_(.+)/ and do {
- if ($command eq "set") {
- unless (defined $hash->{subscribeReadings}->{$value} and $hash->{subscribeReadings}->{$value} eq $1) {
- unless (defined $hash->{subscribeReadings}->{$value}) {
- client_subscribe_topic($hash,$value);
- }
- $hash->{subscribeReadings}->{$value} = $1;
- }
- } else {
- foreach my $topic (keys %{$hash->{subscribeReadings}}) {
- if ($hash->{subscribeReadings}->{$topic} eq $1) {
- client_unsubscribe_topic($hash,$topic);
- delete $hash->{subscribeReadings}->{$topic};
- CommandDeleteReading(undef,"$hash->{NAME} $1");
- last;
- }
- }
- }
- last;
- };
- $attribute eq "autoSubscribeReadings" and do {
- if ($command eq "set") {
- unless (defined $hash->{'.autoSubscribeTopic'} and $hash->{'.autoSubscribeTopic'} eq $value) {
- if (defined $hash->{'.autoSubscribeTopic'}) {
- client_unsubscribe_topic($hash,$hash->{'.autoSubscribeTopic'});
- }
- $hash->{'.autoSubscribeTopic'} = $value;
- $hash->{'.autoSubscribeExpr'} = topic_to_regexp($value);
- client_subscribe_topic($hash,$value);
- }
- } else {
- if (defined $hash->{'.autoSubscribeTopic'}) {
- client_unsubscribe_topic($hash,$hash->{'.autoSubscribeTopic'});
- delete $hash->{'.autoSubscribeTopic'};
- delete $hash->{'.autoSubscribeExpr'};
- }
- }
- last;
- };
- $attribute =~ /^publishSet(_?)(.*)/ and do {
- if ($command eq "set") {
- my @values = split ("[ \t]+",$value);
- my $topic = pop @values;
- $hash->{publishSets}->{$2} = {
- 'values' => \@values,
- topic => $topic,
- };
- if ($2 eq "") {
- foreach my $set (@values) {
- $hash->{sets}->{$set}="";
- }
- } else {
- $hash->{sets}->{$2}=join(",",@values);
- }
- } else {
- if ($2 eq "") {
- foreach my $set (@{$hash->{publishSets}->{$2}->{'values'}}) {
- delete $hash->{sets}->{$set};
- }
- } else {
- CommandDeleteReading(undef,"$hash->{NAME} $2");
- delete $hash->{sets}->{$2};
- }
- delete $hash->{publishSets}->{$2};
- }
- last;
- };
- client_attr($hash,$command,$name,$attribute,$value);
- }
- }
- sub onmessage($$$) {
- my ($hash,$topic,$message) = @_;
- if (defined (my $reading = $hash->{subscribeReadings}->{$topic})) {
- Log3($hash->{NAME},5,"calling readingsSingleUpdate($hash->{NAME},$reading,$message,1");
- readingsSingleUpdate($hash,$reading,$message,1);
- } elsif ($topic =~ $hash->{'.autoSubscribeExpr'}) {
- Log3($hash->{NAME},5,"calling readingsSingleUpdate($hash->{NAME},$1,$message,1");
- CommandAttr(undef,"$hash->{NAME} subscribeReading_$1 $topic");
- readingsSingleUpdate($hash,$1,$message,1);
- }
- }
- 1;
- =pod
- =item [device]
- =item summary MQTT_DEVICE acts as a fhem-device that is mapped to mqtt-topics
- =begin html
- <a name="MQTT_DEVICE"></a>
- <h3>MQTT_DEVICE</h3>
- <ul>
- <p>acts as a fhem-device that is mapped to <a href="http://mqtt.org/">mqtt</a>-topics.</p>
- <p>requires a <a href="#MQTT">MQTT</a>-device as IODev<br/>
- 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>
- <a name="MQTT_DEVICEdefine"></a>
- <p><b>Define</b></p>
- <ul>
- <p><code>define <name> MQTT_DEVICE</code><br/>
- Specifies the MQTT device.</p>
- </ul>
- <a name="MQTT_DEVICEset"></a>
- <p><b>Set</b></p>
- <ul>
- <li>
- <p><code>set <name> <command></code><br/>
- sets reading 'state' and publishes the command to topic configured via attr publishSet</p>
- </li>
- <li>
- <p><code>set <name> <h;reading> <value></code><br/>
- sets reading <h;reading> and publishes the command to topic configured via attr publishSet_<h;reading></p>
- </li>
- </ul>
- <a name="MQTT_DEVICEattr"></a>
- <p><b>Attributes</b></p>
- <ul>
- <li>
- <p><code>attr <name> publishSet [<commands>] <topic></code><br/>
- configures set commands that may be used to both set reading 'state' and publish to configured topic</p>
- </li>
- <li>
- <p><code>attr <name> publishSet_<reading> [<values>] <topic></code><br/>
- configures reading that may be used to both set 'reading' (to optionally configured values) and publish to configured topic</p>
- </li>
- <li>
- <p><code>attr <name> autoSubscribeReadings <topic></code><br/>
- specify a mqtt-topic pattern with wildcard (e.c. 'myhouse/kitchen/+') and MQTT_DEVICE automagically creates readings based on the wildcard-match<br/>
- e.g a message received with topic 'myhouse/kitchen/temperature' would create and update a reading 'temperature'</p>
- </li>
- <li>
- <p><code>attr <name> subscribeReading_<reading> <topic></code><br/>
- mapps a reading to a specific topic. The reading is updated whenever a message to the configured topic arrives</p>
- </li>
- </ul>
- </ul>
- =end html
- =cut
|