parent
e536afb33a
commit
33a7e1bd7d
|
@ -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)
|
||||
|
||||
|
|
38
REST/app.py
38
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
|
||||
|
||||
|
@ -47,18 +40,13 @@ def get_random_numbers():
|
|||
if not initialized:
|
||||
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
|
||||
random_numbers = analyze_data(int(quantity), int(bits), startup=False)
|
||||
|
||||
#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:
|
||||
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'
|
||||
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))
|
|
@ -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
|
162
REST/index.html
162
REST/index.html
|
@ -3,7 +3,7 @@
|
|||
<head>
|
||||
<title>GMTROM - True Random Number Generator</title>
|
||||
<link rel="stylesheet" type="text/css" href="style.css">
|
||||
<link rel="icon" type="image/png" href="logomiddle.png">
|
||||
<link rel="icon" type="image/png href="logomiddle.png">
|
||||
</head>
|
||||
<body>
|
||||
<img src="logo.png" alt="Logo" class="logo">
|
||||
|
@ -14,8 +14,8 @@
|
|||
<input type="number" id="quantity" name="quantity"><br><br>
|
||||
<label for="bits">Number of Bits per Random Number:</label>
|
||||
<input type="number" id="bits" name="bits"><br><br>
|
||||
<input type="submit" value="Generate" id="generateButton">
|
||||
<input type="submit" value="Shutdown" id="shutdownButton">
|
||||
<input type="button" value="Generate" id="generateButton">
|
||||
<input type="button" value="Shutdown" id="shutdownButton">
|
||||
</form>
|
||||
|
||||
<div id="status"></div>
|
||||
|
@ -23,60 +23,132 @@
|
|||
|
||||
<script>
|
||||
document.getElementById('initButton').addEventListener('click', function() {
|
||||
fetch('https://172.16.78.57:5000/trng/randomNum/init')
|
||||
.then(response => {
|
||||
document.getElementById('status').innerText = 'Status: ' + response.status;
|
||||
if (response.status === 200) {
|
||||
console.log('Initialisierung erfolgreich');
|
||||
} else {
|
||||
console.log('Fehler beim Initialisieren');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Fehler beim Initialisieren:', error);
|
||||
});
|
||||
});
|
||||
fetch('https://172.16.78.57:5000/trng/randomNum/init')
|
||||
.then(response => {
|
||||
document.getElementById('status').innerText = 'Status: ' + response.status;
|
||||
if (response.status === 200) {
|
||||
document.getElementById('result').innerText = 'Successful operation; random number generator has been set to "standby mode".';
|
||||
console.log('Initialisierung erfolgreich');
|
||||
} else {
|
||||
console.log('Fehler beim Initialisieren');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Fehler beim Initialisieren:', error);
|
||||
});
|
||||
});
|
||||
|
||||
document.getElementById('generateButton').addEventListener('click', function(event) {
|
||||
event.preventDefault(); // Verhindert das Absenden des Formulars
|
||||
var quantity = document.getElementById('quantity').value;
|
||||
var bits = document.getElementById('bits').value;
|
||||
|
||||
var quantity = document.getElementById('quantity').value;
|
||||
var bits = document.getElementById('bits').value;
|
||||
|
||||
var url = 'https://172.16.78.57:5000/trng/randomNum/getRandom';
|
||||
if (quantity && bits) {
|
||||
var url = 'https://172.16.78.57:5000/trng/randomNum/getRandom';
|
||||
if (quantity && bits) {
|
||||
url += '?quantity=' + quantity + '&numBits=' + bits;
|
||||
}
|
||||
}
|
||||
|
||||
fetch(url)
|
||||
fetch(url)
|
||||
.then(response => {
|
||||
document.getElementById('status').innerText = 'Status: ' + response.status;
|
||||
return response.json();
|
||||
document.getElementById('status').innerText = 'Status: ' + response.status;
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
var resultDiv = document.getElementById('result');
|
||||
var hexNumbers = JSON.parse(data.randomNumbers);
|
||||
resultDiv.innerText = hexNumbers.join(', ');
|
||||
var resultDiv = document.getElementById('result');
|
||||
resultDiv.innerHTML = ''; // Clear previous results
|
||||
|
||||
var table = document.createElement('table');
|
||||
table.classList.add('table');
|
||||
var thead = document.createElement('thead');
|
||||
var tbody = document.createElement('tbody');
|
||||
var trHead = document.createElement('tr');
|
||||
var thNr = document.createElement('th');
|
||||
var thCopy = document.createElement('th');
|
||||
var thRandom = document.createElement('th');
|
||||
var copyAllButton = document.createElement('button');
|
||||
|
||||
thNr.textContent = 'ID';
|
||||
thCopy.textContent = ' ';
|
||||
thRandom.textContent = 'Random Number';
|
||||
|
||||
trHead.appendChild(thNr);
|
||||
trHead.appendChild(thCopy);
|
||||
trHead.appendChild(thRandom);
|
||||
thead.appendChild(trHead);
|
||||
table.appendChild(thead);
|
||||
|
||||
var hexNumbers = JSON.parse(data.randomNumbers);
|
||||
for (var i = 0; i < hexNumbers.length; i++) {
|
||||
var tr = document.createElement('tr');
|
||||
var tdNr = document.createElement('td');
|
||||
var tdCopy = document.createElement('td');
|
||||
var tdRandom = document.createElement('td');
|
||||
var copyButton = document.createElement('button');
|
||||
|
||||
tdNr.textContent = (i + 1).toString();
|
||||
tdRandom.textContent = hexNumbers[i];
|
||||
copyButton.textContent = 'Copy';
|
||||
copyButton.classList.add('copy-button');
|
||||
|
||||
copyButton.addEventListener('click', function() {
|
||||
copyToClipboard(this.parentElement.nextElementSibling.textContent);
|
||||
});
|
||||
|
||||
tdCopy.appendChild(copyButton);
|
||||
tr.appendChild(tdNr);
|
||||
tr.appendChild(tdCopy);
|
||||
tr.appendChild(tdRandom);
|
||||
tbody.appendChild(tr);
|
||||
}
|
||||
|
||||
table.appendChild(tbody);
|
||||
resultDiv.appendChild(table);
|
||||
|
||||
copyAllButton.textContent = 'Copy all';
|
||||
copyAllButton.classList.add('copy-button');
|
||||
|
||||
copyAllButton.addEventListener('click', function() {
|
||||
var allHexNumbers = hexNumbers.join(', ');
|
||||
copyToClipboard(allHexNumbers);
|
||||
});
|
||||
|
||||
resultDiv.insertBefore(copyAllButton, table);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Fehler beim Abrufen der Daten:', error);
|
||||
});
|
||||
console.error('Fehler beim Abrufen der Daten:', error);
|
||||
});
|
||||
});
|
||||
|
||||
document.getElementById('shutdownButton').addEventListener('click', function() {
|
||||
fetch('https://172.16.78.57:5000/trng/randomNum/shutdown')
|
||||
.then(response => {
|
||||
document.getElementById('status').innerText = 'Status: ' + response.status;
|
||||
if (response.status === 200) {
|
||||
console.log('Shutdown erfolgreich');
|
||||
} else {
|
||||
console.log('Fehler beim Shutdown');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Fehler beim Shutdown:', error);
|
||||
});
|
||||
});
|
||||
function copyToClipboard(text) {
|
||||
var textArea = document.createElement('textarea');
|
||||
textArea.value = text;
|
||||
document.body.appendChild(textArea);
|
||||
textArea.select();
|
||||
document.execCommand('copy');
|
||||
document.body.removeChild(textArea);
|
||||
|
||||
// Anzeigen eines Popups zur Bestätigung der Kopie
|
||||
window.alert('Der Text wurde in die Zwischenablage kopiert!');
|
||||
}
|
||||
|
||||
document.getElementById('shutdownButton').addEventListener('click', function() {
|
||||
fetch('https://172.16.78.57:5000/trng/randomNum/shutdown')
|
||||
.then(response => {
|
||||
document.getElementById('status').innerText = 'Status: ' + response.status;
|
||||
if (response.status === 200) {
|
||||
document.getElementById('result').innerText = 'Successful operation; random number generator has been shut down.';
|
||||
console.log('Shutdown erfolgreich');
|
||||
//document.getElementById('quantity').value = ''; // Textfeld "Quantity" zurücksetzen
|
||||
//document.getElementById('bits').value = ''; // Textfeld "Bits" zurücksetzen
|
||||
//document.getElementById('status').innerText = ''; // Element "status" zurücksetzen
|
||||
//document.getElementById('result').innerText = ''; // Element "result" zurücksetzen
|
||||
} else {
|
||||
console.log('Fehler beim Shutdown');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Fehler beim Shutdown:', error);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,15 @@
|
|||
[Unit]
|
||||
Description=Nginx HTTP server
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
ExecStart=/usr/sbin/nginx -g "daemon off;"
|
||||
ExecReload=/usr/sbin/nginx -s reload
|
||||
ExecStop=/usr/sbin/nginx -s stop
|
||||
Restart=on-failure
|
||||
KillMode=process
|
||||
PrivateTmp=true
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
Loading…
Reference in New Issue