view client/src/store/gauges.js @ 2604:85f9bf4a6eba

client: gauge waterlevel diagram: draw reference waterlevels
author Markus Kottlaender <markus@intevation.de>
date Tue, 12 Mar 2019 17:08:07 +0100
parents 8774054959a7
children 27933e66e848
line wrap: on
line source

/* This is Free Software under GNU Affero General Public License v >= 3.0
 * without warranty, see README.md and license for details.
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 * License-Filename: LICENSES/AGPL-3.0.txt
 *
 * Copyright (C) 2018 by via donau
 *   – Österreichische Wasserstraßen-Gesellschaft mbH
 * Software engineering by Intevation GmbH
 *
 * Author(s):
 * Markus Kottländer <markus@intevation.de>
 */
import { HTTP } from "@/lib/http";
import { WFS } from "ol/format.js";

const init = () => {
  return {
    gauges: [],
    selectedGaugeName: null,
    waterlevels: [],
    loading: false
  };
};

export default {
  init,
  namespaced: true,
  state: init(),
  getters: {
    selectedGauge: state => {
      return state.gauges.find(
        g => g.properties.objname === state.selectedGaugeName
      );
    }
  },
  mutations: {
    gauges: (state, gauges) => {
      state.gauges = gauges;
    },
    selectedGaugeName: (state, name) => {
      state.selectedGaugeName = name;
    },
    waterlevels: (state, data) => {
      state.waterlevels = data;
    },
    loading: (state, loading) => {
      state.loading = loading;
    }
  },
  actions: {
    selectedGaugeName: ({ commit }, name) => {
      commit("selectedGaugeName", name);
      commit("application/showGauges", true, { root: true });
      commit("application/showSplitscreen", false, { root: true });
    },
    loadGauges: ({ commit }) => {
      return new Promise((resolve, reject) => {
        var gaugesFeatureCollectionRequest = new WFS().writeGetFeature({
          srsName: "EPSG:4326",
          featureNS: "gemma",
          featurePrefix: "gemma",
          featureTypes: ["gauges_geoserver"],
          outputFormat: "application/json"
        });
        HTTP.post(
          "/internal/wfs",
          new XMLSerializer().serializeToString(gaugesFeatureCollectionRequest),
          {
            headers: {
              "X-Gemma-Auth": localStorage.getItem("token"),
              "Content-type": "text/xml; charset=UTF-8"
            }
          }
        )
          .then(response => {
            commit("gauges", response.data.features);
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    loadWaterlevels({ state, commit }, timePeriod) {
      return new Promise(resolve => {
        if (state.selectedGaugeName && timePeriod) {
          // generate some demo values
          setTimeout(() => {
            let data = [];
            let waterlevel = 380;
            for (let i = 1; i <= 365; i++) {
              let date = new Date();
              date.setFullYear(2018);
              date.setDate(date.getDate() + i);
              waterlevel *= Math.random() * (1.02 - 0.98) + 0.98;
              data.push({ date, waterlevel });
            }
            commit("waterlevels", data);
            resolve(data);
          }, 2000);
        } else {
          commit("waterlevels", []);
        }
      });
    }
  }
};