var destroyMeters = [];

function loadMeters() {
	var meters = document.getElementsByTagName('meter');
	for (var i = 0; i < meters.length; i++) {
		var meter = meters[i];
		destroy.push(getMeters(meter, Number.parseInt(meter.getAttribute('phase'))-1, meter.getAttribute('type')));
	}
}

function destroyMeters() {
	for (var i = 0; i < destroyMeters.length; i++) {
		if (destroyMeters[i] == null) continue;
		destroyMeters[i]();
	}
	destroyMeters = [];
}

function getMeters(ctx, phase, type) {
	var updateMeters = function() {
		fetch('https://5groningen02.housing.rug.nl/api/pq/meters')
			.then(function (response) {
				if (response.status != 200) return new Promise((resolve) => { resolve([]); });
				return response.json()
			})
			.then(function (data) {
				if (data == null) return;

				var value = data

				ctx.className = value;
				var text = value.toFixed(1);
				ctx.innerText = text;
			})
	}

	return function()
}