Skip to content

Commit

Permalink
🐛 Fix logger
Browse files Browse the repository at this point in the history
  • Loading branch information
NoaSecond committed Jun 3, 2024
1 parent 6462578 commit 2e512ee
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 22 deletions.
91 changes: 78 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from datetime import datetime
import scripts.realTimePlot as realTimePlot


def set_app_user_model_id(app_id):
"""
Sets the current process explicit AppUserModelID.
Expand Down Expand Up @@ -123,8 +124,80 @@ def bind_settings(MainWindow):
lambda: settings.write_to_settings_file('logOnOff', pushButton_LogOnOff.isChecked()))
pushButton_LogOnOff.clicked.connect(
lambda: pushButton_LogOnOff.setText(settings.read_from_settings_file('logOnOff')))


def bind_values_to_log(MainWindow):
timer = QTimer(MainWindow)
spinBox_logFrequency = MainWindow.findChild(QtWidgets.QSpinBox, "spinBox_logFrequency")
logFrequencyInMS = spinBox_logFrequency.value() * 1000 # Convert to milliseconds
timer.setInterval(logFrequencyInMS)

spinBox_fileSizeLimit = MainWindow.findChild(QtWidgets.QSpinBox, "spinBox_fileSizeLimit")
fileSizeLimit = spinBox_fileSizeLimit.value()

def log_values_periodically():
spinBox_RayonCanneC1 = MainWindow.findChild(QtWidgets.QSpinBox, "spinBox_RayonCanneC1")
spinBox_RayonCanneC2 = MainWindow.findChild(QtWidgets.QSpinBox, "spinBox_RayonCanneC2")
values_to_log = [
datetime.now().strftime("%H:%M:%S"),
spinBox_RayonCanneC1.value(),
spinBox_RayonCanneC2.value()
]
log_values(values_to_log, fileSizeLimit)

timer.timeout.connect(log_values_periodically)
timer.start()


def bind_actions_to_log(MainWindow):
"""
Binds actions to log.
Parameters:
- MainWindow: The main window object.
Returns:
None
"""
log_action("Application started")
pushButton_LogOnOff = MainWindow.findChild(
QtWidgets.QPushButton, "pushButton_LogOnOff")
pushButton_LogOnOff.clicked.connect(
lambda: log_action("Logging is turned on" if pushButton_LogOnOff.isChecked() else "Logging is turned off"))
pushButton_Diaphragme = MainWindow.findChild(
QtWidgets.QPushButton, "pushButton_Diaphragme")
pushButton_Diaphragme.clicked.connect(
lambda: log_action("Diaphragme is turned on" if pushButton_Diaphragme.isChecked() else "Diaphragme is turned off"))
pushButton_Diaphragme.clicked.connect(
lambda: pushButton_Diaphragme.setText("On" if pushButton_Diaphragme.isChecked() else "Off"))
pushButton_Refresh = MainWindow.findChild(
QtWidgets.QPushButton, "pushButton_Refresh")
pushButton_Refresh.clicked.connect(
lambda: log_action("Ports are refreshed"))
radioButton_C1S1 = MainWindow.findChild(
QtWidgets.QRadioButton, "radioButton_C1S1")
radioButton_C1S1.clicked.connect(
lambda: log_action("Cible 1 Stripper 1 is selected"))
radioButton_C1S2 = MainWindow.findChild(
QtWidgets.QRadioButton, "radioButton_C1S2")
radioButton_C1S2.clicked.connect(
lambda: log_action("Cible 1 Stripper 2 is selected"))
radioButton_C1S3 = MainWindow.findChild(
QtWidgets.QRadioButton, "radioButton_C1S3")
radioButton_C1S3.clicked.connect(
lambda: log_action("Cible 1 Stripper 3 is selected"))
radioButton_C2S1 = MainWindow.findChild(
QtWidgets.QRadioButton, "radioButton_C2S1")
radioButton_C2S1.clicked.connect(
lambda: log_action("Cible 2 Stripper 1 is selected"))
radioButton_C2S2 = MainWindow.findChild(
QtWidgets.QRadioButton, "radioButton_C2S2")
radioButton_C2S2.clicked.connect(
lambda: log_action("Cible 2 Stripper 2 is selected"))
radioButton_C2S3 = MainWindow.findChild(
QtWidgets.QRadioButton, "radioButton_C2S3")
radioButton_C2S3.clicked.connect(
lambda: log_action("Cible 2 Stripper 3 is selected"))


def refresh_ports():
Expand Down Expand Up @@ -168,31 +241,23 @@ def refresh_ports():
MainWindow.showFullScreen()
MainWindow.showMaximized()

# Resize tabWidget to screen size in width and 95% of screen size in height
# Resize tabWidget and set the last tab as the current tab
tabWidget = MainWindow.findChild(QtWidgets.QTabWidget, "tabWidget")
tabWidget.resize(QtWidgets.QApplication.primaryScreen().availableSize())
tabWidget.resize(MainWindow.width(), int(
QtWidgets.QApplication.primaryScreen().availableSize().height() * 0.95))
tabWidget.setCurrentIndex(tabWidget.count() - 1)

# Settings
settings = Settings()
load_settings(MainWindow)
bind_settings(MainWindow)

# Logger
timer = QTimer()
spinBox_logFrequency = MainWindow.findChild(
QtWidgets.QSpinBox, "spinBox_logFrequency")
logFrequencyInMB = spinBox_logFrequency.value() * 1000
timer.setInterval(logFrequencyInMB)
spinBox_fileSizeLimit = MainWindow.findChild(
QtWidgets.QSpinBox, "spinBox_fileSizeLimit")
fileSizeLimit = spinBox_fileSizeLimit.value()
values_to_log = [datetime.now().strftime("%H:%M:%S"), 2, 3]
timer.timeout.connect(log_values(values_to_log, fileSizeLimit))
timer.start()
bind_actions_to_log(MainWindow)
bind_values_to_log(MainWindow)

# Serial Link
# Devices
refresh_ports()
pushButton_Refresh = MainWindow.findChild(
QtWidgets.QPushButton, "pushButton_Refresh")
Expand Down
31 changes: 22 additions & 9 deletions scripts/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import datetime

path = './logs/'
header = ['Time', 'Title2', 'Title3']
header = ['Time', 'RayonCanneC1', 'RayonCanneC2']

def log_action(action):
"""
Expand Down Expand Up @@ -33,12 +33,13 @@ def log_values(values, max_size_mb):
None
"""
directory = create_folder()
if check_file_size(directory, max_size_mb):
if check_csv_file_size(directory, max_size_mb):
file_path = add_csv_file(directory)
else:
latest_file = get_latest_file(directory)
latest_file = get_latest_csv_file(directory)
file_path = os.path.join(directory, latest_file)

print(f"Logging values to {file_path}")
write_values(file_path, values)

def create_folder():
Expand Down Expand Up @@ -97,9 +98,9 @@ def write_values(file_path, values):
writer = csv.writer(file)
writer.writerow(values)

def check_file_size(directory, max_size_mb):
def check_csv_file_size(directory, max_size_mb):
"""
Check if the size of the latest file in the given directory exceeds the maximum size.
Check if the size of the latest CSV file in the given directory exceeds the maximum size.
Args:
directory (str): The directory path to check for files.
Expand All @@ -108,18 +109,20 @@ def check_file_size(directory, max_size_mb):
Returns:
bool: True if the size of the latest file is greater than the maximum size, False otherwise.
"""
if directory is None:
return True
if not os.listdir(directory):
return True

latest_file = get_latest_file(directory)
latest_file = get_latest_csv_file(directory)
file_path = os.path.join(directory, latest_file)
file_size_mb = os.path.getsize(file_path) / (1024 * 1024) # Convert to MB

return file_size_mb > max_size_mb

def get_latest_file(directory):
def get_latest_csv_file(directory):
"""
Returns the name of the latest file in the specified directory.
Returns the name of the latest CSV file in the specified directory.
Args:
directory (str): The directory path.
Expand All @@ -130,6 +133,16 @@ def get_latest_file(directory):
files = os.listdir(directory)
if not files:
return None
latest_file = max(files, key=lambda x: os.path.getctime(os.path.join(directory, x)))
latest_file = None
latest_time = 0

for file in files:
if file.endswith('.csv'):
file_path = os.path.join(directory, file)
file_time = os.path.getctime(file_path)
if file_time > latest_time:
latest_file = file
latest_time = file_time

return latest_file
# Développé avec ❤️ par : www.noasecond.com.

0 comments on commit 2e512ee

Please sign in to comment.