Files
Helmholtz_Test_Bench/main.py
T

94 lines
3.5 KiB
Python

#!/usr/bin/env python
# Main file of the program. Run this file to start the application.
from os.path import exists
import traceback
from tkinter import messagebox
from src.helmholtz_cage_device import HelmholtzCageDevice
import src.globals as g
import src.config_handling as config
import src.csv_logging as log
from src.magnetometer import MagnetometerProxy
from src.user_interface import HelmholtzGUI
from src.socket_control import SocketInterfaceThread
from src.utility import ui_print
def program_start():
""" Application entry point """
print("Starting application")
config.CONFIG_FILE = config.get_default_config_path() # set the config file path
if not exists(config.CONFIG_FILE): # config file does not exist yet
print("Config file not found, creating new from defaults.")
config.reset_config_to_default() # create configuration object from defaults
config.write_config_to_file(config.CONFIG_OBJECT) # write the configuration object to a new file
config.CONFIG_OBJECT = config.get_config_from_file(config.CONFIG_FILE) # read configuration data from config file
# initiate communication with devices and initialize all major program objects
g.CAGE_DEVICE = HelmholtzCageDevice()
# Mostly a data structure to hold field data broadcast by connected tcp client with HW access.
g.MAGNETOMETER = MagnetometerProxy()
# initialize user interface
g.app = HelmholtzGUI()
# tell all functions that the user interface is now runnings
g.exit_flag = False
# Connect hardware to HelmholtzCageDevice adapter object
g.CAGE_DEVICE.connect_hardware_async()
# check config for values exceeding limits
config.check_config(config.CONFIG_OBJECT)
# Create TCP/Socket listener
socket_controller = SocketInterfaceThread()
socket_controller.start()
# call program_end function if user closes the application
g.app.protocol("WM_DELETE_WINDOW", program_end)
ui_print("Application initialized")
# start main program loop
g.app.mainloop()
def program_end():
""" Safely shuts everything down and saves any unsaved data.
Called on exception or when user closes application """
# tell everything else the application has been closed
g.exit_flag = True
# shut down devices and end all threads.
g.CAGE_DEVICE.destroy()
if log.unsaved_data: # Check if there is logged data that has not been saved yet
# open pop-up to ask user if he wants to save the data:
save_log = messagebox.askquestion("Save log data?", "There seems to be unsaved logging data. "
"Do you wish to write it to a file now?")
if save_log == 'yes': # user has chosen yes
filepath = log.select_file() # let user select a file to write to
log.write_to_file(filepath) # write the data to the chosen file
if g.app is not None:
g.app.destroy() # close application
if __name__ == '__main__':
try:
program_start()
except Exception as e: # An error has occurred somewhere in the program
print("\nAn error occurred, shutting down.")
# shop pop-up error message:
message = "%s.\nSee Python console traceback for more details. " \
"\nShutting down devices, check equipment to confirm." % e
messagebox.showerror("Error!", message)
# print error traceback in the python console
print(traceback.print_exc())
# safely close everything and shut down devices
program_end()