2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 12:49:34 +00:00

Updated/New: Max_ERROR_Counter,STAUS, ENABLE

git-svn-id: https://svn.fhem.de/fhem/trunk@624 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parix 2010-04-29 19:45:48 +00:00
parent 0f4407426d
commit 0f160d7cbb

View File

@ -1,112 +1,163 @@
############################################## ##############################################
# Twitter for FHEM # Twitter for FHEM
# Sends Twitter-MSG # Sends Twitter-MSG
# #
# Usage: # Usage:
# twitter <MESSAGE> # twitter <MESSAGE>
# Beispiel # Beispiel
# twitter "MEINE TWITTER NACHRICHT" # twitter "MEINE TWITTER NACHRICHT"
# per AT-Job alle 15min # per AT-Job alle 15min
# define TW001 at +00:15:00 {my $msg = TimeNow() . " " . $defs{<DEVICE-NAME>}{'STATE'} ;; fhem "twitter $msg"} # define TW001 at +00:15:00 {my $msg = TimeNow() . " " . $defs{<DEVICE-NAME>}{'STATE'} ;; fhem "twitter $msg"}
# #
# Twitter Limits # twitter STATUS
# http://help.twitter.com/forums/10711/entries/15364 # Shows status of twitter-command: Twitter:Enabeld MSG-COUNT: 2 ERR-COUNT: 0
# Updates: 1,000 per day. The daily update limit is further broken down into #
# smaller limits for semi-hourly intervals. Retweets are counted as updates. # twitter enable
############################################## # Enables twitter-command
package main; #
use strict; # Twitter Limits
use warnings; # http://help.twitter.com/forums/10711/entries/15364
use POSIX; # Updates: 1,000 per day. The daily update limit is further broken down into
use Data::Dumper; # smaller limits for semi-hourly intervals. Retweets are counted as updates.
use LWP::UserAgent; #
use vars qw(%data); # My Limits ;-)
use vars qw(%cmds); # LPW-Timeout 5sec
sub Commandtwitter($); # Max-Errors = 10
##############################################
##################################### package main;
sub use strict;
twitter_Initialize($) use warnings;
{ use POSIX;
# Config-Data use Data::Dumper;
# Account-Data use LWP::UserAgent;
$data{twitter}{login}{user} = "********"; use vars qw(%data);
$data{twitter}{login}{pwd} = "********"; use vars qw(%cmds);
# disabled ?? -> enable sub Commandtwitter($);
if(defined($data{twitter}{conf}{disabeld})){ #####################################
delete $data{twitter}{conf}{disabeld}; sub
} twitter_Initialize($)
# $data{twitter}{conf}{disabeld} = 1; {
# Max 1000 MSGs per Day # Config-Data
$data{twitter}{conf}{msg_count} = 0; # Account-Data
$data{twitter}{conf}{day} = (localtime(time()))[3]; $data{twitter}{login}{user} = "********";
#FHEM-Command $data{twitter}{login}{pwd} = "********";
$cmds{twitter}{Fn} = "Commandtwitter"; # disabled ?? -> enable
$cmds{twitter}{Hlp} = "Sends Twitter Messages (max. 140 Chars): twitter MSG"; if(defined($data{twitter}{conf}{disabeld})){
} delete $data{twitter}{conf}{disabeld};
##################################### }
sub Commandtwitter($) # $data{twitter}{conf}{disabeld} = 1;
{ # Max 1000 MSGs per Day
my ($cl, $msg) = @_; $data{twitter}{conf}{msg_count} = 0;
if(!$msg) { $data{twitter}{conf}{day} = (localtime(time()))[3];
Log 0, "TWITTER[ERROR] Keine Nachricht"; # Error-Counter
return undef; $data{twitter}{error} = 0;
} $data{twitter}{error_max} = 10;
# Disbaled ?? $data{twitter}{last_msg} = "";
if(defined($data{twitter}{conf}{disabeld})){ #FHEM-Command
Log 0, "TWITTER[INFO] DISABLED"; $cmds{twitter}{Fn} = "Commandtwitter";
return undef; $cmds{twitter}{Hlp} = "Sends Twitter Messages (max. 140 Chars): twitter MSG";
} }
#Changed Day #####################################
my $d = (localtime(time()))[3]; sub Commandtwitter($)
my $d_old = $data{twitter}{conf}{day}; {
if($d_old ne $d){ my ($cl, $msg) = @_;
$data{twitter}{conf}{day} = $d; if(!$msg) {
Log 0,"TWITTER[INFO] DAY-CHANGE: DAY: $d_old MSG-COUNT: " . $data{twitter}{conf}{msg_count}; Log 0, "TWITTER[ERROR] Keine Nachricht";
$data{twitter}{conf}{msg_count} = 0; return undef;
} }
#Count MSG #Show Status
my $msg_cnt = $data{twitter}{conf}{msg_count}; my $status;
if($msg_cnt > 1000) { if($msg eq "STATUS"){
Log 0, "TWITTER[INFO] MessageCount exceede 1000 Messages per Day"; if(defined($data{twitter}{conf}{disabeld})){$status = "Twitter:Disbaled";}
return undef; else{$status = "Twitter:Enabeld";}
} $status .= " MSG-COUNT: " . $data{twitter}{conf}{msg_count};
my $t_user = $data{twitter}{login}{user}; $status .= " ERR-COUNT: " . $data{twitter}{error};
my $t_pdw = $data{twitter}{login}{pwd}; $status .= " MSG-LAST: " . $data{twitter}{last_msg};
my $t_log; return $status;
#Add MSG-Count }
$msg = "[" . $msg_cnt ."] " . $msg; #Enable
# Max ;SG-Length 140 if($msg eq "ENABLE"){
my $ml = length ($msg); if(defined($data{twitter}{conf}{disabeld})){
if($ml > 140){ $status = "Twitter enabled";
Log 0, "TWITTER[INFO] Die Nachricht ist mit $ml Zeichen zu lang (max. 140)"; retunr $status;
$msg = substr($msg, 0, 140); }
} return "Twitter already Enabeld";
my $browser = LWP::UserAgent->new; }
# Proxy-Server #ERROR-Counter
# $browser->proxy(['http'], "http://PROXYSERVER:PORT"); my ($err_cnt,$err_max);
$browser->timeout(5); if(defined($data{twitter}{error})){
my $url = 'http://twitter.com/statuses/update.json'; $err_cnt = $data{twitter}{error};
$err_max = $data{twitter}{error_max};
$browser->credentials('twitter.com:80', 'Twitter API', $t_user , $t_pdw); if($err_cnt > $err_max){
my $response = $browser->get("http://twitter.com/account/verify_credentials.json"); # ERROR disable twitter
if($response->code eq 200){$t_log = "LOGIN=SUCCESS";} Log 0, "TWITTER[INFO] ErrCounter exceeded $err_max DISABLED";
else { $data{twitter}{conf}{disabeld} = 1;
$t_log = "[ERROR] LOGIN: " . $response->code .":".$response->message . " DISABLED"; }
# ERROR disable twitter }
$data{twitter}{conf}{disabeld} = 1; # Disbaled ??
} if(defined($data{twitter}{conf}{disabeld})){
Log 0, "TWITTER[STATUS] DISABLED";
$response = $browser->post($url, {status => $msg}); return undef;
if($response->code eq 200){$t_log .= " UPDATE=SUCCESS";} }
else { #Changed Day
$t_log = "[ERROR] UPDATE: " . $response->code .":".$response->message . " DISABLED"; my $d = (localtime(time()))[3];
# ERROR disable twitter my $d_old = $data{twitter}{conf}{day};
$data{twitter}{conf}{disabeld} = 1; if($d_old ne $d){
} $data{twitter}{conf}{day} = $d;
$msg_cnt++; Log 0,"TWITTER[INFO] DAY-CHANGE: DAY: $d_old MSG-COUNT: " . $data{twitter}{conf}{msg_count};
Log 0, "TWITTER[INFO] " . $t_log . " MSG-$msg_cnt-: $msg"; $data{twitter}{conf}{msg_count} = 0;
$data{twitter}{conf}{msg_count}++; #Reset ERROR-Counter
return undef; $data{twitter}{error} = 0;
} }
#Count MSG
my $msg_cnt = $data{twitter}{conf}{msg_count};
if($msg_cnt > 1000) {
Log 0, "TWITTER[INFO] MessageCount exceede 1000 Messages per Day";
return undef;
}
my $t_user = $data{twitter}{login}{user};
my $t_pdw = $data{twitter}{login}{pwd};
my $t_log;
#Add MSG-Count
$msg = "[" . $msg_cnt ."] " . $msg;
# Max ;SG-Length 140
my $ml = length ($msg);
if($ml > 140){
Log 0, "TWITTER[INFO] Die Nachricht ist mit $ml Zeichen zu lang (max. 140)";
$msg = substr($msg, 0, 140);
}
my $browser = LWP::UserAgent->new;
# Proxy-Server
# $browser->proxy(['http'], "http://PROXYSERVER:PORT");
$browser->timeout(5);
my $url = 'http://twitter.com/statuses/update.json';
$browser->credentials('twitter.com:80', 'Twitter API', $t_user , $t_pdw);
my $response = $browser->get("http://twitter.com/account/verify_credentials.json");
if($response->code eq 200){
$t_log = "LOGIN=SUCCESS";
}
else {
$t_log = "TWITTER[ERROR] LOGIN: " . $response->code .":".$response->message . " DISABLED";
$data{twitter}{error}++;
$data{twitter}{last_msg} = TimeNow() . " " . $t_log;
return undef;
}
$response = $browser->post($url, {status => $msg});
if($response->code eq 200){
$t_log .= " UPDATE=SUCCESS";
}
else {
$t_log = "TWITTER[ERROR] UPDATE: " . $response->code .":".$response->message . " DISABLED";
$data{twitter}{error}++;
$data{twitter}{last_msg} = TimeNow() . " " . $t_log;
return undef;
}
$msg_cnt++;
Log 0, "TWITTER[INFO] " . $t_log . " MSG-$msg_cnt-: $msg";
$data{twitter}{last_msg} = TimeNow() . " TWITTER[INFO] " . $t_log . " MSG-$msg_cnt-: $msg";
$data{twitter}{conf}{msg_count}++;
return undef;
}
1; 1;