voller Funktionsumfang für Version 1.0. Anpassungen für korrektes logging

This commit is contained in:
Marko Oldenburg 2015-08-02 13:53:13 +02:00
parent b834a03229
commit d714b61a43

View File

@ -1,11 +1,29 @@
# $Id: 98_AMAD.pm 1001 2015-07-23 12:58:05Z leongaultier $ ################################################################
############################################################################## # $Id$
# Developed with Kate
# #
# 98_AMAD.pm # (c) 2015 Copyright: Leon Gaultier (leongaultier at gmail dot com)
# All rights reserved
# #
# Get and Set http Requests from/to AutomagicAPP Device # This script 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
# any later version.
# #
############################################################################## # The GNU General Public License can be found at
# http://www.gnu.org/copyleft/gpl.html.
# A copy is found in the textfile GPL.txt and important notices to the license
# from the author is found in LICENSE.txt distributed with these scripts.
#
# This script 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.
#
################################################################
package main;
use strict; use strict;
use warnings; use warnings;
@ -23,7 +41,7 @@ sub AMAD_Initialize($) {
$hash->{AttrFn} = "AMAD_Attr"; $hash->{AttrFn} = "AMAD_Attr";
$hash->{ReadFn} = "AMAD_Read"; $hash->{ReadFn} = "AMAD_Read";
$hash->{AttrList} = $hash->{AttrList} =
"interval disable:0,1 nonblocking:0,1 " "interval disable:0 "
. $readingFnAttributes; . $readingFnAttributes;
} }
@ -80,22 +98,22 @@ sub AMAD_Attr(@) {
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
InternalTimer(gettimeofday()+2, "AMAD_GetUpdateTimer", $hash, 0) if ($hash->{STATE} eq "disabled"); InternalTimer(gettimeofday()+2, "AMAD_GetUpdateTimer", $hash, 0) if ($hash->{STATE} eq "disabled");
$hash->{STATE}='active'; $hash->{STATE}='active';
Log3 $name, 4, "AMAD ($name) - enabled"; Log3 $name, 3, "AMAD ($name) - enabled";
} else { } else {
$hash->{STATE} = 'disabled'; $hash->{STATE} = 'disabled';
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
Log3 $name, 4, "AMAD ($name) - disabled"; Log3 $name, 3, "AMAD ($name) - disabled";
} }
} elsif ($cmd eq "del") { } elsif ($cmd eq "del") {
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
InternalTimer(gettimeofday()+2, "AMAD_GetUpdateTimer", $hash, 0) if ($hash->{STATE} eq "disabled"); InternalTimer(gettimeofday()+2, "AMAD_GetUpdateTimer", $hash, 0) if ($hash->{STATE} eq "disabled");
$hash->{STATE}='active'; $hash->{STATE}='active';
Log3 $name, 4, "AMAD ($name) - enabled"; Log3 $name, 3, "AMAD ($name) - enabled";
} }
} else { } else {
if($cmd eq "set") { if($cmd eq "set") {
$attr{$name}{$attrName} = $attrVal; $attr{$name}{$attrName} = $attrVal;
Log3 $name, 4, "AMAD ($name) - $attrName : $attrVal"; Log3 $name, 3, "AMAD ($name) - $attrName : $attrVal";
} elsif ($cmd eq "del") { } elsif ($cmd eq "del") {
} }
} }
@ -122,7 +140,7 @@ sub AMAD_GetUpdateTimer($)
AMAD_RetrieveAutomagicInfo($hash) if (ReadingsVal($name,"deviceState","online") eq "online" && $hash->{STATE} eq "active"); AMAD_RetrieveAutomagicInfo($hash) if (ReadingsVal($name,"deviceState","online") eq "online" && $hash->{STATE} eq "active");
InternalTimer(gettimeofday()+$hash->{INTERVAL}, "AMAD_GetUpdateTimer", $hash, 1); InternalTimer(gettimeofday()+$hash->{INTERVAL}, "AMAD_GetUpdateTimer", $hash, 1);
Log3 $name, 3, "AMAD ($name) - Call AMAD_GetUpdateTimer"; Log3 $name, 4, "AMAD ($name) - Call AMAD_GetUpdateTimer";
return 1; return 1;
} }
@ -133,25 +151,34 @@ sub AMAD_Set($$@)
my $list = "screenMsg" my $list = "screenMsg"
. " ttsMsg" . " ttsMsg"
. " setVolume" . " setVolume:slider,0,1,15"
. " deviceState:online,offline" . " deviceState:online,offline"
. " mediaPlayer:play,stop,next,back"; . " mediaPlayer:play,stop,next,back";
if ( lc $cmd eq 'screenmsg' if (lc $cmd eq 'screenmsg') {
|| lc $cmd eq 'ttsmsg' Log3 $name, 4, "AMAD ($name) - set $name $cmd ".join(" ", @val);
|| lc $cmd eq 'setvolume' return AMAD_SetScreenMsg ($hash, @val);
|| lc $cmd eq 'mediaplayer') { }
Log3 $name, 3, "AMAD ($name) - set $name $cmd ".join(" ", @val); elsif (lc $cmd eq 'ttsmsg') {
return AMAD_SetScreenMsg ($hash, @val); Log3 $name, 4, "AMAD ($name) - set $name $cmd ".join(" ", @val);
} return AMAD_SetTtsMsg ($hash, @val);
elsif ( lc $cmd eq 'devicestate') { }
Log3 $name, 3, "AMAD ($name) - set $name $cmd ".join(" ", @val); elsif (lc $cmd eq 'setvolume') {
my $v = join(" ", @val); Log3 $name, 4, "AMAD ($name) - set $name $cmd ".join(" ", @val);
return AMAD_SetVolume ($hash, @val);
}
elsif (lc $cmd eq 'mediaplayer') {
Log3 $name, 4, "AMAD ($name) - set $name $cmd ".join(" ", @val);
return AMAD_mediaplayer ($hash, @val);
}
elsif (lc $cmd eq 'devicestate') {
Log3 $name, 4, "AMAD ($name) - set $name $cmd ".join(" ", @val);
my $v = join(" ", @val);
readingsSingleUpdate ($hash,$cmd,$v,1); readingsSingleUpdate ($hash,$cmd,$v,1);
return undef; return undef;
} }
return "Unknown argument $cmd or wrong parameter(s), choose one of $list"; return "Unknown argument $cmd or wrong parameter(s), choose one of $list";
@ -177,7 +204,7 @@ sub AMAD_RetrieveAutomagicInfo($)
callback => \&AMAD_RetrieveAutomagicInfoFinished, callback => \&AMAD_RetrieveAutomagicInfoFinished,
} }
); );
Log3 $name, 3, "AMAD ($name) - NonblockingGet get URL"; Log3 $name, 4, "AMAD ($name) - NonblockingGet get URL";
} }
sub AMAD_RetrieveAutomagicInfoFinished($$$) sub AMAD_RetrieveAutomagicInfoFinished($$$)
@ -193,14 +220,14 @@ sub AMAD_RetrieveAutomagicInfoFinished($$$)
if (defined($err)) { if (defined($err)) {
if ($err ne "") if ($err ne "")
{ {
Log3 $name, 3, "AMAD ($name) - AMAD_RetrieveAutomagicInfoFinished: error while requesting AutomagicInfo: $err"; Log3 $name, 4, "AMAD ($name) - AMAD_RetrieveAutomagicInfoFinished: error while requesting AutomagicInfo: $err";
return; return;
} }
} }
if($data eq "" and exists($param->{code})) if($data eq "" and exists($param->{code}))
{ {
Log3 $name, 3, "AMAD ($name) - AMAD_RetrieveAutomagicInfoFinished: received http code ".$param->{code}." without any data after requesting AMAD AutomagicInfo"; Log3 $name, 4, "AMAD ($name) - AMAD_RetrieveAutomagicInfoFinished: received http code ".$param->{code}." without any data after requesting AMAD AutomagicInfo";
return; return;
} }
@ -242,7 +269,7 @@ sub AMAD_HTTP_POST($$)
doTrigger => 1, doTrigger => 1,
} }
); );
Log3 $name, 3, "AMAD ($name) - Send HTTP POST with URL $url"; Log3 $name, 4, "AMAD ($name) - Send HTTP POST with URL $url";
$hash->{STATE} = $state; $hash->{STATE} = $state;
@ -260,6 +287,7 @@ sub AMAD_SetScreenMsg($@)
$msg =~ s/\s/%20/g; $msg =~ s/\s/%20/g;
my $url = "http://" . $host . ":" . $port . "/automagic/screenMsg?message=$msg"; my $url = "http://" . $host . ":" . $port . "/automagic/screenMsg?message=$msg";
Log3 $name, 4, "AMAD ($name) - Sub AMAD_SetScreenMsg";
return AMAD_HTTP_POST ($hash,$url); return AMAD_HTTP_POST ($hash,$url);
} }
@ -279,11 +307,62 @@ sub AMAD_SetTtsMsg($@) {
} }
sub AMAD_SetVolume($@) { sub AMAD_SetVolume($@) {
my ($hash, @data) = @_;
my $name = $hash->{NAME};
my $host = $hash->{HOST};
my $port = $hash->{PORT};
my $vol = join(" ", @data);
my $url = "http://" . $host . ":" . $port . "/automagic/setVolume?volume=$vol";
AMAD_GetUpdateLocal($hash);
Log3 $name, 4, "AMAD ($name) - Starte Update GetUpdateLocal";
return AMAD_HTTP_POST ($hash,$url);
} }
sub AMAD_mediaplayer($@) { sub AMAD_mediaplayer($@) {
my ($hash, @data) = @_;
my $name = $hash->{NAME};
my $host = $hash->{HOST};
my $port = $hash->{PORT};
my $cmd = join(" ", @data);
my $url = "http://" . $host . ":" . $port . "/automagic/mediaPlayer?button=$cmd";
return AMAD_HTTP_POST ($hash,$url);
} }
1; 1;
=pod
=begin html_DE
<a name="AMAD"></a>
<h3>AMAD - Automagic Android Device</h3>
<ul>
Dieses Modul liefert, <b><u>in Verbindung mit der Android APP Automagic</u></b>, diverse Informationen von Android Ger&auml;ten.
Weiterhin gibt es Dir die M&ouml;glichkeit diese zu steuern. Bis jetzt ist das &auml;ndern der Medialautst&auml;rke m&ouml;glich,
sowie Play, Stop, Titel-Next, Titel-Back eines im Automagic-Flow ausgesuchten Players.<br>
An Informationen k&ouml;nnen alle wiedergegeben werden, welche von Automagic als Action in Form einer Variable gesammelt und als
HTTP Respons Text zur&uuml;ck gegeben werden.
F&uuml;r all diese Informationen/Aktionen ist ein sogenannter Flow in Automagic auf dem entsprechenden Android Ger&auml;t n&ouml;t.
Als Trigger im Flow wird immer ein HTTP Request mit einem selbst vergebenen Port und einer angepassten URL verwendet.
<br><br>
<a name="AMADdefine"></a>
<b>Define</b>
<ul>
<code>define &lt;name&gt; AMAD &lt;IP-ADRESSE&gt; &lt;PORT&gt; &lt;interval&gt;</code>
<br><br>
Beispiel:
<ul>
<code>define Nexus10Wohnzimmer AMAD 192.168.0.23 8080 180</code><br>
</ul>
<br>
Diese Anweisung erstellt ein neues AMAD-Device. Die Parameter IP-ADRESSE und PORT legen die IP Adresse des Android Ger&aaml;tes
sowie den unter Automagic im Trigger HTTP Request angegebenen Port fest.<br>
</ul>
</ul>
=end html_DE
=cut