Autor Beitrag
Alice
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 120



BeitragVerfasst: Mo 17.04.23 18:52 
hi leute,

ich versuche via Delphi die Tesla API (tesla-api.timdorr.com/) anzusteuern, es will noch nicht so ganz.
vielleicht hat ja hier einer eine zündende idee:

als erster schritt will ich die api dazu nutzen um die stromstärke beim laden zu setzen/ändern:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
function TForm1.SetChargeLimit(const AccessToken: stringconst APIEndpoint: stringconst VehicleID: string; ChargeLimit: Integer): Boolean;
var
  URL: string;
  RequestBody: TJSONObject;
  Response: string;
  ResponseJSON: TJSONObject;
  HTTP: TIdHTTP;
begin
  // Konstruieren Sie die API-Endpunkt-URL
  URL := APIEndpoint + 'api/1/vehicles/' + VehicleID + '/command/set_charge_limit';

  // Erstellen Sie den JSON-Request-Body
  RequestBody := TJSONObject.Create;
  RequestBody.AddPair('charge_limit_soc', TJSONNumber.Create(ChargeLimit));

  // Konfigurieren Sie den HTTP-Request
  HTTP := TIdHTTP.Create(nil);
  HTTP.Request.ContentType := 'application/json';
  HTTP.Request.CustomHeaders.Values['Authorization'] := 'Bearer ' + AccessToken;
  HTTP.Request.Accept := 'application/json';

  try
    // Senden Sie den Request an die API-Endpunkt
    Response := HTTP.Post(URL, RequestBody.ToString);
    // Überprüfen Sie die API-Antwort
    ResponseJSON := TJSONObject.ParseJSONValue(Response) as TJSONObject;
    try
      Result := ResponseJSON.GetValue<TJSONValue>('response').GetValue<TJSONPair>('result').JsonValue.GetValue<Boolean>;
      if Result then
        showmessage('Ladeleistung wurde auf ' + IntToStr(ChargeLimit) + 'A erhöht.')
      else
        showmessage('Fehler beim Erhöhen der Ladeleistung.');
    finally
      ResponseJSON.Free;
    end;
  except
    on E: Exception do
    begin
      showmessage('Fehler: ' + E.Message);
      Result := False;
    end;
  end;

  // Freigabe des JSON-Request-Body und HTTP-Objekts
  RequestBody.Free;
  HTTP.Free;
end;


beim ausführen bringt er mir den fehler:
Zitat:
Fehler Datei C:\Users\Desktop\Neuer Ordner\Win321Debug\{"charge_limit_soc":7} kann nicht geöffnet werden.
Die Syntax für den Dateinamen, Verzeichnisnamen oder die Datenträgerbezeichnung ist falsch.

wo liegt der fehler ?

Moderiert von user profile iconNarses: Topic aus Sonstiges (Delphi) verschoben am Mo 17.04.2023 um 22:39
Moderiert von user profile iconNarses: Delphi-Tags hinzugefügt
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19274
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 17.04.23 22:58 
Schau dir einmal an, was bei einem Post der zweite Parameter macht, wenn er ein String ist. Spoiler: Nein, du übergibst in dem String nicht den Inhalt, der gepostet werden soll... ;-)

Wenn du das möchtest, musst du eine Stringliste oder einen Stringstream verwenden.
Alice Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 120



BeitragVerfasst: Mo 17.04.23 23:49 
user profile iconjaenicke hat folgendes geschrieben Zum zitierten Posting springen:
Schau dir einmal an, was bei einem Post der zweite Parameter macht, wenn er ein String ist. Spoiler: Nein, du übergibst in dem String nicht den Inhalt, der gepostet werden soll... ;-)

Wenn du das möchtest, musst du eine Stringliste oder einen Stringstream verwenden.


hmm, stehe da auf'm schlauch :-(
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19274
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Di 18.04.23 08:31 
user profile iconAlice hat folgendes geschrieben Zum zitierten Posting springen:
hmm, stehe da auf'm schlauch :-(

IndyHttpPostParameter

Naja, was könnte denn ASourceFile bedeuten? Du gibst dort einen Dateinamen an, in dem die Post-Daten stehen. Du musst daher eine Stringliste oder einen Stringstream verwenden, sprich deinen String dort hineinschreiben und dann die Stringliste oder den Stringstream übergeben.
Einloggen, um Attachments anzusehen!
Alice Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 120



BeitragVerfasst: Di 18.04.23 09:26 
also das hier:
ausblenden Delphi-Quelltext
1:
2:
3:
StrList.Clear;
StrList.add(RequestBody.AsString);
Response := HTTP.Post(URL, StrList);

ändert nichts am verhalten, falls du das so meinst.

Moderiert von user profile iconTh69: Delphi-Tags hinzugefügt
Moderiert von user profile iconTh69: Vollzitat entfernt.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19274
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Di 18.04.23 09:43 
user profile iconAlice hat folgendes geschrieben Zum zitierten Posting springen:
ändert nichts am verhalten, falls du das so meinst.
Ja, so mache ich das oft. Da kann dann aber nicht die gleiche Fehlermeldung (mit dem Dateinamen) kommen. Ein anderer Fehler ist natürlich möglich.
Alice Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 120



BeitragVerfasst: Di 18.04.23 11:13 
ja, sorry:

nun ist es der fehler "der wert für iohandler ist ungültig".

Moderiert von user profile iconTh69: Vollzitat entfernt.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19274
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Di 18.04.23 11:30 
Hast du den denn hinzugefügt?

Ein Beispiel und weitere Informationen findest du hier: Zugriff auf https

Moderiert von user profile iconTh69: URL-Titel hinzugefügt
Alice Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 120



BeitragVerfasst: Di 18.04.23 13:05 
@jaenicke, danke dir für die Unterstützung! :D

ok, folg. hinzugefügt:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
IOHandler.SSLOptions.Method := sslvSSLv3 ;
.
.
HTTP := TIdHTTP.Create(nil);
HTTP.IOHandler := IOHandler;

die beiden dll's (indy.fulgan.com/SSL/...0.2u-i386-win32.zip) in den prg. path übernommen.

fehler nun, ich denke du ahnst es schon: socket err 10054.

Moderiert von user profile iconTh69: Delphi-Tags hinzugefügt
Moderiert von user profile iconTh69: Vollzitat entfernt.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19274
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Di 18.04.23 13:25 
Socket Fehler 10054 bedeutet, dass zwar der Socket geöffnet werden konnte, aber auf gesendete Pakete kein ACK kommt, sprich keine Empfangsbestätigung, und auch keine Antwort.

Da ich die API nicht kenne, weiß ich nicht, weshalb das passieren kann. Das Bearer-Token kann vorher erfolgreich abgeholt werden und dabei gibt es auch keine Fehler?
Alice Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 120



BeitragVerfasst: Di 18.04.23 13:34 
gute frage, wie könnte ich das prüfen ?