2015-04-15 18:32:56 +00:00
|
|
|
///
|
2023-03-28 11:03:38 +00:00
|
|
|
/// Copyright (c) 2013-2023 Sensus Slovensko a.s.
|
2015-04-15 18:32:56 +00:00
|
|
|
///
|
2014-07-28 07:54:35 +00:00
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Collections.Generic;
|
2023-03-28 11:03:38 +00:00
|
|
|
using System.Drawing;
|
2014-07-28 07:54:35 +00:00
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using log4net;
|
2023-03-28 11:03:38 +00:00
|
|
|
using NHibernate;
|
2021-09-23 09:46:40 +00:00
|
|
|
using Common;
|
2015-12-04 16:17:20 +00:00
|
|
|
using Config.Entities;
|
2017-03-19 09:37:06 +00:00
|
|
|
using Users;
|
2014-07-28 07:54:35 +00:00
|
|
|
using TBF.Resources;
|
2022-06-07 09:58:53 +00:00
|
|
|
using TBF.Rig.Sequences;
|
2019-09-16 08:51:25 +00:00
|
|
|
using TBF.UI.Shared;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
namespace TBF
|
|
|
|
|
{
|
|
|
|
|
public class Program
|
|
|
|
|
{
|
2017-04-06 21:49:51 +00:00
|
|
|
public const string HomeDir = "C:\\Tbf\\"; /// Contains subdirectories Results, Logs, Images, ...
|
2019-07-25 13:13:06 +00:00
|
|
|
public const string ConfigDir = "C:\\Tbf\\Cfg\\";
|
2018-02-11 15:31:25 +00:00
|
|
|
public const string GraphsDir = "C:\\Tbf\\Graphs\\";
|
|
|
|
|
public const string ImagesDir = "C:\\Tbf\\Images\\";
|
2017-07-27 16:02:19 +00:00
|
|
|
public const string TempImagesDir = "C:\\Tbf\\Images\\Temp\\";
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2018-11-15 11:39:29 +00:00
|
|
|
public static string ExecutableDir;
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
/// log4net
|
2016-01-26 12:39:51 +00:00
|
|
|
static ILog log;
|
2014-07-28 07:54:35 +00:00
|
|
|
const string log4netConfigFName = "log4netConfig.xml";
|
|
|
|
|
|
2016-02-18 00:08:14 +00:00
|
|
|
/// Program information
|
|
|
|
|
public static string Version; /// version string
|
|
|
|
|
public static DateTime BuildDateTime; /// date and time of program build
|
2015-11-12 23:03:33 +00:00
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
/// Local settings
|
2015-02-28 20:36:33 +00:00
|
|
|
public static LocalSettings LocalSettings;
|
2014-07-28 07:54:35 +00:00
|
|
|
public const string LocalSettingsFileName = "config.xml";
|
2015-02-28 20:36:33 +00:00
|
|
|
public const string LocalSettingsBackupName = "config.backup.xml";
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2015-04-28 14:17:36 +00:00
|
|
|
public static System.Globalization.CultureInfo AltCulture;
|
2022-06-07 09:58:53 +00:00
|
|
|
|
|
|
|
|
static IList<Procedure> listOfProcedures;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2015-09-26 18:59:04 +00:00
|
|
|
/// <summary>
|
2014-07-28 07:54:35 +00:00
|
|
|
/// Selected procedure data
|
|
|
|
|
/// - before the main program window is open, objects referenced by this variable
|
|
|
|
|
/// are loaded from the database.
|
|
|
|
|
/// - user interface displays data stored in objects referenced by this variable.
|
|
|
|
|
/// When user modifies procedure settings and confirms changes (choses to save them),
|
|
|
|
|
/// data referenced by this variable are updated and written to the database.
|
|
|
|
|
/// - when users starts a test procedure, state machine creates its own copy
|
|
|
|
|
/// of procedure data and uses them for the test. Changes of 'SelectedProcedure'
|
|
|
|
|
/// done during the test do not have any influence on the currently running test.
|
|
|
|
|
/// </summary>
|
2022-06-07 09:58:53 +00:00
|
|
|
public static Procedure SelectedProcedure = null;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Main window object.
|
|
|
|
|
/// </summary>
|
2022-06-07 09:58:53 +00:00
|
|
|
public static TBF.UI.MainWnd MainWnd;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The main entry point for the application.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[STAThread]
|
|
|
|
|
static void Main()
|
|
|
|
|
{
|
2015-02-28 20:36:33 +00:00
|
|
|
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
|
|
|
|
|
|
2015-11-12 23:03:33 +00:00
|
|
|
/// Program version string
|
2016-02-18 00:08:14 +00:00
|
|
|
System.Reflection.Assembly thisAssembly = System.Reflection.Assembly.GetExecutingAssembly();
|
2018-11-15 11:39:29 +00:00
|
|
|
ExecutableDir = Path.GetDirectoryName(thisAssembly.Location);
|
|
|
|
|
Version ver = thisAssembly.GetName().Version;
|
2015-12-20 12:26:33 +00:00
|
|
|
Version = string.Format("{0}.{1}.{2}", ver.Major, ver.Minor, ver.Build);
|
2016-02-18 00:08:14 +00:00
|
|
|
BuildDateTime = new System.IO.FileInfo(thisAssembly.Location).LastWriteTime;
|
2015-11-12 23:03:33 +00:00
|
|
|
|
2014-10-02 18:56:24 +00:00
|
|
|
/// Local program configuration directory including the trailing backslash
|
2014-07-28 07:54:35 +00:00
|
|
|
string locAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) +
|
|
|
|
|
Path.DirectorySeparatorChar + "TestBenchFramework" + Path.DirectorySeparatorChar;
|
|
|
|
|
|
2019-07-25 13:13:06 +00:00
|
|
|
string sampleConfigDir = Application.StartupPath + Path.DirectorySeparatorChar + "SampleConfig" + Path.DirectorySeparatorChar;
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
/// Check whether local application data directory exists.
|
|
|
|
|
/// If not, create it and copy the initial configuration to this directory.
|
|
|
|
|
/// This is done only once after a clean program installation on the first program start-up
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-07-25 13:13:06 +00:00
|
|
|
if (!Directory.Exists(ConfigDir))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(ConfigDir);
|
|
|
|
|
Environment.CurrentDirectory = ConfigDir;
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(locAppDataDir))
|
|
|
|
|
{
|
|
|
|
|
///
|
|
|
|
|
/// Copy configuration from locAppDataDir or from SampleConfig if files are missing in locAppDataDir
|
|
|
|
|
///
|
|
|
|
|
if (File.Exists(locAppDataDir + LocalSettingsFileName))
|
|
|
|
|
File.Copy(locAppDataDir + LocalSettingsFileName, ConfigDir + LocalSettingsFileName);
|
|
|
|
|
else if (File.Exists(locAppDataDir + LocalSettingsBackupName))
|
|
|
|
|
File.Copy(locAppDataDir + LocalSettingsBackupName, ConfigDir + LocalSettingsFileName);
|
|
|
|
|
else if (File.Exists(sampleConfigDir + LocalSettingsFileName))
|
|
|
|
|
File.Copy(sampleConfigDir + LocalSettingsFileName, ConfigDir + LocalSettingsFileName);
|
|
|
|
|
|
|
|
|
|
if (File.Exists(locAppDataDir + LocalSettingsBackupName))
|
|
|
|
|
File.Copy(locAppDataDir + LocalSettingsBackupName, LocalSettingsBackupName);
|
|
|
|
|
|
|
|
|
|
if (File.Exists(locAppDataDir + log4netConfigFName))
|
|
|
|
|
File.Copy(locAppDataDir + log4netConfigFName, log4netConfigFName);
|
|
|
|
|
else if (File.Exists(sampleConfigDir + log4netConfigFName))
|
|
|
|
|
File.Copy(sampleConfigDir + log4netConfigFName, log4netConfigFName);
|
|
|
|
|
|
|
|
|
|
File.SetAttributes(LocalSettingsFileName, FileAttributes.Normal);
|
|
|
|
|
File.SetAttributes(LocalSettingsBackupName, FileAttributes.Normal);
|
|
|
|
|
File.SetAttributes(log4netConfigFName, FileAttributes.Normal);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
///
|
|
|
|
|
/// Copy configuration from SampleConfig directory
|
|
|
|
|
///
|
|
|
|
|
if (File.Exists(sampleConfigDir + LocalSettingsFileName))
|
|
|
|
|
File.Copy(sampleConfigDir + LocalSettingsFileName, LocalSettingsFileName);
|
|
|
|
|
|
|
|
|
|
if (File.Exists(sampleConfigDir + log4netConfigFName))
|
|
|
|
|
File.Copy(sampleConfigDir + log4netConfigFName, log4netConfigFName);
|
|
|
|
|
|
|
|
|
|
File.SetAttributes(LocalSettingsFileName, FileAttributes.Normal);
|
|
|
|
|
File.SetAttributes(log4netConfigFName, FileAttributes.Normal);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Environment.CurrentDirectory = ConfigDir;
|
|
|
|
|
}
|
2017-04-06 21:49:51 +00:00
|
|
|
|
|
|
|
|
if (!Directory.Exists(ImagesDir)) Directory.CreateDirectory(ImagesDir);
|
2017-07-27 16:02:19 +00:00
|
|
|
if (!Directory.Exists(TempImagesDir)) Directory.CreateDirectory(TempImagesDir);
|
|
|
|
|
|
|
|
|
|
IEnumerable<string> files = Directory.EnumerateFiles(TempImagesDir); /// TODO: Implement recursive directory delete
|
2017-04-06 21:49:51 +00:00
|
|
|
foreach (var file in files) File.Delete(file);
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
2022-08-09 16:13:10 +00:00
|
|
|
catch (Exception)
|
2014-07-28 07:54:35 +00:00
|
|
|
{
|
2018-05-25 07:52:44 +00:00
|
|
|
MessageBox.Show("Error creating a local application data directory and preparing an initial configuration", "Fatal error");
|
|
|
|
|
return; /// Fatal error
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
2016-12-29 14:15:45 +00:00
|
|
|
/// Configure and start logging
|
2019-07-25 13:13:06 +00:00
|
|
|
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(ConfigDir + log4netConfigFName));
|
2016-01-26 12:39:51 +00:00
|
|
|
log = LogManager.GetLogger(typeof(Program));
|
2016-01-14 20:49:20 +00:00
|
|
|
log.Fatal("--------------------------------------------------------------------------------");
|
2016-01-26 12:39:51 +00:00
|
|
|
log.Fatal(string.Format("TBF ver.{0}", Version));
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2015-02-28 20:36:33 +00:00
|
|
|
///
|
2014-07-28 07:54:35 +00:00
|
|
|
/// Load the local settings
|
2015-02-28 20:36:33 +00:00
|
|
|
///
|
|
|
|
|
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
|
|
|
|
|
if (Program.LocalSettings == null || Program.LocalSettings.TestBenches == null)
|
|
|
|
|
{
|
|
|
|
|
/// Loading local seetings from regular config file failed. Use the backup
|
|
|
|
|
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsBackupName);
|
|
|
|
|
if (Program.LocalSettings == null || Program.LocalSettings.TestBenches == null)
|
|
|
|
|
{
|
2016-04-07 11:52:57 +00:00
|
|
|
log.Fatal("Application terminated.");
|
2018-05-25 07:52:44 +00:00
|
|
|
MessageBox.Show(string.Format("Could not load file {0}, nor {1}.", Program.LocalSettingsFileName, Program.LocalSettingsBackupName), "Fatal error");
|
2015-02-28 20:36:33 +00:00
|
|
|
return; /// Fatal error
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LocalSettings.Save(); /// Save the settings to overwrite the wrong file
|
2016-04-07 11:52:57 +00:00
|
|
|
log.FatalFormat("BatchNr = {0}", LocalSettings.BatchNr);
|
2015-02-28 20:36:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/// Loading local seetings from the regular config file was successful. Update the backup
|
|
|
|
|
File.Copy(Program.LocalSettingsFileName, Program.LocalSettingsBackupName, true);
|
2016-04-07 11:52:57 +00:00
|
|
|
log.FatalFormat("BatchNr = {0}", LocalSettings.BatchNr);
|
2015-02-28 20:36:33 +00:00
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2016-04-07 11:52:57 +00:00
|
|
|
|
2015-03-27 06:27:36 +00:00
|
|
|
try
|
|
|
|
|
{
|
2015-04-13 09:04:18 +00:00
|
|
|
string culture = LocalSettings.Language.Replace('_', '-');
|
|
|
|
|
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);
|
2015-03-27 06:27:36 +00:00
|
|
|
}
|
2015-04-10 10:59:26 +00:00
|
|
|
catch (Exception e)
|
2015-03-27 06:27:36 +00:00
|
|
|
{
|
2015-04-10 10:59:26 +00:00
|
|
|
string msg = e.Message;
|
2015-03-27 06:27:36 +00:00
|
|
|
MessageBox.Show("Selected language not supported.\nUsing English.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
|
2015-04-13 09:04:18 +00:00
|
|
|
System.Threading.Thread.CurrentThread.CurrentUICulture =
|
|
|
|
|
new System.Globalization.CultureInfo("en");
|
2015-03-27 06:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-28 14:17:36 +00:00
|
|
|
//AltCulture = null;
|
|
|
|
|
AltCulture = new System.Globalization.CultureInfo("SK");
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2016-04-07 11:52:57 +00:00
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
/// This belongs to Application.Run(new MainWnd()) ...
|
|
|
|
|
/// and should be executed before opening 'loginDlg'
|
|
|
|
|
Application.EnableVisualStyles();
|
|
|
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
|
|
|
|
2016-04-07 11:52:57 +00:00
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
/// Ensure that there is at least one bench in the configuration
|
2015-02-28 20:36:33 +00:00
|
|
|
while (Program.LocalSettings.BenchesCount == 0)
|
2014-07-28 07:54:35 +00:00
|
|
|
{
|
|
|
|
|
log.Warn("No test benches in the local configuration -> display an appropriate dialog.");
|
|
|
|
|
|
|
|
|
|
/// Ask what to do, ask for the password, open 'DatabaseSetingsDlg' and continue on OK
|
2019-09-16 08:51:25 +00:00
|
|
|
if ((new NoBenchOrDatabaseDlg { Message = Strings.NoBenchMsg }.ShowDialog() != DialogResult.OK) ||
|
2023-03-28 11:03:38 +00:00
|
|
|
(new Users.Forms.LoginDlg().ShowDialog() == DialogResult.Cancel) ||
|
2019-09-16 08:51:25 +00:00
|
|
|
(new TBF.UI.Settings.BenchesDlg().ShowDialog() == DialogResult.Cancel))
|
2014-07-28 07:54:35 +00:00
|
|
|
{
|
|
|
|
|
return; /// Exit program
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Warn("Loading the local settings again.");
|
2015-02-28 20:36:33 +00:00
|
|
|
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
|
|
|
|
|
if (Program.LocalSettings == null) return; /// Fatal error
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// User login
|
|
|
|
|
bool retryLogin = true; /// true = stay in a login loop
|
2016-04-07 11:52:57 +00:00
|
|
|
do
|
|
|
|
|
{
|
2023-03-28 11:03:38 +00:00
|
|
|
LoadingConfigEnd();
|
|
|
|
|
|
2015-02-19 14:50:31 +00:00
|
|
|
LoginDlgWithBenchSelection loginDlgBench;
|
2014-07-28 07:54:35 +00:00
|
|
|
if (LocalSettings.LastBenchName != null)
|
|
|
|
|
{
|
2015-02-19 14:50:31 +00:00
|
|
|
loginDlgBench = new LoginDlgWithBenchSelection(LocalSettings.LastBenchName);
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-02-19 14:50:31 +00:00
|
|
|
loginDlgBench = new LoginDlgWithBenchSelection();
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
2016-12-21 08:27:25 +00:00
|
|
|
loginDlgBench.Method = LocalSettings.LoginMethod;
|
|
|
|
|
DialogResult dr = loginDlgBench.ShowDialog();
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
if (dr == DialogResult.Cancel)
|
|
|
|
|
{
|
|
|
|
|
return; /// Login canceled --> Exit application
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < LocalSettings.BenchesCount; i++)
|
|
|
|
|
{
|
2015-02-19 14:50:31 +00:00
|
|
|
if (LocalSettings.TestBenches[i].BenchName == loginDlgBench.BenchName)
|
2016-04-07 11:52:57 +00:00
|
|
|
{
|
2023-03-28 11:03:38 +00:00
|
|
|
log.FatalFormat("Test bench name: {0}", loginDlgBench.BenchName);
|
2016-04-07 11:52:57 +00:00
|
|
|
|
2023-03-28 11:03:38 +00:00
|
|
|
LoadingConfigStart();
|
|
|
|
|
TBF.DB.CurrentBench = LocalSettings.TestBenches[i].Clone() as DatabaseSettings;
|
|
|
|
|
TBF.DB.CreateSessionFactories(LocalSettings.TestBenches[i]);
|
2017-03-19 09:37:06 +00:00
|
|
|
|
2017-09-26 05:52:48 +00:00
|
|
|
Users.Entities.User loadedUser = null;
|
2014-07-28 07:54:35 +00:00
|
|
|
try
|
|
|
|
|
{
|
2019-07-24 13:38:49 +00:00
|
|
|
#if TURA_IPERL || TURA_IPERL_NEW || TURA_SPECIAL
|
2021-09-23 09:46:40 +00:00
|
|
|
GID[] reqGrpMembership = new Common.GID[] { Common.GID.Testers, Common.GID.TestingSpecialists, Common.GID.Metrologists, Common.GID.HeadOfLab };
|
2018-09-24 12:15:04 +00:00
|
|
|
#else
|
2019-12-03 19:57:37 +00:00
|
|
|
GID[] reqGrpMembership = null;
|
2018-09-24 12:15:04 +00:00
|
|
|
#endif
|
|
|
|
|
bool authorized = false;
|
2021-09-23 09:46:40 +00:00
|
|
|
AuthorizedAs authorizedAs = Common.AuthorizedAs.PowerUser;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2017-03-19 09:37:06 +00:00
|
|
|
if (Users.Entities.User.IsPowerUser(loginDlgBench.Alias, loginDlgBench.Password))
|
2015-02-19 14:50:31 +00:00
|
|
|
{
|
2017-03-19 09:37:06 +00:00
|
|
|
loadedUser = new Users.Entities.User(loginDlgBench.Alias, 6, true);
|
2023-03-28 11:03:38 +00:00
|
|
|
CurrentUser.Change(loadedUser, null);
|
|
|
|
|
CurrentUser.LastAuthorization = DateTime.Now;
|
|
|
|
|
CurrentUser.AuthorizedAs = Common.AuthorizedAs.PowerUser;
|
|
|
|
|
authorized = true;
|
2015-02-19 14:50:31 +00:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 09:16:08 +00:00
|
|
|
///
|
2017-03-19 09:37:06 +00:00
|
|
|
/// Authorisation using databases
|
2016-12-20 09:16:08 +00:00
|
|
|
///
|
2016-12-20 13:22:56 +00:00
|
|
|
if (!authorized)
|
|
|
|
|
{
|
2021-09-23 09:46:40 +00:00
|
|
|
authorizedAs = Common.AuthorizedAs.RemoteUser; /// Try remote DB first
|
2016-12-20 13:22:56 +00:00
|
|
|
|
2023-03-28 11:03:38 +00:00
|
|
|
foreach (var sf in TBF.DB.UserSessionFactories)
|
2016-12-20 13:22:56 +00:00
|
|
|
{
|
2017-03-19 09:37:06 +00:00
|
|
|
/// Load and authorise user from the UsersDB database
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-03-28 11:03:38 +00:00
|
|
|
|
|
|
|
|
if (sf != null)
|
2018-05-14 15:11:15 +00:00
|
|
|
{
|
2023-03-28 11:03:38 +00:00
|
|
|
var session = sf.OpenSession();
|
|
|
|
|
|
|
|
|
|
if (loginDlgBench.Password == LoginDlgWithBenchSelection.UseTheTagPassword)
|
|
|
|
|
{
|
|
|
|
|
loadedUser = Users.Entities.User.LoadUserByTag(session, loginDlgBench.Alias);
|
|
|
|
|
if (loadedUser != null) authorized = loadedUser.AuthorizeTag(loginDlgBench.Alias, reqGrpMembership, null);
|
|
|
|
|
}
|
|
|
|
|
else
|
2018-05-14 15:11:15 +00:00
|
|
|
{
|
2023-03-28 11:03:38 +00:00
|
|
|
switch (loginDlgBench.Method)
|
|
|
|
|
{
|
|
|
|
|
default:
|
|
|
|
|
case Common.LoginMethod.UserName:
|
|
|
|
|
loadedUser = Users.Entities.User.LoadUserByName(session, loginDlgBench.Alias);
|
|
|
|
|
if (loadedUser != null)
|
|
|
|
|
{
|
|
|
|
|
authorized = loadedUser.Authorize(session, loginDlgBench.Alias, loginDlgBench.Password, reqGrpMembership, null);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Common.LoginMethod.FullName:
|
|
|
|
|
loadedUser = Users.Entities.User.LoadUserByFullName(session, loginDlgBench.Alias);
|
|
|
|
|
if (loadedUser != null)
|
|
|
|
|
{
|
|
|
|
|
authorized = loadedUser.AuthorizeFullName(session, loginDlgBench.Alias, loginDlgBench.Password, reqGrpMembership, null);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Common.LoginMethod.Number:
|
|
|
|
|
int number;
|
|
|
|
|
if (!int.TryParse(loginDlgBench.Alias, out number)) break;
|
|
|
|
|
loadedUser = Users.Entities.User.LoadUserByNumber(session, number);
|
|
|
|
|
if (loadedUser != null)
|
|
|
|
|
{
|
|
|
|
|
authorized = loadedUser.AuthorizeNumber(session, number, loginDlgBench.Password, reqGrpMembership, null);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-05-14 15:11:15 +00:00
|
|
|
}
|
2023-03-28 11:03:38 +00:00
|
|
|
|
|
|
|
|
session.Close();
|
2018-05-14 15:11:15 +00:00
|
|
|
}
|
2023-03-28 11:03:38 +00:00
|
|
|
}
|
2017-03-19 09:37:06 +00:00
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
authorized = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (authorized) break;
|
|
|
|
|
|
2021-09-23 09:46:40 +00:00
|
|
|
authorizedAs = Common.AuthorizedAs.LocalUser; /// Try local DB afterwards
|
2016-12-20 13:22:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
if (authorized)
|
|
|
|
|
{
|
2017-09-26 05:52:48 +00:00
|
|
|
loadedUser.SetLegalizator(loginDlgBench.Legalizator);
|
2022-05-23 08:38:14 +00:00
|
|
|
Users.CurrentUser.AuthorizedAs = authorizedAs;
|
2017-03-19 09:37:06 +00:00
|
|
|
|
|
|
|
|
LocalSettings.LoginMethod = loginDlgBench.Method; /// Save the login method actually used
|
2016-12-21 08:27:25 +00:00
|
|
|
|
2016-08-18 09:38:14 +00:00
|
|
|
if (LocalSettings.TestBenches[i].IsRealBench)
|
|
|
|
|
{
|
|
|
|
|
/// This is a real bench ==> check if another instance is running
|
|
|
|
|
string processName = System.IO.Path.GetFileNameWithoutExtension(thisAssembly.Location);
|
|
|
|
|
if (System.Diagnostics.Process.GetProcessesByName(processName).Length > 1)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(Strings.Another_instance_is_running, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.InfoFormat("Password accepted, bench '{0}' parameters loaded.",
|
2015-12-04 16:17:20 +00:00
|
|
|
LocalSettings.TestBenches[i].BenchName);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
/// Copy the selected bench settings to CurrentBench.
|
|
|
|
|
/// Clone() guarantees that current bench settings wont be modified
|
|
|
|
|
/// when user modifies the database settings in DatabaseSettingsDlg.
|
2023-03-28 11:03:38 +00:00
|
|
|
if (TBF.DB.CurrentBench == null)
|
|
|
|
|
{
|
|
|
|
|
TBF.DB.CurrentBench = LocalSettings.TestBenches[i].Clone() as DatabaseSettings;
|
|
|
|
|
TBF.DB.CreateSessionFactories(TBF.DB.CurrentBench);
|
|
|
|
|
}
|
2021-09-19 20:04:16 +00:00
|
|
|
Results.DB.DbType = TBF.DB.CurrentBench.WaterMetersDBSettings.DbType;
|
|
|
|
|
Results.DB.ConnectionString = TBF.DB.CurrentBench.WaterMetersDBSettings.ConnectionString;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
/// Save the selection to local settings
|
2021-09-19 20:04:16 +00:00
|
|
|
LocalSettings.LastBenchName = TBF.DB.CurrentBench.BenchName;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
2020-03-05 12:59:43 +00:00
|
|
|
///
|
|
|
|
|
/// Connect to Config database.
|
|
|
|
|
/// This triggers an exception in case user is a power user and there is no Config database.
|
2022-06-07 09:58:53 +00:00
|
|
|
///
|
2023-03-28 11:03:38 +00:00
|
|
|
var session = TBF.DB.ConfigDBSessionFactory.OpenSession();
|
2022-06-07 09:58:53 +00:00
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(LocalSettings.LastProcedureName))
|
|
|
|
|
{
|
|
|
|
|
listOfProcedures = session.QueryOver<Procedure>()
|
|
|
|
|
.Where(x => (x.ProcedureState == ProcedureState.Active))
|
|
|
|
|
.And(x => (x.Name == LocalSettings.LastProcedureName))
|
|
|
|
|
.List();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Search for 'BenchInfo' component
|
|
|
|
|
var components = session.QueryOver<Config.Entities.Component>()
|
|
|
|
|
.Where(x => x.ClassName == "BenchInfoEx")
|
|
|
|
|
.List();
|
|
|
|
|
if (components.Count > 0)
|
|
|
|
|
{
|
2023-03-28 11:03:38 +00:00
|
|
|
var factory = new TBF.Rig.DataContainers.BenchInfo.Extended.ComponentFactory();
|
2022-06-07 09:58:53 +00:00
|
|
|
var cfg = factory.CmpntCfgFromCmpntEntity(components[0])
|
2023-03-28 11:03:38 +00:00
|
|
|
as TBF.Rig.DataContainers.BenchInfo.Extended.ComponentCfg;
|
2022-06-07 09:58:53 +00:00
|
|
|
ProcessData.BenchInfo = new TBF.Rig.DataContainers.BenchInfo.Extended.Component(cfg);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/// Search for 'iPerlBenchInfo' component
|
|
|
|
|
components = session.QueryOver<Config.Entities.Component>()
|
|
|
|
|
.Where(x => x.ClassName == "BenchInfo.iPerl")
|
|
|
|
|
.List();
|
|
|
|
|
if (components.Count > 0)
|
|
|
|
|
{
|
2023-03-28 11:03:38 +00:00
|
|
|
var factory = new TBF.Rig.DataContainers.BenchInfo.iPerl.ComponentFactory();
|
2022-06-07 09:58:53 +00:00
|
|
|
var cfg = factory.CmpntCfgFromCmpntEntity(components[0])
|
2023-03-28 11:03:38 +00:00
|
|
|
as TBF.Rig.DataContainers.BenchInfo.iPerl.ComponentCfg;
|
2022-06-07 09:58:53 +00:00
|
|
|
ProcessData.BenchInfo = new TBF.Rig.DataContainers.BenchInfo.iPerl.Component(cfg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
session.Close();
|
2020-03-05 12:59:43 +00:00
|
|
|
|
|
|
|
|
///
|
|
|
|
|
/// Connect to Results database and determine the last saved batch number.
|
|
|
|
|
///
|
2023-03-28 11:03:38 +00:00
|
|
|
var rsltDBSession = TBF.DB.ResultsDBSessionFactory.OpenSession();
|
|
|
|
|
Results.DB.LoadSharedData(rsltDBSession);
|
|
|
|
|
int maxBatchNr = Results.DB.GetMaxSavedBatchNr(rsltDBSession);
|
2021-09-19 20:04:16 +00:00
|
|
|
if (!TBF.DB.CurrentBench.IsRealBench || Program.LocalSettings.BatchNr <= maxBatchNr)
|
2017-10-31 08:45:29 +00:00
|
|
|
{
|
|
|
|
|
log.FatalFormat("Max. BatchNr in DB = {0}, LocalSettings.BatchNr = {1}", maxBatchNr, Program.LocalSettings.BatchNr);
|
|
|
|
|
Program.LocalSettings.BatchNr = maxBatchNr + 1;
|
|
|
|
|
Program.LocalSettings.Save();
|
|
|
|
|
log.FatalFormat("LocalSettings.BatchNr updated to {0}", Program.LocalSettings.BatchNr);
|
|
|
|
|
}
|
2023-03-28 11:03:38 +00:00
|
|
|
if (rsltDBSession != null && rsltDBSession.IsOpen) rsltDBSession.Close();
|
2015-12-31 23:00:03 +00:00
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
retryLogin = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (retryLogin)
|
|
|
|
|
{
|
|
|
|
|
/// Close Fluent NHibernate
|
|
|
|
|
MessageBox.Show(Strings.Invalid_user_password_or_bench, Strings.Error, MessageBoxButtons.OK);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-16 11:06:48 +00:00
|
|
|
catch (Exception exc)
|
2014-07-28 07:54:35 +00:00
|
|
|
{
|
2017-05-16 11:06:48 +00:00
|
|
|
/// Database connect failed, create a log
|
|
|
|
|
log.FatalFormat("Connection to database failed: {0}", exc.Message);
|
|
|
|
|
if (exc.InnerException != null && !string.IsNullOrEmpty(exc.InnerException.Message))
|
|
|
|
|
{
|
|
|
|
|
log.FatalFormat("Inner exception: {0}", exc.InnerException.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Ask what to do
|
2019-09-16 08:51:25 +00:00
|
|
|
switch ((new NoBenchOrDatabaseDlg { Message = Strings.NoDatabaseMsg, ShowRetry = true }).ShowDialog())
|
2014-07-28 07:54:35 +00:00
|
|
|
{
|
2017-10-31 08:45:29 +00:00
|
|
|
case DialogResult.Abort:
|
|
|
|
|
return; /// Exit program
|
|
|
|
|
|
|
|
|
|
case DialogResult.Retry:
|
|
|
|
|
break; /// Retry connection to the database, stay inside the loop
|
|
|
|
|
|
|
|
|
|
case DialogResult.Yes:
|
2019-09-16 08:51:25 +00:00
|
|
|
new TBF.UI.Settings.UpgradeSelectionDlg().ShowDialog();
|
2017-10-31 08:45:29 +00:00
|
|
|
break; /// Stay inside the loop
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
default:
|
|
|
|
|
/// Open 'DatabaseSetingsDlg' and retry DB connect on OK
|
2023-03-28 11:03:38 +00:00
|
|
|
if (new Users.Forms.LoginDlg().ShowDialog() == DialogResult.Cancel ||
|
2019-09-16 08:51:25 +00:00
|
|
|
new TBF.UI.Settings.BenchesDlg().ShowDialog() == DialogResult.Cancel)
|
2014-07-28 07:54:35 +00:00
|
|
|
{
|
2017-10-31 08:45:29 +00:00
|
|
|
return; /// Exit program
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
2017-10-31 08:45:29 +00:00
|
|
|
if (Program.LocalSettings == null)
|
|
|
|
|
return; /// Fatal error -> Exit program
|
|
|
|
|
else
|
|
|
|
|
break; /// Stay inside the loop
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} // if benchname is OK
|
|
|
|
|
} // for all benches in local settings loop
|
|
|
|
|
} // not Canceled
|
|
|
|
|
} //do
|
|
|
|
|
while (retryLogin);
|
|
|
|
|
|
|
|
|
|
///
|
|
|
|
|
/// Load the last procedure or create a new, default one
|
|
|
|
|
///
|
|
|
|
|
if (LocalSettings.LastProcedureName != null)
|
|
|
|
|
{
|
2023-03-28 11:03:38 +00:00
|
|
|
using (var session = TBF.DB.ConfigDBSessionFactory.OpenSession())
|
2014-07-28 07:54:35 +00:00
|
|
|
{
|
2023-03-28 11:03:38 +00:00
|
|
|
var listOfProcedures = session.QueryOver<Procedure>()
|
|
|
|
|
.Where(x => (x.ProcedureState == Common.ProcedureState.Active))
|
|
|
|
|
.And(x => (x.Name == LocalSettings.LastProcedureName))
|
|
|
|
|
.List();
|
|
|
|
|
|
|
|
|
|
if (listOfProcedures.Count == 1)
|
|
|
|
|
{
|
|
|
|
|
log.InfoFormat("Pre-selecting the procedure {0}", LocalSettings.LastProcedureName);
|
|
|
|
|
SelectedProcedure = listOfProcedures[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
session.Close();
|
2014-07-28 07:54:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-28 20:36:33 +00:00
|
|
|
try
|
2023-03-28 11:03:38 +00:00
|
|
|
{
|
|
|
|
|
/// Open the main application window
|
2015-02-28 20:36:33 +00:00
|
|
|
log.Info("Creating the main window");
|
2019-09-16 08:51:25 +00:00
|
|
|
MainWnd = new UI.MainWnd();
|
2015-02-28 20:36:33 +00:00
|
|
|
log.Info("Opening the main window");
|
2023-03-28 11:03:38 +00:00
|
|
|
LoadingConfigEnd();
|
|
|
|
|
Application.Run(MainWnd);
|
2015-02-28 20:36:33 +00:00
|
|
|
log.Info("The main window was closed");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
LogException(log, "Exception in Application.Run(MainWnd)", e);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
/// Save local settings
|
|
|
|
|
LocalSettings.Save();
|
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
/// Close Fluent NHibernate
|
|
|
|
|
/// Todo
|
|
|
|
|
|
|
|
|
|
log.Info("Exiting application.");
|
|
|
|
|
}
|
2015-02-28 20:36:33 +00:00
|
|
|
|
|
|
|
|
static void MyHandler(object sender, UnhandledExceptionEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
LogException(log, "Unhandled exception", (Exception)args.ExceptionObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void LogException(ILog log, string description, Exception e)
|
|
|
|
|
{
|
|
|
|
|
log.FatalFormat("---------------( {0} )---------------", description);
|
|
|
|
|
log.FatalFormat("Message : {0}", e.Message);
|
|
|
|
|
if (e.InnerException != null)
|
|
|
|
|
{
|
|
|
|
|
log.FatalFormat("InnerMessage : {0}", e.InnerException.Message);
|
|
|
|
|
}
|
|
|
|
|
log.FatalFormat("StackTrace : {0}{1}", Environment.NewLine, e.StackTrace);
|
|
|
|
|
log.Fatal("--------------------------------------");
|
|
|
|
|
}
|
2023-03-28 11:03:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
static UI.Shared.ModelessActivityForm form;
|
|
|
|
|
static System.Threading.Thread formThread;
|
|
|
|
|
|
|
|
|
|
static void LoadingConfigStart()
|
|
|
|
|
{
|
|
|
|
|
form = new UI.Shared.ModelessActivityForm()
|
|
|
|
|
{
|
|
|
|
|
Message = Strings.Starting_system,
|
|
|
|
|
FontFamily = "Arial",
|
|
|
|
|
FontSize = 24,
|
|
|
|
|
FontStyle = FontStyle.Regular,
|
|
|
|
|
BackgroundColor = Color.LightBlue,
|
|
|
|
|
};
|
|
|
|
|
formThread = new System.Threading.Thread(() => form.ShowDialog());
|
|
|
|
|
formThread.Start();
|
|
|
|
|
System.Threading.Thread.Sleep(500);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void LoadingConfigEnd()
|
|
|
|
|
{
|
|
|
|
|
if (form != null && formThread != null)
|
|
|
|
|
{
|
|
|
|
|
form.CloseForm(null, new EventArgs());
|
|
|
|
|
formThread.Join();
|
|
|
|
|
}
|
|
|
|
|
form = null;
|
|
|
|
|
formThread = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|