import tkinter
from os.path import exists
from os import getcwd
from threading import Thread
import tkinter.messagebox
import customtkinter
from time import time, strftime, localtime, sleep
from urllib.request import urlopen
from socket import setdefaulttimeout
import data_process.brake as brake
import data_process.current as current
import data_process.iso as iso
import data_process.wavelogger as wavelogger

customtkinter.set_appearance_mode("System")  # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue")  # Themes: "blue" (standard), "green", "dark-blue"
customtkinter.set_widget_scaling(1.1)  # widget dimensions and text size
customtkinter.set_window_scaling(1.1)  # window geometry dimensions
setdefaulttimeout(10)
# global vars
btns_func = {
    'start': {'btn': '', 'row': 1, 'text': '开始运行'},
    'check': {'btn': '', 'row': 2, 'text': '检查参数'},
    'log': {'btn': '', 'row': 3, 'text': '保存日志'},
    'end': {'btn': '', 'row': 4, 'text': '结束运行'},
}
widgits_dp = {
    'path': {'label': '', 'entry': '', 'row': 1, 'col': 2, 'text': '数据文件夹路径'},
    'av': {'label': '', 'entry': '', 'row': 2, 'col': 2, 'text': '角速度'},
    'rc': {'label': '', 'entry': '', 'row': 2, 'col': 4, 'text': '额定电流'},
    'rpm': {'label': '', 'entry': '', 'row': 2, 'col': 6, 'text': '额定转速'},
    'rr': {'label': '', 'entry': '', 'row': 2, 'col': 8, 'text': '减速比'},
    'dur': {'label': '', 'entry': '', 'row': 2, 'col': 10, 'text': '周期时间'},
    'axis': {'label': '', 'optionmenu': '', 'row': 3, 'col': 2, 'text': ''},
    'vel': {'label': '', 'optionmenu': '', 'row': 3, 'col': 4, 'text': ''},
    'trq': {'label': '', 'optionmenu': '', 'row': 3, 'col': 6, 'text': ''},
    'trqh': {'label': '', 'optionmenu': '', 'row': 3, 'col': 8, 'text': ''},
    'estop': {'label': '', 'optionmenu': '', 'row': 3, 'col': 10, 'text': ''},
    'rc1': {'label': '', 'entry': '', 'row': 4, 'col': 2, 'text': '额定电流'},
    'rc2': {'label': '', 'entry': '', 'row': 4, 'col': 4, 'text': '额定电流'},
    'rc3': {'label': '', 'entry': '', 'row': 4, 'col': 6, 'text': '额定电流'},
    'rc4': {'label': '', 'entry': '', 'row': 4, 'col': 8, 'text': '额定电流'},
    'rc5': {'label': '', 'entry': '', 'row': 4, 'col': 10, 'text': '额定电流'},
    'rc6': {'label': '', 'entry': '', 'row': 4, 'col': 12, 'text': '额定电流'},
}
widgits_at = {
    'path': {'label': '', 'entry': '', 'row': 2, 'col': 2, 'text': '数据文件夹路径'},
    'loadsel': {'label': '', 'optionmenu': '', 'row': 2, 'col': 1, 'text': '负载信息'},
    'av1': {'label': '', 'entry': '', 'row': 3, 'col': 2, 'text': '角速度'},
    'av2': {'label': '', 'entry': '', 'row': 3, 'col': 4, 'text': '角速度'},
    'av3': {'label': '', 'entry': '', 'row': 3, 'col': 6, 'text': '角速度'},
    'av4': {'label': '', 'entry': '', 'row': 3, 'col': 8, 'text': '角速度'},
    'av5': {'label': '', 'entry': '', 'row': 3, 'col': 10, 'text': '角速度'},
    'av6': {'label': '', 'entry': '', 'row': 3, 'col': 12, 'text': '角速度'},
    'rc1': {'label': '', 'entry': '', 'row': 4, 'col': 2, 'text': '额定电流'},
    'rc2': {'label': '', 'entry': '', 'row': 4, 'col': 4, 'text': '额定电流'},
    'rc3': {'label': '', 'entry': '', 'row': 4, 'col': 6, 'text': '额定电流'},
    'rc4': {'label': '', 'entry': '', 'row': 4, 'col': 8, 'text': '额定电流'},
    'rc5': {'label': '', 'entry': '', 'row': 4, 'col': 10, 'text': '额定电流'},
    'rc6': {'label': '', 'entry': '', 'row': 4, 'col': 12, 'text': '额定电流'},
    'rr1': {'label': '', 'entry': '', 'row': 5, 'col': 2, 'text': '额定转速'},
    'rr2': {'label': '', 'entry': '', 'row': 5, 'col': 4, 'text': '额定转速'},
    'rr3': {'label': '', 'entry': '', 'row': 5, 'col': 6, 'text': '额定转速'},
    'rr4': {'label': '', 'entry': '', 'row': 5, 'col': 8, 'text': '额定转速'},
    'rr5': {'label': '', 'entry': '', 'row': 5, 'col': 10, 'text': '额定转速'},
    'rr6': {'label': '', 'entry': '', 'row': 5, 'col': 12, 'text': '额定转速'},
}


class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        self.my_font = customtkinter.CTkFont(family="Consolas", size=16, weight="bold")
        self.w_param = 84
        # =====================================================================
        # configure window
        self.title("AIO - All in one automatic toolbox")
        # self.iconbitmap('./icon.ico')
        self.geometry("1200x550+30+30")
        self.protocol("WM_DELETE_WINDOW", self.func_end_callback)
        self.config(bg='#E9E9E9')
        self.grid_rowconfigure(6, weight=1)
        self.grid_columnconfigure((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13), weight=1)
        self.minsize(1200, 550)
        # =====================================================================
        # create frame sidebar(left)
        self.frame_func = customtkinter.CTkFrame(self, width=120, corner_radius=0, fg_color='#E9E9E9')
        self.frame_func.grid(row=0, column=0, rowspan=7, sticky='nsew')
        # create AIO logo
        self.label_logo = customtkinter.CTkLabel(self.frame_func, text="Rokae AIO", height=60, font=customtkinter.CTkFont(family="Segoe Script Bold", size=24, weight="bold"), text_color="#4F4F4F")
        self.label_logo.grid(row=0, column=0, padx=15, pady=15)
        # create buttons
        for func in btns_func:
            btns_func[func]['btn'] = customtkinter.CTkButton(self.frame_func, corner_radius=10, text=btns_func[func]['text'], fg_color='#4F4F4F', font=self.my_font)
            btns_func[func]['btn'].grid(row=btns_func[func]['row'], column=0, sticky='new', padx=10, pady=10, ipadx=5, ipady=5)
        btns_func['start']['btn'].configure(command=lambda: self.thread_it(self.func_start_callback))
        btns_func['check']['btn'].configure(command=lambda: self.thread_it(self.func_check_callback))
        btns_func['log']['btn'].configure(command=lambda: self.thread_it(self.func_log_callback))
        btns_func['end']['btn'].configure(command=lambda: self.thread_it(self.func_end_callback))
        # create version info
        self.label_version = customtkinter.CTkLabel(self.frame_func, justify='left', text="Vers: 0.1.6.3\nDate: 06/18/2024", font=self.my_font, text_color="#4F4F4F")
        self.frame_func.rowconfigure(6, weight=1)
        self.label_version.grid(row=6, column=0, padx=20, pady=20, sticky='s')
        # =====================================================================
        # create tabviews
        self.tabview = customtkinter.CTkTabview(self, width=10000, height=100, anchor='w', fg_color='#E9E9E9', border_width=2, border_color='#CDCDCD', command=self.tabview_click)
        self.tabview.grid(row=0, column=1, padx=10, pady=5, sticky="nsew")
        self.tabview.add("Data Process")
        self.tabview.add("Automatic Test")
        # create main menu for data process
        self.menu_main_dp = customtkinter.CTkOptionMenu(self.tabview.tab('Data Process'), values=["init", "brake", "current", "iso", "wavelogger"], font=self.my_font, text_color='yellow', button_color='red', fg_color='green', command=self.func_main_callback)
        self.menu_main_dp.grid(row=1, column=1, sticky='we', padx=5, pady=5)
        self.menu_main_dp.set("Start Here!")
        # create sub menu for data process
        self.menu_sub_dp = customtkinter.CTkOptionMenu(self.tabview.tab('Data Process'))
        # create main menu for automatic test
        self.menu_main_at = customtkinter.CTkOptionMenu(self.tabview.tab('Automatic Test'), values=["init", "brake", "current"], font=self.my_font, text_color='yellow', button_color='red', fg_color='green', command=self.func_main_callback)
        self.menu_main_at.grid(row=1, column=1, sticky='we', padx=5, pady=5)
        self.menu_main_at.set("Start Here!")
        # For data process tab START =====================================================================
        # create widgits_dp
        for widgit in widgits_dp:
            if widgit == 'path':
                widgits_dp[widgit]['label'] = customtkinter.CTkLabel(self.tabview.tab('Data Process'), text=f'{widgit.upper()}', font=self.my_font)
                widgits_dp[widgit]['label'].grid(row=widgits_dp[widgit]['row'], column=widgits_dp[widgit]['col'], sticky='e', pady=5)
                widgits_dp[widgit]['entry'] = customtkinter.CTkEntry(self.tabview.tab('Data Process'), width=670, placeholder_text=widgits_dp[widgit]['text'], font=self.my_font)
                widgits_dp[widgit]['entry'].grid(row=widgits_dp[widgit]['row'], column=widgits_dp[widgit]['col']+1, columnspan=11, padx=(5, 10), pady=5, sticky='we')
                widgits_dp[widgit]['entry'].configure(state='disabled')
            elif widgit in ['av', 'rc', 'rpm', 'rr', 'dur', 'rc1', 'rc2', 'rc3', 'rc4', 'rc5', 'rc6']:
                widgits_dp[widgit]['label'] = customtkinter.CTkLabel(self.tabview.tab('Data Process'), text=f"{widgit.upper()}", font=self.my_font)
                widgits_dp[widgit]['label'].grid(row=widgits_dp[widgit]['row'], column=widgits_dp[widgit]['col'], sticky='e', pady=5)
                widgits_dp[widgit]['entry'] = customtkinter.CTkEntry(self.tabview.tab('Data Process'), width=self.w_param, placeholder_text=f"{widgits_dp[widgit]['text']}", font=self.my_font)
                widgits_dp[widgit]['entry'].grid(row=widgits_dp[widgit]['row'], column=widgits_dp[widgit]['col']+1, padx=(5, 10), pady=5, sticky='w')
                widgits_dp[widgit]['entry'].configure(state='disabled')
            elif widgit in ['axis', 'vel', 'trq', 'trqh', 'estop']:
                widgits_dp[widgit]['label'] = customtkinter.CTkLabel(self.tabview.tab('Data Process'), text=f"{widgit.upper()}", font=self.my_font)
                widgits_dp[widgit]['label'].grid(row=widgits_dp[widgit]['row'], column=widgits_dp[widgit]['col'], sticky='e', pady=5)
                widgits_dp[widgit]['optionmenu'] = customtkinter.CTkOptionMenu(self.tabview.tab('Data Process'), button_color='#708090', fg_color='#778899', values=["1", "2", "3", "4", "5", "6", "7"], width=self.w_param, font=self.my_font)
                widgits_dp[widgit]['optionmenu'].grid(row=widgits_dp[widgit]['row'], column=widgits_dp[widgit]['col']+1, padx=(5, 10), pady=5, sticky='w')
                widgits_dp[widgit]['optionmenu'].configure(state='disabled')
        # For data process tab END =====================================================================
        # For automatic test tab START =====================================================================
        # create buttons
        self.seg_button = customtkinter.CTkSegmentedButton(self.tabview.tab('Automatic Test'), font=self.my_font, command=lambda value='机器状态': self.thread_it(self.segmented_button_callback()))
        self.seg_button.grid(row=1, column=2, columnspan=12, padx=(20, 10), pady=(10, 10), sticky="ew")
        self.seg_button.configure(values=["无效功能", "触发急停", "停止运动", "继续运动", "零点位姿", "机器状态", "告警信息"])
        self.seg_button.set("无效功能")
        # self.seg_button.configure(state="disabled")

        self.progressbar = customtkinter.CTkProgressBar(self.tabview.tab('Automatic Test'))
        self.progressbar.grid(row=5, column=1, padx=5, pady=5, sticky="ew")
        self.progressbar.configure(mode="determinnate")
        self.progressbar.start()
        # create widgits_at
        for widgit in widgits_at:
            if widgit == 'path':
                widgits_at[widgit]['label'] = customtkinter.CTkLabel(self.tabview.tab('Automatic Test'), text=f'{widgit.upper()}', font=self.my_font)
                widgits_at[widgit]['label'].grid(row=widgits_at[widgit]['row'], column=widgits_at[widgit]['col'], sticky='e', pady=5)
                widgits_at[widgit]['entry'] = customtkinter.CTkEntry(self.tabview.tab('Automatic Test'), width=670, placeholder_text=widgits_at[widgit]['text'], font=self.my_font)
                widgits_at[widgit]['entry'].grid(row=widgits_at[widgit]['row'], column=widgits_at[widgit]['col']+1, columnspan=11, padx=(5, 10), pady=5, sticky='we')
                widgits_at[widgit]['entry'].configure(state='disabled')
            elif widgit in ['av1', 'av2', 'av3', 'av4', 'av5', 'av6', 'rc1', 'rc2', 'rc3', 'rc4', 'rc5', 'rc6', 'rr1', 'rr2', 'rr3', 'rr4', 'rr5', 'rr6']:
                widgits_at[widgit]['label'] = customtkinter.CTkLabel(self.tabview.tab('Automatic Test'), text=f"{widgit.upper()}", font=self.my_font)
                widgits_at[widgit]['label'].grid(row=widgits_at[widgit]['row'], column=widgits_at[widgit]['col'], sticky='e', pady=5)
                widgits_at[widgit]['entry'] = customtkinter.CTkEntry(self.tabview.tab('Automatic Test'), width=self.w_param, placeholder_text=f"{widgits_at[widgit]['text']}", font=self.my_font)
                widgits_at[widgit]['entry'].grid(row=widgits_at[widgit]['row'], column=widgits_at[widgit]['col']+1, padx=(5, 10), pady=5, sticky='w')
                widgits_at[widgit]['entry'].configure(state='disabled')
            elif widgit in ['loadsel', ]:
                widgits_at[widgit]['optionmenu'] = customtkinter.CTkOptionMenu(self.tabview.tab('Automatic Test'), button_color='#708090', fg_color='#778899', values=["tool33", "tool66", "tool100"], width=self.w_param, font=self.my_font)
                widgits_at[widgit]['optionmenu'].grid(row=widgits_at[widgit]['row'], column=widgits_at[widgit]['col'], padx=5, pady=5, sticky='we')
                widgits_at[widgit]['optionmenu'].set(widgits_at[widgit]['text'])
                widgits_at[widgit]['optionmenu'].configure(state='disabled')
        # For automatic test tab END =====================================================================
        # create textbox
        self.textbox = customtkinter.CTkTextbox(self, wrap='none', font=customtkinter.CTkFont(family="consolas", size=14), text_color="blue", fg_color='#E9E9E9', border_width=2, border_color='#CDCDCD', border_spacing=5)
        self.textbox.grid(row=6, column=1, columnspan=13, ipadx=10, ipady=10, padx=10, pady=(5, 10), sticky='nsew')
        self.textbox.configure(state='disabled')
        # =====================================================================
        # version check
        cur_vers = self.label_version.cget("text").replace('\n', ' @ ').replace("Vers: ", '').replace("Date: ", '')
        url_vers = 'http://10.2.23.150:10008/vers'
        try:
            new_vers = urlopen(url_vers).read().decode('utf-8')
            if cur_vers.strip() != new_vers.strip():
                msg = f"""当前版本：{cur_vers}\n更新版本：{new_vers}\n\n请及时更新 http://10.2.23.150:10003/s/jRfM"""
                tkinter.messagebox.showwarning(title="版本更新", message=msg)
        except:
            tkinter.messagebox.showwarning(title="版本更新", message="连接服务器失败，无法确认当前是否是最新版本......")
    # functions below ↓ ----------------------------------------------------------------------------------------

    def segmented_button_callback(self):
        value = self.seg_button.get()
        print(f"segment button is triggered: {value}")
        match value:
            case '触发急停':
                pass
            case '停止运动':
                pass
            case '继续运动':
                pass
            case '零点位姿':
                pass
            case '机器状态':
                pass
            case '告警信息':
                pass

    def detect_network(self):
        while True:
            with open('./automatic_test/templates/heartbeat', 'r', encoding='utf-8') as f_h:
                pb_color = 'green' if f_h.read().strip() == '1' else 'red'
                self.progressbar.configure(progress_color=pb_color)
            sleep(3)

    def thread_it(self, func, *args):
        """ 将函数打包进线程 """
        self.myThread = Thread(target=func, args=args)
        self.myThread.daemon = True  # 主线程退出就直接让子线程跟随退出,不论是否运行完成。
        self.myThread.start()

    def tabview_click(self):
        self.initialization()
        tab_name = self.tabview.get()
        if tab_name == 'Data Process':
            self.menu_main_dp.set("Start Here!")
        elif tab_name == 'Automatic Test':
            self.menu_main_at.set("Start Here!")

    def initialization(self):
        tab_name = self.tabview.get()
        if tab_name == 'Data Process':
            for widgit in widgits_dp:
                if widgit in ['path', 'av', 'rc', 'rpm', 'rr', 'dur', 'rc1', 'rc2', 'rc3', 'rc4', 'rc5', 'rc6']:
                    widgits_dp[widgit]['label'].configure(text=f'{widgit.upper()}', text_color='black')
                    widgits_dp[widgit]['entry'].delete(0, tkinter.END)
                    widgits_dp[widgit]['entry'].configure(placeholder_text=widgits_dp[widgit]['text'], state='normal')
                    widgits_dp[widgit]['entry'].configure(state='disabled')
                elif widgit in ['axis', 'vel', 'trq', 'trqh', 'estop']:
                    widgits_dp[widgit]['label'].configure(text=f'{widgit.upper()}', text_color="black")
                    widgits_dp[widgit]['optionmenu'].configure(state='normal')
                    widgits_dp[widgit]['optionmenu'].set('1')
                    widgits_dp[widgit]['optionmenu'].configure(state='disabled')

            self.menu_sub_dp.grid_forget()
            self.textbox.delete(index1='1.0', index2='end')
            self.textbox.configure(state='disabled')
        elif tab_name == 'Automatic Test':
            for widgit in widgits_at:
                if widgit in ['path', 'av1', 'av2', 'av3', 'av4', 'av5', 'av6', 'rc1', 'rc2', 'rc3', 'rc4', 'rc5', 'rc6', 'rr1', 'rr2', 'rr3', 'rr4', 'rr5', 'rr6']:
                    widgits_at[widgit]['label'].configure(text=f'{widgit.upper()}', text_color='black')
                    widgits_at[widgit]['entry'].delete(0, tkinter.END)
                    widgits_at[widgit]['entry'].configure(placeholder_text=widgits_at[widgit]['text'], state='normal')
                    widgits_at[widgit]['entry'].configure(state='disabled')
                elif widgit in ['loadsel']:
                    widgits_at[widgit]['optionmenu'].configure(state='normal')
                    widgits_at[widgit]['optionmenu'].set(widgits_at[widgit]['text'])
                    widgits_at[widgit]['optionmenu'].configure(state='disabled')
            self.seg_button.set("无效按钮")

    def func_main_callback(self, func_name):
        self.initialization()
        tab_name = self.tabview.get()
        if tab_name == 'Data Process':
            if func_name == 'brake':
                for widgit in widgits_dp:
                    if widgit in ['path', 'av', 'rr']:
                        widgits_dp[widgit]['label'].configure(text_color='red')
                        widgits_dp[widgit]['entry'].configure(state='normal')
                    elif widgit in ['axis', 'vel', 'trq', 'estop']:
                        widgits_dp[widgit]['label'].configure(text_color="red")
                        widgits_dp[widgit]['optionmenu'].configure(state='normal')
            elif func_name == 'current':
                self.menu_sub_dp = customtkinter.CTkOptionMenu(self.tabview.tab('Data Process'), values=["max", "avg", "cycle"], font=self.my_font, button_color='red', fg_color='green', command=self.func_sub_callback)
                self.menu_sub_dp.grid(row=2, column=1, sticky='we', padx=5, pady=5)
                self.menu_sub_dp.set("--select--")
                self.menu_sub_dp.configure(text_color='yellow')

                for widgit in widgits_dp:
                    if widgit in ['path', 'rc', 'rc1', 'rc2', 'rc3', 'rc4', 'rc5', 'rc6']:
                        color = 'blue' if widgit == 'rc' else 'red'
                        widgits_dp[widgit]['label'].configure(text_color=color)
                        widgits_dp[widgit]['entry'].configure(state='normal')
                    elif widgit in ['trqh',]:
                        widgits_dp[widgit]['label'].configure(text_color="red")
                        widgits_dp[widgit]['optionmenu'].configure(state='normal')
            elif func_name == 'iso' or func_name == 'wavelogger':
                for widgit in widgits_dp:
                    if widgit in ['path',]:
                        widgits_dp[widgit]['label'].configure(text_color='red')
                        widgits_dp[widgit]['entry'].configure(state='normal')
            else:
                self.initialization()
                self.menu_main_dp.set("Start Here!")
        elif tab_name == 'Automatic Test':
            pass

    def func_sub_callback(self, func_name):
        if func_name == "max":
            for widgit in widgits_dp:
                if widgit in ['rpm', 'dur']:
                    widgits_dp[widgit]['label'].configure(text_color='black')
                    widgits_dp[widgit]['entry'].configure(state='disabled')
                elif widgit in ['vel', 'trq']:
                    widgits_dp[widgit]['label'].configure(text_color='black')
                    widgits_dp[widgit]['optionmenu'].configure(state='disabled')
        elif func_name == 'avg':
            for widgit in widgits_dp:
                if widgit in ['rpm', 'dur']:
                    widgits_dp[widgit]['label'].configure(text_color='black')
                    widgits_dp[widgit]['entry'].configure(state='disabled')
                elif widgit in ['vel', 'trq']:
                    widgits_dp[widgit]['label'].configure(text_color='black')
                    widgits_dp[widgit]['optionmenu'].configure(state='disabled')
        elif func_name == 'cycle':
            for widgit in widgits_dp:
                if widgit in ['rpm', 'dur']:
                    widgits_dp[widgit]['label'].configure(text_color='blue')
                    widgits_dp[widgit]['entry'].configure(state='normal')
                elif widgit in ['vel', 'trq']:
                    widgits_dp[widgit]['label'].configure(text_color="red")
                    widgits_dp[widgit]['optionmenu'].configure(state='normal')

    def write2textbox(self, text, wait=0, exitcode=0, color='blue'):
        self.textbox.tag_add(color, 'insert', 'end')
        self.textbox.tag_config(tagName=color, foreground=color)

        if wait != 0:
            self.textbox.insert(index='end', text=text, tags=color)
            self.textbox.update()
            self.textbox.see('end')
        elif exitcode != 0:
            self.textbox.insert(index='end', text=text + '\n', tags=color)
            self.textbox.update()
            self.textbox.see('end')
            self.textbox.configure(state='disabled')
            raise Exception(f"Error code: {exitcode}")
        else:
            self.textbox.insert(index='end', text=text + '\n', tags=color)
            self.textbox.update()
            self.textbox.see('end')

    def is_float(self, flag, *args):
        for item in args:
            try:
                if flag == 'optional':
                    item = 0 if item == '' else item
                    _ = float(item)
                elif flag == 'required':
                    _ = float(item)
            except Exception as Err:
                tkinter.messagebox.showerror(title="参数错误", message="请检查对应参数是否填写正确！", )
                self.write2textbox(f"错误信息：{Err}\n参数数据缺失，或者数据类型错误，更正后重新运行...\n", 0, 3, 'red')
        return True

    def check_param(self):
        tab_name = self.tabview.get()
        if tab_name == 'Data Process':
            func_name = self.menu_main_dp.get()
            if func_name == 'brake':
                path = widgits_dp['path']['entry'].get().strip()
                av = widgits_dp['av']['entry'].get().strip('- ')
                rr = widgits_dp['rr']['entry'].get().strip('- ')
                axis = widgits_dp['axis']['optionmenu'].get()
                vel = widgits_dp['vel']['optionmenu'].get()
                trq = widgits_dp['trq']['optionmenu'].get()
                estop = widgits_dp['estop']['optionmenu'].get()

                c1 = exists(path)
                c2 = self.is_float('required', av, rr)
                c3 = True if len({vel, trq, estop}) == 3 else False

                if c1 and c2 and c3:
                    return 1, path, float(av), float(rr), int(axis), int(vel), int(trq), int(estop)
                else:
                    return 0, 0
            # =======================================================
            elif func_name == 'current':
                path = widgits_dp['path']['entry'].get().strip()
                rc = widgits_dp['rc']['entry'].get().strip('- ')
                rpm = widgits_dp['rpm']['entry'].get().strip()
                dur = widgits_dp['dur']['entry'].get().strip()
                rc1 = widgits_dp['rc1']['entry'].get().strip()
                rc2 = widgits_dp['rc2']['entry'].get().strip()
                rc3 = widgits_dp['rc3']['entry'].get().strip()
                rc4 = widgits_dp['rc4']['entry'].get().strip()
                rc5 = widgits_dp['rc5']['entry'].get().strip()
                rc6 = widgits_dp['rc6']['entry'].get().strip()
                vel = widgits_dp['vel']['optionmenu'].get()
                trq = widgits_dp['trq']['optionmenu'].get()
                trqh = widgits_dp['trqh']['optionmenu'].get()
                sub = self.menu_sub_dp.get()

                c1 = exists(path)
                c2 = sub in ['max', 'avg', 'cycle']
                c3 = self.is_float('optional', rc, rpm)
                c4 = self.is_float('required', rc1, rc2, rc3, rc4, rc5, rc6)

                c5 = c6 = True
                if sub == 'cycle':
                    c5 = True if len({vel, trq}) == 2 else False
                    c6 = self.is_float('optional', dur)

                if c1 and c2 and c3 and c4 and c5 and c6:
                    rcs = []
                    for x in [rc1, rc2, rc3, rc4, rc5, rc6]:
                        rcs.append(float(x))
                    rc = 0 if rc == '' else rc
                    dur = 0 if sub != 'cycle' or dur == '' else dur
                    rpm = 0 if sub != 'cycle' or rpm == '' else rpm
                    rcs.append(float(rc))
                    return 2, path, sub, rcs, int(vel), int(trq), int(trqh), float(dur), float(rpm)
                else:
                    return 0, 0
            # =======================================================
            elif func_name == 'iso':
                path = widgits_dp['path']['entry'].get().strip()
                c1 = exists(path)
                if c1:
                    return 3, path
                else:
                    return 0, 0
            # =======================================================
            elif func_name == 'wavelogger':
                path = widgits_dp['path']['entry'].get().strip()
                c1 = exists(path)
                if c1:
                    return 4, path
                else:
                    return 0, 0
            # =======================================================
            else:
                return 0, 0
        elif tab_name == 'Automatic Test':
            func_name = self.menu_main_at.get()
            if func_name == 'brake':
                pass
            elif func_name == 'current':
                pass

    def func_start_callback(self):
        self.textbox.configure(state='normal')
        self.textbox.delete(index1='1.0', index2='end')

        flag, *args = self.check_param()
        func_dict = {1: brake.main, 2: current.main, 3: iso.main, 4: wavelogger.main}
        if flag == 1:
            func_dict[flag](path=args[0], av=args[1], rr=args[2],  axis=args[3], vel=args[4], trq=args[5], estop=args[6], w2t=self.write2textbox)
        elif flag == 2:
            func_dict[flag](path=args[0], sub=args[1], rcs=args[2], vel=args[3], trq=args[4], trqh=args[5], dur=args[6], rpm=args[7], w2t=self.write2textbox)
        elif flag == 3:
            func_dict[flag](path=args[0], w2t=self.write2textbox)
        elif flag == 4:
            func_dict[flag](path=args[0], w2t=self.write2textbox)
        else:
            tkinter.messagebox.showerror(title="参数错误", message="请检查对应参数是否填写正确！", )

        self.textbox.configure(state='disabled')

    def func_check_callback(self):
        self.textbox.configure(state='normal')
        self.textbox.delete(index1='1.0', index2='end')

        flag, *args = self.check_param()
        if flag:
            tkinter.messagebox.showinfo(title="参数正确", message="所有参数形式上填写无误，可以开始运行！")
        else:
            tkinter.messagebox.showerror(title="参数错误", message="需要检查对应参数是否填写正确！", )

        self.textbox.configure(state='disabled')

    def func_log_callback(self):
        content = self.textbox.get(index1='1.0', index2='end')
        if len(content) > 1:
            try:
                now = strftime('%Y%m%d%H%M%S', localtime(time()))
                log_name = f"{now}_aio.log"
                with open(f'{log_name}', 'w', encoding='utf-8') as objlog:
                    objlog.write(content)
                tkinter.messagebox.showinfo(title="保存成功", message=f'{log_name}已被保存存至↓↓↓\n{getcwd()}')

            except Exception as Err:
                self.write2textbox(Err)
                tkinter.messagebox.showerror(title="保存失败", message="未能保存本次日志，或未能完整保存，请准备好相关数据，联系fanmingfu@rokae.com查看详细信息！", )
        else:
            tkinter.messagebox.showwarning(title="未能保存", message="日志数据为空，不可保存！")

    def func_end_callback(self):
        if tkinter.messagebox.askyesno(title="关闭程序", message="相关数据可能未保存，正在运行程序时有概率会损坏数据文件，确定要终止程序运行吗？"):
            self.destroy()


if __name__ == "__main__":
    with open("./automatic_test/templates/heartbeat", "w", encoding='utf-8') as f_h:
        f_h.write('0')

    aio = App()
    aio.net_detect = Thread(target=aio.detect_network)
    aio.net_detect.daemon = True
    aio.net_detect.start()
    aio.mainloop()
