blueweather/js/blueweather.js

183 lines
5.6 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)
this.session = {
loggedIn: false,
token: ""
}
this.restoreSession()
}
// ----------------------
// - session management -
// ----------------------
restoreSession() {
if(this.getCookie("session") !== "") {
// old session found
this.session.loggedIn = true
this.session.token = this.getCookie("session")
}
}
login(async, processingFunction, username, password) {
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("login response was: " + this.responseText, 3)
var ret = JSON.parse(this.responseText)
if(ret && ret['status'] === 200) {
this.session.token = ret["token"]
this.session.loggedIn = true
processingFunction(true)
return
}
else {
this.session.token = ""
this.session.loggedIn = false
}
}
processingFunction(false)
}
}
}
var url = this.host + "/api/json.php"
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"
}
// --------------------
// - getter functions -
// --------------------
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 = this.host + "/api/json.php"
this.log("starting location request; URL is: " + url, 3)
this.xhttp.open("GET", url, async)
this.xhttp.send()
}
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
var url = this.host + "/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()
}
// --------------------
// - 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)
}
}
}