50_SSChatBot: contrib 1.0.0
git-svn-id: https://svn.fhem.de/fhem/trunk@20687 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
@@ -63,7 +63,7 @@ my %SSChatBot_errlist = (
|
|||||||
102 => "API does not exist - may be the Synology Chat Server package is stopped",
|
102 => "API does not exist - may be the Synology Chat Server package is stopped",
|
||||||
120 => "payload has wrong format",
|
120 => "payload has wrong format",
|
||||||
404 => "bot is not legal - may be the bot is not active or the botToken is wrong",
|
404 => "bot is not legal - may be the bot is not active or the botToken is wrong",
|
||||||
407 => "record is not valid",
|
407 => "record not valid",
|
||||||
800 => "malformed or unsupported URL",
|
800 => "malformed or unsupported URL",
|
||||||
805 => "empty API data received - may be the Synology Chat Server package is stopped",
|
805 => "empty API data received - may be the Synology Chat Server package is stopped",
|
||||||
806 => "couldn't get Synology Chat API informations",
|
806 => "couldn't get Synology Chat API informations",
|
||||||
@@ -129,6 +129,7 @@ sub SSChatBot_Define($@) {
|
|||||||
$hash->{INPORT} = $inport;
|
$hash->{INPORT} = $inport;
|
||||||
$hash->{MODEL} = "ChatBot";
|
$hash->{MODEL} = "ChatBot";
|
||||||
$hash->{INPROT} = $inprot;
|
$hash->{INPROT} = $inprot;
|
||||||
|
$hash->{RESEND} = "next planned SendQueue start: immediately by next entry";
|
||||||
$hash->{HELPER}{MODMETAABSENT} = 1 if($modMetaAbsent); # Modul Meta.pm nicht vorhanden
|
$hash->{HELPER}{MODMETAABSENT} = 1 if($modMetaAbsent); # Modul Meta.pm nicht vorhanden
|
||||||
$hash->{HELPER}{USERFETCHED} = 0; # Chat User sind noch nicht abgerufen
|
$hash->{HELPER}{USERFETCHED} = 0; # Chat User sind noch nicht abgerufen
|
||||||
|
|
||||||
@@ -147,9 +148,10 @@ sub SSChatBot_Define($@) {
|
|||||||
# Index der Sendequeue initialisieren
|
# Index der Sendequeue initialisieren
|
||||||
$data{SSChatBot}{$name}{sendqueue}{index} = 0;
|
$data{SSChatBot}{$name}{sendqueue}{index} = 0;
|
||||||
|
|
||||||
readingsBeginUpdate($hash);
|
readingsBeginUpdate ($hash);
|
||||||
readingsBulkUpdate($hash, "state", "Initialized"); # Init state
|
readingsBulkUpdateIfChanged ($hash, "QueueLenth", 0); # Länge Sendqueue initialisieren
|
||||||
readingsEndUpdate($hash,1);
|
readingsBulkUpdate ($hash, "state", "Initialized"); # Init state
|
||||||
|
readingsEndUpdate ($hash,1);
|
||||||
|
|
||||||
# initiale Routinen nach Start ausführen , verzögerter zufälliger Start
|
# initiale Routinen nach Start ausführen , verzögerter zufälliger Start
|
||||||
SSChatBot_initonboot($hash);
|
SSChatBot_initonboot($hash);
|
||||||
@@ -272,7 +274,7 @@ sub SSChatBot_Set($@) {
|
|||||||
|
|
||||||
return if(IsDisabled($name));
|
return if(IsDisabled($name));
|
||||||
|
|
||||||
my $idxlist = join(",",(sort keys %{$data{SSChatBot}{$name}{sendqueue}{entries}}));
|
my $idxlist = join(",", SSCam_sortVersion("asc",keys %{$data{SSChatBot}{$name}{sendqueue}{entries}}));
|
||||||
|
|
||||||
if(!$hash->{TOKEN}) {
|
if(!$hash->{TOKEN}) {
|
||||||
# initiale setlist für neue Devices
|
# initiale setlist für neue Devices
|
||||||
@@ -283,7 +285,7 @@ sub SSChatBot_Set($@) {
|
|||||||
$setlist = "Unknown argument $opt, choose one of ".
|
$setlist = "Unknown argument $opt, choose one of ".
|
||||||
"botToken ".
|
"botToken ".
|
||||||
"listSendqueue:noArg ".
|
"listSendqueue:noArg ".
|
||||||
($idxlist?"purgeSendqueue:-all-,$idxlist ":"purgeSendqueue:-all- ").
|
($idxlist?"purgeSendqueue:-all-,-permError-,$idxlist ":"purgeSendqueue:-all-,-permError- ").
|
||||||
"sendItem:textField-long "
|
"sendItem:textField-long "
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
@@ -324,7 +326,13 @@ sub SSChatBot_Set($@) {
|
|||||||
delete $hash->{OPIDX};
|
delete $hash->{OPIDX};
|
||||||
delete $data{SSChatBot}{$name}{sendqueue}{entries};
|
delete $data{SSChatBot}{$name}{sendqueue}{entries};
|
||||||
$data{SSChatBot}{$name}{sendqueue}{index} = 0;
|
$data{SSChatBot}{$name}{sendqueue}{index} = 0;
|
||||||
return "All entries of SendQueue deleted";
|
return "All entries of SendQueue are deleted";
|
||||||
|
} elsif($prop eq "-permError-") {
|
||||||
|
foreach my $idx (keys %{$data{SSChatBot}{$name}{sendqueue}{entries}}) {
|
||||||
|
delete $data{SSChatBot}{$name}{sendqueue}{entries}{$idx}
|
||||||
|
if($data{SSChatBot}{$name}{sendqueue}{entries}{$idx}{forbidSend});
|
||||||
|
}
|
||||||
|
return "All entries with state \"permanent send error\" are deleted";
|
||||||
} else {
|
} else {
|
||||||
delete $data{SSChatBot}{$name}{sendqueue}{entries}{$prop};
|
delete $data{SSChatBot}{$name}{sendqueue}{entries}{$prop};
|
||||||
return "SendQueue entry with index \"$prop\" deleted";
|
return "SendQueue entry with index \"$prop\" deleted";
|
||||||
@@ -642,6 +650,8 @@ sub SSChatBot_addQueue ($$$$$$$$) {
|
|||||||
|
|
||||||
$data{SSChatBot}{$name}{sendqueue}{entries}{$index} = $pars;
|
$data{SSChatBot}{$name}{sendqueue}{entries}{$index} = $pars;
|
||||||
|
|
||||||
|
SSChatBot_updQLength ($hash); # updaten Länge der Sendequeue
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -662,24 +672,23 @@ sub SSChatBot_checkretry ($$) {
|
|||||||
|
|
||||||
if(!keys %{$data{SSChatBot}{$name}{sendqueue}{entries}}) {
|
if(!keys %{$data{SSChatBot}{$name}{sendqueue}{entries}}) {
|
||||||
Log3($name, 4, "$name - SendQueue is empty. Nothing to do ...");
|
Log3($name, 4, "$name - SendQueue is empty. Nothing to do ...");
|
||||||
|
SSChatBot_updQLength ($hash);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$retry) {
|
if(!$retry) { # Befehl erfolgreich, Senden nur neu starten wenn weitere Einträge in SendQueue
|
||||||
# Befehl erfolgreich, Senden nur neu starten wenn weitere Einträge in SendQueue
|
|
||||||
delete $hash->{OPIDX};
|
delete $hash->{OPIDX};
|
||||||
delete $data{SSChatBot}{$name}{sendqueue}{entries}{$idx};
|
delete $data{SSChatBot}{$name}{sendqueue}{entries}{$idx};
|
||||||
Log3($name, 4, "$name - Opmode \"$hash->{OPMODE}\" finished successfully, Sendqueue index \"$idx\" deleted.");
|
Log3($name, 4, "$name - Opmode \"$hash->{OPMODE}\" finished successfully, Sendqueue index \"$idx\" deleted.");
|
||||||
return SSChatBot_getapisites($name) if((sort{$a<=>$b} keys %{$data{SSChatBot}{$name}{sendqueue}{entries}})[0]); # nächsten Eintrag abarbeiten wenn SendQueue nicht leer
|
SSChatBot_updQLength ($hash);
|
||||||
|
return SSChatBot_getapisites($name); # nächsten Eintrag abarbeiten (wenn SendQueue nicht leer)
|
||||||
|
|
||||||
} else {
|
} else { # Befehl nicht erfolgreich, (verzögertes) Senden einplanen
|
||||||
# Befehl nicht erfolgreich, (verzögertes) Senden einplanen
|
|
||||||
$data{SSChatBot}{$name}{sendqueue}{entries}{$idx}{retryCount}++;
|
$data{SSChatBot}{$name}{sendqueue}{entries}{$idx}{retryCount}++;
|
||||||
my $rc = $data{SSChatBot}{$name}{sendqueue}{entries}{$idx}{retryCount};
|
my $rc = $data{SSChatBot}{$name}{sendqueue}{entries}{$idx}{retryCount};
|
||||||
|
|
||||||
my $errorcode = ReadingsVal($name, "Errorcode", 0);
|
my $errorcode = ReadingsVal($name, "Errorcode", 0);
|
||||||
if($errorcode =~ /100|101|120|407|800|900/) {
|
if($errorcode =~ /100|101|120|407|800|900/) { # bei diesen Errorcodes den Queueeintrag nicht wiederholen, da dauerhafter Fehler !
|
||||||
# bei diesen Errorcodes den Queueeintrag nicht wiederholen, da dauerhafter Fehler !
|
|
||||||
$forbidSend = 1;
|
$forbidSend = 1;
|
||||||
$data{SSChatBot}{$name}{sendqueue}{entries}{$idx}{forbidSend} = $forbidSend;
|
$data{SSChatBot}{$name}{sendqueue}{entries}{$idx}{forbidSend} = $forbidSend;
|
||||||
|
|
||||||
@@ -687,19 +696,23 @@ sub SSChatBot_checkretry ($$) {
|
|||||||
|
|
||||||
delete $hash->{OPIDX};
|
delete $hash->{OPIDX};
|
||||||
delete $hash->{OPMODE};
|
delete $hash->{OPMODE};
|
||||||
|
|
||||||
|
SSChatBot_updQLength ($hash); # updaten Länge der Sendequeue
|
||||||
|
|
||||||
|
return SSChatBot_getapisites($name); # nächsten Eintrag abarbeiten (wenn SendQueue nicht leer);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$forbidSend) {
|
if(!$forbidSend) {
|
||||||
my $rs = 0;
|
my $rs = 0;
|
||||||
if($rc <= 5) {
|
if($rc <= 1) {
|
||||||
$rs = 5;
|
$rs = 5;
|
||||||
} elsif ($rc < 10) {
|
} elsif ($rc < 3) {
|
||||||
$rs = 20;
|
$rs = 20;
|
||||||
} elsif ($rc < 15) {
|
} elsif ($rc < 5) {
|
||||||
$rs = 60;
|
$rs = 60;
|
||||||
} elsif ($rc < 20) {
|
} elsif ($rc < 7) {
|
||||||
$rs = 1800;
|
$rs = 1800;
|
||||||
} elsif ($rc < 25) {
|
} elsif ($rc < 9) {
|
||||||
$rs = 3600;
|
$rs = 3600;
|
||||||
} else {
|
} else {
|
||||||
$rs = 86400;
|
$rs = 86400;
|
||||||
@@ -707,12 +720,15 @@ sub SSChatBot_checkretry ($$) {
|
|||||||
|
|
||||||
Log3($name, 2, "$name - ERROR - \"$hash->{OPMODE}\" SendQueue index \"$idx\" not executed. Restart SendQueue in $rs seconds (retryCount $rc).");
|
Log3($name, 2, "$name - ERROR - \"$hash->{OPMODE}\" SendQueue index \"$idx\" not executed. Restart SendQueue in $rs seconds (retryCount $rc).");
|
||||||
|
|
||||||
|
my $rst = gettimeofday()+$rs; # resend Timer
|
||||||
|
SSChatBot_updQLength ($hash,$rst); # updaten Länge der Sendequeue mit resend Timer
|
||||||
|
|
||||||
RemoveInternalTimer($hash, "SSChatBot_getapisites");
|
RemoveInternalTimer($hash, "SSChatBot_getapisites");
|
||||||
InternalTimer(gettimeofday()+$rs, "SSChatBot_getapisites", "$name", 0);
|
InternalTimer($rst, "SSChatBot_getapisites", "$name", 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
#############################################################################################################################
|
#############################################################################################################################
|
||||||
@@ -1379,6 +1395,29 @@ sub SSChatBot_trim ($) {
|
|||||||
return ($str);
|
return ($str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#############################################################################################
|
||||||
|
# Länge Senedequeue updaten
|
||||||
|
#############################################################################################
|
||||||
|
sub SSChatBot_updQLength ($;$) {
|
||||||
|
my ($hash,$rst) = @_;
|
||||||
|
my $name = $hash->{NAME};
|
||||||
|
|
||||||
|
my $ql = keys %{$data{SSChatBot}{$name}{sendqueue}{entries}};
|
||||||
|
|
||||||
|
readingsBeginUpdate ($hash);
|
||||||
|
readingsBulkUpdateIfChanged ($hash, "QueueLenth", $ql); # Länge Sendqueue updaten
|
||||||
|
readingsEndUpdate ($hash,1);
|
||||||
|
|
||||||
|
my $head = "next planned SendQueue start:";
|
||||||
|
if($rst) { # resend Timer gesetzt
|
||||||
|
$hash->{RESEND} = $head." ".FmtDateTime($rst);
|
||||||
|
} else {
|
||||||
|
$hash->{RESEND} = $head." immediately by next entry";
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
#############################################################################################
|
#############################################################################################
|
||||||
# Text für den Versand an Synology Chat formatieren
|
# Text für den Versand an Synology Chat formatieren
|
||||||
# und nicht erlaubte Zeichen entfernen
|
# und nicht erlaubte Zeichen entfernen
|
||||||
|
|||||||
Reference in New Issue
Block a user