diff --git a/REST/I2C_Function.py b/REST/I2C_Function.py index 78448bb..b7b27c8 100644 --- a/REST/I2C_Function.py +++ b/REST/I2C_Function.py @@ -1,6 +1,7 @@ import os import time import json +from flask import jsonify from smbus import SMBus from StartUpTest import StartUPTest from TotOnline import TotOnline @@ -135,13 +136,15 @@ def analyze_data(num_numbers, bits_per_number, startup): # Remove any empty strings hex_numbers = [hex_num for hex_num in hex_numbers if hex_num] # Create the JSON object with the specified structure - data = {'randomNumbers': hex_numbers} + #data = {'randomNumbers': hex_numbers} + data = hex_numbers # Delete the temporary files os.unlink(filename) os.unlink(binary_filename) os.unlink(hex_filename) # Return the JSON object using Flask's jsonify function - return jsonify(data) + #return jsonify(data) + return json.dumps(data) else: os.unlink(filename) @@ -150,7 +153,9 @@ def analyze_data(num_numbers, bits_per_number, startup): return False # Teste den Code -#result = analyze_data(50, 2560, startup=False) #result = analyze_data(8, 8, startup=True) #print(result) +#result = analyze_data(1, 7200000, startup=False) +#result = analyze_data(10, 100, startup=False) +#print(result) diff --git a/REST/app.py b/REST/app.py index 26d31dc..a830295 100644 --- a/REST/app.py +++ b/REST/app.py @@ -4,29 +4,22 @@ import time from flask import Flask, request, jsonify, session from I2C_Function import analyze_data, read_data from flask_cors import CORS -from flask_session import Session app = Flask(__name__) -app.config.from_pyfile('config.py') -Session(app) CORS(app) initialized = False @app.route('/trng/randomNum/init', methods=['GET']) def initialize_generator(): - #quantity = 1 - #bits = 1 - #startup = True # Boolean-Wert für die Initialisierung global initialized # Zugriff auf die globale Variable - - #result = analyze_data(int(quantity), int(bits), startup=true) + + #parameter 1 und 2 sind default values für Tests result = analyze_data(int(1), int(1), startup=True) - #session['initialized'] = True if result is True: initialized = True - return jsonify({'error': 'successful operation; random number generator is ready and random numbers can be requested'}), 200 + 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 @@ -45,20 +38,15 @@ def get_random_numbers(): # Überprüfe den Initialisierungsstatus if not initialized: - return jsonify({'error': 'system not ready; try init'}), 432 + return jsonify({'error': 'system not ready; try init'}), 432 - #if 'initialized' not in session or not session['initialized']: - #return jsonify({'error': 'system not ready; try init'}), 432 - - #if 'initialized' not in session or not session['initialized', 'false']: - #return jsonify({'error': 'system not ready; try init'}), 432 - - filename = analyze_data(int(quantity), int(bits), startup=False) - - if filename is False: + 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 filename: - return filename, 200 + 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 @@ -67,20 +55,22 @@ def shutdown_generator(): global initialized # Zugriff auf die globale Variable initialized = False # Setze den Initialisierungsstatus zurück - #session['initialized'] = False # Beispielantwort - return jsonify({'message': 'Generator shutdown successfully.'}), 200 + 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/server.crt' - #key_path = '/etc/nginx/ssl/server.key' + + # 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='172.16.78.57', port=5000, ssl_context=(cert_path, key_path)) 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)) \ No newline at end of file diff --git a/REST/flaskapp.service b/REST/flaskapp.service new file mode 100644 index 0000000..a7395ad --- /dev/null +++ b/REST/flaskapp.service @@ -0,0 +1,14 @@ +[Unit] +Description=Flask App Service +After=network.target + +[Service] +ExecStart=/usr/bin/python /var/www/html/app.py +WorkingDirectory=/var/www/html +StandardOutput=syslog +StandardError=syslog +Restart=always +User=adminadmin + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/REST/index.html b/REST/index.html index b1f3e99..f97e053 100644 --- a/REST/index.html +++ b/REST/index.html @@ -3,7 +3,7 @@