ganz früher status
This commit is contained in:
Manfred 2022-10-30 12:46:15 +01:00
parent 8c7caf9066
commit fd027437a9
3 changed files with 83 additions and 5104 deletions

View File

@ -24,7 +24,7 @@ sub Matrix_PerformHttpRequest($$$)
$hash->{BUSY} = $hash->{BUSY} + 1; # queue is busy until response is received
$hash->{stage} = $def;
$hash->{'LASTSEND'} = $now; # remember when last sent
my $device_id = $hash->{device_id} ? ', "device_id":"'.$hash->{device_id}.'"' : "";
my $device_id = ReadingsVal($name, 'device_id', undef) ? ', "device_id":"'.ReadingsVal($name, 'device_id', undef).'"' : "";
if ($def eq "register"){
$param->{'url'} = $hash->{server}."/_matrix/client/v3/register";
$param->{'data'} = '{"type":"m.login.password", "identifier":{ "type":"m.id.user", "user":"'.$hash->{user}.'" }, "password":"'.$hash->{password}.'"}';
@ -49,8 +49,22 @@ sub Matrix_PerformHttpRequest($$$)
$param->{'url'} = $hash->{server}.'/_matrix/client/r0/rooms/'.ReadingsVal($name, 'room', '!!').'/send/m.room.message?access_token='.ReadingsVal($name, '.access_token', 'xx');
$param->{'data'} = '{"msgtype":"m.text", "body":"'.$value.'"}';
}
if ($def eq "pollstart"){
$param->{'url'} = $hash->{server}.'/_matrix/client/v3/rooms/'.ReadingsVal($name, 'room', '!!').'/send/m.poll.start?access_token='.ReadingsVal($name, '.access_token', 'xx');
$param->{'data'} = '{"org.matrix.msc3381.poll.start": {"max_selections": 1,'.
'"question": {"org.matrix.msc1767.text": "[Kategorie] Frage"},'.
'"kind": "org.matrix.msc3381.poll.undisclosed",'.
'"answers": [{"id": "Antwort1", "org.matrix.msc1767.text": "Antwort1"},{"id":"Antwort2","org.matrix.msc1767.text": "Antwort2"}],'.
'"org.matrix.msc1767.text": "[Kategorie] Frage\nAntwort1\nAntwort2"}}';
}
if ($def eq "pollend"){
$param->{'url'} = $hash->{server}.'/_matrix/client/v3/rooms/'.ReadingsVal($name, 'room', '!!').'/send/m.poll.end?access_token='.ReadingsVal($name, '.access_token', 'xx');
# ""'.ReadingsVal($name, 'questionEventId', '!!').'
$param->{'data'} = '{"m.relates_to": {"rel_type": "m.reference","event_id": "$8v_5pJZY9Ql_x93TfoHmqSBTiY6IYNdRpqiJuhMylxo"},"org.matrix.msc3381.poll.end": {},'.
'"org.matrix.msc1767.text": "Antort '.ReadingsVal($name, "answer", "").' erhalten von '.ReadingsVal($name, "sender", "").'"}';
}
if ($def eq "sync"){
my $since = $hash->{since} ? '&since='.$hash->{since} : "";
my $since = ReadingsVal($name, "since", undef) ? '&since='.ReadingsVal($name, "since", undef) : "";
my $full_state = ReadingsVal($name, "poll.fullstate",undef);
if ($full_state){
$full_state = "&full_state=true";
@ -64,10 +78,10 @@ sub Matrix_PerformHttpRequest($$$)
}
if ($def eq "filter"){
if ($value){ # get
$param->{'url'} = $hash->{server}.'/_matrix/client/v3/user/'.$hash->{user_id}.'/filter/'.$value.'?access_token='.ReadingsVal($name, '.access_token', 'xx');
$param->{'url'} = $hash->{server}.'/_matrix/client/v3/user/'.ReadingsVal($name, "user_id",0).'/filter/'.$value.'?access_token='.ReadingsVal($name, '.access_token', 'xx');
$param->{'method'} = 'GET';
} else {
$param->{'url'} = $hash->{server}.'/_matrix/client/v3/user/'.$hash->{user_id}.'/filter?access_token='.ReadingsVal($name, '.access_token', 'xx');
$param->{'url'} = $hash->{server}.'/_matrix/client/v3/user/'.ReadingsVal($name, "user_id",0).'/filter?access_token='.ReadingsVal($name, '.access_token', 'xx');
$param->{'data'} = '{';
$param->{'data'} .= '"event_fields": ["type","content","sender"],';
$param->{'data'} .= '"event_format": "client", ';
@ -81,7 +95,7 @@ sub Matrix_PerformHttpRequest($$$)
. ( $param->{data} ? "\r\ndata: $param->{data}, " : "" )
. ( $param->{header} ? "\r\nheader: $param->{header}" : "" );
readingsSingleUpdate($hash, "fullRequest", $test, 1); # Readings erzeugen
$test = "$name: Matrixe sends with timeout $param->{timeout} to ".$test;
$test = "$name: Matrix sends with timeout $param->{timeout} to ".$test;
Log3 $name, 5, $test;
HttpUtils_NonblockingGet($param); # Starten der HTTP Abfrage. Es gibt keinen Return-Code.
@ -106,23 +120,33 @@ sub Matrix_ParseHttpResponse($)
$hash->{FAILS} = 3;
}
elsif($data ne "") { # wenn die Abfrage erfolgreich war ($data enthält die Ergebnisdaten des HTTP Aufrufes)
Log3 $name, 3, "url ".$param->{url}." returned: $data"; # Eintrag fürs Log
Log3 $name, 3, $hash->{stage}." returned: $data"; # Eintrag fürs Log
my $decoded = eval { decode_json($data) };
Log3 $name, 2, "$name: json error: $@ in data" if( $@ );
if ($param->{code} == 200){
$hash->{FAILS} = 0;
} else {
$hash->{FAILS}++;
if ($decoded->{'errcode'} eq "M_UNKNOWN_TOKEN" && $decoded->{'error'} eq "Access token has expired"){
if ($decoded->{'soft_logout'} eq "true"){
}
# default next request
$nextRequest = "sync" ;
# An dieser Stelle die Antwort parsen / verarbeiten mit $data
# {"errcode":"M_UNKNOWN_TOKEN","error":"refresh token does not exist"}
my $errcode = $decoded->{'errcode'} ? $decoded->{'errcode'} : "";
if ($decoded->{'errcode'} eq "M_UNKNOWN_TOKEN"){
if ($decoded->{'error'} eq "Access token has expired"){
if ($decoded->{'soft_logout'} eq "true"){
#$hash->{polling} = $hash->{polling}.'; '.$param->{code}.' soft_logout->refresh';
$nextRequest = 'refresh';
}else{
#$hash->{polling} = $hash->{polling}.'; '.$param->{code}.' hard_logout->login?';
}
} elsif ($decoded->{'error'} eq "refresh token does not exist"){
#$hash->{polling} = $hash->{polling}.'; '.$param->{code}.' soft_logout->refresh';
$nextRequest = 'login';
}
}
# An dieser Stelle die Antwort parsen / verarbeiten mit $data
$hash->{session} = $decoded->{'session'};
$hash->{last_session} = $now;
readingsBulkUpdate($hash, "fullResponse", $data);
@ -130,9 +154,9 @@ sub Matrix_ParseHttpResponse($)
$nextRequest = "reg2";
}
if ($param->{code} == 200 && ($hash->{stage} eq "reg2" || $hash->{stage} eq "login" || $hash->{stage} eq "refresh")){
$hash->{user_id} = $decoded->{'user_id'} if ($decoded->{'user_id'});
$hash->{home_server} = $decoded->{'home_server'} if ($decoded->{'home_server'});
$hash->{device_id} = $decoded->{'device_id'} if ($decoded->{'device_id'});
readingsBulkUpdate($hash, "user_id", $decoded->{'user_id'}) if ($decoded->{'user_id'});
readingsBulkUpdate($hash, "home_server", $decoded->{'home_server'}) if ($decoded->{'home_server'});
readingsBulkUpdate($hash, "device_id", $decoded->{'device_id'}) if ($decoded->{'device_id'});
readingsBulkUpdate($hash, ".expires_in_ms", $decoded->{'expires_in_ms'}) if ($decoded->{'expires_in_ms'});
readingsBulkUpdate($hash, ".refresh_token", $decoded->{'refresh_token'}) if ($decoded->{'refresh_token'});
@ -142,13 +166,12 @@ sub Matrix_ParseHttpResponse($)
readingsBulkUpdate($hash, "last_login", $param->{code}) if $hash->{stage} eq "login";
readingsBulkUpdate($hash, "last_refresh", $param->{code}) if $hash->{stage} eq "refresh";
$hash->{polling} = '200 login' if $hash->{stage} eq "login";
$nextRequest = "sync" ;
}
if ($hash->{stage} eq "wellknown"){
# https://spec.matrix.org/unstable/client-server-api/
}
if ($param->{code} == 200 && $hash->{stage} eq "sync"){
$hash->{since} = $decoded->{'next_batch'} if ($decoded->{'next_batch'});
readingsBulkUpdate($hash, "since", $decoded->{'next_batch'}) if ($decoded->{'next_batch'});
# roomlist
my $list = $decoded->{'rooms'}->{'join'};
#my @roomlist = ();
@ -181,16 +204,28 @@ sub Matrix_ParseHttpResponse($)
readingsBulkUpdate($hash, "message", 'ignoriert, nicht '.AttrVal($name, 'MatrixSender', ''));
readingsBulkUpdate($hash, "sender", $sender);
}
} elsif ($tl->{'type'} eq "org.matrix.msc3381.poll.response"){
my $sender = $tl->{'sender'};
if (AttrVal($name, 'MatrixSender', '') =~ $sender){
readingsBulkUpdate($hash, "answer", $tl->{'content'}->{'org.matrix.msc3381.poll.response'}->{'answers'}[0]);
readingsBulkUpdate($hash, "sender", $sender);
# poll.end and
$nextRequest = "pollend" ;
# command
}
}
}
#push(@roomlist,"$id: ";
}
}
$nextRequest = "sync";
}
if ($hash->{stage} eq "filter"){
readingsBulkUpdate($hash, "filter_id", $decoded->{'filter_id'}) if ($decoded->{'filter_id'});
}
if ($hash->{stage} eq "msg" || $hash->{stage} eq "pollstart" || $hash->{stage} eq "pollend"){
readingsBulkUpdate($hash, "event_id", $decoded->{'event_id'}) if ($decoded->{'event_id'});
#m.relates_to
}
}
readingsEndUpdate($hash, 1);
$hash->{BUSY} = $hash->{BUSY} - 1; # queue is busy until response is received
@ -353,6 +388,12 @@ sub Matrix_Set {
elsif ($opt eq "filter") {
return Matrix_PerformHttpRequest($hash, $opt, '');
}
elsif ($opt eq "pollstart") {
return Matrix_PerformHttpRequest($hash, $opt, '');
}
elsif ($opt eq "pollend") {
return Matrix_PerformHttpRequest($hash, $opt, '');
}
elsif ($opt eq "register") {
return Matrix_PerformHttpRequest($hash, $opt, ''); # 2 steps (ToDo: 3 steps empty -> dummy -> registration_token o.a.)
}
@ -363,7 +404,7 @@ sub Matrix_Set {
return Matrix_PerformHttpRequest($hash, $opt, '');
}
else {
return "Unknown argument $opt, choose one of filter poll:0,1 poll.fullstate:0,1 msg register login:noArg refresh:noArg";
return "Unknown argument $opt, choose one of filter pollstart pollend poll:0,1 poll.fullstate:0,1 msg register login:noArg refresh:noArg";
}
#return "$opt set to $value. Try to get it.";
@ -394,21 +435,18 @@ sub Matrix_Attr {
<a name="Matrix"></a>
<h3>Matrix</h3>
<ul>
<i>Matrix</i> implements the classical "Matrix World" as a starting point for module development.
You may want to copy 98_Matrix.pm to start implementing a module of your very own. See
<a href="http://wiki.fhem.de/wiki/DevelopmentModuleIntro">DevelopmentModuleIntro</a> for an
in-depth instruction to your first module.
<i>Matrix</i> implements a client to Matrix-Synapse-Servers. It is in a very early development state.
<br><br>
<a name="Matrixdefine"></a>
<b>Define</b>
<ul>
<code>define &lt;name&gt; Matrix &lt;greet&gt;</code>
<code>define &lt;name&gt; <server> <user> <password></code>
<br><br>
Example: <code>define Matrix Matrix TurnUrRadioOn</code>
Example: <code>define matrix Matrix matrix.com fhem asdf</code>
<br><br>
The "greet" parameter has no further meaning, it just demonstrates
how to set a so called "Internal" value. See <a href="http://fhem.de/commandref.html#define">commandref#define</a>
for more info about the define command.
noch ins Englische:
1. Anmerkung: Zur einfachen Einrichtung habe ich einen Matrix-Element-Client mit "--profile=fhem" gestartet und dort die Registrierung und die Räume vorbereitet. Achtung: alle Räume müssen noch unverschlüsselt sein um FHEM anzubinden. Alle Einladungen in Räume und Annehmen von Einladungen geht hier viel einfacher. Aus dem Element-Client dann die Raum-IDs merken für das Modul.
2. Anmerkung: sets, gets, Attribute und Readings müssen noch besser bezeichnet werden.
</ul>
<br>
@ -417,18 +455,26 @@ sub Matrix_Attr {
<ul>
<code>set &lt;name&gt; &lt;option&gt; &lt;value&gt;</code>
<br><br>
You can <i>set</i> any value to any of the following options. They're just there to
<i>get</i> them. See <a href="http://fhem.de/commandref.html#set">commandref#set</a>
for more info about the set command.
You can <i>set</i> any value to any of the following options.
<br><br>
Options:
<ul>
<li><i>satisfaction</i><br>
Defaults to "no"</li>
<li><i>whatyouwant</i><br>
Defaults to "can't"</li>
<li><i>whatyouneed</i><br>
Defaults to "try sometimes"</li>
<li><i>register</i><br>
without function, do not use this</li>
<li><i>login</i><br>
Login to the Matrix-Server and sync endless if poll is set to "1"</li>
<li><i>refresh</i><br>
If logged in or in state "soft-logout" refresh gets a new access_token and syncs endless if poll is set to "1"</li>
<li><i>filter</i><br>
A Filter must be set for syncing in long poll. This filter is in the moment experimentell and must be set manual</li>
<li><i>poll</i><br>
Defaults to "0": Set poll to "1" for starting the sync-loop</li>
<li><i>poll.fullstate</i><br>
Defaults to "0": Set poll.fullstate to "1" for getting in the next sync a full state of all rooms</li>
<li><i>pollstart</i><br>
Experimental: start a in this time fixed question in the room from reading room. The first answer to the question is logged and ends the question.</li>
<li><i>pollend</i><br>
Experimental: stop a question also it is not answered.</li>
</ul>
</ul>
<br>
@ -438,10 +484,7 @@ sub Matrix_Attr {
<ul>
<code>get &lt;name&gt; &lt;option&gt;</code>
<br><br>
You can <i>get</i> the value of any of the options described in
<a href="#Matrixset">paragraph "Set" above</a>. See
<a href="http://fhem.de/commandref.html#get">commandref#get</a> for more info about
the get command.
</ul>
<br>
@ -450,12 +493,11 @@ sub Matrix_Attr {
<ul>
<code>attr &lt;name&gt; &lt;attribute&gt; &lt;value&gt;</code>
<br><br>
See <a href="http://fhem.de/commandref.html#attr">commandref#attr</a> for more info about
the attr command.
<br><br>
Attributes:
<ul>
<li><i>formal</i> no|yes<br>
<li><i>....</i> no|yes<br>
When you set formal to "yes", all output of <i>get</i> will be in a
more formal language. Default is "no".
</li>

File diff suppressed because it is too large Load Diff

View File

@ -1 +0,0 @@
define dummyHello Hello HiHo