parent
ee8eba4c11
commit
b1b667f4e0
78
REST/app.py
78
REST/app.py
|
@ -1,14 +1,16 @@
|
||||||
import subprocess
|
import subprocess
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
from flask import Flask, request, jsonify, session
|
from flask import Flask, request, jsonify
|
||||||
from I2C_Function import analyze_data, read_data
|
from I2C_Function import analyze_data, read_data
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
|
from threading import Lock
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
CORS(app)
|
CORS(app)
|
||||||
|
|
||||||
initialized = False
|
initialized = False
|
||||||
|
lock = Lock() # Sperre (Lock) für die analyze_data-Methode
|
||||||
|
|
||||||
def create_response(status_code, description):
|
def create_response(status_code, description):
|
||||||
return jsonify({
|
return jsonify({
|
||||||
|
@ -20,49 +22,57 @@ def create_response(status_code, description):
|
||||||
def initialize_generator():
|
def initialize_generator():
|
||||||
global initialized
|
global initialized
|
||||||
|
|
||||||
result = analyze_data(int(1), int(1), startup=True)
|
if initialized:
|
||||||
|
return create_response(200, 'System is already initialised, random numbers can be requested')
|
||||||
|
|
||||||
|
with lock: # Sperre (Lock) verwenden, um sicherzustellen, dass nur ein Thread die Methode ausführt
|
||||||
|
result = analyze_data(int(1), int(1), startup=True)
|
||||||
|
|
||||||
if result is True:
|
if result is True:
|
||||||
initialized = True
|
initialized = True
|
||||||
return create_response(200, 'Successful operation; random number generator is ready and random numbers can be requested')
|
return create_response(200, 'Successful operation; random number generator is ready and random numbers can be requested')
|
||||||
if result == 400:
|
if result == 400:
|
||||||
return create_response(543, 'Tests failed, try again')
|
return create_response(543, 'Tests failed, try again')
|
||||||
if result is False:
|
if result is False:
|
||||||
return create_response(500, 'Unable to generate random numbers. Restart/Reset System')
|
return create_response(500, 'Unable to generate random numbers. Restart/Reset System')
|
||||||
else:
|
else:
|
||||||
return create_response(555, 'Unable to initialize the random number generator within a timeout of 60 seconds')
|
return create_response(555, 'Unable to initialize the random number generator within a timeout of 60 seconds')
|
||||||
|
|
||||||
|
|
||||||
@app.route('/trng/randomNum/getRandom', methods=['GET'])
|
@app.route('/trng/randomNum/getRandom', methods=['GET'])
|
||||||
def get_random_numbers():
|
def get_random_numbers():
|
||||||
global initialized
|
global initialized
|
||||||
quantity = request.args.get('quantity')
|
quantity = request.args.get('quantity', default=1)
|
||||||
bits = request.args.get('numBits')
|
bits = request.args.get('numBits', default=1)
|
||||||
|
|
||||||
if not quantity.isdigit() or not bits.isdigit():
|
try:
|
||||||
|
quantity = int(quantity)
|
||||||
|
bits = int(bits)
|
||||||
|
except ValueError:
|
||||||
return create_response(400, 'Invalid input. Quantity and bits must be numeric')
|
return create_response(400, 'Invalid input. Quantity and bits must be numeric')
|
||||||
|
|
||||||
if int(quantity) <= 0 or int(bits) <= 0:
|
if quantity <= 0 or bits <= 0:
|
||||||
return create_response(400, 'Invalid input. Quantity and bits must be positive integers')
|
return create_response(400, 'Invalid input. Quantity and bits must be positive integers')
|
||||||
|
|
||||||
if not initialized:
|
if not initialized:
|
||||||
return create_response(432, 'System not ready; try init')
|
return create_response(432, 'System not ready; try init')
|
||||||
|
|
||||||
random_numbers = analyze_data(int(quantity), int(bits), startup=False)
|
with lock: # Sperre (Lock) verwenden, um sicherzustellen, dass nur ein Thread die Methode ausführt
|
||||||
|
random_numbers = analyze_data(quantity, bits, startup=False)
|
||||||
|
|
||||||
if random_numbers == 400:
|
if random_numbers == 400:
|
||||||
return create_response(500, 'Tests failed, try again')
|
return create_response(543, 'Tests failed, try again')
|
||||||
if random_numbers is False:
|
if random_numbers is False:
|
||||||
return create_response(500, 'Unable to generate random numbers. Restart/Reset System')
|
return create_response(500, 'Unable to generate random numbers. Restart/Reset System')
|
||||||
if random_numbers:
|
if random_numbers:
|
||||||
data = {
|
data = {
|
||||||
'status': 200,
|
'status': 200,
|
||||||
'description': 'Successful operation; HEX-encoded bit arrays (with leading zeros if required)',
|
'description': 'Successful operation; HEX-encoded bit arrays (with leading zeros if required)',
|
||||||
'randomNumbers': random_numbers
|
'randomNumbers': random_numbers
|
||||||
}
|
}
|
||||||
return jsonify(data)
|
return jsonify(data)
|
||||||
else:
|
else:
|
||||||
return create_response(500, 'Unable to generate random numbers.')
|
return create_response(500, 'Unable to generate random numbers.')
|
||||||
|
|
||||||
@app.route('/trng/randomNum/shutdown', methods=['GET'])
|
@app.route('/trng/randomNum/shutdown', methods=['GET'])
|
||||||
def shutdown_generator():
|
def shutdown_generator():
|
||||||
|
@ -71,6 +81,12 @@ def shutdown_generator():
|
||||||
initialized = False
|
initialized = False
|
||||||
|
|
||||||
return create_response(200, "Successful operation; random number generator has been set to 'standby mode'")
|
return create_response(200, "Successful operation; random number generator has been set to 'standby mode'")
|
||||||
|
|
||||||
|
# Fehlerbehandlungsroutine für nicht übereinstimmende Routen
|
||||||
|
@app.route('/', defaults={'path': ''})
|
||||||
|
@app.route('/<path:path>')
|
||||||
|
def handle_invalid_routes(path):
|
||||||
|
return create_response(404, 'Route not found')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue