SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
SECI_GUI.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Xml;
9 using System.Collections;
10 using System.IO;
11 using System.Runtime.Remoting;
12 using System.Runtime.Remoting.Channels;
13 using System.Runtime.Remoting.Channels.Tcp;
14 using System.Runtime.Remoting.Channels.Http;
15 using System.Threading;
16 
17 namespace Sample_Environment_Control_Interface
18 {
19  public partial class SECI_GUI : Form
20  {
21  //Members
22  private Seci.SerialisableList<Seci.LabView.MessageInfo> _lvErrorMessages = new Seci.SerialisableList<Seci.LabView.MessageInfo>();
24  private static Boolean _manager;
25  private Boolean _closing = false;
26  private static GUILayout _currentLayout = new GUILayout();
27  private Dialogs.Help.HelpForm help; //Help form, should only have one open at a time
28  private Boolean _helpOpen = false;
29  private Boolean _changesMade;
30 
31  //Properties
32  public static Dialogs.Messages.SeciMessages errorForm { get { return _errorForm; } set { _errorForm = value; } }
33  public static GUILayout CurrentLayout { get { return _currentLayout; } set { _currentLayout = value; } }
34 
35  //Delegates
36  private delegate void UpdateGuiDelegate();
37  private delegate void OpenConfigRemotelyDelegate(String filename, Boolean checkVIs);
38 
39  #region "GUI Constructor, initialisation and closing"
40 
41  public SECI_GUI()
42  {
43  GeneralSplashScreen.ShowSplashScreen("The Sample Environment Control Interface is loading...");
44 
46 
47  //Force initialisation of error form, this is to allow errors to be posted.
48  //Otherwise it will complain unless the form have been opened before the error
49  //was sent.
50  IntPtr dummyHandle = _errorForm.Handle;
51 
52  //Load gui app settings
53  GuiSettings.LoadSettings();
54 
55  //Add handler for remoting request for configuration change
56  Seci.SeciInterface.ConfigChangeRequested += new Seci.SeciInterface.RemotingEventHandler(Interface_ConfigChangeRequested);
57 
58  //Set up directories
59  Seci.SeciInterface.Initialise(Environment.CurrentDirectory);
60 
61  //Set up event handler to listen for a question being asked and answered in the LabView messages control
62  _errorForm.lvErrors.QuestionAsked += new Dialogs.Messages.LabView_Errors.QuestionHandler(Pause_Timers);
63  _errorForm.lvErrors.QuestionAnswered += new Dialogs.Messages.LabView_Errors.QuestionHandler(Resume_Timers);
64 
65  //Set status label to nothing
66  toolStripStatusLabel.Text = "";
67 
68  //Set up the GUI
70  }
71 
72  private void Interface_ConfigChangeRequested(string[] vars)
73  {
74  Object[] args = { vars[0], false };
75 
76  Invoke(new OpenConfigRemotelyDelegate(OpenConfig), args);
77  }
78 
79  private void Form1_Load(object sender, EventArgs e)
80  {
81  //First Check that LabView is not running
82  if (!labViewRunning())
83  {
84  //Manager logged out
85  _manager = false;
86 
87  //If using DAE monitor vi - allow its visible to be set
88  showHideDAEMonitorToolStripMenuItem.Visible = Seci.SeciInterface.SeciStatus.UseDaeMonitorVi;
89 
90  //Start LabVIEW and the standard VIs
91  Seci.SeciInterface.LV_Initialise(true);
92 
93  //Enable remoting
94  //Seci.SeciInterface.EnableDotNetRemoting();
95 
96  //Start the update and error timers
97  Start_Timers();
98 
99  GeneralSplashScreen.CloseForm();
100  this.Opacity = 1;
101  }
102  else
103  {
104  //if labview is running just exit
105  this.DialogResult = DialogResult.Cancel;
106  Application.Exit();
107  return;
108  }
109 
110  //Start Open GENIE
111  Seci.SeciInterface.OpenGenie_Start();
112 
113  Seci.SeciInterface.Messages_LogMessage("SeciGui", "SECI started");
114 
115  if (Sample_Environment_Control_Interface.Properties.Settings.Default.Crashed)
116  {
117  if (MessageBox.Show("SECI closed unexpectedly, do you wish to recover the last setup?", "Crash Recovery", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
118  {
119  Seci.SeciInterface.Messages_LogMessage("SeciGui", "Recovering last setup.");
120  OpenConfig(Seci.SeciInterface.SeciStatus.ConfigDir + "autosave", true);
121  Properties.Settings.Default.Crashed = false;
122  Properties.Settings.Default.Save();
123  _changesMade = true;
124  }
125  }
126  }
127 
128  private Boolean labViewRunning()
129  {
130  System.Diagnostics.Process[] procList;
131 
132  procList = System.Diagnostics.Process.GetProcessesByName("LabView");
133 
134  if (procList.GetLength(0) > 0)
135  {
136  if (MessageBox.Show("It appears that LabVIEW is running - SECI cannot start if LabVIEW is already running.\n\nWould you like SECI to close LabVIEW and continue loading? \n\nNote: any unsaved changes to VI's will be lost.", "LabVIEW Running!", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
137  {
138  try
139  {
140  procList[0].Kill();
141 
142  return false;
143  }
144  catch
145  {
146  }
147  }
148 
149  return true;
150  }
151 
152  return false;
153  }
154 
155  private void Form1_FormClosing(object sender, FormClosingEventArgs e)
156  {
157  //If the user has clicked "No" on start-up to restarting LabVIEW
158  //then we do not ask if he is sure.
159  //Likewise, if it is closing for an unhandled exception we do not ask.
160  if (this.DialogResult != DialogResult.Cancel && Properties.Settings.Default.Crashed != true)
161  {
162  if (_changesMade || Seci.SeciInterface.Assoc_AnyModifications())
163  {
164  if (MessageBox.Show("There are unsaved changes to the configuration. Are you sure you wish to close SECI?", "Unsaved Changes!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
165  {
166  e.Cancel = true;
167  return;
168  }
169  }
170  else
171  {
172  if (MessageBox.Show("Close SECI?", "Close?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
173  {
174  e.Cancel = true;
175  return;
176  }
177  }
178  }
179 
180  //Form is closing
181  _closing = true;
182 
183  //Stop the timers
184  Pause_Timers();
185 
186  //Close the configuration
187  Seci.SeciInterface.Configuration_Close();
188 
189  Seci.SeciInterface.Messages_LogMessage("SeciGui", "User closed SECI");
190  }
191 
192  #endregion
193 
194  #region "Tool bar"
195 
196  private void toolStripLabel_DoubleClick(object sender, EventArgs e)
197  {
198  _errorForm.Show();
199  }
200 
201  #endregion
202 
203  #region "Timers"
204 
205  private void Start_Timers()
206  {
207  Seci.SeciInterface.Threads_Start(true, true);
208  Seci.SeciInterface.OnValuesUpdated += new EventHandler(SeciInterface_ValuesUpdated);
209  Seci.SeciInterface.OnNewLVMessage += new EventHandler(SeciInterface_OnNewLVMessage);
210  }
211 
212  private void SeciInterface_OnNewLVMessage(object sender, EventArgs e)
213  {
214  try
215  {
216  if (!_closing) //If the form is closing then don't update
217  {
218  Invoke(new UpdateGuiDelegate(UpdateErrorsGUI));
219  }
220  }
221  catch (Exception err)
222  {
223  WriteError("SeciInterface_OnNewLVMessage", err);
224  }
225  }
226 
227  private void SeciInterface_ValuesUpdated(object sender, EventArgs e)
228  {
229  try
230  {
231  if (!_closing) //If the form is closing then don't update
232  {
233  Invoke(new UpdateGuiDelegate(UpdateMainGUI));
234  }
235  }
236  catch (Exception err)
237  {
238  WriteError("SeciInterface_ValuesUpdated", err);
239  }
240  }
241 
242  public static void Pause_Timers()
243  {
244  Seci.SeciInterface.Threads_Pause();
245 
246  //Wait a second to give threads fired off time to finish
247  System.Threading.Thread.Sleep(1000);
248  }
249 
250  public static void Resume_Timers()
251  {
252  Seci.SeciInterface.Threads_Resume();
253  }
254 
255  #endregion
256 
257  #region "Background Save"
258 
259  private void backgroundSave_DoWork(object sender, DoWorkEventArgs e)
260  {
261  if (Seci.SeciInterface.IsAComponent)
262  {
263  Seci.SeciInterface.Configuration_Save(_manager, false);
264  }
265  else
266  {
267  Seci.SeciInterface.Configuration_Save(_manager, false);
268 
269  //If it is a config then save the GUI layout
270  _currentLayout.Save(Seci.SeciInterface.SeciStatus.ConfigDir + Seci.SeciInterface.ConfigurationName + ".lay");
271  }
272  }
273 
274  private void backgroundSave_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e)
275  {
276  toolStripStatusLabel.Text = "";
277  _changesMade = false;
278  }
279 
280  private void backgroundAutosave_DoWork(object sender, DoWorkEventArgs e)
281  {
282  try
283  {
284  Seci.SeciInterface.Configuration_Save(true, true);
285 
286  if (!Seci.SeciInterface.IsAComponent)
287  {
288  //If it is a config then save the GUI layout
289  _currentLayout.Save(Seci.SeciInterface.SeciStatus.ConfigDir + "autosave.lay");
290  }
291  else
292  {
293  FileInfo fi = new FileInfo(Seci.SeciInterface.SeciStatus.ConfigDir + "autosave.lay");
294  if (fi.Exists)
295  {
296  fi.Delete();
297  }
298  }
299  }
300  catch (Exception err)
301  {
302  WriteError("autoSave ", err);
303  }
304  }
305 
306  #endregion
307 
308  #region "Write to Error log"
309 
310  //All non-LV errors make their way here
311  private void WriteError(String source, Exception e)
312  {
313  Seci.SeciInterface.Error_LogError(source, e);
314  _errorForm.addSeciMessage(DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString(), source, e.Message);
315  }
316 
317  #endregion
318 
319  #region "Update GUI"
320 
321  private void UpdateMainGUI()
322  {
323  //Update main dashboard
324  MainDashboard.UpdateControl();
325  Standard_RunInfo.UpdateControl();
326 
327  //Temporary test?
328  _currentLayout.UpdateGUIvalues();
329  }
330 
331  private void UpdateErrorsGUI()
332  {
333  //Get the messages
334  _lvErrorMessages = Seci.SeciInterface.LVQ_GetMessages();
335 
336  //Check for question(s)
337  Boolean question = false;
338  Boolean error = false;
339 
340  //Check for questions and errors
341  for (int i = 0; i < _lvErrorMessages.Count; ++i)
342  {
343  if (_lvErrorMessages[i].WaitForResponse)
344  {
345  //There is at least one question
346  question = true;
347  }
348  else
349  {
350  //There is at least one error message
351  error = true;
352  }
353  }
354 
355  if (question)
356  {
357  toolStripQuestionLabel.Visible = true;
358  }
359  else
360  {
361  toolStripQuestionLabel.Visible = false;
362  }
363 
364  if (error)
365  {
366  toolStripErrorLabel.Visible = true;
367  }
368  else
369  {
370  toolStripErrorLabel.Visible = false;
371  }
372 
373  //Display the messages
374  if (_lvErrorMessages != null)
375  {
376  _errorForm.addLabViewMessages(_lvErrorMessages);
377  }
378  }
379 
380  #endregion
381 
382  private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
383  {
384  statusStrip1.BringToFront();
385  }
386 
387  private void loadPreviousVersionToolStripMenuItem_Click(object sender, EventArgs e)
388  {
389  Dialogs.Config.LoadPreviousVersion v = new Sample_Environment_Control_Interface.Dialogs.Config.LoadPreviousVersion();
390 
391  if (v.ShowDialog() == DialogResult.OK)
392  {
393  if (okayToCloseConfig())
394  {
395  try
396  {
397  GeneralSplashScreen.ShowSplashScreen("Opening Configuration...");
398  OpenConfig(v.FileToLoad, true);
399  GeneralSplashScreen.CloseForm();
400  MessageBox.Show("Previous version successfully loaded. Note: saving this configuration will overwrite the most recent version.", "Loaded Previous Version", MessageBoxButtons.OK, MessageBoxIcon.Information);
401  }
402  catch (Exception err)
403  {
404  MessageBox.Show("Failed to open configuration/component!\nError message was: " + err.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
406  GeneralSplashScreen.CloseForm();
407  }
408  }
409  }
410  }
411  }
412 }
void backgroundSave_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e)
Definition: SECI_GUI.cs:274
void backgroundSave_DoWork(object sender, DoWorkEventArgs e)
Definition: SECI_GUI.cs:259
Seci.SerialisableList< Seci.LabView.MessageInfo > _lvErrorMessages
Definition: SECI_GUI.cs:22
void OpenConfig(String filename, Boolean checkVIs)
void Form1_FormClosing(object sender, FormClosingEventArgs e)
Definition: SECI_GUI.cs:155
void WriteError(String source, Exception e)
Definition: SECI_GUI.cs:311
void toolStripLabel_DoubleClick(object sender, EventArgs e)
Definition: SECI_GUI.cs:196
void SeciInterface_ValuesUpdated(object sender, EventArgs e)
Definition: SECI_GUI.cs:227
void loadPreviousVersionToolStripMenuItem_Click(object sender, EventArgs e)
Definition: SECI_GUI.cs:387
delegate void OpenConfigRemotelyDelegate(String filename, Boolean checkVIs)
void Form1_Load(object sender, EventArgs e)
Definition: SECI_GUI.cs:79
void Interface_ConfigChangeRequested(string[] vars)
Definition: SECI_GUI.cs:72
void backgroundAutosave_DoWork(object sender, DoWorkEventArgs e)
Definition: SECI_GUI.cs:280
static Dialogs.Messages.SeciMessages _errorForm
Definition: SECI_GUI.cs:23
void SeciInterface_OnNewLVMessage(object sender, EventArgs e)
Definition: SECI_GUI.cs:212
static Dialogs.Messages.SeciMessages errorForm
Definition: SECI_GUI.cs:32
void InitializeComponent()
Required method for Designer support - do not modify the contents of this method with the code editor...
void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
Definition: SECI_GUI.cs:382