76 lines
2.9 KiB
Python
76 lines
2.9 KiB
Python
import subprocess
|
|
import json
|
|
import time
|
|
from flask import Flask, request, jsonify, session
|
|
from I2C_Function import analyze_data, read_data
|
|
from flask_cors import CORS
|
|
|
|
app = Flask(__name__)
|
|
CORS(app)
|
|
|
|
initialized = False
|
|
|
|
@app.route('/trng/randomNum/init', methods=['GET'])
|
|
def initialize_generator():
|
|
global initialized # Zugriff auf die globale Variable
|
|
|
|
#parameter 1 und 2 sind default values für Tests
|
|
result = analyze_data(int(1), int(1), startup=True)
|
|
|
|
if result is True:
|
|
initialized = True
|
|
return jsonify({'message': 'successful operation; random number generator is ready and random numbers can be requested'}), 200
|
|
else:
|
|
return jsonify({'error': 'Unable to initialize the random number generator within a timeout of 60 seconds.'}), 555
|
|
|
|
|
|
@app.route('/trng/randomNum/getRandom', methods=['GET'])
|
|
def get_random_numbers():
|
|
global initialized # Zugriff auf die globale Variable
|
|
quantity = request.args.get('quantity')
|
|
bits = request.args.get('numBits')
|
|
|
|
if not quantity.isdigit() or not bits.isdigit():
|
|
return jsonify({'error': 'Invalid input. Quantity and bits must be numeric.'}), 400
|
|
|
|
if int(quantity) <= 0 or int(bits) <= 0:
|
|
return jsonify({'error': 'Invalid input. Quantity and bits must be positive integers.'}), 400
|
|
|
|
# Überprüfe den Initialisierungsstatus
|
|
if not initialized:
|
|
return jsonify({'error': 'system not ready; try init'}), 432
|
|
|
|
random_numbers = analyze_data(int(quantity), int(bits), startup=False)
|
|
|
|
if random_numbers is False:
|
|
return jsonify({'error': 'Unable to generate random numbers.'}), 500
|
|
if random_numbers:
|
|
data = {'randomNumbers': random_numbers} # Erstellen des Datenobjekts mit dem JSON-String
|
|
return jsonify(data), 200
|
|
else:
|
|
return jsonify({'error': 'Unable to generate random numbers.'}), 500
|
|
|
|
@app.route('/trng/randomNum/shutdown', methods=['GET'])
|
|
def shutdown_generator():
|
|
global initialized # Zugriff auf die globale Variable
|
|
|
|
initialized = False # Setze den Initialisierungsstatus zurück
|
|
|
|
# Beispielantwort
|
|
return jsonify({'message': "successful operation; random number generator has been set to 'standby mode'"}), 200
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# Pfade zu den SSL-Zertifikat- und Schlüsseldateien
|
|
cert_path = '/etc/nginx/ssl/cert-gmtrom.pem'
|
|
key_path = '/etc/nginx/ssl/cert-gmtrom-key.pem'
|
|
|
|
#alte Zertifikate self signed
|
|
#cert_path = '/etc/nginx/ssl/server.crt'
|
|
#key_path = '/etc/nginx/ssl/server.key'
|
|
|
|
# Starte die Flask-Anwendung mit SSL-Konfiguration
|
|
app.run(host='0.0.0.0', ssl_context=(cert_path, key_path))
|
|
#app.run(host='172.16.78.57', port=5000, ssl_context=(cert_path, key_path))
|
|
#app.run(host='0.0.0.0', ssl_context=adhoc)
|
|
#app.run(ssl_context=(cert_path, key_path)) |