Koordinaten ermitteln -> http Search

Hallo,
ich hab da mal was "provisorisches" hingebastelt:

Code:
TYPE
    JsonPlace :     STRUCT
        place_id : UDINT;
        licence : STRING[255];
        osm_type : STRING[20];
        osm_id : UDINT;
        lat : REAL;
        lon : REAL;
        class : STRING[20];
        type : STRING[20];
        place_rank : INT;
        importance : REAL;
        addresstype : STRING[20];
        name : STRING[100];
        display_name : STRING[255];
        boundingbox : ARRAY[1..4]OF REAL;
    END_STRUCT;
END_TYPE


Code:
VAR
    Client : httpsClient;
    PlaceData : JsonPlace;
    jsonString : STRING[1024];
    posStart : INT;
    posEnd : INT;
    RequestHeader : httpRequestHeader_t;
    tempStr : STRING[255];
    restStr : STRING[1024];
    Strasse : STRING[128];
    Ort : STRING[128];
END_VAR

Code:
PROGRAM _INIT
   RequestHeader.protocol := 'HTTP/1.1';
   RequestHeader.contentType := 'text/html';
   RequestHeader.contentLength := 0;
   RequestHeader.connection := 'Close';
   RequestHeader.keepAlive := 'timeout=5, max=100';
   RequestHeader.userLine[0].name := 'User-agent';
   RequestHeader.userLine[0].value := 'BuR Client';
   RequestHeader.host := 'localhost';

   Client.method             := httpMETHOD_GET;
   Client.option             := httpOPTION_HTTP_11;
   Client.pHost := ADR('nominatim.openstreetmap.org');
   Client.pUri := ADR('search?q=Schanzenstrasse%2090,8500%20Hamburg&format=json&limit=1');
   Client.pRequestHeader := ADR(RequestHeader);
   Client.hostPort         := 443;
   Client.pResponseData     := ADR(jsonString);
   Client.responseDataSize := SIZEOF(jsonString);
END_PROGRAM

PROGRAM _CYCLIC
    Client(enable := TRUE); (* Call httpClient() *)  
   
    IF Client.status <> 0 THEN
      Client.send := FALSE;
    END_IF

    IF Client.httpStatus = 200 THEN
         // place_id
        posStart := FIND(IN1 := jsonString, IN2 := '"place_id":') + LEN('"place_id":');
        restStr := MID(jsonString, LEN(jsonString), posStart);
        posEnd := FIND(IN1 := restStr, IN2 := ',');
        tempStr := MID(restStr, posEnd - 1, 1);
        PlaceData.place_id := STRING_TO_UDINT(tempStr);
       
        // licence
        posStart := FIND(IN1 := jsonString, IN2 := '"licence":"') + LEN('"licence":"');
        restStr := MID(jsonString, LEN(jsonString), posStart);
        posEnd := FIND(IN1 := restStr, IN2 := '"');
        PlaceData.licence := MID(restStr, posEnd - 1, 1);
       
        // osm_type
        posStart := FIND(IN1 := jsonString, IN2 := '"osm_type":"') + LEN('"osm_type":"');
        restStr := MID(jsonString, LEN(jsonString), posStart);
        posEnd := FIND(IN1 := restStr, IN2 := '"');
        PlaceData.osm_type := MID(restStr, posEnd - 1, 1);
       
        // osm_id
        posStart := FIND(IN1 := jsonString, IN2 := '"osm_id":') + LEN('"osm_id":');
        restStr := MID(jsonString, LEN(jsonString), posStart);
        posEnd := FIND(IN1 := restStr, IN2 := ',');
        tempStr := MID(restStr, posEnd - 1, 1);
        PlaceData.osm_id := STRING_TO_UDINT(tempStr);
       
        // lat
        posStart := FIND(IN1 := jsonString, IN2 := '"lat":"') + LEN('"lat":"');
        restStr := MID(jsonString, LEN(jsonString), posStart);
        posEnd := FIND(IN1 := restStr, IN2 := '"');
        tempStr := MID(restStr, posEnd - 1, 1);
        PlaceData.lat := STRING_TO_REAL(tempStr);
       
        // lon
        posStart := FIND(IN1 := jsonString, IN2 := '"lon":"') + LEN('"lon":"');
        restStr := MID(jsonString, LEN(jsonString), posStart);
        posEnd := FIND(IN1 := restStr, IN2 := '"');
        tempStr := MID(restStr, posEnd - 1, 1);
        PlaceData.lon := STRING_TO_REAL(tempStr);
       
        // class
        posStart := FIND(IN1 := jsonString, IN2 := '"class":"') + LEN('"class":"');
        restStr := MID(jsonString, LEN(jsonString), posStart);
        posEnd := FIND(IN1 := restStr, IN2 := '"');
        PlaceData.class := MID(restStr, posEnd - 1, 1);
       
        // type
        posStart := FIND(IN1 := jsonString, IN2 := '"type":"') + LEN('"type":"');
        restStr := MID(jsonString, LEN(jsonString), posStart);
        posEnd := FIND(IN1 := restStr, IN2 := '"');
        PlaceData.type := MID(restStr, posEnd - 1, 1);
       
        // place_rank
        posStart := FIND(IN1 := jsonString, IN2 := '"place_rank":') + LEN('"place_rank":');
        restStr := MID(jsonString, LEN(jsonString), posStart);
        posEnd := FIND(IN1 := restStr, IN2 := ',');
        tempStr := MID(restStr, posEnd - 1, 1);
        PlaceData.place_rank := STRING_TO_INT(tempStr);
       
        // importance
        posStart := FIND(IN1 := jsonString, IN2 := '"importance":') + LEN('"importance":');
        restStr := MID(jsonString, LEN(jsonString), posStart);
        posEnd := FIND(IN1 := restStr, IN2 := ',');
        tempStr := MID(restStr, posEnd - 1, 1);
        PlaceData.importance := STRING_TO_REAL(tempStr);
       
        // addresstype
        posStart := FIND(IN1 := jsonString, IN2 := '"addresstype":"') + LEN('"addresstype":"');
        restStr := MID(jsonString, LEN(jsonString), posStart);
        posEnd := FIND(IN1 := restStr, IN2 := '"');
        PlaceData.addresstype := MID(restStr, posEnd - 1, 1);
       
        // name
        posStart := FIND(IN1 := jsonString, IN2 := '"name":"') + LEN('"name":"');
        restStr := MID(jsonString, LEN(jsonString), posStart);
        posEnd := FIND(IN1 := restStr, IN2 := '"');
        PlaceData.name := MID(restStr, posEnd - 1, 1);
       
        // display_name
        posStart := FIND(IN1 := jsonString, IN2 := '"display_name":"') + LEN('"display_name":"');
        restStr := MID(jsonString, LEN(jsonString), posStart);
        posEnd := FIND(IN1 := restStr, IN2 := '"');
        PlaceData.display_name := MID(restStr, posEnd - 1, 1);
       
        // boundingbox[1] bis [4]
        posStart := FIND(IN1 := jsonString, IN2 := '"boundingbox":["') + LEN('"boundingbox":["');
        restStr := MID(jsonString, LEN(jsonString), posStart);
       
        // boundingbox[1]
        posEnd := FIND(IN1 := restStr, IN2 := '"');
        tempStr := MID(restStr, posEnd - 1, 1);
        PlaceData.boundingbox[1] := STRING_TO_REAL(tempStr);
       
        // boundingbox[2]
        restStr := MID(restStr, LEN(restStr), posEnd + 2);
        posEnd := FIND(IN1 := restStr, IN2 := '"');
        tempStr := MID(restStr, posEnd - 1, 1);
        PlaceData.boundingbox[2] := STRING_TO_REAL(tempStr);
       
        // boundingbox[3]
        restStr := MID(restStr, LEN(restStr), posEnd + 2);
        posEnd := FIND(IN1 := restStr, IN2 := '"');
        tempStr := MID(restStr, posEnd - 1, 1);
        PlaceData.boundingbox[3] := STRING_TO_REAL(tempStr);
       
        // boundingbox[4]
        restStr := MID(restStr, LEN(restStr), posEnd + 2);
        posEnd := FIND(IN1 := restStr, IN2 := '"');
        tempStr := MID(restStr, posEnd - 1, 1);
        PlaceData.boundingbox[4] := STRING_TO_REAL(tempStr);

    ELSE
        // GGF. FEHLERBEHANDLUNG
    END_IF

END_PROGRAM

PROGRAM _EXIT
    (* Release Client instance *)
    Client(enable := 0, send := 0);
END_PROGRAM

Es fehler natürlich Fehlerbehandlungen, Behandlung von HTML-Sonderzeichen etc. und das Parsen müsste als Funktion gemacht werden. Also nur mal so für die ersten Schritte gut. Ausgeführt wird das, wenn du z.B. im Watchfenster den Parameter "send" auf TRUE setzt. Beachte auch, das ein Polling dieser Webseite immer > 1 Sekunde ist, sonst wirst du eventuell gesperrt :)

Viel Erfolg damit...
 
Zurück
Oben