Exemplo n.º 1
0
 public void Initialize()
 {
     method1.Initialize();
     method2.Initialize();
     method1.OnCommandReceived += (s, e) => OnCommandReceived?.Invoke(s, e);
     method2.OnCommandReceived += (s, e) => OnCommandReceived?.Invoke(s, e);
 }
Exemplo n.º 2
0
        static void Main()
        {
            bool  createdNew;
            Mutex mutex = new Mutex(true, "RemoteControlV2", out createdNew);

            Logger = Logger.AllocateLogger();
            Logger.Initialize();
            Logger.Log(LogType.Runtime, LogSeverity.Info, "Application Started");
            Logger.Log(LogType.Runtime, LogSeverity.Debug, "Loading configuration...");
            if (File.Exists("config.json"))
            {
                try
                {
                    Logger.Log(LogType.Runtime, LogSeverity.Debug, "Found configuration file. Loading...");
                    Config = Configuration.FromFile("config.json");
                }
                catch (Exception ex)
                {
                    Logger.Log(LogType.Runtime, LogSeverity.Debug, $"Exception of type {ex.GetType()}: {ex.Message}");
                    Logger.Log(LogType.Runtime, LogSeverity.Trace, ex.StackTrace);
                    Logger.Log(LogType.Runtime, LogSeverity.Fatal, "Could not load configuration file.");
                    Exit(-1);
                }
            }
            else
            {
                try
                {
                    Logger.Log(LogType.Runtime, LogSeverity.Debug, "No configuration file found. Creating it...");
                    Config.Save("config.json");
                }
                catch (Exception ex)
                {
                    Logger.Log(LogType.Runtime, LogSeverity.Debug, $"Exception of type {ex.GetType()}: {ex.Message}");
                    Logger.Log(LogType.Runtime, LogSeverity.Trace, ex.StackTrace);
                    Logger.Log(LogType.Runtime, LogSeverity.Fatal, "Could not create configuration file.");
                    Exit(-1);
                }
            }
            AddStandardCommands();

            try
            {
                Logger.Log(LogType.Runtime, LogSeverity.Debug, "Initializing serial connection method...");
                var v1 = new SerialConnectionMethod(Config.Port, Config.BaudRate);
                Logger.Log(LogType.Runtime, LogSeverity.Debug, "Initializing TCP connection method...");
                var v2 = new TCPConnectionMethod(Config.NetPort);
                Connection = new AggregateConnectionMethod(v1, v2);
            }
            catch (Exception ex)
            {
                Logger.Log(LogType.Runtime, LogSeverity.Debug, $"Exception of type {ex.GetType()}: {ex.Message}");
                Logger.Log(LogType.Runtime, LogSeverity.Trace, ex.StackTrace);
                Logger.Log(LogType.Runtime, LogSeverity.Fatal, "Could not initialize connection method.");
                Exit(-1);
            }

            Manager.Plugins = EnumeratePlugins();
            Manager.LoadAllPlugins();
            InitializePlugins();
            Logger.Log(LogType.Runtime, LogSeverity.Info, "Plugins loaded and initialized.");

            Connection.Initialize();
            Connection.OnCommandReceived += Connection_OnCommandReceived;
            Logger.Log(LogType.Runtime, LogSeverity.Debug, "Adding notification tray icon...");
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Icon         = new NotifyIcon();
            Icon.Icon    = Resources.Icon;
            Icon.Text    = "RemoteControlV2";
            Icon.Visible = Config.ShowIcon;
            SubMenu      = new ContextMenuStrip();
            MainItem     = new ToolStripMenuItem("Main Window");
            ExitItem     = new ToolStripMenuItem("Exit");
            SubMenu.Items.AddRange(new ToolStripMenuItem[] { MainItem, ExitItem });
            Icon.ContextMenuStrip = SubMenu;
            MainItem.Click       += MainItem_Click;
            ExitItem.Click       += (o, e) => Exit(0);
            Logger.Log(LogType.Runtime, LogSeverity.Debug, "Notification tray icon added.");
            Logger.Log(LogType.Runtime, LogSeverity.Debug, "Starting message loop.");
            Application.Run();
        }