Add automatic data pulling

This commit is contained in:
Darko Lukic 2018-03-24 19:21:14 +01:00
parent dfe70924c0
commit bf840d5961
2 changed files with 7 additions and 6 deletions

View file

@ -85,6 +85,10 @@ export default {
}, },
beforeCreate() { beforeCreate() {
this.$store.dispatch('requestSeries'); this.$store.dispatch('requestSeries');
setInterval(function() {
this.$store.dispatch('requestSeries');
}.bind(this), 5000);
}, },
} }
</script> </script>

View file

@ -3,7 +3,7 @@ import Vuex from 'vuex';
import moment from 'moment'; import moment from 'moment';
const SERIES_MAX_SIZE = 50; const SERIES_MAX_SIZE = 100;
Vue.use(Vuex); Vue.use(Vuex);
@ -27,8 +27,7 @@ const getters = {
}, },
getLastValue: (state) => (key) => { getLastValue: (state) => (key) => {
if (state.series.length > 0) { if (state.series.length > 0) {
let last = state.series.reduce((l, e) => e.UTCUnixTime > l.UTCUnixTime ? e : l); return state.series[state.series.length - 1][key];
return last[key];
} }
return 'NA'; return 'NA';
}, },
@ -64,14 +63,12 @@ const actions = {
let params = { let params = {
format: 'json', format: 'json',
limit: SERIES_MAX_SIZE, limit: SERIES_MAX_SIZE,
from: state.series.length > 0 ? state.series[state.series.length - 1].UTCUnixTime : 0,
}; };
Vue.http.get('series', { params }).then(response => { Vue.http.get('series', { params }).then(response => {
commit('setSeries', response.body); commit('setSeries', response.body);
}); });
}, },
addRandomValues({ commit }) {
commit('setSeries', [ { 'UTCUnixTime': 123123, 'TemperatureC': 26.0 } ]);
},
requestWifi({ commit }) { requestWifi({ commit }) {
let params = { let params = {
token: state.token, token: state.token,