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"
}
]
}
}
他の例と同様に、バックグラウンドではブラウザからAPIへHTTP GETリクエストが送信されます。Wiresharkなどのネットワーク監視ツールを使用すると、GETリクエストの構造を確認できます。Wiresharkには、特定のTCPストリームを追跡できる機能があります。言い換えれば、そのTCPソケット内で送信されているすべてのメッセージを表示できます。接続のTCPストリームを追跡すると、GETリクエストの概要が示されます:
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.