import matplotlib
matplotlib.use('Agg')
import argparse
import sys
import time
from Decoder import Decoder_main
from PubLibrary.RunOnce import is_program_running
from PubLibrary.InifileHelper import IniRead

def get_device_info(device_type):
    
    section = f'device_type_{device_type}'
    device_info = {
        'sample_rate': int(IniRead(section, 'sample_rate')) if IniRead(section, 'sample_rate') is not None else 250,
        'channel_nums': int(IniRead(section, 'channel_nums')) if IniRead(section, 'channel_nums') is not None else 66,
        'channel_names': IniRead(section, 'channel_names') if IniRead(section, 'channel_names') is not None else None,
        'channel_index': IniRead(section, 'channel_index') if IniRead(section, 'channel_index') is not None else None, 
    }
    
    return device_info


if __name__ == "__main__":
    if not is_program_running():
        # 解析命令行参数
        # parser = argparse.ArgumentParser(description="EEG Decoder Application")
        # parser.add_argument('-dt', '-t','--device-type', type=int, default=None, help="Device Type")
        # parser.add_argument('-dh', '--device-host', type=str, default=None, help="Device Host IP")
        # parser.add_argument('-dp', '--device-port', type=int, default=None, help="Device Port")
        # parser.add_argument('-uh', '--upper-host', type=str, default=None, help="Upper Computer Host IP")
        # parser.add_argument('-up', '--upper-port', type=int, default=None, help="Upper Computer Port")
        # args = parser.parse_args()

        # decoder.connect(
        #     device_type=args.device_type,
        #     device_host=args.device_host,
        #     device_port=args.device_port,
        #     upper_host=args.upper_host,
        #     upper_port=args.upper_port
        # )

        device_info= get_device_info(1)
        decoder = Decoder_main(device_info=device_info)

        try:
            decoder.start()
            while not decoder.zmqServer.IsExitApp:
                time.sleep(1)
        except KeyboardInterrupt:
            decoder.stop()
