Smarter price fetch from GUI

This commit is contained in:
Gunnar Skjold 2023-09-23 20:16:30 +02:00
parent fc01d7fd56
commit 545be710ba
3 changed files with 50 additions and 24 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,5 @@
import { readable, writable } from 'svelte/store';
import { isBusPowered } from './Helpers';
import { isBusPowered, zeropad } from './Helpers';
async function fetchWithTimeout(resource, options = {}) {
const { timeout = 8000 } = options;
@ -57,9 +57,9 @@ export const dataStore = readable(data, (set) => {
lastTemp = data.t;
setTimeout(getTemperatures, 2000);
}
if(lastPrice != data.p && data.pe) {
if(lastPrice == null && data.pe && data.p) {
lastPrice = data.p;
setTimeout(getPrices, 1000);
getPrices();
}
if(sysinfo.upgrading) {
window.location.reload();
@ -104,11 +104,37 @@ export const dataStore = readable(data, (set) => {
});
let prices = {};
let priceShiftTimeout;
export const pricesStore = writable(prices);
export async function shiftPrices() {
let fetchUpdate = false;
pricesStore.update(p => {
for(var i = 0; i < 36; i++) {
if(p[zeropad(i)] == null) {
console.log(i);
fetchUpdate = i < 12;
break;
}
p[zeropad(i)] = p[zeropad(i+1)];
}
return p;
});
if(fetchUpdate) {
getPrices();
}
let date = new Date();
priceShiftTimeout = setTimeout(shiftPrices, ((60-date.getMinutes())*60000))
}
export async function getPrices() {
const response = await fetchWithTimeout("/energyprice.json");
prices = (await response.json())
pricesStore.set(prices);
let date = new Date();
priceShiftTimeout = setTimeout(shiftPrices, ((60-date.getMinutes())*60000))
}
let dayPlot = {};

View File

@ -17,18 +17,18 @@ export default defineConfig({
plugins: [svelte()],
server: {
proxy: {
"/data.json": "http://192.168.233.244",
"/energyprice.json": "http://192.168.233.244",
"/dayplot.json": "http://192.168.233.244",
"/monthplot.json": "http://192.168.233.244",
"/temperature.json": "http://192.168.233.244",
"/sysinfo.json": "http://192.168.233.244",
"/configuration.json": "http://192.168.233.244",
"/tariff.json": "http://192.168.233.244",
"/save": "http://192.168.233.244",
"/reboot": "http://192.168.233.244",
"/configfile": "http://192.168.233.244",
"/upgrade": "http://192.168.233.244"
"/data.json": "http://192.168.233.209",
"/energyprice.json": "http://192.168.233.209",
"/dayplot.json": "http://192.168.233.209",
"/monthplot.json": "http://192.168.233.209",
"/temperature.json": "http://192.168.233.209",
"/sysinfo.json": "http://192.168.233.209",
"/configuration.json": "http://192.168.233.209",
"/tariff.json": "http://192.168.233.209",
"/save": "http://192.168.233.209",
"/reboot": "http://192.168.233.209",
"/configfile": "http://192.168.233.209",
"/upgrade": "http://192.168.233.209"
}
}
})