0

I am trying to average data from multiple mqtt topics and am getting the error in the title and i'm pretty sure the error is coming from calcavgGPSdata, but do not know why the error is being thrown. carpath[] also ends up being empty.

    const store = new Vuex.Store({
  state: {
    dashChartVal: {}, // temporary cache for chart data
    triggerCharts: 0, // updated to trigger real time updates while on dashboard
    prevPayload: [], // stores the last valid payload recieved, used for mapping
    triggerMap: 0,
    // array used to select data types and charts
    selected: [
      {
       //values
      },

    ],

    carPath: [],
    sensorLatData: [],
    sensorLongData: [],
    sumLat: 0,
    sumLong: 0,
    avgLat: 0,
    avgLong: 0,
  },
  mutations: {
        increaseMapTrigger: function (state) {
      state.triggerMap++;
    },
    pushValue: function (state, data) {
      if (!state.dashChartVal[data.name]) {
        state.dashChartVal[data.name] = [];
      }

      state.dashChartVal[data.name].push({
        name: data.name,
        value: data.value,
      });
    },
    shiftPoints: function (state, dataType) {
      store.state.dashChartVal[dataType].shift();
    },
    storeSelected: function (state, data) {
      state.selected = data;
    },
    calcAvgGPSData: function (data) {

      //latitude data
      store.state.sumLat = store.state.sensorLatData.reduce((a, b) => a + b, 0);
      store.state.avgLat = store.state.sumLat / (store.state.sensorLatData.length - 1);     

      //longitude data
      store.state.sumLong = store.state.sensorLongData.reduce((a, b) => a + b, 0);
      store.state.avgLong = store.state.sumLong / (store.state.sensorLongData.length - 1);
      console.log(
        "Avg LAT:",
        store.state.avgLat.toFixed(8),
        ", Avg LNG",
        store.state.avgLong.toFixed(8)
      );
      // update array for latitude and longitude
            this.$store.commit("addPointPath", {
              pmThresh: this.$store.state.prevPayload
                ? this.$store.state.prevPayload
                : 0,
              payload: data,
            });
      
  

    },
    addPointPath: function (state, data) {
      state.carPath.push({
        pmThresh: data.pmThresh,
        coord: [
          this.$store.state.avgLat,
          this.$store.state.avgLong,
        ],
      });
    },
  },

and here is the code for gathering data from the mqtt topics and processing it

  mqtt: {
    "xxxx"(payload) {
      if (payload != null) {
        try {
          if (JSON.parse(payload.toString())) {
            payload = JSON.parse(payload.toString());
          }
        } catch (error) {
          // handle NaN errors
          payload = JSON.parse(payload.toString().replace(/NaN/g, '"NaN"'));
        }

        //negative values
        }
      }
    },

    "xxxx"(payload) {
      if (payload != null) {
        try {
          if (JSON.parse(payload.toString())) {
            payload = JSON.parse(payload.toString());

            
            this.$store.state.sensorLatData.push(
                            payload.latitudeCoordinate
            );
            this.$store.state.sensorLongData.push(
                            payload.longCoordinate
            );
            console.log(
              "LAT gga1:",
              payload.latitudeCoordinate.toFixed(8),
              ", LNG gga1:",
              payload.longitudeCoordinate.toFixed(8)
            );
            this.$store.commit("increaseMapTrigger");
          }
        } catch (error) {
          alert(error, "=>", payload.toString());
          // handle NaN errors
          payload = JSON.parse(payload.toString().replace(/NaN/g, '"NaN"'));
        }
      }
    },

    "xxxx"(payload) {
      if (payload != null) {
        try {
          if (JSON.parse(payload.toString())) {
            payload = JSON.parse(payload.toString());

            this.$store.state.sensorLatData.push(
                            payload.latitudeCoordinate
            );
            this.$store.state.sensorLongData.push(
                            payload.longCoordinate
            );
              
            console.log(
              "LAT gga2:",
              payload.latitudeCoordinate.toFixed(8),
              ", LNG gga2:",
              payload.longitudeCoordinate.toFixed(8)
            );
            this.$store.commit("increaseMapTrigger");
          }
        } catch (error) {
          alert(error, "=>", payload.toString());
          // handle NaN errors
          payload = JSON.parse(payload.toString().replace(/NaN/g, '"NaN"'));
        }
      }
    },

    "xxxx"(payload) {
      if (payload != null) {
        try {
          if (JSON.parse(payload.toString())) {
            payload = JSON.parse(payload.toString());

            this.$store.state.sensorLatData.push(
                            payload.latitudeCoordinate
            );
            this.$store.state.sensorLongData.push(
                            payload.longCoordinate
            );        
            console.log(
              "LAT gga3:",
              payload.latitudeCoordinate.toFixed(8),
              ", LNG gga3:",
              payload.longitudeCoordinate.toFixed(8)
            );
            this.$store.commit("increaseMapTrigger");
          }
        } catch (error) {
          alert(error, "=>", payload.toString());
          // handle NaN errors
          payload = JSON.parse(payload.toString().replace(/NaN/g, '"NaN"'));
        }
      }
      this.$store.commit("calcAvgGPSData");
    },   
    
  },
cyz2km01
  • 3
  • 2
  • to call one mutation from another use `this.commit("addPointPath", payload)`, remove `$store`. you'll not have `$store` within mutations. – Ravikumar Apr 18 '21 at 06:28
  • Check [this post](https://stackoverflow.com/q/40487627/6691953) for more information – Uchendu Apr 18 '21 at 06:49

0 Answers0