(1) TBF program shutdown modifications, (2) Obligatory tank draining before TBF exit, (3) Shutdown PC after TBF exit option, ver. 2.18.1156

This commit is contained in:
Milan Hanajik
2019-03-05 12:28:58 +01:00
parent b10ab44fa9
commit 771af696ae
15 changed files with 773 additions and 400 deletions
+160 -186
View File
@@ -1,5 +1,5 @@
///
/// Copyright (c) 2013-2018 Sensus Slovensko a.s.
/// Copyright (c) 2013-201 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
@@ -25,14 +25,19 @@ namespace TBF
public UiControls.BenchControlPanel BenchControlPanel;
public ProcedureInfo SelectedProcedure;
public Procedure CurrentProcedure;
public bool CycleRunning;
public bool IsShutdownDisabled;
public bool IsShutdownPCAfterClosingTbf;
/// <summary>
/// This dialog is shown when emergency stop is activated
/// </summary>
TBF.Forms.EmergencyStopForm emergencyStopModelessDlg;
bool benchInitializationFailed;
/// <summary>
/// This dialog is shown when TBF program is shutting down
/// </summary>
TBF.Forms.ModelessActivityForm closingTbfMessageForm;
TestProgressControls testProgressControls;
/// <summary>
@@ -119,11 +124,12 @@ namespace TBF
iPerlHeadsToolStripMenuItem.Visible = false;
#endif
CycleRunning = true;
IsShutdownDisabled = true;
IsShutdownPCAfterClosingTbf = false;
benchInitializationFailed = false;
BenchControl.StateMachine.MachineState = Config.Data.CurrentBench.IsRealBench ? BenchControl.MachineState.StartingUp : BenchControl.MachineState.Disabled;
///
if (Config.Data.CurrentBench.IsRealBench)
if (BenchControl.StateMachine.MachineState == BenchControl.MachineState.StartingUp)
{
try
{
@@ -146,7 +152,7 @@ namespace TBF
}
catch (Exception exc)
{
benchInitializationFailed = true;
BenchControl.StateMachine.MachineState = BenchControl.MachineState.FailedToStart;
string errMsg;
if (string.IsNullOrEmpty(BenchControl.TbfComponents.CurrentlyLoadedComponentName))
@@ -197,9 +203,8 @@ namespace TBF
Left = (ls.MainWndLeft != 0) ? ls.MainWndLeft : 75;
Top = (ls.MainWndTop != 0) ? ls.MainWndTop : 5;
}
if (Config.Data.CurrentBench.IsRealBench && !benchInitializationFailed)
if (TBF.BenchControl.StateMachine.MachineState == TBF.BenchControl.MachineState.StartingUp)
{
var form = new TBF.Forms.ModelessActivityForm()
{
@@ -264,7 +269,7 @@ namespace TBF
}
catch (Exception exc)
{
benchInitializationFailed = true;
TBF.BenchControl.StateMachine.MachineState = TBF.BenchControl.MachineState.FailedToStart;
form.CloseForm(null, new EventArgs());
formThread.Join();
@@ -320,21 +325,18 @@ namespace TBF
UpdateUser();
mainTabControl.SelectTab((int)MainTabPageId.Hydraulics);
if (Config.Data.CurrentBench.IsRealBench)
if (TBF.BenchControl.StateMachine.MachineState == TBF.BenchControl.MachineState.StartingUp)
{
if (!benchInitializationFailed)
{
BenchControl.StateMachine.Start();
log.Info("Test bench started");
}
else
{
BenchControlPanel.OnButtonsEtc(null, new ButtonsEtcEventArgs(TBF.UiBridge.ButtonsEtc.None));
MessageBox.Show(Strings.Bench_is_not_running,
Strings.Warning,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Exclamation);
}
BenchControl.StateMachine.Start();
log.Info("Test bench started");
}
else if (TBF.BenchControl.StateMachine.MachineState == TBF.BenchControl.MachineState.FailedToStart)
{
BenchControlPanel.OnButtonsEtc(null, new ButtonsEtcEventArgs(ButtonsEtc.None));
MessageBox.Show(Strings.Bench_is_not_running,
Strings.Warning,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Exclamation);
}
}
@@ -402,23 +404,48 @@ namespace TBF
mainProgressBar.Value = 0;
}
/// <summary>
/// This handler is onvoked when state of buttons changes
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
void OnButtonsEtc(object sender, UiBridge.ButtonsEtcEventArgs args)
{
if ((args.Flags & ButtonsEtc.Shutdown) != 0)
{
///
/// TBF program is shutting down and the state machine was proparly stopped
///
UI2Settings();
if (closingTbfMessageForm != null) closingTbfMessageForm.CloseForm(this, null);
if (IsShutdownPCAfterClosingTbf)
{
var psi = new System.Diagnostics.ProcessStartInfo("shutdown", "/s /t 0");
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
System.Diagnostics.Process.Start(psi);
}
Close();
return;
}
if (((args.Flags & ButtonsEtc.ShowBenchEmpty) != 0) ||
((args.Flags & ButtonsEtc.ShowBenchFilled) != 0))
{
return;
}
CycleRunning = ((args.Flags & UiBridge.ButtonsEtc.ProcedureCmbBoxEn) == 0);
procedureComboBox.Enabled = ((args.Flags & ButtonsEtc.ProcedureCmbBoxEn) != 0);
iPerlHeadsToolStripMenuItem.Enabled = ((args.Flags & ButtonsEtc.ProcedureCmbBoxEn) != 0);
procedureComboBox.Enabled = !CycleRunning;
iPerlHeadsToolStripMenuItem.Enabled = !CycleRunning;
if (!CycleRunning && ProceduresUpdated)
if (procedureComboBox.Enabled && ProceduresUpdated)
{
ReloadProcedureComboBoxItems();
}
IsShutdownDisabled = ((args.Flags & ButtonsEtc.ProcedureCmbBoxEn) == 0);
}
public void UpdateUser()
@@ -607,37 +634,63 @@ namespace TBF
}
}
private void MainWnd_FormClosing(object sender, FormClosingEventArgs e)
/// <summary>
/// Save UI settings to Program.LocalSettings and save them to a file.
/// UI settings are:
/// MainWnd size and location and splitter distances.
/// </summary>
void UI2Settings()
{
Program.LocalSettings.RightPaneHorizSplitterDistance = rightHorizSplitContainer.SplitterDistance;
Program.LocalSettings.TopPaneVerticalSplitterDistance = topVerticalSplitContainer.SplitterDistance;
LocalSettings ls = Program.LocalSettings;
Program.LocalSettings.ManiWndMaximized = (WindowState == FormWindowState.Maximized);
if (WindowState == FormWindowState.Normal)
if (ls != null)
{
Program.LocalSettings.MainWndLeft = Location.X;
Program.LocalSettings.MainWndTop = Location.Y;
Program.LocalSettings.MainWndWidth = Size.Width;
Program.LocalSettings.MainWndHeight = Size.Height;
}
else
{
/// Maximized or minimized (when restarted, minimized window is restored as normal)
Program.LocalSettings.MainWndLeft = RestoreBounds.Left;
Program.LocalSettings.MainWndTop = RestoreBounds.Top;
Program.LocalSettings.MainWndWidth = RestoreBounds.Width;
Program.LocalSettings.MainWndHeight = RestoreBounds.Height;
}
Program.LocalSettings.Save();
ls.RightPaneHorizSplitterDistance = rightHorizSplitContainer.SplitterDistance;
ls.TopPaneVerticalSplitterDistance = topVerticalSplitContainer.SplitterDistance;
if (DialogResult == DialogResult.None)
{
exitToolStripMenuItem_Click(sender, e);
ls.ManiWndMaximized = (WindowState == FormWindowState.Maximized);
if (WindowState == FormWindowState.Normal)
{
ls.MainWndLeft = Location.X;
ls.MainWndTop = Location.Y;
ls.MainWndWidth = Size.Width;
ls.MainWndHeight = Size.Height;
}
else
{
/// Maximized or minimized (when restarted, minimized window is restored as normal)
ls.MainWndLeft = RestoreBounds.Left;
ls.MainWndTop = RestoreBounds.Top;
ls.MainWndWidth = RestoreBounds.Width;
ls.MainWndHeight = RestoreBounds.Height;
}
ls.Save();
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
bool TryShutdownTBF()
{
if (TBF.BenchControl.StateMachine.MachineState == TBF.BenchControl.MachineState.Undefined ||
TBF.BenchControl.StateMachine.MachineState == TBF.BenchControl.MachineState.Disabled ||
TBF.BenchControl.StateMachine.MachineState == TBF.BenchControl.MachineState.FailedToStart ||
TBF.BenchControl.StateMachine.MachineState == TBF.BenchControl.MachineState.Off)
{
///
/// StateMachine is not running --> Shutdown immediately without displaying a message box
///
if (closingTbfMessageForm != null) closingTbfMessageForm.CloseForm(this, null);
return true;
}
if (TBF.BenchControl.StateMachine.MachineState == TBF.BenchControl.MachineState.ShuttingDown)
{
///
/// Already shutting down --> Prevent a new shutdown without displaying a message box
///
return false;
}
/// Save emergency stop form state
bool oriEmgStopState = false;
if (emergencyStopModelessDlg != null)
{
@@ -645,155 +698,76 @@ namespace TBF
emergencyStopModelessDlg.Visible = false;
}
if (BenchControl.StateMachine.Running && (messageLabel.ForeColor != Color.Red) && CycleRunning)
if ((messageLabel.ForeColor != Color.Red) && IsShutdownDisabled)
{
///
/// It is not possible to shutdown (most likely a cycle is not completed) --> Display a message and prevent shutdown now
///
MessageBox.Show(Strings.Finish_the_session_please, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
if (e is FormClosingEventArgs)
{
(e as FormClosingEventArgs).Cancel = true;
}
return;
if (emergencyStopModelessDlg != null) emergencyStopModelessDlg.Visible = oriEmgStopState;
return false;
}
if (DialogResult.Yes == MessageBox.Show(Strings.Do_you_want_to_exit_TBF,
Strings.Closing_the_program,
MessageBoxButtons.YesNo,
MessageBoxIcon.Question))
Forms.ClosingTbfForm closingForm = new Forms.ClosingTbfForm();
DialogResult dr = closingForm.ShowDialog();
if (dr == DialogResult.Yes)
{
if (BenchControl.StateMachine.Running)
///
/// User confirmed TBF program shutdown --> 1. Show modeless form, 2. Stop MainSeq and StateMachine, 3. Prevent TBF exit now
///
IsShutdownPCAfterClosingTbf = closingForm.ShutDownPC;
/// Show 'Closing_Test_Bench_Framework' modeless window in separate thread
closingTbfMessageForm = new TBF.Forms.ModelessActivityForm()
{
/// Show 'Closing_Test_Bench_Framework' modeless window in separate thread
var form = new TBF.Forms.ModelessActivityForm()
{
Message = Strings.Closing_Test_Bench_Framework,
FontFamily = "Arial",
FontSize = 24,
FontStyle = FontStyle.Regular,
BackgroundColor = Color.PeachPuff,
StartActivityHandler = true,
};
Thread formThread = new Thread(() => form.ShowDialog());
formThread.Start();
Message = Strings.Closing_Test_Bench_Framework,
FontFamily = "Arial",
FontSize = 24,
FontStyle = FontStyle.Regular,
BackgroundColor = Color.PeachPuff,
StartActivityHandler = true,
};
Thread formThread = new Thread(() => closingTbfMessageForm.ShowDialog());
formThread.Start();
/// Stop the system
BenchControl.StateMachine.Stop();
while (BenchControl.StateMachine.Running) { } /// wait until the last StopOperaions() completes
BenchControl.StateMachine.StopDevices();
BenchControl.StateMachine.StopDevices2();
UiBridge.Bridge.OnEmergencyStopChanged(this, UiBridge.EmergencyStopEventArgs.State.CloseForm);
/// Stop MainSeq and StateMachine
Bridge.Ui2Bench(UI2BenchCmd.Shutdown);
/// Close 'Closing_Test_Bench_Framework' modeless window
form.CloseForm(null, new EventArgs());
formThread.Join();
}
/// Close the main window
DialogResult = DialogResult.OK;
Close();
return;
/// Prevent TBF program exit now
return false;
}
else if (e is FormClosingEventArgs)
else
{
(e as FormClosingEventArgs).Cancel = true;
/// User aborted the shutdown --> Restore emergency stop form state and prevent shutdown now
if (emergencyStopModelessDlg != null) emergencyStopModelessDlg.Visible = oriEmgStopState;
return false;
}
if (emergencyStopModelessDlg != null) emergencyStopModelessDlg.Visible = oriEmgStopState;
}
private void MainWnd_FormClosing(object sender, FormClosingEventArgs e)
{
if (TryShutdownTBF())
{
UI2Settings(); /// Save settings and exit TBF
}
else
{
e.Cancel = true; /// Prevent TBF exit
}
}
//private void printToolStripMenuItem_Click(object sender, EventArgs e)
//{
//TBF.Forms.PrintOrderForm dlg = new TBF.Forms.PrintOrderForm();
//if (dlg.ShowDialog() == DialogResult.OK)
//{
// string purchaseOrder = dlg.PurchaseOrder;
// string tester = dlg.Tester;
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
if (TryShutdownTBF())
{
/// Save settings and exit TBF
UI2Settings();
Close();
}
}
// NHibernate.ISession session = Config.FluentCommon.CreateSession(Users.Entities.DBKind.Config);
// IList<TestResult> testResults = session.QueryOver<TestResult>()
// .Where(x => (x.PurchaseOrder == purchaseOrder))
// .List();
// if (testResults.Count == 0)
// {
// MessageBox.Show(Strings.No_results_to_print, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
// return;
// }
// if (dlg.OnlyGoodResults)
// {
// /// Proceed from the end of the list so that items can be removed by index
// for (int i = testResults.Count - 1; i >= 0; i--)
// {
// // TODO: Complete
// }
// }
// if (dlg.OnlyLastResult)
// {
// /// Remove test results if there exist a later test results for the same sn
// /// Proceed from the end of the list so that items can be removed by index
// for (int i = testResults.Count - 1; i >= 0; i--)
// {
// string name = testResults[i].Name;
// int batchNr = testResults[i].BatchNr;
// foreach (var tr in testResults)
// {
// if (tr.Name == name && tr.BatchNr > batchNr)
// {
// bool allWMsAreTheSame = true;
// int cnt = Math.Min(testResults[i].Meters.Count, tr.Meters.Count);
// for (int j = 0; j < cnt; j++)
// {
// if (testResults[i].Meters[j].SerialNr != tr.Meters[j].SerialNr)
// {
// allWMsAreTheSame = false;
// break;
// }
// }
// if (allWMsAreTheSame)
// {
// /// Remove as there exists a more recent measurement with the same meters
// testResults.RemoveAt(i);
// break;
// }
// }
// }
// }
// }
// IList<Procedure> procedure = session.QueryOver<Procedure>()
// .Where(x => (x.ProcedureState == ProcedureState.Active))
// .And(x => (x.Name == testResults[0].ProcedureName))
// .List();
// if (procedure.Count != 1)
// {
// MessageBox.Show(Strings.No_results_to_print, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
// return;
// }
// if ((testResults != null) && (testResults.Count != 0) && (testResults[0].MetersKind == MetersKind.Single))
// {
// PrintOrderDocument doc = new PrintOrderDocument(procedure[0], testResults, tester);
// doc.DefaultPageSettings.Landscape = true;
// doc.Print();
// }
// else if((testResults != null) && (testResults.Count != 0) && (testResults[0].MetersKind == MetersKind.Combined))
// {
// /// Do nothing right now
// }
// else
// {
// MessageBox.Show(Strings.No_results_to_print, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
// return;
// }
//}
//}
private void upgradeTSMenuItem_Click(object sender, EventArgs e)
{