blueweather/js/blueweather.js

128 lines
3.9 KiB
JavaScript
Executable File

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)
}
getLocations(async, processingFunction, errorFunction) {
this.xhttp.abort()
var thisObject = this
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)
}
}
};
}
var url = "https://weather.itsblue.de/api/json.php"
this.log("starting location request; URL is: " + url, 3)
this.xhttp.open("GET", url, async)
this.xhttp.send()
}
getLocationData(locId, range = {from:"", to:""}, maxVals = 0, async, processingFunction, errorFunction) {
this.xhttp.abort()
var thisObject = this
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)
}
}
};
}
var url = "https://weather.itsblue.de/api/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)
}
}
}