from flask import Flask, request
from sqlalchemy import create_engine
from datetime import datetime

app = Flask(__name__)
filename = 'access.log'
engine = create_engine('mysql+pymysql://gaps:Gcbs12ma@192.168.2.41/status')


@app.route("/ping/<kunde>")
def ping(kunde):
    uptime = int(request.args.get('uptime', '0'))
    current_date = datetime.now().timestamp()
    print(kunde + ' hat sich um ' + str(current_date) + ' gemeldet')
    with open(filename, 'a') as f:
        f.write(str(current_date) + ';' + kunde + '\n')
    with engine.connect() as con:
        query = f"INSERT INTO ping (kunde, pingtime, uptime) VALUES ('{kunde}', '{int(current_date)}', '{uptime}')"
        con.execute(query)
    return 'Hallo ' + kunde + '!'
