WEATHER

by whelkaholism

HTML

<div id="fc">

</div>

CSS

#fc > div {display:flex; flex-wrap: wrap}
#fc > div > div {
  display: flex;
  flex-direction: column;
  border: 1px solid #909090;
  margin: 1px;
  padding: 2px;
  align-items: center;
}

JavaScript

function OpenMeteo() {
  var _this = this;
  var _lastdata = null;
  this.endpoint = 'https://api.open-meteo.com/v1/forecast?';
  this.lastLatitude = null;
  this.lastLongitude = null;
  this.lastDatapoints = [];
  this.units = {
    windspeed: 'mph',
  };
  this.settings = {
    forecastDays: 1,
    pressureWarning: -1
  };

  this.url = function(options) {
    var a = [];
    a.push('latitude=' + options.lat);
    a.push('longitude=' + options.lng);
    a.push('current_weather=true');
    var datapoints = options.datapoints || [];
    a.push('hourly=' + datapoints.join(','));
    a.push('windspeed_unit=' + _this.units.windspeed);
    a.push('forecast_days=' + _this.settings.forecastDays);

    return _this.endpoint + a.join('&');
  }

  var _fetchJson = function(url, cb) {
    fetch(url).then(resp => resp.json().then(data => cb(data)));
  };

  var _processData = function(data, options, resolve) {
    var remaining = {
      hourly: []
    };
    var current = data.current_weather;
    var all = {
      hourly: []
    };

    var h = data.hourly;
    h.pressure_msl && (h.pressure_msl_3h_delta = []);
    h.winddirection_10m && (h.winddirection_cardinal_10m = []);

    var now = new Date();
    var adding = false;
    for (var i = 0; i < h.time.length; i++) {
      var dt = new Date(h.time[i]);

      h.pressure_msl && h.pressure_msl_3h_delta.push(_calc3hdelta(h.pressure_msl, i));
      h.winddirection_10m && h.winddirection_cardinal_10m.push(_this.getCardinal(h.winddirection_10m[i]));

      if (dt >= now) {
        if (!adding && i > 0)
          _adddp(remaining.hourly, h, i - 1, options);

        adding = true;

        _adddp(remaining.hourly, h, i, options);
      }

      _adddp(all.hourly, h, i, options);
    }

    all.pressure_msl_warning = !!(h.pressure_msl_3h_delta && h.pressure_msl_3h_delta.find(x => x.warn));
    remaining.pressure_msl_warning = !!(h.pressure_msl_3h_delta && remaining.hourly.find(x => x.pressure_msl_3h_delta.warn));

    resolve({
...