blueweather/js/blueweather.js

98 lines
2.9 KiB
JavaScript
Raw Normal View History

class BlueWeather {
constructor() {
// cunstruct xmlhttp
this.xhttp = new XMLHttpRequest();
this.host = window.location.href.substring(0, window.location.href.lastIndexOf("/"));
this.log("init done", 2)
}
2019-07-13 20:05:46 +02:00
getLocationData(locId, range = {from:"", to:""}, maxVals = 0, async, processingFunction, errorFunction) {
this.xhttp.abort()
var thisObject = this
2019-07-12 18:08:39 +02:00
if(async) {
this.xhttp.onreadystatechange = function () {
if (this.readyState === 4) {
// Typical action to be performed when the document is ready:
if(this.status === 200) {
thisObject.log("getting sensors response was: " + this.responseText, 3)
var ret = JSON.parse(this.responseText)
if(ret){
processingFunction(ret);
return
}
}
if(errorFunction !== null) {
errorFunction(this.status, this.text)
}
}
};
}
2019-07-12 18:08:39 +02:00
2019-07-13 20:05:46 +02:00
var url = "https://api.itsblue.de/weather/json.php?locId="+locId+"&range[from]="+range.from + "&range[to]="+range.to + "&maxVals=" + maxVals
this.log("starting location request; URL is: " + url, 3)
this.xhttp.open("GET", url, async)
this.xhttp.send()
}
endSession() {
document.cookie = "session=;"
window.location.href = "./index.html"
}
// --------------------
// - helper functions -
// --------------------
getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
findGetParameter(parameterName) {
var result = null,
tmp = [];
location.search
.substr(1)
.split("&")
.forEach(function (item) {
tmp = item.split("=");
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
});
return result;
}
log(text, level){
// levels:
// 0: error
// 1: warning
// 2: info
// 3: debug
var preString = "[BlueWeather]"
switch(level){
case 0:
preString += "[error] "
console.error(preString+text)
break
case 1:
preString += "[warining] "
console.warn(preString+text)
break
case 2:
preString += "[info] "
console.log(preString+text)
break
case 3:
preString += "[debug] "
console.debug(preString+text)
}
}
}