klaassischebrauerei/app.js
2021-03-11 20:48:26 +01:00

248 lines
10 KiB
JavaScript

$(document).ready(function () {
var chart24hLabel = [];
var chart24hData = [];
var chart24hColor = [];
var chart24hColorRoom = [];
var chart24hDataRoom = [];
var chartLabel = [];
var chartData = [];
var chartColor = [];
var chartColorRoom = [];
var chartDataRoom = [];
var arr24h = [];
var arr = [];
var entrylist = [];
function unixToFormatted(unixdate, type) {
var date = new Date((unixdate- 3600) * 1000 );
var months = ['Jan','Feb','Mär','Apr','Mai','Jun','Jul','Aug','Sep','Okt','Nov','Dez'];
var day = date.getDate();
var month = date.getMonth();
if(month=="12"){ month="1"; } else { month=parseInt(month)+parseInt("1");}
var year = date.getFullYear();
var hours = date.getHours();
var minutes = "0" + date.getMinutes();
var formattedTime = hours + ':' + minutes.substr(-2);
//var formattedDate = day + ' ' + month + ' ' + year
var formattedDate = day + "." + month + "." + year;
var formattedDate2 = day + "." + month + ".";
if (type == "time")
return formattedTime;
if (type == "date")
return formattedDate;
if (type == "date2")
return formattedDate2;
}
$.ajax({
type: 'GET',
crossDomain: true,
dataType: 'json',
url: 'https://brauereiapi.truenoobs.de/',
success: function(jsondata){
jQuery.each(jsondata, function() {
entrylist.push(this["time"]);
})
entrylist.sort();
var latestTimestamp = entrylist[entrylist.length-1];
jQuery.each(entrylist, function() {
if(this > latestTimestamp-86400) {
chart24hLabel.push(unixToFormatted(this, "time"));
chart24hColor.push("rgba(223, 63, 63, 1)");
chart24hColorRoom.push("rgba(102, 178, 255, 1)");
chart24hData.push(jsondata[this]["temp"]);
chart24hDataRoom.push(jsondata[this]["temproom"]);
}
chartLabel.push(unixToFormatted(this, "date2"));
chartColor.push("rgba(223, 63, 63, 1)");
chartColorRoom.push("rgba(102, 178, 255, 1)");
chartData.push(jsondata[this]["temp"]);
chartDataRoom.push(jsondata[this]["temproom"]);
});
/*jQuery.each(entrylist, function() {
var date = new Date(this * 1000);
var day = date.getDate();
var month = date.getMonth();
var year = date.getFullYear();
var hours = date.getHours();
var minutes = date.getMinutes();
if(this > latestTimestamp-86400) {
if(!(year in arr24h)){arr24h[year]=[]};
if(!(month in arr24h[year])){arr24h[year][month]=[]};
if(!(day in arr24h[year][month])){arr24h[year][month][day]=[]};
if(!(hours in arr24h[year][month][day])){arr24h[year][month][day][hours]=[]};
arr24h[year][month][day][hours][minutes]=[];
arr24h[year][month][day][hours][minutes].push(jsondata[this]["time"]);
arr24h[year][month][day][hours][minutes].push(unixToFormatted(jsondata[this]["time"], "date"));
arr24h[year][month][day][hours][minutes].push(unixToFormatted(jsondata[this]["time"], "time"));
arr24h[year][month][day][hours][minutes].push(jsondata[this]["temproom"]);
arr24h[year][month][day][hours][minutes].push(jsondata[this]["temp"]);
arr24h[year][month][day][hours][minutes].push(jsondata[this]["bubbles"]);
}
});
for(var i=0;i<arr24h.length;i++){
console.log(arr24h[i]);
}*/
entrylist.reverse();
jQuery.each(entrylist, function() {
$("#table").append(
`<tr>
<td style="display:none">${jsondata[this]["time"]}</td>
<td>${unixToFormatted(jsondata[this]["time"], "date")}</td>
<td>${unixToFormatted(jsondata[this]["time"], "time")}</td>
<td>${jsondata[this]["temproom"]}</td>
<td>${jsondata[this]["temp"]}</td>
<td>${jsondata[this]["bubbles"]}</td>
<!-- <td>0</td> -->
</tr>`
);
});
jQuery.each(entrylist, function() {
if(this > latestTimestamp-86400) {
$("#table24h").append(
`<tr>
<td style="display:none">${jsondata[this]["time"]}</td>
<td>${unixToFormatted(jsondata[this]["time"], "date")}</td>
<td>${unixToFormatted(jsondata[this]["time"], "time")}</td>
<td>${jsondata[this]["temproom"]}</td>
<td>${jsondata[this]["temp"]}</td>
<td>${jsondata[this]["bubbles"]}</td>
<!-- <td>0</td> -->
</tr>`
);
}
});
var ctx = document.getElementById('myChart24h');
var myChart24h = new Chart(ctx, {
type: 'line',
data: {
labels: chart24hLabel,
datasets: [{
label: 'Biertemperatur',
data: chart24hData,
backgroundColor: [
'rgba(223, 63, 63, 0.1)'
],
borderColor: chart24hColor,
borderWidth: 3,
pointRadius: 1
},
{
label: 'Raumtemperatur',
data: chart24hDataRoom,
backgroundColor: [
'rgba(102, 178, 255, 0.1)'
],
borderColor: chart24hColorRoom,
borderWidth: 3,
pointRadius: 1
}]
},
options: {
scales: {
yAxes: [{
stacked: false
}]
}
}
});
var ctx = document.getElementById('myChart');
var myChart = new Chart(ctx, {
type: 'line',
yAxisID: "25",
data: {
labels: chartLabel,
datasets: [{
label: 'Biertemperatur',
data: chartData,
backgroundColor: [
'rgba(223, 63, 63, 0.1)'
],
borderColor: chartColor,
borderWidth: 3,
pointRadius: 1,
},
{
label: 'Raumtemperatur',
data: chartDataRoom,
backgroundColor: [
'rgba(102, 178, 255, 0.1)'
],
borderColor: chartColorRoom,
borderWidth: 3,
pointRadius: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
suggestedMin: 0,
suggestedMax: 30
}
}]
}
}
});
$("#import").click(function(){
var xhr = new XMLHttpRequest();
var url = "https://brauereiapi.truenoobs.de/import";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var json = JSON.parse(xhr.responseText);
}
};
var data = JSON.stringify(jsondata);
xhr.send(data);
});
}
});
$.prototype.kb_table_csv = function() {
var kb_tidy_content = function(text){
text = text.replace(/"/g, '""');
return '"'+text+'"';
};
$(this).each(function(){
var table = $(this);
var caption = $(this).find('caption').text();
var title = [];
var rows = [];
$(this).find('tr').each(function(){
var data = [];
$(this).find('td').each(function(){
var text = kb_tidy_content($(this).text());
if(!text.includes(" ") && !text.includes(":")) {
data.push(text);
}
});
data = data.join(",");
if(data!=""){
rows.push(data);
}
});
title = title.join(",");
rows = rows.join("\n");
var csv = title + rows;
var uri = 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv);
var download_link = document.createElement('a');
download_link.href = uri;
var ts = new Date().getTime();
if(caption==""){
download_link.download = ts+".csv";
} else {
download_link.download = caption+"-"+ts+".csv";
}
document.body.appendChild(download_link);
download_link.click();
document.body.removeChild(download_link);
});
};
$("#export").click(function(){
$("table").kb_table_csv();
});
});