package, commandref and internationalization

This commit is contained in:
Manfred 2022-11-06 12:54:41 +01:00
parent 31438f91b2
commit 87687371e8
2 changed files with 115 additions and 53 deletions

View File

@ -23,7 +23,7 @@
# Usage:
#
##########################################################################
# $Id: 98_Matrix.pm 11656 2022-11-05 12:26:00Z Man-fred $
# $Id: 98_Matrix.pm 13172 2022-11-06 12:52:00Z Man-fred $
package FHEM::Devices::Matrix;
use strict;
@ -35,12 +35,11 @@ use GPUtils qw(GP_Export GP_Import);
use JSON;
use vars qw(%data);
use FHEM::Core::Authentication::Passwords qw(:ALL);
#use FHEM::Core::Authentication::Passwords qw(&setStorePassword);
require FHEM::Devices::Matrix::Matrix;
#-- Run before package compilation
BEGIN {
#-- Export to main context with different name
GP_Export(qw(
Initialize
@ -62,11 +61,12 @@ sub Initialize {
$hash->{RenameFn} = \&FHEM::Devices::Matrix::Rename;
$hash->{NotifyFn} = \&FHEM::Devices::Matrix::Notify;
$hash->{AttrList} = "MatrixRoom MatrixSender MatrixMessage MatrixQuestion_0 MatrixQuestion_1 " . $readingFnAttributes;
$hash->{parseParams} = 1;
#$hash->{AttrList} = $FHEM::Devices::Matrix::attr_list;
$hash->{AttrList} = Attr_List();
#$hash->{parseParams} = 1;
return FHEM::Meta::InitMod( __FILE__, $hash );
}
}
1;
@ -151,19 +151,31 @@ sub Initialize {
Attributes:
<ul>
<a id="Matrix-attr-MatrixMessage"></a>
<li><i>MatrixMessage</i> <room-id><br>
<li><i>MatrixMessage</i> &lt;room-id&gt;<br>
Set the room-id to wich messagesare sent.
</li>
<a id="Matrix-attr-MatrixQuestion_[0..9]"></a>
<li><i>MatrixQuestion_[0..9]</i> <room-id><br>
<a id="Matrix-attr-MatrixQuestion_"></a>
<li><i>MatrixQuestion_[0..9]+</i> &lt;question&gt;:&lt;answer 1&gt;:&lt;answer 2&gt;:...&lt;answer max. 20&gt;<br>
Prepared questions.
</li>
<a id="Matrix-attr-MatrixQuestion__0-9__"></a>
<li><i>MatrixQuestion_[0..9]+</i> &lt;question&gt;:&lt;answer 1&gt;:&lt;answer 2&gt;:...&lt;answer max. 20&gt;<br>
Prepared questions.
</li>
<a id="Matrix-attr-MatrixAnswer_"></a>
<li><i>MatrixAnswer_[0..9]</i><br>
Prepared commands.
</li>
<a id="Matrix-attr-MatrixAnswer__0-9__"></a>
<li><i>MatrixAnswer_[0..9]</i><br>
Prepared commands.
</li>
<a id="Matrix-attr-MatrixRoom"></a>
<li><i>MatrixRoom</i> <room-id 1> <room-id 2> ...<br>
<li><i>MatrixRoom</i> &lt;room-id 1&gt; &lt;room-id 2&gt; ...<br>
Set the room-id's from wich are messages received.
</li>
<a id="Matrix-attr-MatrixSender"></a>
<li><i>MatrixSender</i> <code><user 1> <user 2> ...</code><br>
<li><i>MatrixSender</i> <code>&lt;user 1&gt; &lt;user 2&gt; ...</code><br>
Set the user's from wich are messages received.<br><br>
Example: <code>attr matrix MatrixSender @name:matrix.server @second.name:matrix.server</code><br>
</li>
@ -182,7 +194,7 @@ sub Initialize {
<a id="Matrix-define"></a>
<h4>Define</h4>
<ul>
<code>define &lt;name&gt; <server> <user></code>
<code>define &lt;name&gt; &lt;server&gt; &lt;user&gt;</code>
<br><br>
Beispiel: <code>define matrix Matrix matrix.com fhem</code>
<br><br>
@ -252,20 +264,35 @@ sub Initialize {
Attributes:
<ul>
<a id="Matrix-attr-MatrixMessage"></a>
<li><i>MatrixMessage</i> <room-id><br>
<li><i>MatrixMessage</i> &lt;room-id&gt;<br>
Setzt die Raum-ID in die alle Nachrichten gesendet werden. Zur Zeit ist nur ein Raum möglich.
</li>
<a id="Matrix-attr-MatrixQuestion_[0..9]"></a>
<li><i>MatrixQuestion_[0..9]</i> <room-id><br>
Vorbereitete Fragen, die mit set mt question.start 0..9 gestartet werden können.<br>
<a id="Matrix-attr-MatrixAnswer_"></a>
<li><i>MatrixAnswer_</i><br>
Antworten = Befehle ausführen ist noch nicht freigegeben</code>
</li>
<a id="Matrix-attr-MatrixAnswer__0-9__"></a>
<li><i>MatrixAnswer_[0-9]+</i><br>
Antworten = Befehle ausführen ist noch nicht freigegeben</code>
</li>
<a id="Matrix-attr-MatrixQuestion_"></a>
<li><i>MatrixQuestion_</i> <br>
Vorbereitete Fragen, die mit set mt question.start 0..9 gestartet werden können. Es sind maximal 20 Antworten möglich.<br>
Format der Fragen: <code>Frage:Antwort 1:Antwort 2:....:Antwort n</code>
Eingabe in der Attribut-Liste: <code>[0-9]+ Frage:Antwort 1:Antwort 2:....:Antwort n</code>
</li>
<a id="Matrix-attr-MatrixQuestion__0-9__"></a>
<li><i>MatrixQuestion_[0-9]+</i><br>
Vorbereitete Fragen, die mit set mt question.start 0..9 gestartet werden können. Es sind maximal 20 Antworten möglich.<br>
Format der Fragen: <code>Frage:Antwort 1:Antwort 2:....:Antwort n</code>
Eingabe in der Attribut-Liste: <code>[0-9]+ Frage:Antwort 1:Antwort 2:....:Antwort n</code>
</li>
<a id="Matrix-attr-MatrixRoom"></a>
<li><i>MatrixRoom</i> <room-id 1> <room-id 2> ...<br>
<li><i>MatrixRoom</i> &lt;room-id 1&gt; &lt;room-id 2&gt; ...<br>
Alle Raum-ID's aus denen Nachrichten empfangen werden.
</li>
<a id="Matrix-attr-MatrixSender"></a>
<li><i>MatrixSender</i> <code><user 1> <user 2> ...</code><br>
<li><i>MatrixSender</i> <code>&lt;user 1&gt; &lt;user 2&gt; ...</code><br>
Alle Personen von denen Nachrichten empfangen werden.<br>
Beispiel: <code>attr matrix MatrixSender @name:matrix.server @second.name:matrix.server</code><br>
</li>

View File

@ -2,7 +2,7 @@
# Usage:
#
##########################################################################
# $Id: Matrix.pm 20020 2022-11-05 12:26:00Z Man-fred $
# $Id: Matrix.pm 21301 2022-11-06 12:52:00Z Man-fred $
package FHEM::Devices::Matrix;
use strict;
@ -37,27 +37,33 @@ BEGIN {
HttpUtils_NonblockingGet
data
gettimeofday
fhem
))
};
my $Module_Version = '0.0.7';
my $language = 'EN';
sub Attr_List{
return "MatrixRoom MatrixSender MatrixMessage MatrixQuestion_ MatrixQuestion_[0-9]+ MatrixAnswer_ MatrixAnswer_[0-9]+ $readingFnAttributes";
}
sub Define {
my ($hash, $param) = @_;
# my @param = split('[ \t]+', $def);
my $name = $param->[0]; #$param[0];
my ($hash, $def) = @_;
my @param = split('[ \t]+', $def);
my $name = $param[0]; #$param[0];
Log3($name, 1, "$name: Define: $param->[2] ".int(@$param));
Log3($name, 1, "$name: Define: $param[2] ".int(@param));
if(int(@$param) < 1) {
if(int(@param) < 1) {
return "too few parameters: define <name> Matrix <server> <user>";
}
$hash->{name} = $param->[0];
$hash->{server} = $param->[2];
$hash->{user} = $param->[3];
$hash->{password} = $param->[4];
$hash->{name} = $param[0];
$hash->{server} = $param[2];
$hash->{user} = $param[3];
$hash->{password} = $param[4];
$hash->{helper}->{passwdobj} = FHEM::Core::Authentication::Passwords->new($hash->{TYPE});
#$hash->{helper}->{i18} = Get_I18n();
$hash->{NOTIFYDEV} = "global";
Startproc($hash) if($init_done);
return ;
@ -78,6 +84,7 @@ sub Startproc {
Log3($name, 1, "$name: Startproc V".$hash->{ModuleVersion}." -> V".$Module_Version) if ($hash->{ModuleVersion});
# Update necessary?
$hash->{ModuleVersion} = $Module_Version;
$language = AttrVal('global','language','EN');
}
##########################
@ -87,20 +94,21 @@ sub Notify($$)
my $name = $hash->{NAME};
my $devName = $dev->{NAME};
return "" if(IsDisabled($name));
Log3($name, 1, "$name : X_Notify $devName");
#Log3($name, 1, "$name : X_Notify $devName");
my $events = deviceEvents($dev,1);
return if( !$events );
if($devName eq "global" && grep(m/^INITIALIZED|REREADCFG$/, @{$events}))
if(($devName eq "global") && grep(m/^INITIALIZED|REREADCFG$/, @{$events}))
{
Startproc($hash);
}
foreach my $event (@{$events}) {
$event = "" if(!defined($event));
### Writing log entry
Log3($name, 4, "$name : X_Notify $devName - $event");
$language = AttrVal('global','language','EN') if ($event =~ /ATTR global language.*/);
# Examples:
# $event = "ATTR global language DE"
# $event = "readingname: value"
# or
# $event = "INITIALIZED" (for $devName equal "global")
@ -124,13 +132,30 @@ sub Rename($$) {
#my $nhash = $defs{$new};
}
sub I18N {
my $value = shift;
my $def = {
'EN' => {
'require2' => 'requires 2 arguments'
},
'DE' => {
'require2' => 'benötigt 2 Argumente'
},
};
my $result = $def->{$language}->{$value};
return ($result ? $result : $value);
}
sub Get {
my $hash = shift;
my $aArg = shift;
my $def = shift;
my @param = split('[ \t]+', $def);
my $name = shift @$aArg;
my $cmd = shift @$aArg;
my $value = shift @$aArg;
my $name = shift @param;
my $cmd = shift @param;
my $value = join(" ", @param);
$cmd = '?' if (!$cmd);
if ($cmd eq "wellknown") {
return PerformHttpRequest($hash, $cmd, '');
@ -148,14 +173,16 @@ sub Get {
sub Set {
my $hash = shift;
my $param = shift;
my $def = shift;
my $hArg = shift;
my @param = split('[ \t]+', $def);
my $name = shift @$param;
my $opt = shift @$param;
my $value = join("", @$param);
my $name = shift @param;
my $opt = shift @param;
my $value = join(" ", @param);
$opt = '?' if (!$opt);
#Log3($name, 5, "Set $name - $opt - $value - $hash->{NAME}");
#Log3($name, 5, "Set $hash->{NAME}: $name - $opt - $value -- $def");
#return "set $name needs at least one argument" if (int(@$param) < 3);
if ($opt eq "msg") {
@ -196,14 +223,20 @@ sub Set {
sub Attr {
my ($cmd,$name,$attr_name,$attr_value) = @_;
Log3($name, 1, "Attr - $cmd - $name - $attr_name - $attr_value");
if($cmd eq "set") {
if($attr_name eq "xxMatrixRoom") {
$attr_value =~ tr/: /~:/;
addToDevAttrList("mt", "MatrixMessage:".$attr_value);
} elsif($attr_name eq "xxMatrixMessage") {
$_[3] =~ tr/~/:/;
} else {
return ;
if ($attr_name eq "MatrixQuestion_") {
my @erg = split(/ /, $attr_value, 2);
#$_[2] = "MatrixQuestion_n";
return qq("attr $name $attr_name" ).I18N('require2') if (!$erg[1] || $erg[0] !~ /[0-9]/);
$_[2] = "MatrixQuestion_$erg[0]";
$_[3] = $erg[1];
}
if ($attr_name eq "MatrixAnswer_") {
my @erg = split(/ /, $attr_value, 2);
return qq(wrong arguments $attr_name") if (!$erg[1] || $erg[0] !~ /[0-9]+/);
$_[2] = "MatrixAnswer_$erg[0]";
$_[3] = $erg[1];
}
}
return ;
@ -418,11 +451,12 @@ sub ParseHttpResponse($)
readingsBulkUpdate($hash, "room$pos.name", $tl->{'content'}->{'name'}) if ($tl->{'type'} eq 'm.room.name');
if ($tl->{'type'} eq 'm.room.message' && $tl->{'content'}->{'msgtype'} eq 'm.text'){
my $sender = $tl->{'sender'};
my $message = $tl->{'content'}->{'body'};
if (AttrVal($name, 'MatrixSender', '') =~ $sender){
readingsBulkUpdate($hash, "message", $tl->{'content'}->{'body'});
readingsBulkUpdate($hash, "message", $message);
readingsBulkUpdate($hash, "sender", $sender);
# command
fhem($message) if ($message =~ /set .*/i);
}
#else {
# readingsBulkUpdate($hash, "message", 'ignoriert, nicht '.AttrVal($name, 'MatrixSender', ''));
@ -430,13 +464,14 @@ sub ParseHttpResponse($)
#}
} elsif ($tl->{'type'} eq "org.matrix.msc3381.poll.response"){
my $sender = $tl->{'sender'};
my $message = $tl->{'content'}->{'org.matrix.msc3381.poll.response'}->{'answers'}[0];
if (AttrVal($name, 'MatrixSender', '') =~ $sender){
readingsBulkUpdate($hash, "answer", $tl->{'content'}->{'org.matrix.msc3381.poll.response'}->{'answers'}[0]);
readingsBulkUpdate($hash, "answer", $message);
readingsBulkUpdate($hash, "sender", $sender);
# poll.end and
$nextRequest = "question.end" ;
# command
fhem($message) if ($message =~ /set .*/i);
}
}
}