64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
#!/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) |