You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.9 KiB

from flask import Flask
from flask import render_template, send_from_directory
import json, time
app = Flask(__name__)
@app.route('/')
def index():
return '{"onFire": false}'
@app.route('/status')
def somehtml():
history = []
with open('history.json', 'r') as json_data:
history = json.load(json_data)
history = history[-15:]
for event in history:
event['time'] = epochFormat(event['time'])
return render_template('status.html', events = history[::-1])
@app.route('/getcardinfo/<cardID>')
def getCardInfo(cardID):
return send_from_directory('cards', cardID + '.json')
@app.route('/addcard/<cardID>/<name>/<authCode>')
def addCard(cardID, name, authCode):
authCode = int(authCode)
data = {}
data["name"] = name
if authCode == 0:
data["fridge"] = False
data["secretTools"] = False
elif authCode == 1:
data["fridge"] = True
data["secretTools"] = False
elif authCode == 2:
data["fridge"] = False
data["secretTools"] = True
elif authCode == 3:
data["fridge"] = True
data["secretTools"] = True
else:
return '{"status" : "error_unknownAuthCode"}'
with open('cards/' + cardID + '.json', 'w') as outfile:
json.dump(data, outfile)
history = []
with open('history.json', 'r') as history_in:
history = json.load(history_in)
for event in history:
if event["cardID"] == cardID:
event["name"] = name
with open('history.json', 'w') as history_out:
json.dump(history, history_out)
return '{"status": "ok"}'
def epochFormat(timestamp):
timeObject = time.localtime(timestamp)
timeString = str(timeObject.tm_year) + '/' + str(timeObject.tm_mon) + '/' + str(timeObject.tm_mday) + ' - '
if timeObject.tm_hour < 10:
timeString = timeString + '0'
timeString = timeString + str(timeObject.tm_hour) + ":"
if timeObject.tm_min < 10:
timeString = timeString + '0'
timeString = timeString + str(timeObject.tm_min) + ":"
if timeObject.tm_sec < 10:
timeString = timeString + '0'
timeString = timeString + str(timeObject.tm_sec)
return timeString