Update 'REST/I2C_Function.py'

main
Orell-Pieter Schwarzbach 2023-06-09 15:30:11 +02:00
parent 33a7e1bd7d
commit 9cf70dc3a5
1 changed files with 169 additions and 161 deletions

View File

@ -1,161 +1,169 @@
import os import os
import time import time
import json import json
from flask import jsonify from flask import jsonify
from smbus import SMBus from smbus import SMBus
from StartUpTest import StartUPTest from StartUpTest import StartUPTest
from TotOnline import TotOnline from TotOnline import TotOnline
def read_data(num_numbers, bits_per_number): def read_data(num_numbers, bits_per_number):
addr = 0x8 addr = 0x8
bus = SMBus(1) bus = SMBus(1)
# Calculate the total number of bytes required to accommodate the desired number of bits # Calculate the total number of bytes required to accommodate the desired number of bits
total_bytes = (num_numbers * bits_per_number + 7) // 8 total_bytes = (num_numbers * bits_per_number + 7) // 8
# Define the filename # Define the filename
filename = f'{num_numbers}numbers_{bits_per_number}bits.bin' filename = f'{num_numbers}numbers_{bits_per_number}bits.bin'
with open(filename, 'wb') as file: try:
bytes_received = 0 with open(filename, 'wb') as file:
while bytes_received < total_bytes: bytes_received = 0
# Read a byte from the serial port while bytes_received < total_bytes:
data = bus.read_byte(addr) # Read a byte from the serial port
file.write(bytes([data])) data = bus.read_byte(addr)
bytes_received += 1 file.write(bytes([data]))
time.sleep(0.0000001) bytes_received += 1
time.sleep(0.0000001)
return filename
return filename
def convert_to_hex(binary_filename, num_numbers, bits_per_number):
with open(binary_filename, 'r') as f: except IOError as e:
# Read the contents of the file return False
binary_content = f.read()
def convert_to_hex(binary_filename, num_numbers, bits_per_number):
# Calculate the number of bits per chunk with open(binary_filename, 'r') as f:
bits_per_chunk = len(binary_content) // num_numbers # Read the contents of the file
binary_content = f.read()
# Divide the binary content into equal chunks
binary_chunks = [binary_content[i * bits_per_chunk:(i + 1) * bits_per_chunk] for i in range(num_numbers)] # Calculate the number of bits per chunk
bits_per_chunk = len(binary_content) // num_numbers
# Calculate the number of hexadecimal digits per chunk
hex_digits_per_chunk = (bits_per_chunk + 3) // 4 # Round up to the nearest integer # Divide the binary content into equal chunks
binary_chunks = [binary_content[i * bits_per_chunk:(i + 1) * bits_per_chunk] for i in range(num_numbers)]
# Convert each binary chunk to a hexadecimal string with leading zeros
hex_chunks = [format(int(chunk, 2), f'0{hex_digits_per_chunk}X') for chunk in binary_chunks] # Calculate the number of hexadecimal digits per chunk
hex_digits_per_chunk = (bits_per_chunk + 3) // 4 # Round up to the nearest integer
# Write the hex chunks to the file with separators and newlines
hex_filename = f'{num_numbers}numbers_{bits_per_number}bits.txt' # Convert each binary chunk to a hexadecimal string with leading zeros
with open(hex_filename, 'w') as f: hex_chunks = [format(int(chunk, 2), f'0{hex_digits_per_chunk}X') for chunk in binary_chunks]
for i in range(num_numbers):
hex_number = hex_chunks[i] # Write the hex chunks to the file with separators and newlines
# write the separator between numbers except for the last one hex_filename = f'{num_numbers}numbers_{bits_per_number}bits.txt'
if i != num_numbers - 1: with open(hex_filename, 'w') as f:
hex_number += ';' for i in range(num_numbers):
f.write(hex_number) hex_number = hex_chunks[i]
# write the separator between numbers except for the last one
return hex_filename if i != num_numbers - 1:
hex_number += ';'
f.write(hex_number)
def convert_to_binary(filename, num_numbers, bits_per_number):
with open(filename, 'rb') as f: return hex_filename
# Read the contents of the file as bytes
content = f.read()
def convert_to_binary(filename, num_numbers, bits_per_number):
# Convert the bytes to a string of binary digits with open(filename, 'rb') as f:
binary_str = ''.join(format(byte, '08b') for byte in content) # Read the contents of the file as bytes
content = f.read()
# Truncate the binary string to the desired length
truncated_binary_str = binary_str[:num_numbers * bits_per_number] # Convert the bytes to a string of binary digits
binary_str = ''.join(format(byte, '08b') for byte in content)
# Write the truncated binary string back to the file with separators
binary_filename = f'{num_numbers}numbers_{bits_per_number}bits_binary.txt' # Truncate the binary string to the desired length
with open(binary_filename, 'w') as f: truncated_binary_str = binary_str[:num_numbers * bits_per_number]
for i in range(0, len(truncated_binary_str), bits_per_number):
binary_number = truncated_binary_str[i:i+bits_per_number] # Write the truncated binary string back to the file with separators
f.write(binary_number) binary_filename = f'{num_numbers}numbers_{bits_per_number}bits_binary.txt'
with open(binary_filename, 'w') as f:
return binary_filename for i in range(0, len(truncated_binary_str), bits_per_number):
binary_number = truncated_binary_str[i:i+bits_per_number]
f.write(binary_number)
def process_data_in_chunks(data, chunk_size=1000000):
chunks = [data[i:i+chunk_size] for i in range(0, len(data), chunk_size)] return binary_filename
return chunks
def perform_startup_tests(filename): def process_data_in_chunks(data, chunk_size=1000000):
with open(filename, 'rb') as f: chunks = [data[i:i+chunk_size] for i in range(0, len(data), chunk_size)]
# Read the contents of the file as bytes return chunks
content = f.read()
def perform_startup_tests(filename):
#print("Run all StartUPTests") with open(filename, 'rb') as f:
binary_chunks = process_data_in_chunks(content) # Read the contents of the file as bytes
first_chunk = binary_chunks[0] content = f.read()
result = StartUPTest.run_all_tests(first_chunk)
#print(result) #print("Run all StartUPTests")
return result binary_chunks = process_data_in_chunks(content)
first_chunk = binary_chunks[0]
def perform_tot_online_tests(filename): result = StartUPTest.run_all_tests(first_chunk)
with open(filename, 'rb') as f: #print(result)
# Read the contents of the file as bytes return result
content = f.read()
def perform_tot_online_tests(filename):
#print("Run all TotOnline tests") with open(filename, 'rb') as f:
binary_chunks = process_data_in_chunks(content) # Read the contents of the file as bytes
first_chunk = binary_chunks[0] content = f.read()
result = TotOnline.run_all_tests(first_chunk)
#print(result) #print("Run all TotOnline tests")
return result binary_chunks = process_data_in_chunks(content)
first_chunk = binary_chunks[0]
def analyze_data(num_numbers, bits_per_number, startup): result = TotOnline.run_all_tests(first_chunk)
#print(result)
if startup: return result
filename = read_data(10, 80000)
binary_filename = convert_to_binary(filename, 10, 80000) def analyze_data(num_numbers, bits_per_number, startup):
perform_startup_tests(binary_filename)
perform_tot_online_tests(binary_filename) if startup:
os.unlink(filename) filename = read_data(10, 80000)
os.unlink(binary_filename) if not filename:
return True return False
else: binary_filename = convert_to_binary(filename, 10, 80000)
filename = read_data(10, 20000) perform_startup_tests(binary_filename)
binary_filename = convert_to_binary(filename, 10, 20000) perform_tot_online_tests(binary_filename)
result = perform_tot_online_tests(binary_filename) os.unlink(filename)
os.unlink(filename) os.unlink(binary_filename)
os.unlink(binary_filename) return True
if result: else:
filename = read_data(num_numbers, bits_per_number) filename = read_data(10, 20000)
binary_filename = convert_to_binary(filename, num_numbers, bits_per_number) if not filename:
hex_filename = convert_to_hex(binary_filename, num_numbers, bits_per_number) return False
# Read the hex numbers from the file binary_filename = convert_to_binary(filename, 10, 20000)
with open(hex_filename, 'rb') as f: result = perform_tot_online_tests(binary_filename)
hex_numbers_bytes = f.read() os.unlink(filename)
# Decode the bytes to a string os.unlink(binary_filename)
hex_numbers_str = hex_numbers_bytes.decode('utf-8') if result:
# Split the semicolon-separated string into a list of hex numbers filename = read_data(num_numbers, bits_per_number)
hex_numbers = hex_numbers_str.split(';') binary_filename = convert_to_binary(filename, num_numbers, bits_per_number)
# Remove any empty strings hex_filename = convert_to_hex(binary_filename, num_numbers, bits_per_number)
hex_numbers = [hex_num for hex_num in hex_numbers if hex_num] # Read the hex numbers from the file
# Create the JSON object with the specified structure with open(hex_filename, 'rb') as f:
#data = {'randomNumbers': hex_numbers} hex_numbers_bytes = f.read()
data = hex_numbers # Decode the bytes to a string
# Delete the temporary files hex_numbers_str = hex_numbers_bytes.decode('utf-8')
os.unlink(filename) # Split the semicolon-separated string into a list of hex numbers
os.unlink(binary_filename) hex_numbers = hex_numbers_str.split(';')
os.unlink(hex_filename) # Remove any empty strings
# Return the JSON object using Flask's jsonify function hex_numbers = [hex_num for hex_num in hex_numbers if hex_num]
#return jsonify(data) # Create the JSON object with the specified structure
return json.dumps(data) #data = {'randomNumbers': hex_numbers}
data = hex_numbers
else: # Delete the temporary files
os.unlink(filename) os.unlink(filename)
os.unlink(binary_filename) os.unlink(binary_filename)
os.unlink(hex_filename) os.unlink(hex_filename)
return False # Return the JSON object using Flask's jsonify function
#return jsonify(data)
# Teste den Code return json.dumps(data)
#result = analyze_data(8, 8, startup=True)
#print(result) else:
#result = analyze_data(1, 7200000, startup=False) os.unlink(filename)
#result = analyze_data(10, 100, startup=False) os.unlink(binary_filename)
#print(result) os.unlink(hex_filename)
return 400
# Teste den Code
#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)