|
From: <sic...@us...> - 2012-08-09 16:40:16
|
Revision: 929
http://openautomation.svn.sourceforge.net/openautomation/?rev=929&view=rev
Author: sicariusx
Date: 2012-08-09 16:40:09 +0000 (Thu, 09 Aug 2012)
Log Message:
-----------
Erweiterung um den Prowl-Versand.
Modified Paths:
--------------
wiregate/plugin/generic/Logikprozessor.pl
Modified: wiregate/plugin/generic/Logikprozessor.pl
===================================================================
--- wiregate/plugin/generic/Logikprozessor.pl 2012-08-08 20:10:42 UTC (rev 928)
+++ wiregate/plugin/generic/Logikprozessor.pl 2012-08-09 16:40:09 UTC (rev 929)
@@ -27,7 +27,7 @@
# }
#}
-# Tools und vorbesetzte Variablen fue die Logiken
+# Tools und vorbesetzte Variablen fur die Logiken
sub limit { my ($lo,$x,$hi)=@_; return $x<$lo?$lo:($x>$hi?$hi:$x); }
my $date=`/bin/date +"%W,%a,%u,%m,%d,%Y,%j,%H,%M,%X"`;
plugin_log($plugname, "Datum/Uhrzeit konnte nicht lesbar.") unless $date=~/^(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+)$/;
@@ -53,6 +53,7 @@
# Konfigurationsfile einlesen
my $eibd_backend_address='1.1.254';
my %logic=();
+my %settings=();
my $conf="/etc/wiregate/plugin/generic/conf.d/$plugname";
$conf.='.conf' unless $conf=~s/\.pl$/.conf/;
open FILE, "<$conf" || return "no config found";
@@ -121,6 +122,14 @@
next;
}
+ if(defined $logic{$t}{prowl} && ref $logic{$t}{prowl}
+ && ! ((ref $logic{$t}{prowl} eq 'HASH') || (ref $logic{$t}{prowl} eq 'CODE')))
+ {
+ plugin_log($plugname, "Config err: \$logic{$t}{prowl} ist weder Skalar noch "
+ . "HASH-Referenz ({...}) noch CODE-Referenz (sub {...})." . (ref $logic{$t}{prowl}));
+ next;
+ }
+
if(defined $logic{$t}{timer} && defined $logic{$t}{delay})
{
plugin_log($plugname, "Config err: \$logic{$t}: delay und timer festgelegt, ignoriere delay");
@@ -700,6 +709,9 @@
my ($t, $receive, $ga, $in)=@_; # Logikindex $t, Bustelegramm erhalten auf $ga mit Inhalt $in
# $receive muss die direkten Gruppenadressen enthalten - Decodierung von Kuerzeln wird nicht vorgenommen
+ # Debuggingflag gesetzt
+ my $debug = $logic{debug} || $logic{$t}{debug};
+
# als erstes definiere das Input-Array fuer die Logik
my $input=$in;
@@ -729,11 +741,15 @@
# ab hier liegt $input komplett vor, und nun muss die Logik ausgewertet
# und das Resultat auf der Transmit-GA uebertragen werden
my $result=undef;
+ my %prowlContext=();
unless(ref $logic{$t}{translate})
{
# Trivialer Fall: translate enthaelt einen fixen Rueckgabewert
$plugin_info{$plugname.'_'.$t.'_result'}=$result=$logic{$t}{translate};
+ # prowlContext bef\xFCllen
+ $prowlContext{result}=$result;
+ $prowlContext{input}=$input;
}
elsif(!ref $logic{$t}{state})
{
@@ -744,6 +760,11 @@
# Funktionsaufruf, das Ergebnis vom letzten Mal steht in $state
$result=$logic{$t}{translate}($state,$input);
+ # prowlContext bef\xFCllen
+ $prowlContext{result}=$result;
+ $prowlContext{state}=$state;
+ $prowlContext{input}=$input;
+
# Ergebnis des letzten Aufrufs zurueckschreiben
if(defined $result)
{
@@ -769,6 +790,11 @@
# Funktionsaufruf, das Ergebnis vom letzten Mal steht in $state->{result}
$result=$state->{result}=$logic{$t}{translate}($state,$input);
+
+ # prowlContext bef\xFCllen
+ $prowlContext{result}=$result;
+ $prowlContext{state}=$state;
+ $prowlContext{input}=$input;
# Alle dynamischen Variablen wieder nach plugin_info schreiben
# Damit plugin_info nicht durch Konfigurationsfehler vollgemuellt wird,
@@ -787,6 +813,30 @@
}
}
}
+
+ # Prowl-Nachrichten senden, falls definiert
+ if(defined $logic{$t}{prowl})
+ {
+ my %prowlParametersSource;
+ if (ref $logic{$t}{prowl}) {
+ %prowlParametersSource = %{$logic{$t}{prowl}} if (ref $logic{$t}{prowl} eq 'HASH');
+ %prowlParametersSource = $logic{$t}{prowl}(%prowlContext) if (ref $logic{$t}{prowl} eq 'CODE');
+ }
+ else
+ {
+ %prowlParametersSource = ( event => $logic{$t}{prowl} );
+ }
+
+ sendProwl((
+ debug => $debug,
+ priority => $prowlParametersSource{priority} || $settings{prowl}{priority},
+ event => $prowlParametersSource{event} || $settings{prowl}{event},
+ description => $prowlParametersSource{description} || $settings{prowl}{description},
+ application => $prowlParametersSource{application} || $settings{prowl}{application},
+ url => $prowlParametersSource{url} || $settings{prowl}{url},
+ apikey => $prowlParametersSource{url} || $settings{prowl}{apikey}
+ ));
+ }
return $result;
}
@@ -828,3 +878,55 @@
}
}
+sub sendProwl {
+ my (%parameters)=@_;
+ my ($priority, $event, $description, $application, $url, $apikey);
+
+ # Parameter ermitteln
+ $priority = $parameters{priority} || 0;
+ $event = $parameters{event} || '[unbenanntes Ereignis]';
+ $description = $parameters{description} || '';
+ $application = $parameters{application} || 'WireGate KNX';
+ $url = $parameters{url} || '';
+ $apikey = $parameters{apikey} || '';
+
+ use LWP::UserAgent;
+ use URI::Escape;
+
+ # Falls nur ein einziger skalarer API key geliefert wurde, muss dieser in
+ # ein Array geh\xFCllt werden
+ if(ref $apikey ne 'ARRAY') {
+ $apikey = [$apikey];
+ }
+
+ # Nachricht senden an jeden API key
+ for my $singleApikey (@{$apikey}) {
+ # HTTP Request aufsetzen
+ my ($userAgent, $request, $response, $requestURL);
+ $userAgent = LWP::UserAgent->new;
+ $userAgent->agent("WireGatePlugin/1.0");
+
+ $requestURL = sprintf("https://prowl.weks.net/publicapi/add?apikey=%s&application=%s&event=%s&description=%s&priority=%d&url=%s",
+ uri_escape($singleApikey),
+ uri_escape($application),
+ uri_escape($event),
+ uri_escape($description),
+ uri_escape($priority),
+ uri_escape($url));
+
+ $request = HTTP::Request->new(GET => $requestURL);
+ #$request->timeout(5);
+
+ $response = $userAgent->request($request);
+
+ if ($response->is_success) {
+ plugin_log($plugname, "Prowl-Nachricht erfolgreich abgesetzt: $priority, $event, $description, $application") if $parameters{debug};
+ } elsif ($response->code == 401) {
+ plugin_log($plugname, "Prowl-Nachricht nicht abgesetzt: API key g\xFCltig?");
+ } else {
+ plugin_log($plugname, "Prowl-Nachricht nicht abgesetzt: " . $response->content);
+ }
+ }
+ return undef;
+}
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|