For the sake of the upcoming demonstration, we will be working with the Open Weather Map API located at api.openweathermap.org. This open API provides the ability to issue HTTP GET requests to read weather data for a given area. Here is an example of the HTTP endpoint being accessed by a web browser and the associated payload within the response.
http://api.openweathermap.org/data/2.5/onecall?lat=43.65&lon=70.25&exclude=hourly,daily,minutely&appid={Your app key}
Response:
{
"lat": 43.65,
"lon": -70.25,
"timezone": "America/New_York",
"timezone_offset": -14400,
"current": {
"dt": 1687889314,
"sunrise": 1687856474,
"sunset": 1687911995,
"temp": 294.15,
"feels_like": 294.66,
"pressure": 1009,
"humidity": 90,
"dew_point": 292.45,
"uvi": 1.35,
"clouds": 100,
"visibility": 10000,
"wind_speed": 5.14,
"wind_deg": 160,
"weather": [
{
"id": 804,
"main": "Clouds",
"description": "overcast clouds",
"icon": "04d"
}
]
}
}
Dietro le quinte, analogamente agli altri esempi, una richiesta HTTP GET viene inviata dal browser all'API. Puoi individuare la struttura della richiesta GET utilizzando strumenti di monitoraggio della rete come Wireshark. All'interno di Wireshark è presente una funzione che consente di seguire uno stream TCP specifico o, in altre parole, di visualizzare tutti i messaggi trasmessi all'interno di quel socket TCP. Seguendo lo stream TCP della connessione, la richiesta GET viene delineata come segue:
GET /data/2.5/onecall?lat=43.65&lon=-70.25&exclude=hourly,daily,minutely&appid={my personalized app key} HTTP/1.1
Host: api.openweathermap.org
The HTTP response is as follows:
HTTP/1.1 200 OK
Server: openresty
Date: Tue, 27 Jun 2023 18:21:00 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 393
Connection: keep-alive
X-Cache-Key: /data/2.5/onecall?exclude=hourly,daily,minutely&lat=43.65&lon=-70.25
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST
{"lat":43.65,"lon":-70.25,"timezone":"America/New_York","timezone_offset":-14400,"current":{"dt":1687890040,"sunrise":1687856474,"sunset":1687911995,"temp":294.21,"feels_like":294.72,"pressure":1009,"humidity":90,"dew_point":292.51,"uvi":1.35,"clouds":100,"visibility":10000,"wind_speed":5.14,"wind_deg":160,"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}]}}
The goal within the Kepware custom driver is to rebuild this exact based HTTP request and to parse the associated response.