Neue Version von HTTPMOD mit neuen Features zum Testen

Begonnen von StefanStrobel, 05 Dezember 2015, 08:31:32

Vorheriges Thema - Nächstes Thema

StefanStrobel

Hallo,

Ja, das bodyDecode-Feature hat bisher nie funktioniert.
Für die neue Version habe ich einige automatisierte Tests eingeführt und dabei ist das aufgefallen.
Jetzt funktioniert bodyDecode, was jedoch zur Folge hat, dass der charset-Header einer HTTP-Response erkannt wird und Response-Daten automatisch mit decode in das interne Format von Perl konvertiert werden.
Bisher stand das zwar im Log, war aber effektiv wirkungslos.
Entsprechend muss man ein readingEncode utf8 setzen, um die Readings wieder in utf8 zu bekommen.
Oder man setzt bodyDecode auf none (der Default ist auto, dann wird der charset-Header verwendet).

Ich bin mir nicht sicher, womit ich weniger Ärger verursache. Den Bug wie bisher belassen, per Default auch readingEncode auf utf8 setzen oder bodyDecode nicht anwenden, wenn der Response-Body als utf8 kommt ...
Alles kann irgendwo Probleme machen, insbesondere bei alten Perl-Versionen mit Memory Leaks. Deshalb hatte ich das mit bodyDecode auch mal eingebaut.

Am saubersten wäre es nach meinem Verständnis, wenn die eingelesenen Daten tatsächlich per Default zunächst dekodiert werden und wenn dann die Readings ebenso per Default als utf8 encoded werden.

Wie seht Ihr das?

Gruß
    Stefan


amenomade

Wenn Du ja die Möglichkeit hast, bei automatischen bodyDecode die Readings dann standardmässig wieder zu encoden, wird es keine Regression geben, und das wird Problemen bei anderen lösen, die bisher bodydecode nicht vernünftig benutzen konnten.

Wenn ich beim HTTPMOD oben reading01Encode utf8 noch dazu definiere, geht es ja.
Pi 3B, Alexa, CUL868+Selbstbau 1/2λ-Dipol-Antenne, USB Optolink / Vitotronic, Debmatic und HM / HmIP Komponenten, Rademacher Duofern Jalousien, Fritz!Dect Thermostaten, Proteus

ch.eick

Hallo Ihr Spezialisten,

ich habe da eine für mich etwas komplexe Anmeldeprozedur implementiert, die Euch wahrscheinlich die Haare zu berge steigen lässt :-)
Deshalb stelle ich das nun zur Diskussion und hoffe auf Vereinfachungen. Es geht also erst mal um HTTPMOD.

Im Hintergrund gibt es zwei Python Skripte, die momentan die Schlüssel generieren. Die hätte ich liebend gerne weg und versuche mich auch an einer Perl Migration...

Das Device mit der mehrstufigen Anmeldung wird über einige userreadings von Step zu Step getriggert.
Weiterhin wird auch bereits eine Anmeldenotwendigkeit erkannt und dann mit /auth/start begonnen. Hier kann ich momentan nicht den original HTTP request requeuen, weil es ansonsten mit den ganzen zwischenschritten und Python Calls nicht passt. Die Python Skripte füllen asyncon die auth_* readings, worauf dann die userreadings reagieren. Ich liste auch mal Beispiel auth_* readings im nächsten Listing an und ein verbose 5 Log kommt auch noch.

defmod PV_Anlage_1_API HTTPMOD http://%IP-Address_Plenticore%/auth/me 0

attr PV_Anlage_1_API DbLogExclude .*
attr PV_Anlage_1_API authRetries 1
attr PV_Anlage_1_API dontRequeueAfterAuth 1
attr PV_Anlage_1_API enableControlSet 1
attr PV_Anlage_1_API enableCookies 1
attr PV_Anlage_1_API get01Data {"nonce": "%randomString64%","username": "user"}
attr PV_Anlage_1_API get01Name 01_/auth/start
attr PV_Anlage_1_API get01URL http://%IP-Address_Plenticore%/api/v1/auth/start
attr PV_Anlage_1_API get02Data {"transactionId": "%auth_transactionId%", "proof": "%auth_proof%"}
attr PV_Anlage_1_API get02Name 02_/auth/finish
attr PV_Anlage_1_API get02URL http://%IP-Address_Plenticore%/api/v1/auth/finish
attr PV_Anlage_1_API get03Data {"transactionId": "%auth_transactionId%", "iv": "%auth_iv%", "tag": "%auth_authtag%", "payload": "%auth_payload%"}
attr PV_Anlage_1_API get03Name 03_/auth/create_session
attr PV_Anlage_1_API get03URL http://%IP-Address_Plenticore%/api/v1/auth/create_session
attr PV_Anlage_1_API get04Header authorization: Session %auth_sessionId%
attr PV_Anlage_1_API get04Name 04_/auth/me
attr PV_Anlage_1_API get04URL http://%IP-Address_Plenticore%/api/v1/auth/me
attr PV_Anlage_1_API get05Header authorization: Session %auth_sessionId%
attr PV_Anlage_1_API get05Name 05_/info/version
attr PV_Anlage_1_API get05URL http://%IP-Address_Plenticore%/api/v1/info/version
attr PV_Anlage_1_API get06Name 06_/auth/logout
attr PV_Anlage_1_API get06URL http://%IP-Address_Plenticore%/api/v1/auth/logout
attr PV_Anlage_1_API get20Header authorization: Session %auth_sessionId%
attr PV_Anlage_1_API get20Name 20_/processdata/scb_statistic_EnergyFlow
attr PV_Anlage_1_API get20URL http://%IP-Address_Plenticore%/api/v1/processdata/scb:statistic:EnergyFlow
attr PV_Anlage_1_API get21Header authorization: Session %auth_sessionId%
attr PV_Anlage_1_API get21Name 21_/modules_list
attr PV_Anlage_1_API get21URL http://%IP-Address_Plenticore%/api/v1/modules
attr PV_Anlage_1_API get22Header authorization: Session %auth_sessionId%
attr PV_Anlage_1_API get22Name 22_SmartBatteryControl
attr PV_Anlage_1_API get22URL http://%IP-Address_Plenticore%/api/v1/settings/devices:local/Battery:SmartBatteryControl:Enable
attr PV_Anlage_1_API getHeader01 Accept-Encoding: gzip,deflate
attr PV_Anlage_1_API getHeader02 Content-type:application/json, Accept:application/json, Connection:keep-alive
attr PV_Anlage_1_API group PV Eigenverbrauch
attr PV_Anlage_1_API icon sani_solar
attr PV_Anlage_1_API reAuthRegex "authenticated":false|"processdata":\[\]
attr PV_Anlage_1_API reading0100JSON anonymous
attr PV_Anlage_1_API reading0100Name auth_me_anonymous
attr PV_Anlage_1_API reading0101JSON role
attr PV_Anlage_1_API reading0101Name auth_me_role
attr PV_Anlage_1_API reading0102JSON active
attr PV_Anlage_1_API reading0102Name auth_me_active
attr PV_Anlage_1_API reading0103JSON locked
attr PV_Anlage_1_API reading0103Name auth_me_locked
attr PV_Anlage_1_API reading0104JSON authenticated
attr PV_Anlage_1_API reading0104Name auth_me_authenticated
attr PV_Anlage_1_API reading0105JSON salt
attr PV_Anlage_1_API reading0105Name auth_salt
attr PV_Anlage_1_API reading0106JSON transactionId
attr PV_Anlage_1_API reading0106Name auth_transactionId
attr PV_Anlage_1_API reading0107JSON rounds
attr PV_Anlage_1_API reading0107Name auth_rounds
attr PV_Anlage_1_API reading0108JSON nonce
attr PV_Anlage_1_API reading0108Name auth_nonce
attr PV_Anlage_1_API reading0109JSON token
attr PV_Anlage_1_API reading0109Name auth_token
attr PV_Anlage_1_API reading0110JSON message
attr PV_Anlage_1_API reading0111JSON signature
attr PV_Anlage_1_API reading0111Name auth_signature
attr PV_Anlage_1_API reading0112JSON sessionId
attr PV_Anlage_1_API reading0112Name auth_sessionId
attr PV_Anlage_1_API reading0200JSON api_version
attr PV_Anlage_1_API reading0200Name info_api_version
attr PV_Anlage_1_API reading0201JSON sw_version
attr PV_Anlage_1_API reading0201Name info_sw_version
attr PV_Anlage_1_API reading0202JSON name
attr PV_Anlage_1_API reading0202Name info_name
attr PV_Anlage_1_API reading0203JSON hostname
attr PV_Anlage_1_API reading0203Name info_hostname
attr PV_Anlage_1_API reading2001JSON 01_processdata_01_value
attr PV_Anlage_1_API reading2001Name Statistic_Autarky_Day
attr PV_Anlage_1_API reading2002JSON 01_processdata_02_value
attr PV_Anlage_1_API reading2002Name Statistic_Autarky_Month
attr PV_Anlage_1_API reading2003JSON 01_processdata_03_value
attr PV_Anlage_1_API reading2003Name Statistic_Autarky_Total
attr PV_Anlage_1_API reading2004JSON 01_processdata_04_value
attr PV_Anlage_1_API reading2004Name Statistic_Autarky_Year
attr PV_Anlage_1_API reading2011JSON 01_processdata_05_value
attr PV_Anlage_1_API reading2011Name Statistic_CO2Saving_Day
attr PV_Anlage_1_API reading2012JSON 01_processdata_06_value
attr PV_Anlage_1_API reading2012Name Statistic_CO2Saving_Month
attr PV_Anlage_1_API reading2013JSON 01_processdata_08_value
attr PV_Anlage_1_API reading2013Name Statistic_CO2Saving_Year
attr PV_Anlage_1_API reading2021JSON 01_processdata_09_value
attr PV_Anlage_1_API reading2021Name Statistic_EnergyHome_Day
attr PV_Anlage_1_API reading2022JSON 01_processdata_10_value
attr PV_Anlage_1_API reading2022Name Statistic_EnergyHome_Month
attr PV_Anlage_1_API reading2023JSON 01_processdata_11_value
attr PV_Anlage_1_API reading2023Name Statistic_EnergyHome_Total
attr PV_Anlage_1_API reading2024JSON 01_processdata_12_value
attr PV_Anlage_1_API reading2024Name Statistic_EnergyHome_Year
attr PV_Anlage_1_API reading2031JSON 01_processdata_13_value
attr PV_Anlage_1_API reading2031Name Statistic_EnergyHomeBat_Day
attr PV_Anlage_1_API reading2032JSON 01_processdata_14_value
attr PV_Anlage_1_API reading2032Name Statistic_EnergyHomeBat_Month
attr PV_Anlage_1_API reading2033JSON 01_processdata_15_value
attr PV_Anlage_1_API reading2033Name Statistic_EnergyHomeBat_Total
attr PV_Anlage_1_API reading2034JSON 01_processdata_16_value
attr PV_Anlage_1_API reading2034Name Statistic_EnergyHomeBat_Year
attr PV_Anlage_1_API reading2041JSON 01_processdata_17_value
attr PV_Anlage_1_API reading2041Name Statistic_EnergyHomeGrid_Day
attr PV_Anlage_1_API reading2042JSON 01_processdata_18_value
attr PV_Anlage_1_API reading2042Name Statistic_EnergyHomeGrid_Month
attr PV_Anlage_1_API reading2043JSON 01_processdata_19_value
attr PV_Anlage_1_API reading2043Name Statistic_EnergyHomeGrid_Total
attr PV_Anlage_1_API reading2044JSON 01_processdata_20_value
attr PV_Anlage_1_API reading2044Name Statistic_EnergyHomeGrid_Year
attr PV_Anlage_1_API reading2051JSON 01_processdata_21_value
attr PV_Anlage_1_API reading2051Name Statistic_EnergyHomePv_Day
attr PV_Anlage_1_API reading2052JSON 01_processdata_22_value
attr PV_Anlage_1_API reading2052Name Statistic_EnergyHomePv_Month
attr PV_Anlage_1_API reading2053JSON 01_processdata_23_value
attr PV_Anlage_1_API reading2053Name Statistic_EnergyHomePv_Total
attr PV_Anlage_1_API reading2054JSON 01_processdata_24_value
attr PV_Anlage_1_API reading2054Name Statistic_EnergyHomePv_Year
attr PV_Anlage_1_API reading2061JSON 01_processdata_25_value
attr PV_Anlage_1_API reading2061Name Statistic_OwnConsumptionRate_Day
attr PV_Anlage_1_API reading2062JSON 01_processdata_26_value
attr PV_Anlage_1_API reading2062Name Statistic_OwnConsumptionRate_Month
attr PV_Anlage_1_API reading2063JSON 01_processdata_27_value
attr PV_Anlage_1_API reading2063Name Statistic_OwnConsumptionRate_Total
attr PV_Anlage_1_API reading2064JSON 01_processdata_28_value
attr PV_Anlage_1_API reading2064Name Statistic_OwnConsumptionRate_Year
attr PV_Anlage_1_API reading2071JSON 01_processdata_29_value
attr PV_Anlage_1_API reading2071Name Statistic_Yield_Day
attr PV_Anlage_1_API reading2072JSON 01_processdata_30_value
attr PV_Anlage_1_API reading2072Name Statistic_Yield_Month
attr PV_Anlage_1_API reading2073JSON 01_processdata_31_value
attr PV_Anlage_1_API reading2073Name Statistic_Yield_Total
attr PV_Anlage_1_API reading2074JSON 01_processdata_32_value
attr PV_Anlage_1_API reading2074Name Statistic_Yield_Year
attr PV_Anlage_1_API replacement01Mode expression
attr PV_Anlage_1_API replacement01Regex %IP-Address_Plenticore%
attr PV_Anlage_1_API replacement01Value {ReadingsVal("PV_Anlage_1_config","IP-Address_Plenticore","")}
attr PV_Anlage_1_API replacement02Mode reading
attr PV_Anlage_1_API replacement02Regex %auth_transactionId%
attr PV_Anlage_1_API replacement02Value auth_transactionId
attr PV_Anlage_1_API replacement03Mode reading
attr PV_Anlage_1_API replacement03Regex %auth_proof%
attr PV_Anlage_1_API replacement03Value auth_proof
attr PV_Anlage_1_API replacement04Mode expression
attr PV_Anlage_1_API replacement04Regex %randomString64%
attr PV_Anlage_1_API replacement04Value {my $NAME = "PV_Anlage_1_API" ;;;;fhem("deletereading ".$NAME." message");;;;fhem("deletereading ".$NAME." auth.*");;;;my @chars=('a'..'z','A'..'Z','0'..'9');; my $r;; foreach(1..16) {$r.=$chars[rand @chars];;};; fhem("setreading ".$NAME." auth_randomString64 ".$r);; $r;;}
attr PV_Anlage_1_API replacement05Mode reading
attr PV_Anlage_1_API replacement05Regex %auth_randomString64%
attr PV_Anlage_1_API replacement05Value auth_randomString64
attr PV_Anlage_1_API replacement06Mode reading
attr PV_Anlage_1_API replacement06Regex %auth_token%
attr PV_Anlage_1_API replacement06Value auth_token
attr PV_Anlage_1_API replacement07Mode reading
attr PV_Anlage_1_API replacement07Regex %auth_signature%
attr PV_Anlage_1_API replacement07Value auth_signature
attr PV_Anlage_1_API replacement08Mode reading
attr PV_Anlage_1_API replacement08Regex %auth_authtag%
attr PV_Anlage_1_API replacement08Value auth_authtag
attr PV_Anlage_1_API replacement09Mode reading
attr PV_Anlage_1_API replacement09Regex %auth_payload%
attr PV_Anlage_1_API replacement09Value auth_payload
attr PV_Anlage_1_API replacement10Mode reading
attr PV_Anlage_1_API replacement10Regex %auth_iv%
attr PV_Anlage_1_API replacement10Value auth_iv
attr PV_Anlage_1_API replacement11Mode reading
attr PV_Anlage_1_API replacement11Regex %auth_sessionId%
attr PV_Anlage_1_API replacement11Value auth_sessionId
attr PV_Anlage_1_API room Strom->Photovoltaik
attr PV_Anlage_1_API showBody 1
attr PV_Anlage_1_API showError 1
attr PV_Anlage_1_API sid01Data {"nonce": "%randomString64%","username": "user"}
attr PV_Anlage_1_API sid01ParseResponse 1
attr PV_Anlage_1_API sid01URL http://%IP-Address_Plenticore%/api/v1/auth/start
attr PV_Anlage_1_API sidHeader01 Accept-Encoding: gzip,deflate
attr PV_Anlage_1_API sidHeader02 Content-type: application/json, Accept: application/json, Connection: keep-alive
attr PV_Anlage_1_API sortby 02
attr PV_Anlage_1_API timeout 5
attr PV_Anlage_1_API userReadings auth_Step1_Message:auth_nonce.* {system("/usr/bin/python3 /opt/fhem/python/bin/plenticore_auth_finish.py ".ReadingsVal("PV_Anlage_1_config","IP-Address_FHEM","")." ".ReadingsVal("$NAME","auth_randomString64","")." ".ReadingsVal("$NAME","auth_nonce","")." ".ReadingsVal("$NAME","auth_salt","")." ".ReadingsVal("$NAME","auth_rounds","")." &");;;; "Prepare auth_finish started with auth_nonce ".ReadingsVal("$NAME","auth_nonce","")},\
\
auth_Step2_Message:auth_proof.* { fhem("get ".$NAME." 02_/auth/finish") ;;;; "HTTP Request 02_/auth/finish with auth_proof ".ReadingsVal("$NAME","auth_proof","")},\
\
auth_Step3_Message:auth_token.* {system("/usr/bin/python3 /opt/fhem/python/bin/plenticore_auth_session.py ".ReadingsVal("PV_Anlage_1_config","IP-Address_FHEM","")." ".ReadingsVal("$NAME","auth_randomString64","")." ".ReadingsVal("$NAME","auth_nonce","")." ".ReadingsVal("$NAME","auth_salt","")." ".ReadingsVal("$NAME","auth_rounds","")." ".ReadingsVal("$NAME","auth_token","")." &");;;; "Prepare auth_session started with auth_token ".ReadingsVal("$NAME","auth_token","")},\
\
auth_Step4_Message:auth_payload.* { fhem("get ".$NAME." 03_/auth/create_session") ;;;; "HTTP Request 03_/auth/create_session with auth_authtag ".ReadingsVal("$NAME","auth_authtag","")},\
\
auth_Step5_Message:auth_sessionId.* { fhem("get ".$NAME." 04_/auth/me") ;;;; "HTTP Request 04_/auth/me with auth_sessionId ".ReadingsVal("$NAME","auth_sessionId","")}
attr PV_Anlage_1_API verbose 0


Readings vom Login aus dem Verbose 5 Log im Anhang

setstate PV_Anlage_1_API 2020-08-31 17:35:32 auth_Step1_Message Prepare auth_finish started with auth_nonce p5NyrqY18ApsAfScCi7/T/EbIdL3kr5a
setstate PV_Anlage_1_API 2020-08-31 17:35:33 auth_Step2_Message HTTP Request 02_/auth/finish with auth_proof XbY/r/bWVcymH/z+DY0SW0U7c45bx3eALRBkZo3O9Hk=
setstate PV_Anlage_1_API 2020-08-31 17:35:33 auth_Step3_Message Prepare auth_session started with auth_token fa62e94ef9be096ba7ef64186464eb18600c77aa4e09f9524c7e6e9bebfd06d7
setstate PV_Anlage_1_API 2020-08-31 17:35:33 auth_Step4_Message HTTP Request 03_/auth/create_session with auth_authtag tUw9M0yqFYE2Fzd0DZieLQ==
setstate PV_Anlage_1_API 2020-08-31 17:35:34 auth_Step5_Message HTTP Request 04_/auth/me with auth_sessionId e4977e08087e6746d3b838b086accd749ba2efcc1f158075b1cd19e8e7008de0
setstate PV_Anlage_1_API 2020-08-31 17:35:33 auth_authtag tUw9M0yqFYE2Fzd0DZieLQ==
setstate PV_Anlage_1_API 2020-08-31 17:35:33 auth_iv aIyU55mGYnpQF8ijDGKahQ==
setstate PV_Anlage_1_API 2020-08-31 17:35:35 auth_me_active 1
setstate PV_Anlage_1_API 2020-08-31 17:35:35 auth_me_anonymous 0
setstate PV_Anlage_1_API 2020-08-31 17:35:35 auth_me_authenticated 1
setstate PV_Anlage_1_API 2020-08-31 17:35:35 auth_me_locked 0
setstate PV_Anlage_1_API 2020-08-31 17:35:35 auth_me_role USER
setstate PV_Anlage_1_API 2020-08-31 17:35:32 auth_nonce p5NyrqY18ApsAfScCi7/T/EbIdL3kr5a
setstate PV_Anlage_1_API 2020-08-31 17:35:33 auth_payload MRvXZiaZrX7LiH9H1IcPsFmJjotoMc07j9/+yyZHhYLvIds3ctU5+jmqx/OEVtYH13xGbRCVJwXwAc9+YUXM4w==
setstate PV_Anlage_1_API 2020-08-31 17:35:33 auth_proof XbY/r/bWVcymH/z+DY0SW0U7c45bx3eALRBkZo3O9Hk=
setstate PV_Anlage_1_API 2020-08-31 17:35:32 auth_randomString64 p5NyrqY18ApsAfSc
setstate PV_Anlage_1_API 2020-08-31 17:35:32 auth_rounds 29000
setstate PV_Anlage_1_API 2020-08-31 17:35:32 auth_salt DbAC0R85jwF0rh+r
setstate PV_Anlage_1_API 2020-08-31 17:35:34 auth_sessionId e4977e08087e6746d3b838b086accd749ba2efcc1f158075b1cd19e8e7008de0
setstate PV_Anlage_1_API 2020-08-31 17:35:33 auth_signature CWm0BWcJg7WEW9Cg7Osya0LL0lM2gw7Koo4H+S7APnM=
setstate PV_Anlage_1_API 2020-08-31 17:35:33 auth_token fa62e94ef9be096ba7ef64186464eb18600c77aa4e09f9524c7e6e9bebfd06d7
setstate PV_Anlage_1_API 2020-08-31 17:35:32 auth_transactionId 63ae48fd4da53c956299393c737b3696ccfeef1d768b4233bb96005e0f6b89da
setstate PV_Anlage_1_API 2020-08-30 16:34:36 info_api_version 0.2.0
setstate PV_Anlage_1_API 2020-08-30 16:34:36 info_hostname scb
setstate PV_Anlage_1_API 2020-08-30 16:34:36 info_name PUCK RESTful API
setstate PV_Anlage_1_API 2020-08-30 16:34:36 info_sw_version 01.15.04581


Ab nun kann man bei Übergabe der auth_sessionId beliebig viele Abfragen durchführen.
Verbose 5 log mit einem initialen Login ist im Anhang.

Meine Hauptfrage ist nun, kann ich das noch vereinfachen?
Habe ich irgend welche Mechanismen in der HTTPMOD Attributen übersehen?

Viele Grüße
     Christian
RPI4; Docker; CUNX; Eltako FSB61NP; SamsungTV H-Serie; Sonos; Vallox; Luxtronik; 3x FB7490; Stromzähler mit DvLIR; wunderground; Plenticore 10 mit BYD; EM410; SMAEM; Modbus TCP
Contrib: https://svn.fhem.de/trac/browser/trunk/fhem/contrib/ch.eick

ch.eick

Und nochmals hallo,

ich habe noch ein weiteres Problem mit der API vom letzten Post.
Es gibt in der undokumentierten Auflistung der Request auch explizit API Aufrufe mit "POST" anstelle von "GET", wie kann ich das denn beeinflussen?

Gruß
    Christian
RPI4; Docker; CUNX; Eltako FSB61NP; SamsungTV H-Serie; Sonos; Vallox; Luxtronik; 3x FB7490; Stromzähler mit DvLIR; wunderground; Plenticore 10 mit BYD; EM410; SMAEM; Modbus TCP
Contrib: https://svn.fhem.de/trac/browser/trunk/fhem/contrib/ch.eick

amenomade

Deine getNN... sind schon alle POST Requests

Zitat von: CommandRef(get|set)[0-9]*Data
optional data to be sent to the device as POST data when the get oer set command is executed. if this attribute is specified, an HTTP POST method will be sent instead of an HTTP GET

set[0-9]*Method
HTTP Method (GET, POST or PUT) which shall be used for the set.

(get|set)[0-9]*NoData
can be used to override a more generic attribute that specifies POST data for all get commands. With NoData no data is sent and therefor the request will be an HTTP GET.
Pi 3B, Alexa, CUL868+Selbstbau 1/2λ-Dipol-Antenne, USB Optolink / Vitotronic, Debmatic und HM / HmIP Komponenten, Rademacher Duofern Jalousien, Fritz!Dect Thermostaten, Proteus

ch.eick

#815
Zitat von: amenomade am 02 September 2020, 00:28:28
Deine getNN... sind schon alle POST Requests
Vielen Dank, diese Attribute waren mir einfach nicht aufgefallen.
Dann muss da noch irgend ein anderer Syntax Fehler sein :-(
Gruß
    Christian
RPI4; Docker; CUNX; Eltako FSB61NP; SamsungTV H-Serie; Sonos; Vallox; Luxtronik; 3x FB7490; Stromzähler mit DvLIR; wunderground; Plenticore 10 mit BYD; EM410; SMAEM; Modbus TCP
Contrib: https://svn.fhem.de/trac/browser/trunk/fhem/contrib/ch.eick

ch.eick

Ich habe nun weiter getestet und rum probiert und meine einen Fehler gefunden zu haben. Es liegt wohl am Header, der beim POST wohl anders zusammen gesetzt wird.

EDIT: Ich vermisse bei allen requests den getHeader02 ???


# Das habe ich für alle requests gesetzt
attr PV_Anlage_1_API getHeader01 Accept-Encoding: gzip,deflate
attr PV_Anlage_1_API getHeader02 Content-type:application/json, Accept:application/json, Connection:keep-alive

# Dieser Header kommt dann bei einigen requests dazu
attr PV_Anlage_1_API get23Header authorization: Session %auth_sessionId%

# Das ist der betroffene request
attr PV_Anlage_1_API get23Data {"max":10,"language":"en-gb"}
attr PV_Anlage_1_API get23Name 23_/events/latest
attr PV_Anlage_1_API get23URL http://%IP-Address_Plenticore%/api/v1/events/latest

# Wenn ich nun den Header "getHeader02" für den betroffenen request explizit setze, dann geht es.
attr PV_Anlage_1_API get23Header2 Content-type:application/json, Accept:application/json, Connection:keep-alive

Hier das Lauffähige Log

2020.09.03 18:42:03.465 4: PV_Anlage_1_API: HandleSendQueue sends get23 with timeout 5 to http://192.168.178.18/api/v1/events/latest,
data: {"max":10,"language":"en-gb"},
header: authorization: Session 42ef55c276797818c0ebebc095a081cfefc01e7ae267c6eed27e8459c4a9170a
Content-type:application/json, Accept:application/json, Connection:keep-alive
2020.09.03 18:42:03.465 5: HttpUtils url=http://192.168.178.18/api/v1/events/latest
2020.09.03 18:42:03.465 4: IP: 192.168.178.18 -> 192.168.178.18
2020.09.03 18:42:03.468 5: HttpUtils request header:
POST /api/v1/events/latest HTTP/1.0
Host: 192.168.178.18
User-Agent: fhem
Accept-Encoding: gzip,deflate
authorization: Session 42ef55c276797818c0ebebc095a081cfefc01e7ae267c6eed27e8459c4a9170a
Content-type:application/json, Accept:application/json, Connection:keep-alive
Content-Length: 29

2020.09.03 18:42:03.671 4: http://192.168.178.18/api/v1/events/latest: HTTP response code 200
2020.09.03 18:42:03.671 5: HttpUtils http://192.168.178.18/api/v1/events/latest: Got data, length: 3367
2020.09.03 18:42:03.672 5: HttpUtils response header:
HTTP/1.1 200 OK
Server: nginx/1.15.2
Date: Thu, 03 Sep 2020 16:42:03 GMT
Content-Type: application/json
Content-Length: 3367
Connection: close
Access-Control-Allow-Origin: *
Cache-Control: max-age=0, no-cache, no-store, must-revalidate
2020.09.03 18:42:03.672 4: PV_Anlage_1_API: Read callback: request type was get23 retry 0,
header: HTTP/1.1 200 OK
Server: nginx/1.15.2
Date: Thu, 03 Sep 2020 16:42:03 GMT
Content-Type: application/json
Content-Length: 3367
Connection: close
Access-Control-Allow-Origin: *
Cache-Control: max-age=0, no-cache, no-store, must-revalidate, body length 3367
2020.09.03 18:42:03.672 5: PV_Anlage_1_API: Read callback: body
[{"end_time":"2020-07-21T11:22:18","description":"Reduction of AC power due to external command or high grid frequency.","category":"info","code":5013,"is_active":false,"group":"Information","start_time":"2020-07-21T11:20:53","long_description":"Reduction of AC power due to external command or high grid frequency."},{"end_time":"2020-07-21T09:35:59","description":"Grid parameters are outside the permitted range. If the fault persists after a few minutes, please contact your installer or call our hotline.","category":"error","code":5096,"is_active":false,"group":"External grid fault","start_time":"2020-07-21T09:34:57","long_description":"Grid parameters are outside the permitted range. If the fault persists after a few minutes, please contact your installer or call our hotline."},{"end_time":"2020-07-20T18:01:23","description":"Reduction of AC power due to external command or high grid frequency.","category":"info","code":5013,"is_active":false,"group":"Information","start_time":"2020-07-20T17:58:57","long_description":"Reduction of AC power due to external command or high grid frequency."},{"end_time":"2020-07-20T18:00:50","description":"Energy meter cannot be read.\nPlease check the connection to the sensor.","category":"error","code":6006,"is_active":false,"group":"System fault","start_time":"2020-07-20T17:58:30","long_description":"Energy meter cannot be read.\nPlease check the connection to the sensor."},{"end_time":"2020-07-20T17:44:57","description":"Energy meter cannot be read.\nPlease check the connection to the sensor.","category":"error","code":6006,"is_active":false,"group":"System fault","start_time":"2020-07-20T17:42:22","long_description":"Energy meter cannot be read.\nPlease check the connection to the sensor."},{"end_time":"2020-07-20T17:28:54","description":"Energy meter cannot be read.\nPlease check the connection to the sensor.","category":"error","code":6006,"is_active":false,"group":"System fault","start_time":"2020-07-20T17:28:13","long_description":"Energy meter cannot be read.\nPlease check the connection to the sensor."},{"end_time":"2020-07-20T16:47:45","description":"Energy meter cannot be read.\nPlease check the connection to the sensor.","category":"error","code":6006,"is_active":false,"group":"System fault","start_time":"2020-07-20T16:47:26","long_description":"Energy meter cannot be read.\nPlease check the connection to the sensor."},{"end_time":"2020-07-20T17:44:54","description":"Reduction of AC power due to external command or high grid frequency.","category":"info","code":5013,"is_active":false,"group":"Information","start_time":"2020-07-20T16:44:38","long_description":"Reduction of AC power due to external command or high grid frequency."},{"end_time":"2020-07-20T17:21:38","description":"Reduction of AC power due to external command or high grid frequency.","category":"info","code":5013,"is_active":false,"group":"Information","start_time":"2020-07-20T16:44:38","long_description":"Reduction of AC power due to external command or high grid frequency."},{"end_time":"2020-07-20T16:44:48","description":"Energy meter cannot be read.\nPlease check the connection to the sensor.","category":"error","code":6006,"is_active":false,"group":"System fault","start_time":"2020-07-20T16:42:08","long_description":"Energy meter cannot be read.\nPlease check the connection to the sensor."}]

2020.09.03 18:42:03.672 4: PV_Anlage_1_API: Read found no charset header (bodyDecode was set to auto)


Und hier die Gegenprobe

2020.09.03 18:50:09.662 4: PV_Anlage_1_API: HandleSendQueue sends get23 with timeout 5 to http://192.168.178.18/api/v1/events/latest,
data: {"max":10,"language":"en-gb"},
header: authorization: Session 42ef55c276797818c0ebebc095a081cfefc01e7ae267c6eed27e8459c4a9170a
2020.09.03 18:50:09.662 5: HttpUtils url=http://192.168.178.18/api/v1/events/latest
2020.09.03 18:50:09.662 4: IP: 192.168.178.18 -> 192.168.178.18
2020.09.03 18:50:09.667 5: HttpUtils request header:
POST /api/v1/events/latest HTTP/1.0
Host: 192.168.178.18
User-Agent: fhem
Accept-Encoding: gzip,deflate
authorization: Session 42ef55c276797818c0ebebc095a081cfefc01e7ae267c6eed27e8459c4a9170a
Content-Length: 29
Content-Type: application/x-www-form-urlencoded

2020.09.03 18:50:09.702 4: http://192.168.178.18/api/v1/events/latest: HTTP response code 400
2020.09.03 18:50:09.702 5: HttpUtils http://192.168.178.18/api/v1/events/latest: Got data, length: 91
2020.09.03 18:50:09.703 5: HttpUtils response header:
HTTP/1.1 400 BAD REQUEST
Server: nginx/1.15.2
Date: Thu, 03 Sep 2020 16:50:09 GMT
Content-Type: application/json
Content-Length: 91
Connection: close
Access-Control-Allow-Origin: *
2020.09.03 18:50:09.703 4: PV_Anlage_1_API: Read callback: request type was get23 retry 0,
header: HTTP/1.1 400 BAD REQUEST
Server: nginx/1.15.2
Date: Thu, 03 Sep 2020 16:50:09 GMT
Content-Type: application/json
Content-Length: 91
Connection: close
Access-Control-Allow-Origin: *, body length 91
2020.09.03 18:50:09.703 5: PV_Anlage_1_API: Read callback: body
{"message":"Input payload validation failed","errors":{"":"None is not of type 'object'"}}

2020.09.03 18:50:09.703 4: PV_Anlage_1_API: Read found no charset header (bodyDecode was set to auto)


Gruß
    Christian
RPI4; Docker; CUNX; Eltako FSB61NP; SamsungTV H-Serie; Sonos; Vallox; Luxtronik; 3x FB7490; Stromzähler mit DvLIR; wunderground; Plenticore 10 mit BYD; EM410; SMAEM; Modbus TCP
Contrib: https://svn.fhem.de/trac/browser/trunk/fhem/contrib/ch.eick

ch.eick

Hmm, habe ich jetzt auch das decoding Problem?

attr PV_Anlage_1_API reading22015Encode ???   <<< utf8 geht nicht :-)

Event_03_description Abregelung der AC-Leistung auf Grund externer Signale oder erh�hter Netzfrequenz.
RPI4; Docker; CUNX; Eltako FSB61NP; SamsungTV H-Serie; Sonos; Vallox; Luxtronik; 3x FB7490; Stromzähler mit DvLIR; wunderground; Plenticore 10 mit BYD; EM410; SMAEM; Modbus TCP
Contrib: https://svn.fhem.de/trac/browser/trunk/fhem/contrib/ch.eick

amenomade

1236         $header = join ("\r\n", map ($attr{$name}{$_}, sort grep (/${context}${num}Header/, keys %{$attr{$name}})));
1237         if (length $header == 0) {
1238             $header = join ("\r\n", map ($attr{$name}{$_}, sort grep (/${context}Header/, keys %{$attr{$name}})));
1239         }


(request|get|set)Header.* werden nur benutzt, wenn keine (request|get|set)<num>Header.* gesetzt.
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

Zitat von: ch.eick am 03 September 2020, 19:25:22
Hmm, habe ich jetzt auch das decoding Problem?

attr PV_Anlage_1_API reading22015Encode ???   <<< utf8 geht nicht :-)

Event_03_description Abregelung der AC-Leistung auf Grund externer Signale oder erh�hter Netzfrequenz.

Anscheinend ja ;) Sollte in der neuen Version (noch nicht eingecheckt aber hier verfügbar) behoben sein. Aber diese Version ist noch nicht fertig getestet
Siehe hier https://forum.fhem.de/index.php/topic,45176.msg1082208.html#msg1082208
Pi 3B, Alexa, CUL868+Selbstbau 1/2λ-Dipol-Antenne, USB Optolink / Vitotronic, Debmatic und HM / HmIP Komponenten, Rademacher Duofern Jalousien, Fritz!Dect Thermostaten, Proteus

ch.eick

Super, vielen Dank für beide Antworten und das mit dem "setMethod POST" habe ich auch noch gebraucht.
RPI4; Docker; CUNX; Eltako FSB61NP; SamsungTV H-Serie; Sonos; Vallox; Luxtronik; 3x FB7490; Stromzähler mit DvLIR; wunderground; Plenticore 10 mit BYD; EM410; SMAEM; Modbus TCP
Contrib: https://svn.fhem.de/trac/browser/trunk/fhem/contrib/ch.eick

ch.eick

Moin,
ich bin schon blind vom Suchen:

Ich lese JSON Rückgabe Werte, möchte diese jedoch auf ein get einschränken, damit nicht immer alle readings gesucht werden.
Andere wiederum sind nicht eineindeutig und können nur durch das get korrekt zugeordnet werden.

get20 holt z.B. Statistiken

attr PV_Anlage_1_API get20Header authorization: Session %auth_sessionId%
attr PV_Anlage_1_API get20Name 20_/processdata/scb_statistic_EnergyFlow
attr PV_Anlage_1_API get20URL http://%IP-Address_Plenticore%/api/v1/processdata/scb:statistic:EnergyFlow


Und solche readings lesen dann die Werte. Im Namen habe ich schon die 20 eingebaut, 01-99 wären dann die Werte

attr PV_Anlage_1_API reading2004JSON 01_processdata_04_value
attr PV_Anlage_1_API reading2004Name Statistic_Autarky_Year


Dann habe ich das hier gefunden, kann ich damit auch JSON Werte einlesen?

formating and manipulating values / readings

Values that are parsed from an HTTP response can be further treated or formatted with the following attributes:
(reading|get)[0-9]*(-[0-9]+)?OExpr
(reading|get)[0-9]*(-[0-9]+)?OMap
(reading|get)[0-9]*(-[0-9]+)?Format
(reading|get)[0-9]*(-[0-9]+)?Decode
(reading|get)[0-9]*(-[0-9]+)?Encode


Ich bekomme es gerade einfach nicht hin, sorry
RPI4; Docker; CUNX; Eltako FSB61NP; SamsungTV H-Serie; Sonos; Vallox; Luxtronik; 3x FB7490; Stromzähler mit DvLIR; wunderground; Plenticore 10 mit BYD; EM410; SMAEM; Modbus TCP
Contrib: https://svn.fhem.de/trac/browser/trunk/fhem/contrib/ch.eick

amenomade

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

ch.eick

Zitat von: amenomade am 04 September 2020, 21:34:58
Nicht lesen, sondern nur nachformattieren.
Okay, also gibt es für das get** kein gezieltes Lesen...
RPI4; Docker; CUNX; Eltako FSB61NP; SamsungTV H-Serie; Sonos; Vallox; Luxtronik; 3x FB7490; Stromzähler mit DvLIR; wunderground; Plenticore 10 mit BYD; EM410; SMAEM; Modbus TCP
Contrib: https://svn.fhem.de/trac/browser/trunk/fhem/contrib/ch.eick

amenomade

Bin mir nicht sicher, was dein Problem ist.

get20JSON, get20Regex und get20XPath sind auf get20 beschränkt. Damit wird nur ein Reading (Name davon ist was in get20Name steht) extrahiert.
Pi 3B, Alexa, CUL868+Selbstbau 1/2λ-Dipol-Antenne, USB Optolink / Vitotronic, Debmatic und HM / HmIP Komponenten, Rademacher Duofern Jalousien, Fritz!Dect Thermostaten, Proteus