private void AutoProcessWindowTws(TwsWatchDogOption option) { var processs = SearchTwsProcess(); if (processs.Count() > 1) { RestartTwsProcess(); return; } if (processs.Count() == 0) { logger.LogDebug("TWS Process not found"); try { logger.LogDebug("Try start new TWS process..."); RunNewProcess(); logger.LogInformation("TWS process running successfully"); return; } catch (Exception e) { logger.LogError("TWS Process not started. error: {1}:{2}", e.GetType().Name, e.Message); return; } } foreach (var process in processs) { var state = TwsWindowState(process, option); switch (state.Item1) { case TwsWindowStatus.EnterSecurityCode: InputHelper.SendEnterSecurityCode(state.Item2); break; case TwsWindowStatus.AuthentificateFailed: logger.LogError("Automatic authorization error. Please sign in manually"); RestartTwsProcess(); break; case TwsWindowStatus.AuthentificateProcess: logger.LogInformation("Authentificating waitng..."); break; case TwsWindowStatus.StartingApplication: logger.LogInformation("Starting application waiting..."); break; case TwsWindowStatus.LoginInput: WindowInfo pwi = new WindowInfo(); User32Methods.GetWindowInfo(state.Item2, ref pwi); logger.LogDebug("type: {1}, status: {2}, rect: {3}, style: {4}, exstyle: {5}", pwi.WindowType, pwi.WindowStatus, pwi.WindowRect, pwi.Styles, pwi.ExStyles); InputHelper.SendLoginAndPassword(state.Item2, option.Login, option.Password, logger); break; case TwsWindowStatus.ReloginIsRequired: // RestartTwsProcess(); InputHelper.SendExitApplicationFromReloginForm(); // return; break; case TwsWindowStatus.ExistingSessionDetected: InputHelper.SendExitApplicationExistingSessionDetectedForm(); // RestartTwsProcess(); // return; break; case TwsWindowStatus.Success: //logger.LogDebug("Tws Window Success"); break; case TwsWindowStatus.MainWindowIsNotDrawn: logger.LogInformation("Waiting main window..."); break; default: logger.LogError("Unwnown Tws Window status. Failed authentification. Please log in manually"); break; } } Thread.Sleep(TimeSpan.FromSeconds(2)); }
private string WorkingDirectory(TwsWatchDogOption option) { return(Directory.GetDirectoryRoot(option.Path)); }
private (TwsWindowStatus, IntPtr) TwsWindowState(Process process, TwsWatchDogOption option) { process.Refresh(); var MainWindow = process.MainWindowHandle; if (MainWindow == IntPtr.Zero) { return(TwsWindowStatus.MainWindowIsNotDrawn, IntPtr.Zero); } IntPtr LoginWindow; if (process.MainWindowTitle == option.LoginWindow.Header) { LoginWindow = process.MainWindowHandle; } else { LoginWindow = User32Methods.FindWindow(option.LoginWindow.Class, option.LoginWindow.Header); } IntPtr AuthenticatingWindow; if (process.MainWindowTitle == option.AuthenticatingWindow.Header) { AuthenticatingWindow = process.MainWindowHandle; } else { AuthenticatingWindow = User32Methods.FindWindow(option.AuthenticatingWindow.Class, option.AuthenticatingWindow.Header); } IntPtr StartingApplicationWindow; if (process.MainWindowTitle == option.StartingApplicationWindow.Header) { StartingApplicationWindow = process.MainWindowHandle; } else { StartingApplicationWindow = User32Methods.FindWindow(option.StartingApplicationWindow.Class, option.StartingApplicationWindow.Header); } var EnterSecurityCodeWindow = User32Methods.FindWindow(option.EnterSecurityCodeWindow.Class, option.EnterSecurityCodeWindow.Header); var LoginFailedWindow = User32Methods.FindWindow(option.LoginFailedWindow.Class, option.LoginFailedWindow.Header); var ExistingSessionDetectedWindow = User32Methods.FindWindow(option.ExistingSessionDetectedWindow.Class, option.ExistingSessionDetectedWindow.Header); var ReloginIsRequiredWindow = User32Methods.FindWindow(option.ReloginIsRequiredWindow.Class, option.ReloginIsRequiredWindow.Header); logger.LogDebug("MainWindow: {0}, LoginWindow: {1}, AuthenticatingWindow: {2}, StartingApplicationWindow: {3}, EnterSecurityCodeWindow: {4}, LoginFailedWindow: {5}, ExistingSessionDetectedWindow: {6}, ReloginIsRequiredWindow: {7}", MainWindow, LoginWindow, AuthenticatingWindow, StartingApplicationWindow, EnterSecurityCodeWindow, LoginFailedWindow, ExistingSessionDetectedWindow, ReloginIsRequiredWindow); // Если есть окно процесса аутентификации, то необходимо проверить наличие других окон if (LoginFailedWindow != IntPtr.Zero) { return(TwsWindowStatus.AuthentificateFailed, LoginFailedWindow); } // Ппоявилось окно ввода дополнительной авторизации if (EnterSecurityCodeWindow != IntPtr.Zero) { return(TwsWindowStatus.EnterSecurityCode, EnterSecurityCodeWindow); } // Появилось окно с сообщением о том, что уже залогинен другой пользователь if (ExistingSessionDetectedWindow != IntPtr.Zero) { return(TwsWindowStatus.EnterSecurityCode, ExistingSessionDetectedWindow); } // Вылезло окно с сообщение о том, что нужно произвести авторизацию заново if (ReloginIsRequiredWindow != IntPtr.Zero) { return(TwsWindowStatus.ReloginIsRequired, ReloginIsRequiredWindow); } // происходит процесс загрузки приложения после успешной авторизации if (StartingApplicationWindow != IntPtr.Zero) { return(TwsWindowStatus.StartingApplication, StartingApplicationWindow); } // Происходит процесс аутентификации после ввода логина с паролем if (AuthenticatingWindow != IntPtr.Zero) { return(TwsWindowStatus.AuthentificateProcess, AuthenticatingWindow); } // Если есть окно Login и нет окна процесса авторизации, значит терминал ждёт ввода логина с паролем if (LoginWindow != IntPtr.Zero) { // Если нет процесса авторизации, значит терминал запросил логин с паролем if (AuthenticatingWindow == IntPtr.Zero) { return(TwsWindowStatus.LoginInput, LoginWindow); } else { return(TwsWindowStatus.AuthentificateProcess, AuthenticatingWindow); } } else { if (process.MainWindowHandle != IntPtr.Zero) { return(TwsWindowStatus.Success, process.MainWindowHandle); } } // Статус терминала неизвестен return(TwsWindowStatus.UnknownStatus, IntPtr.Zero); }