import uuid

from flask import Flask, request, jsonify

from util import *

app = Flask(__name__)
# sensor_csv_file = '/root/python_server/data'

sensor_csv_file = '/home/yxy/python_server/data'
random_csv_file = 'random.csv'


# sensor_csv_file = r'D:\pycharmProjects\python_server\data'
# random_csv_file = r'D:\pycharmProjects\python_server\data\random.csv'

# sensor_csv_file = r'D:\pycharmProjects\python\data'
login_csv_path = 'login.csv'

@app.route('/api', methods=['POST'])
def api():
    try:
        # 解析JSON数据

        data = request.get_json()
        if data is None:
            return jsonify({'error': 'Invalid JSON data'}), 400
        l_uuid = ''.join(str(uuid.uuid4()).split('-'))
        # 打印接收到的数据
        # print("uuid:", data['uuid'])
        # print("gesture:", data['gesture'])
        # print("gesture_phone:", data['gesture_phone'])
        # print("input_duration:", data['input_duration'])
        # print("accelerometer_data:", data['accelerometer_data'])
        # print("accelerometer_time:", data['accelerometer_time'])
        # print("gyroscope_data:", data['gyroscope_data'])
        # print("gyroscope_time:", data['gyroscope_time'])
        # print("magnetometer_data:", data['magnetometer_data'])
        # print("magnetometer_time:", data['magnetometer_time'])
        # print("gapList:", data['gapList'])
        # print("offsetList:", data['offsetList'])
        # print("username_len:", data['username_len'])
        # print("pswd_len:", data['pswd_len'])
        # print("data_stamp:", data['data_stamp'])
        # print("pressures:", data['pressures'])
        # print("touch_size:", data['touch_size'])
        save_login_action(l_uuid, data, login_csv_path)
        save_sensor(sensor_csv_file, l_uuid, data)
        # 响应客户端
        response = {
            'status': 'success',
            'received_data': data
        }
        return jsonify(response), 200
    except Exception as e:
        print(f"Error: {e}")
        return jsonify({'error': str(e)}), 500


@app.route('/random', methods=['POST'])
def getRandomData():
    try:
        # 解析JSON数据

        data = request.get_json()
        if data is None:
            return jsonify({'error': 'Invalid JSON data'}), 400
        l_uuid = ''.join(str(uuid.uuid4()).split('-'))
        # 打印接收到的数据
        # print("uuid:", data['uuid'])
        # print("gesture:", data['gesture'])
        # print("gesture_phone:", data['gesture_phone'])
        # print("input_duration:", data['input_duration'])
        # print("accelerometer_data:", data['accelerometer_data'])
        # print("accelerometer_time:", data['accelerometer_time'])
        # print("gyroscope_data:", data['gyroscope_data'])
        # print("gyroscope_time:", data['gyroscope_time'])
        # print("magnetometer_data:", data['magnetometer_data'])
        # print("magnetometer_time:", data['magnetometer_time'])
        # print("gapList:", data['gapList'])
        # print("offsetList:", data['offsetList'])
        # print("username_len:", data['username_len'])
        # print("pswd_len:", data['pswd_len'])
        # print("data_stamp:", data['data_stamp'])
        # print("pressures:", data['pressures'])
        # print("touch_size:", data['touch_size'])
        save_random_action(l_uuid, data, random_csv_file)
        save_sensor(sensor_csv_file, l_uuid, data)
        # 响应客户端
        response = {
            'status': 'success',
            'received_data': data
        }
        return jsonify(response), 200
    except Exception as e:
        print(f"Error: {e}")
        return jsonify({'error': str(e)}), 500

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)
