(Gelöst) JSON Senden in Post Request

Begonnen von Esjay, 09 September 2020, 21:22:53

Vorheriges Thema - Nächstes Thema

Esjay

Guten Abend zusammen,
ich sitze mal wieder an einem Problem, bei dem ich nicht weiter komme.
Ich würde gerne den im Anhang zu sehenden Request in Fhem umsetzen.

Ich dachte an etwas wie
defmod MyDevice HTTPMOD none 0
attr MyDevice userattr set01Data set01Name set01TextArg:0,1 set01URL setHeader1
attr MyDevice DbLogExclude .*
attr MyDevice set01Data {"set" :{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":$val}}}
attr MyDevice set01Name LEDDevice
attr MyDevice set01TextArg 1
attr MyDevice set01URL http://192.168.178.76:8090/json-rpc
attr MyDevice setHeader1 Content-Type: application/json
attr MyDevice showBody 1
attr MyDevice showError 1
attr MyDevice verbose 5


Log
2020.09.09 21:16:53 5:  MyDevice: set called with LEDDevice false
2020.09.09 21:16:53 5:  MyDevice: set found option LEDDevice in attribute set01Name
2020.09.09 21:16:53 4:  MyDevice: set will now set LEDDevice -> false
2020.09.09 21:16:53 5:  MyDevice: AddToQueue adds type set01 to URL http://192.168.178.76:8090/json-rpc, data {"set" :{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":$val}}}, header Content-Type: application/json, retry 0, initial queue len: 0
2020.09.09 21:16:53 5:  MyDevice: HandleSendQueue called, qlen = 1
2020.09.09 21:16:53 4:  MyDevice: HandleSendQueue sends set01 with timeout 2 to http://192.168.178.76:8090/json-rpc,
data: {"set" :{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":false}}},
header: Content-Type: application/json
2020.09.09 21:16:53 5:  HttpUtils url=http://192.168.178.76:8090/json-rpc
2020.09.09 21:16:53 4:  IP: 192.168.178.76 -> 192.168.178.76
2020.09.09 21:16:53 5:  HttpUtils request header:
POST /json-rpc HTTP/1.0
Host: 192.168.178.76:8090
User-Agent: fhem
Accept-Encoding: gzip,deflate
Content-Type: application/json
Content-Length: 94

2020.09.09 21:16:55 3:  MyDevice: Read callback: Error: read from http://192.168.178.76:8090 timed out
2020.09.09 21:16:55 4:  MyDevice: Read callback: request type was set01 retry 0, no headers, no body
2020.09.09 21:16:55 5:  MyDevice: Read callback: body empty
2020.09.09 21:16:55 4:  MyDevice: Read found no charset header (bodyDecode was set to auto)
2020.09.09 21:16:55 5:  MyDevice: GetCookies is looking for Cookies
2020.09.09 21:16:55 5:  MyDevice: ExtractSid called, context set, num 01
2020.09.09 21:16:55 4:  MyDevice: no header to look for redirects
2020.09.09 21:16:55 5:  MyDevice: CheckAuth decided no authentication required


Abgeleitet von hier
https://wiki.fhem.de/wiki/HTTPMOD#Advanced_configuration_to_define_a_set_command_and_send_data_to_a_device

Ursprung des Ganzen ist der Versuch mein Hyperion Ng über fhem zu steuern.
Siehe hier:
https://docs.hyperion-project.org/en/json/#what-is-json

Wäre für jeden Tipp dankbar.

Grüße



Otto123

#1
Hi,

ich habe keine Ahnung davon, aber ich lese Wiki und was Du senden sollst/willst anders:
attr MyDevice set01Data {"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":$val}}

Ein set MyDevice LEDDevice false würde dann $val durch false ersetzen und diesen String senden {"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":false}}

Gruß Otto
Viele Grüße aus Leipzig  ⇉  nächster Stammtisch an der Lindennaundorfer Mühle
RaspberryPi B B+ B2 B3 B3+ ZeroW,HMLAN,HMUART,Homematic,Fritz!Box 7590,WRT3200ACS-OpenWrt,Sonos,VU+,Arduino nano,ESP8266,MQTT,Zigbee,deconz

Esjay

Hallo Otto

ich möchte ja keine "10" sondern ein "true" oder "false" senden. Daher auch das Attribut "set01TextArg"

Hab gerade gesehen das du nochmal editiert hast :-).

Wegen dem set war ich mir auch unsicher, aber ich dachte ich übernehme das aus dem Wiki.

Aber auch mit deinem Aufruf, welchen ich vorher bereits getestet habe, erhalte ich das gleiche in grün im Log.

2020.09.09 21:49:45 1:  Logfile gelöscht
2020.09.09 21:49:55 5:  MyDevice: set called with LEDDevice true
2020.09.09 21:49:55 5:  MyDevice: set found option LEDDevice in attribute set01Name
2020.09.09 21:49:55 4:  MyDevice: set will now set LEDDevice -> true
2020.09.09 21:49:55 5:  MyDevice: AddToQueue adds type set01 to URL http://192.168.178.76:8090/json-rpc, data {"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":$val}}, header Content-Type: application/json, retry 0, initial queue len: 0
2020.09.09 21:49:55 5:  MyDevice: HandleSendQueue called, qlen = 1
2020.09.09 21:49:55 4:  MyDevice: HandleSendQueue sends set01 with timeout 2 to http://192.168.178.76:8090/json-rpc,
data: {"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":true}},
header: Content-Type: application/json
2020.09.09 21:49:55 5:  HttpUtils url=http://192.168.178.76:8090/json-rpc
2020.09.09 21:49:55 4:  IP: 192.168.178.76 -> 192.168.178.76
2020.09.09 21:49:55 5:  HttpUtils request header:
POST /json-rpc HTTP/1.0
Host: 192.168.178.76:8090
User-Agent: fhem
Accept-Encoding: gzip,deflate
Content-Type: application/json
Content-Length: 84

2020.09.09 21:49:57 3:  MyDevice: Read callback: Error: read from http://192.168.178.76:8090 timed out
2020.09.09 21:49:57 4:  MyDevice: Read callback: request type was set01 retry 0, no headers, no body
2020.09.09 21:49:57 5:  MyDevice: Read callback: body empty
2020.09.09 21:49:57 4:  MyDevice: Read found no charset header (bodyDecode was set to auto)
2020.09.09 21:49:57 5:  MyDevice: GetCookies is looking for Cookies
2020.09.09 21:49:57 5:  MyDevice: ExtractSid called, context set, num 01
2020.09.09 21:49:57 4:  MyDevice: no header to look for redirects
2020.09.09 21:49:57 5:  MyDevice: CheckAuth decided no authentication required


Grüße

amenomade

Geht das mit curl auf dem Fhem Rechner?

curl --data '{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":$val}}'  http://192.168.178.76:8090/json-rpc
Pi 3B, Alexa, CUL868+Selbstbau 1/2λ-Dipol-Antenne, USB Optolink / Vitotronic, Debmatic und HM / HmIP Komponenten, Rademacher Duofern Jalousien, Fritz!Dect Thermostaten, Proteus

Otto123

@amenomade da wird doch aber $val nicht aufgelöst!? wegen ' $variable'
entweder so ?
curl --data {"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":$val}}  http://192.168.178.76:8090/json-rpcoder so testen:
curl --data '{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":false}}'  http://192.168.178.76:8090/json-rpc
Viele Grüße aus Leipzig  ⇉  nächster Stammtisch an der Lindennaundorfer Mühle
RaspberryPi B B+ B2 B3 B3+ ZeroW,HMLAN,HMUART,Homematic,Fritz!Box 7590,WRT3200ACS-OpenWrt,Sonos,VU+,Arduino nano,ESP8266,MQTT,Zigbee,deconz

Esjay

Zitat von: amenomade am 09 September 2020, 22:30:27
Geht das mit curl auf dem Fhem Rechner?

curl --data '{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":$val}}'  http://192.168.178.76:8090/json-rpc

Nabend:
fhem@FHEM:~$ curl --data '{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":$val}}'  http://192.168.178.76:8090/json-rpc

{
    "command": "",
    "error": "Errors during message parsing, please consult the Hyperion Log.",
    "success": false,
    "tan": 0
}
fhem@FHEM:~$
fhem@FHEM:~$ curl --data '{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":true}}'  http://192.168.178.76:8090/json-rpc
{
    "command": "componentstate",
    "success": true,
    "tan": 0
}
fhem@FHEM:~$


Bin erst auf das $val hereingefallen, aber wenn ich true nehme bekomme ich eine positive Rückmeldung.

Grüße

amenomade

Zitat von: Otto123 am 09 September 2020, 22:40:22
@amenomade da wird doch aber $val nicht aufgelöst!? wegen ' $variable'
entweder so ?
curl --data {"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":$val}}  http://192.168.178.76:8090/json-rpcoder so testen:
curl --data '{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":false}}'  http://192.168.178.76:8090/json-rpc

Oops ja sorry wegen $val.
Pi 3B, Alexa, CUL868+Selbstbau 1/2λ-Dipol-Antenne, USB Optolink / Vitotronic, Debmatic und HM / HmIP Komponenten, Rademacher Duofern Jalousien, Fritz!Dect Thermostaten, Proteus

amenomade

#7
Also... seeeeehr ähnlich wie das andere Problem, wo ich auch versuche zu helfen.
https://forum.fhem.de/index.php/topic,45176.msg1083919.html#msg1083919

Ich kann mir im Moment das nicht erklären. Ich habe dort einen Test vorgeschlagen:
ZitatEDIT: eine andere Idee wäre, ein PRESENCE Device zu definieren, nur um zu sehen, ob der Sonoff aus Fhem erreichbar ist:

define sonoffpres PRESENCE lan-ping 192.168.178.93 10

Meldet er "present"?
Also bei dir die IP anpassen

Läuft fhem in einem Container?
Pi 3B, Alexa, CUL868+Selbstbau 1/2λ-Dipol-Antenne, USB Optolink / Vitotronic, Debmatic und HM / HmIP Komponenten, Rademacher Duofern Jalousien, Fritz!Dect Thermostaten, Proteus

Esjay

Hier das Ergebnis :
Internals:
   ADDRESS    192.168.178.76
   CFGFN     
   DEF        lan-ping 192.168.178.76
   FUUID      5f5940e2-f33f-a126-8539-fe366cdb6cb72e29
   INTERVAL_NORMAL 30
   INTERVAL_PRESENT 30
   MODE       lan-ping
   NAME       Hyperiontest
   NOTIFYDEV  global
   NR         54512
   NTFY_ORDER 50-Hyperiontest
   STATE      present
   TYPE       PRESENCE
   READINGS:
     2020-09-09 22:53:54   model           lan-ping
     2020-09-09 22:56:20   presence        present
     2020-09-09 22:56:20   state           present
   helper:
     CURRENT_STATE present
Attributes:
   DbLogExclude .*


Nochmal ein List vom Device
Internals:
   BUSY       0
   CFGFN     
   DEF        none 0
   FUUID      5f591f94-f33f-a126-b74b-06ed878acd21a42e
   Interval   0
   LASTSEND   1599680995.05848
   MainURL   
   ModuleVersion 3.5.22 - 7.2.2020
   NAME       MyDevice
   NOTIFYDEV  global
   NR         53979
   NTFY_ORDER 50-MyDevice
   STATE      ???
   TRIGGERTIME 0
   TRIGGERTIME_FMT
   TYPE       HTTPMOD
   addr       http://192.168.178.76:8090
   auth       0
   buf       
   data       {"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":true}}
   displayurl http://192.168.178.76:8090/json-rpc
   header     Content-Type: application/json
   host       192.168.178.76
   httpbody   
   httpversion 1.0
   ignoreredirects 1
   loglevel   4
   path       /json-rpc
   protocol   http
   redirects  0
   timeout    2
   url        http://192.168.178.76:8090/json-rpc
   value      true
   QUEUE:
   READINGS:
     2020-09-09 21:49:57   LAST_ERROR      read from http://192.168.178.76:8090 timed out
   REQUEST:
     data       {"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":$val}}
     header     Content-Type: application/json
     ignoreredirects 0
     retryCount 0
     type       set01
     url        http://192.168.178.76:8090/json-rpc
     value      true
   sslargs:
Attributes:
   DbLogExclude .*
   set01Data  {"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":$val}}
   set01Name  LEDDevice
   set01TextArg 1
   set01URL   http://192.168.178.76:8090/json-rpc
   set03NoArg 1
   setHeader1 Content-Type: application/json
   showBody   1
   showError  1
   userattr   set01Data set01Name set01TextArg:0,1 set01URL set03NoArg:0,1 setHeader1
   verbose    5


Ehem läuft auf einem LXC unter Proxmox.

amenomade

Und das "curl" hast Du IM Container gemacht? (sorry, wenn das nicht möglich ist, und die Frage blöd ist, ich kenne mich nicht so gut mit Container aus)
Pi 3B, Alexa, CUL868+Selbstbau 1/2λ-Dipol-Antenne, USB Optolink / Vitotronic, Debmatic und HM / HmIP Komponenten, Rademacher Duofern Jalousien, Fritz!Dect Thermostaten, Proteus

Esjay

Alles gut.. Jap den habe ich in dem Container gemacht.
Habe jetzt nochmal einen anderen curl abgesetzt:
Response siehe unten.


fhem@FHEM:~$ curl -X POST -i 'http://192.168.178.76:8090/json-rpc' --data '{
comm>  "command":"componentstate",         
>  "componentstate":{
>    "component":"LEDDEVICE",
>    "state":false
> }
>   }'
HTTP/1.1 200 OK.
Date: Wed, 09 Sep 2020 21:06:51 UTC
Content-Type: application/json
Server: Hyperion Webserver
Content-Length: 71

{
    "command": "componentstate",
    "success": true,
    "tan": 0
}


Kannst du daraus etwas erkennen?

amenomade

Zitat von: Esjay am 09 September 2020, 23:08:48


Kannst du daraus etwas erkennen?
Ja, dass es geht! ;)
Kannst Du noch mit Option -v das curl machen?
Pi 3B, Alexa, CUL868+Selbstbau 1/2λ-Dipol-Antenne, USB Optolink / Vitotronic, Debmatic und HM / HmIP Komponenten, Rademacher Duofern Jalousien, Fritz!Dect Thermostaten, Proteus

Esjay

fhem@FHEM:~$ curl -X  POST -v -i 'http://192.168.178.76:8090/json-rpc' --data '{
"command":"componentstate",         
"componentstate":{
   "component":"LEDDEVICE",
   "state":false
}
  }'
Note: Unnecessary use of -X or --request, POST is already inferred.
* Expire in 0 ms for 6 (transfer 0x563b7b532e90)
*   Trying 192.168.178.76...
* TCP_NODELAY set
* Expire in 200 ms for 4 (transfer 0x563b7b532e90)
* Connected to 192.168.178.76 (192.168.178.76) port 8090 (#0)
> POST /json-rpc HTTP/1.1
> Host: 192.168.178.76:8090
> User-Agent: curl/7.64.0
> Accept: */*
> Content-Length: 111
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 111 out of 111 bytes
< HTTP/1.1 200 OK.
HTTP/1.1 200 OK.
< Content-Type: application/json
Content-Type: application/json
< Server: Hyperion Webserver
Server: Hyperion Webserver
< Date: Wed, 09 Sep 2020 21:19:56 UTC
Date: Wed, 09 Sep 2020 21:19:56 UTC
< Content-Length: 71
Content-Length: 71

<
{
    "command": "componentstate",
    "success": true,
    "tan": 0
}
* Connection #0 to host 192.168.178.76 left intact

amenomade

#13
Dann versuch mal noch die gleiche Headers im HTTPMOD zu setzen:
setHeader2 User-Agent: curl/7.64.0
setHeader3 Accept: */*

Length wird nw vom HTTPMOD automatisch kalkuliert, und ContentType auch hast Du schon.
Pi 3B, Alexa, CUL868+Selbstbau 1/2λ-Dipol-Antenne, USB Optolink / Vitotronic, Debmatic und HM / HmIP Komponenten, Rademacher Duofern Jalousien, Fritz!Dect Thermostaten, Proteus

Esjay

Leider auch keine Änderung

2020.09.09 23:28:06 5:  MyDevice: set called with LEDDevice false
2020.09.09 23:28:06 5:  MyDevice: set found option LEDDevice in attribute set01Name
2020.09.09 23:28:06 4:  MyDevice: set will now set LEDDevice -> false
2020.09.09 23:28:06 5:  MyDevice: AddToQueue adds type set01 to URL http://192.168.178.76:8090/json-rpc, data {"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":$val}}, header Content-Type: application/json
User-Agent: curl/7.64.0
Accept: */*, retry 0, initial queue len: 0
2020.09.09 23:28:06 5:  MyDevice: HandleSendQueue called, qlen = 1
2020.09.09 23:28:06 5:  MyDevice: HandleSendQueue - call with HTTP METHOD: POST
2020.09.09 23:28:06 4:  MyDevice: HandleSendQueue sends set01 with timeout 2 to http://192.168.178.76:8090/json-rpc,
data: {"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":false}},
header: Content-Type: application/json
User-Agent: curl/7.64.0
Accept: */*
2020.09.09 23:28:06 5:  HttpUtils url=http://192.168.178.76:8090/json-rpc
2020.09.09 23:28:06 4:  IP: 192.168.178.76 -> 192.168.178.76
2020.09.09 23:28:06 5:  HttpUtils request header:
POST /json-rpc HTTP/1.0
Host: 192.168.178.76:8090
Accept-Encoding: gzip,deflate
Content-Type: application/json
User-Agent: curl/7.64.0
Accept: */*
Content-Length: 85

2020.09.09 23:28:08 3:  MyDevice: Read callback: Error: read from http://192.168.178.76:8090 timed out
2020.09.09 23:28:08 4:  MyDevice: Read callback: request type was set01 retry 0, no headers, no body
2020.09.09 23:28:08 5:  MyDevice: Read callback: body empty
2020.09.09 23:28:08 4:  MyDevice: Read found no charset header (bodyDecode was set to auto)
2020.09.09 23:28:08 5:  MyDevice: GetCookies is looking for Cookies
2020.09.09 23:28:08 5:  MyDevice: ExtractSid called, context set, num 01
2020.09.09 23:28:08 4:  MyDevice: no header to look for redirects
2020.09.09 23:28:08 5:  MyDevice: CheckAuth decided no authentication required