From 8ed13863c39a7b6cdac7f5164ad67d2a0c03e1d4 Mon Sep 17 00:00:00 2001 From: darthsandmann Date: Thu, 11 Mar 2021 20:48:26 +0100 Subject: [PATCH] First Commit --- app.js | 248 ++++++++++++++++++++++++++++++++++++++++++++++++++ database.csv | 26 ++++++ database.json | 17 ++++ index.html | 84 +++++++++++++++++ run.py | 64 +++++++++++++ webfile.txt | 26 ++++++ 6 files changed, 465 insertions(+) create mode 100644 app.js create mode 100644 database.csv create mode 100644 database.json create mode 100644 index.html create mode 100644 run.py create mode 100644 webfile.txt diff --git a/app.js b/app.js new file mode 100644 index 0000000..0be17e5 --- /dev/null +++ b/app.js @@ -0,0 +1,248 @@ +$(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 + ${jsondata[this]["time"]} + ${unixToFormatted(jsondata[this]["time"], "date")} + ${unixToFormatted(jsondata[this]["time"], "time")} + ${jsondata[this]["temproom"]} + ${jsondata[this]["temp"]} + ${jsondata[this]["bubbles"]} + + ` + ); + }); + jQuery.each(entrylist, function() { + if(this > latestTimestamp-86400) { + $("#table24h").append( + ` + ${jsondata[this]["time"]} + ${unixToFormatted(jsondata[this]["time"], "date")} + ${unixToFormatted(jsondata[this]["time"], "time")} + ${jsondata[this]["temproom"]} + ${jsondata[this]["temp"]} + ${jsondata[this]["bubbles"]} + + ` + ); + } + }); + 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(); + }); +}); \ No newline at end of file diff --git a/database.csv b/database.csv new file mode 100644 index 0000000..a7565e2 --- /dev/null +++ b/database.csv @@ -0,0 +1,26 @@ +1615301362,38.00,88 +1615301382,38.00,88 +1615301403,38.00,88 +1615301423,38.00,88 +1615301443,38.00,88 +1615301463,38.00,88 +1615301483,38.00,88 +1615301503,38.00,88 +1615301524,38.00,88 +1615301544,38.00,88 +1615301564,38.00,88 +1615301584,38.00,88 +1615301604,38.00,88 +1615301624,38.00,88 +1615301645,38.00,88 +1615301665,38.00,88 +1615301685,38.00,88 +1615301705,38.00,88 +1615301725,38.00,88 +1615301745,38.00,88 +1615301766,38.00,88 +1615301786,38.00,88 +1615301806,38.00,88 +1615301826,38.00,88 +1615301846,38.00,88 +1615301867,38.00,88 \ No newline at end of file diff --git a/database.json b/database.json new file mode 100644 index 0000000..73b850a --- /dev/null +++ b/database.json @@ -0,0 +1,17 @@ +2021/03/09 14:37:20 38.00 88 +2021/03/09 14:37:40 39.00 78 +2021/03/09 14:38:01 40.00 90 +2021/03/09 14:38:21 35.00 88 +2021/03/09 14:38:41 36.00 86 +2021/03/09 14:39:04 37.00 84 +2021/03/09 14:39:24 38.00 85 +2021/03/09 14:39:44 31.50 86 +2021/03/09 14:40:04 32.00 87 +2021/03/09 14:40:25 33.50 88 +2021/03/09 14:40:45 34.70 88 +2021/03/09 14:41:05 35.00 90 +2021/03/09 14:41:25 37.00 91 +2021/03/09 14:41:45 41.00 92 +2021/03/09 14:42:06 43.00 90 +2021/03/09 14:42:26 45.00 70 +2021/03/09 14:42:46 47.00 75 \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..6460d45 --- /dev/null +++ b/index.html @@ -0,0 +1,84 @@ + + + + + + + Brauvorgang + + + + + + + + + + +
+
+
+
+

Die letzten 24 Stunden

+
+
+ +
+
+
+
+

Seit Braubeginn

+
+
+ +
+
+
+
+
+
+

Die letzten 24 Stunden

+
+
+ + + + + + + + + + + + +
TimestampDatumUhrzeit°C Raum°C BierBlasen
+
+
+
+
+

Seit Braubeginn

+
+
+ + + + + + + + + + + +
TimestampDatumUhrzeit°C Raum°C BierBlasen
+
+
+
+
+ +
+
+
+
+ \ No newline at end of file diff --git a/run.py b/run.py new file mode 100644 index 0000000..bd67cd1 --- /dev/null +++ b/run.py @@ -0,0 +1,64 @@ +#!/usr/bin/python3 + +from flask import Flask, json, request +import csv +from collections import OrderedDict + +api = Flask(__name__) + +database = {} +ordered_dict = {} + +def open_csv(): + result = "" + with open('database.csv') as csv_file: + csv_reader = csv.reader(csv_file, delimiter=',') + line_count = 0 + for row in csv_reader: + id = row[0] + database[id] = { "time":row[0], "temproom":row[1], "temp":row[2],"bubbles":row[3]} + #print("Time: " + row[0] + " | Raumtemperatur: " + row[1]+ " | Biertemperatur: " + row[2]+ " | Blasen: " + row[3]) + sort_dict() + +def sort_dict(): + newlist = sorted(database, reverse=True) + #print(newlist) + for entry in newlist: + ordered_dict[entry] = {} + ordered_dict[entry] = database[entry] + #print(ordered_dict) + +def write_dict(): + f = open('database.csv', 'w') + with f: + fnames = ['time', 'temproom', 'temp', 'bubbles'] + writer = csv.DictWriter(f, fieldnames=fnames) + #writer.writeheader() + for entry in ordered_dict: + writer.writerow({"time":ordered_dict[entry]["time"], "temproom":ordered_dict[entry]["temproom"], "temp":ordered_dict[entry]["temp"], "bubbles":ordered_dict[entry]["bubbles"]}) + +@api.route('/', methods=['GET']) +def get_values(): + #print (ordered_dict) + open_csv() + return (ordered_dict) + + +@api.route('/b6dg4V', methods=['POST']) +def post_values(): + s = str(request.data) + s = s.replace("b'", "") + s = s.replace("'", "") + split = s.split(" ") + print("Zeit: " + split[0] ) + print("Raumtemp: " + split[1] ) + print("Temp: " + split[2] ) + print("Blasen: " + split[3] ) + database[split[0]] = { "time":split[0], "temproom":split[1], "temp":split[2], "bubbles":split[3] } + sort_dict() + write_dict() + return json.dumps({"success": True}), 201 + +if __name__ == '__main__': + open_csv() + api.run(host='0.0.0.0', port=5632) \ No newline at end of file diff --git a/webfile.txt b/webfile.txt new file mode 100644 index 0000000..a7565e2 --- /dev/null +++ b/webfile.txt @@ -0,0 +1,26 @@ +1615301362,38.00,88 +1615301382,38.00,88 +1615301403,38.00,88 +1615301423,38.00,88 +1615301443,38.00,88 +1615301463,38.00,88 +1615301483,38.00,88 +1615301503,38.00,88 +1615301524,38.00,88 +1615301544,38.00,88 +1615301564,38.00,88 +1615301584,38.00,88 +1615301604,38.00,88 +1615301624,38.00,88 +1615301645,38.00,88 +1615301665,38.00,88 +1615301685,38.00,88 +1615301705,38.00,88 +1615301725,38.00,88 +1615301745,38.00,88 +1615301766,38.00,88 +1615301786,38.00,88 +1615301806,38.00,88 +1615301826,38.00,88 +1615301846,38.00,88 +1615301867,38.00,88 \ No newline at end of file