Sensetif / sensetif-app-plugin

The Grafana Application Plugin for the Sensetif platform.
Apache License 2.0
0 stars 0 forks source link

Tight Integration with the OpenWeather API #44

Open niclash opened 2 years ago

niclash commented 2 years ago

Weather Data is very common for our customers.

Assumptions;

  1. We don't need to support Imperial units. People have to modify that manually. Datapoint is set up to convert Kelvin to Celsius.
  2. We only support "current" weather for now. Maybe predictions later.
  3. We don't need backend support for this API. We simply use the "web"/"http" datasource type.

Overview;

  1. Select Project, Subsystem (create one on the fly is preferable), Poll Interval and provide "appid".
  2. Get the Location from Project.
  3. Show the location of the Project on OpenStreetView map. Allow user to update the project location via the map.
  4. Poll the "current weather" from the OpenWeather API corresponding to that location.
  5. Select which variables are wanted.
  6. Note, not all weather data points are available at all locations. So it needs to be dynamic. But our system knows the scaling, units, min, max values so don't bother the user with those.
  7. NOTE; IIRC, wind is a JSON object, rather than primitive values, so needs to be handled intelligently.
  8. Create "Http(s)" datapoints from user selections, with the names from OpenWeather API's result object.

The "legacy" API is free to use up to a certain amount of requests per day. That is version 2.5 of the API. They have recently introduced version 3.0, so called "One Call", which requires one to put credit card on file. It says that there are 1000 free calls, and then 12 pence per 100 calls up to 2000.

I think we need to support both.

API v2.5 (Documentation; https://openweathermap.org/current);

A v2.5 request returns

{
  "coord": {
    "lon": 13,
    "lat": 57
  },
  "weather": [
    {
      "id": 804,
      "main": "Clouds",
      "description": "overcast clouds",
      "icon": "04d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 289.38,
    "feels_like": 288.65,
    "temp_min": 288.38,
    "temp_max": 289.66,
    "pressure": 998,
    "humidity": 61,
    "sea_level": 998,
    "grnd_level": 981
  },
  "visibility": 10000,
  "wind": {
    "speed": 5.78,
    "deg": 270,
    "gust": 10.71
  },
  "clouds": {
    "all": 100
  },
  "dt": 1663156953,
  "sys": {
    "type": 2,
    "id": 2040401,
    "country": "SE",
    "sunrise": 1663130181,
    "sunset": 1663176673
  },
  "timezone": 7200,
  "id": 2667757,
  "name": "Torup",
  "cod": 200
}

API v3.0 (Documentation; https://openweathermap.org/api/one-call-3); https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&exclude=minutely,hourly,daily,alerts&appid={API key}

A v3 request returns;

{
  "lat": 57,
  "lon": 13,
  "timezone": "Europe/Stockholm",
  "timezone_offset": 7200,
  "current": {
    "dt": 1663156767,
    "sunrise": 1663130181,
    "sunset": 1663176673,
    "temp": 289.38,
    "feels_like": 288.65,
    "pressure": 998,
    "humidity": 61,
    "dew_point": 281.86,
    "uvi": 0.86,
    "clouds": 100,
    "visibility": 10000,
    "wind_speed": 5.78,
    "wind_deg": 270,
    "wind_gust": 10.71,
    "weather": [
      {
        "id": 804,
        "main": "Clouds",
        "description": "overcast clouds",
        "icon": "04d"
      }
    ]
  }
}

NOTE; By not providing units query parameter, temperature is returned in Kelvin rather than degrees Celsius nor degrees Fahrenheit.