SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
DAE.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Text;
5 using System.Xml;
6 using System.Diagnostics;
7 using System.Runtime.InteropServices;
8 
9 //This class stores all the values read off the DAE
10 
11 namespace Seci.Standard
12 {
18  public delegate void DaeEventHandler(DateTime time, String runNumber);
19 
25  public static class Dae
26  {
27  //Actual DAE Stuff
28  private static String _filePath; //the location of the DAE VI
29  private static Boolean _visible;
30  private static Boolean _inWaitingState;
31  private static isisicpLib.dae _icp; //This is used to talk to the ICP programme without going through LabVIEW
32 
33  private static String _daeData; //This is passed to the icp programme to get values back
34  private static float _updateRate = 1.0f; //How often to update the DAE values (in seconds)
35  private static DateTime _lastUpdated; //Keeps track of when the DAE was last updated
36 
37  //Values
38  //Basics
39  private static String _runStatus;
40  private static String _runNumber;
41  private static String _rbNumber;
42  private static String _userName;
43  private static String _runTitle;
44  private static String _startTime;
45  private static String _runDurationTotal;
46  private static String _runDurationPeriod;
47 
48  //Frames
49  private static String _goodFramesTotal;
50  private static String _goodFramesPeriod;
51  private static String _rawFramesTotal;
52  private static String _rawFramesPeriod;
53 
54  //Periods
55  private static String _currentPeriod;
56  private static String _numberOfPeriods;
57  private static String _periodSequence;
58 
59  //Beam
60  private static String _beamCurrent;
61  private static String _totalUAmps;
62  private static String _countRate;
63 
64  //DAE
65  private static String _daeMemoryUsed;
66  private static String _totalDaeCounts;
67  private static String _DaeTimingSource;
68 
69  //Monitor
70  private static String _monitorCounts;
71  private static String _monitorSpectrum;
72  private static String _monitorFrom;
73  private static String _monitorTo;
74 
75  //Misc
76  private static String _numberOfTimeChannels;
77  private static String _numberOfSpectra;
78 
79  //Properties
80  public static String RunStatus { get { return _runStatus; } }
81  public static String RunNumber { get { return _runNumber; } }
82  public static String RbNumber { get { return _rbNumber; } }
83  public static String UserName { get { return _userName; } }
84  public static String RunTitle { get { return _runTitle; } }
85  public static String StartTime { get { return _startTime; } }
86  public static String RunDurationTotal { get { return _runDurationTotal; } }
87  public static String RunDurationPeriod { get { return _runDurationPeriod; } }
88  public static String GoodFramesTotal { get { return _goodFramesTotal; } }
89  public static String GoodFramesPeriod { get { return _goodFramesPeriod; } }
90  public static String RawFramesTotal { get { return _rawFramesTotal; } }
91  public static String RawFramesPeriod { get { return _rawFramesPeriod; } }
92  public static String CurrentPeriod { get { return _currentPeriod; } }
93  public static String NumberOfPeriods { get { return _numberOfPeriods; } }
94  public static String PeriodSequence { get { return _periodSequence; } }
95  public static String BeamCurrent { get { return _beamCurrent; } }
96  public static String TotalUAmps { get { return _totalUAmps; } }
97  public static String CountRate { get { return _countRate; } }
98  public static String DaeMemoryUsed { get { return _daeMemoryUsed; } }
99  public static String TotalDaeCounts { get { return _totalDaeCounts; } }
100  public static String DaeTimingSource { get { return _DaeTimingSource; } }
101  public static String MonitorCounts { get { return _monitorCounts; } }
102  public static String MonitorSpectrum { get { return _monitorSpectrum; } }
103  public static String MonitorFrom { get { return _monitorFrom; } }
104  public static String MonitorTo { get { return _monitorTo; } }
105  public static String NumberOfTimeChannels { get { return _numberOfTimeChannels; } }
106  public static String NumberOfSpectra { get { return _numberOfSpectra; } }
107 
108  //Other properties
109  public static String FilePath { get { return _filePath; } set { _filePath = value; } }
110  internal static Boolean Visible { get { return _visible; } set { _visible = value; } }
111  internal static Boolean InWaitingState { get { return _inWaitingState; } set { _inWaitingState = value; } }
112 
116  public static event DaeEventHandler RunStarted;
117 
121  public static event DaeEventHandler RunEnded;
122 
128  internal static void Initialise(String fileName)
129  {
130  _filePath = fileName;
131 
132  //Read the dae data from the resource - the data values are irrelevant but it is necessary
133  //to send the ICP programme something with the correct XML format
134  _daeData = Seci.Properties.Resources.cluster;
135  //Start VI
136  LoadDaeVI();
137  }
138 
142  private static void LoadDaeVI()
143  {
144  //Load VI
145  LabView.LabViewApp.LoadVI(_filePath, false);
146 
147  //Start it running
148  LabView.LabViewApp.StartVI(_filePath);
149 
150  connectToIcp();
151  }
152 
153  private static void connectToIcp()
154  {
155  //Create instance of pcicp (actually gets the one already running)
156  try
157  {
158  _icp = new isisicpLib.dae();
159  }
160  catch
161  {
162  Helpers.ErrorLogger.SeciError("LoadDaeVI", new Exception("Could not connect to DAE"));
163  }
164  }
165 
169  public static void KillIcp()
170  {
171  try
172  {
173  //Stop ICP by killing the process.
174  Process[] procList;
175 
176  procList = Process.GetProcessesByName("isisicp");
177 
178  foreach (Process proc in procList)
179  {
180  proc.Kill();
181  }
182  }
183  catch
184  {
185  }
186  }
187 
188  public static void KillIsisDataSvr()
189  {
190  try
191  {
192  //Stop ISIS Data Server by killing the process.
193  Process[] procList;
194 
195  procList = Process.GetProcessesByName("isisdatasvr");
196 
197  foreach (Process proc in procList)
198  {
199  try
200  {
201  proc.Kill();
202  }
203  catch
204  {
205  }
206  }
207  }
208  catch
209  {
210  }
211  }
212 
216  public static void ShowDae()
217  {
218  LabView.LabViewApp.ShowVI(_filePath);
219  }
220 
224  public static void HideDae()
225  {
226  LabView.LabViewApp.HideVI(_filePath);
227  }
228 
233  public static void SetTitleBarVisibility(Boolean visible)
234  {
235  LabView.LabViewApp.SetTitleBarVisibility(_filePath, visible);
236  }
237 
242  internal static Boolean CheckDaeVIRunning()
243  {
244  return LabView.LabViewApp.IsVIRunning(_filePath);
245  }
246 
251  internal static List<String> GetDaeControls()
252  {
253  return LabView.LabViewApp.GetListOfControls(_filePath);
254  }
255 
260  public static void SetTitle(String title)
261  {
262  //Set value
263  LabView.LabViewApp.SetValue(_filePath, "New Run Title", title);
264  //Press button
265  LabView.LabViewApp.SetValue(_filePath, "Set Title", true);
266  }
267 
268  #region Update data
269 
274  internal static Boolean updateRunInformation()
275  {
276  try
277  {
278  //Check that it is time to log; if it is first time then _LastLogged is 01:01:0001, so it will log by default
279  if (DateTime.Now >= _lastUpdated.AddSeconds(_updateRate))
280  {
281  if (_icp != null)
282  {
283  String results = ""; //this is a dummy
284  //pass the current data and get the new data back
285  _icp.updateStatusXML(ref _daeData, out results);
286  getValuesFromXML();
287  }
288  _lastUpdated = DateTime.Now;
289 
290  return true;
291  }
292  }
293  catch (InvalidCastException e)
294  {
295  //See if there is an instance of the icp we can re-connect to
296  System.Diagnostics.Process[] procList;
297 
298  procList = System.Diagnostics.Process.GetProcessesByName("isisicp");
299 
300  if (procList.Length > 0)
301  {
302  connectToIcp();
303  }
304 
305  throw new Exception(e.Message);
306  }
307  catch (COMException e)
308  {
309  //See if there is an instance of the icp we can re-connect to
310  System.Diagnostics.Process[] procList;
311 
312  procList = System.Diagnostics.Process.GetProcessesByName("isisicp");
313 
314  if (procList.Length > 0)
315  {
316  connectToIcp();
317  }
318 
319  throw new Exception(e.Message);
320  }
321  catch (Exception e)
322  {
323  throw new Exception(e.Message);
324  }
325 
326  return false;
327  }
328 
332  private static void getValuesFromXML()
333  {
334  //Create xml document
335  XmlDocument xml = new XmlDocument();
336  xml.InnerXml = _daeData;
337 
338  String oldRN = "";
339 
340  //Get the strings
341  XmlNodeList nodelist = xml.SelectNodes("/Cluster/String");
342 
343  for (int i = 0; i < nodelist.Count; ++i)
344  {
345  if (nodelist[i].FirstChild.InnerText == "RBNumber")
346  {
347  _rbNumber = nodelist[i].LastChild.InnerText;
348  }
349  else if (nodelist[i].FirstChild.InnerText == "RunTitle")
350  {
351  _runTitle = nodelist[i].LastChild.InnerText;
352  }
353  else if (nodelist[i].FirstChild.InnerText == "UserName")
354  {
355  _userName = nodelist[i].LastChild.InnerText;
356  }
357  else if (nodelist[i].FirstChild.InnerText == "RunNumber")
358  {
359  oldRN = _runNumber;
360  _runNumber = nodelist[i].LastChild.InnerText;
361 
362  //if (oldRN != null)
363  //{
364  // if (_runNumber != oldRN)
365  // {
366  // if (RunEnded != null)
367  // {
368  // RunEnded(DateTime.Now, oldRN);
369  // }
370  // }
371  //}
372  }
373  else if (nodelist[i].FirstChild.InnerText == "StartTime")
374  {
375  _startTime = nodelist[i].LastChild.InnerText;
376  }
377  }
378 
379  //Get the U32s
380  nodelist = xml.SelectNodes("/Cluster/U32");
381 
382  for (int i = 0; i < nodelist.Count; ++i)
383  {
384  if (nodelist[i].FirstChild.InnerText == "GoodFramesTotal")
385  {
386  _goodFramesTotal = nodelist[i].LastChild.InnerText;
387  }
388  else if (nodelist[i].FirstChild.InnerText == "RawFramesTotal")
389  {
390  _rawFramesTotal = nodelist[i].LastChild.InnerText;
391  }
392  else if (nodelist[i].FirstChild.InnerText == "GoodFramesPeriod")
393  {
394  _goodFramesPeriod = nodelist[i].LastChild.InnerText;
395  }
396  else if (nodelist[i].FirstChild.InnerText == "RawFramesPeriod")
397  {
398  _rawFramesPeriod = nodelist[i].LastChild.InnerText;
399  }
400  else if (nodelist[i].FirstChild.InnerText == "RunDurationTotal")
401  {
402  int time = Convert.ToInt32(nodelist[i].LastChild.InnerText);
403  TimeSpan actual = new TimeSpan(0, 0, 0, time);
404 
405  _runDurationTotal = actual.ToString();
406  }
407  else if (nodelist[i].FirstChild.InnerText == "RunDurationPeriod")
408  {
409  int time = Convert.ToInt32(nodelist[i].LastChild.InnerText);
410  TimeSpan actual = new TimeSpan(0, 0, 0, time);
411 
412  _runDurationPeriod = actual.ToString();
413  }
414  else if (nodelist[i].FirstChild.InnerText == "NumberOfTimeChannels")
415  {
416  _numberOfTimeChannels = nodelist[i].LastChild.InnerText;
417  }
418  else if (nodelist[i].FirstChild.InnerText == "DAEMemoryUsed")
419  {
420  _daeMemoryUsed = nodelist[i].LastChild.InnerText;
421  }
422  else if (nodelist[i].FirstChild.InnerText == "NumberOfPeriods")
423  {
424  _numberOfPeriods = nodelist[i].LastChild.InnerText;
425  }
426  else if (nodelist[i].FirstChild.InnerText == "CurrentPeriod")
427  {
428  _currentPeriod = nodelist[i].LastChild.InnerText;
429  }
430  else if (nodelist[i].FirstChild.InnerText == "NumberOfSpectra")
431  {
432  _numberOfSpectra = nodelist[i].LastChild.InnerText;
433  }
434  else if (nodelist[i].FirstChild.InnerText == "MonitorCounts")
435  {
436  _monitorCounts = nodelist[i].LastChild.InnerText;
437  }
438  else if (nodelist[i].FirstChild.InnerText == "PeriodSequence")
439  {
440  _periodSequence = nodelist[i].LastChild.InnerText;
441  }
442  }
443 
444  //Get the I32s
445  nodelist = xml.SelectNodes("/Cluster/I32");
446 
447  for (int i = 0; i < nodelist.Count; ++i)
448  {
449  if (nodelist[i].FirstChild.InnerText == "MonitorSpectrum")
450  {
451  _monitorSpectrum = nodelist[i].LastChild.InnerText;
452  }
453  }
454 
455  //Get the DBLs
456  nodelist = xml.SelectNodes("/Cluster/DBL");
457 
458  for (int i = 0; i < nodelist.Count; ++i)
459  {
460  if (nodelist[i].FirstChild.InnerText == "BeamCurrent")
461  {
462  _beamCurrent = nodelist[i].LastChild.InnerText;
463  }
464  else if (nodelist[i].FirstChild.InnerText == "TotalUAmps")
465  {
466  _totalUAmps = nodelist[i].LastChild.InnerText;
467  }
468  else if (nodelist[i].FirstChild.InnerText == "TotalDAECounts")
469  {
470  _totalDaeCounts = nodelist[i].LastChild.InnerText;
471  }
472  else if (nodelist[i].FirstChild.InnerText == "MonitorFrom")
473  {
474  _monitorFrom = nodelist[i].LastChild.InnerText;
475  }
476  else if (nodelist[i].FirstChild.InnerText == "MonitorTo")
477  {
478  _monitorTo = nodelist[i].LastChild.InnerText;
479  }
480  else if (nodelist[i].FirstChild.InnerText == "CountRate")
481  {
482  _countRate = nodelist[i].LastChild.InnerText;
483  }
484  }
485 
486  //Get the EWs
487  nodelist = xml.SelectNodes("/Cluster/EW");
488 
489  for (int i = 0; i < nodelist.Count; ++i)
490  {
491  if (nodelist[i].FirstChild.InnerText == "RunStatus")
492  {
493  String val = nodelist[i].LastChild.InnerText;
494  String oldStatus = _runStatus;
495 
496  if (val == "0")
497  {
498  _runStatus = "PROCESSING";
499  }
500  else if (val == "1")
501  {
502  _runStatus = "SETUP";
503  }
504  else if (val == "2")
505  {
506  _runStatus = "RUNNING";
507  }
508  else if (val == "3")
509  {
510  _runStatus = "PAUSED";
511  }
512  else if (val == "4")
513  {
514  _runStatus = "WAITING";
515  }
516  else if (val == "5")
517  {
518  _runStatus = "VETOING";
519  }
520  else if (val == "6")
521  {
522  _runStatus = "ENDING";
523  }
524  else if (val == "7")
525  {
526  _runStatus = "SAVING";
527  }
528  else
529  {
530  _runStatus = "UNKNOWN";
531  }
532 
533  if (oldStatus == "SETUP" && _runStatus != "SETUP")
534  {
535  //run has started
536  if (RunStarted != null)
537  {
538  RunStarted(DateTime.Now, _runNumber);
539  }
540  }
541 
542  if (oldStatus != "SETUP" && _runStatus == "SETUP")
543  {
544  if (RunEnded != null)
545  {
546  RunEnded(DateTime.Now, oldRN);
547  }
548  }
549 
550  }
551  else if (nodelist[i].FirstChild.InnerText == "DAETimingSource")
552  {
553  String val = nodelist[i].LastChild.InnerText;
554 
555  if (val == "0")
556  {
557  _DaeTimingSource = "ISIS";
558  }
559  else if (val == "1")
560  {
561  _DaeTimingSource = "Internal";
562  }
563  else if (val == "2")
564  {
565  _DaeTimingSource = "SMP";
566  }
567  else if (val == "3")
568  {
569  _DaeTimingSource = "Muon";
570  }
571  else if (val == "4")
572  {
573  _DaeTimingSource = "Muon MS";
574  }
575  else if (val == "5")
576  {
577  _DaeTimingSource = "ISIS (first TS1)";
578  }
579  }
580  }
581  }
582 
589  internal static List<String> GetFormattedValues(Boolean includeUserInfo, Boolean isMuon)
590  {
591  List<String> lines = new List<String>();
592 
593  lines.Add("Run Status: " + _runStatus);
594  lines.Add("Run Number: " + _runNumber);
595 
596  if (includeUserInfo)
597  {
598  lines.Add("RB Number: " + _rbNumber);
599  lines.Add("User(s): " + _userName);
600  lines.Add("Title: " + _runTitle);
601  }
602  else
603  {
604  lines.Add("RB Number: Unavailable");
605  lines.Add("User(s): Unavailable");
606  lines.Add("Title: Unavailable");
607  }
608 
609  lines.Add("Start Time: " + _startTime);
610  lines.Add("Total Run Time: " + _runDurationTotal);
611  lines.Add("Period Run Time: " + _runDurationPeriod);
612  lines.Add("Good Frames (Total): " + _goodFramesTotal);
613  lines.Add("Good Frames (Period): " + _goodFramesPeriod);
614  lines.Add("Raw Frames (Total): " + _rawFramesTotal);
615  lines.Add("Raw Frames (Period): " + _rawFramesPeriod);
616  lines.Add("Current Period: " + _currentPeriod);
617  lines.Add("Number Of Periods: " + _numberOfPeriods);
618  lines.Add("Period Sequence: " + _periodSequence);
619  lines.Add("Beam Current: " + _beamCurrent);
620 
621  if (isMuon)
622  {
623  lines.Add("Total MEv: " + _totalDaeCounts);
624  }
625  else
626  {
627  lines.Add("Total UAmps: " + _totalUAmps);
628  }
629 
630  lines.Add("Count-Rate: " + _countRate);
631 
632  if (!isMuon)
633  {
634  lines.Add("DAE Memory Used: " + _daeMemoryUsed);
635  lines.Add("Total DAE Counts: " + _totalDaeCounts);
636  lines.Add("DAE Timing Source: " + _DaeTimingSource);
637  lines.Add("Monitor Counts: " + _monitorCounts);
638  lines.Add("Monitor Spectrum: " + _monitorSpectrum);
639  lines.Add("Monitor From: " + _monitorFrom);
640  lines.Add("Monitor To: " + _monitorTo);
641  lines.Add("Number Of Time Channels: " + _numberOfTimeChannels);
642  lines.Add("Number Of Spectra: " + _numberOfSpectra);
643  }
644 
645  return lines;
646  }
647 
648  #endregion
649 
650  #region Set run status
651 
655  internal static void BeginRun()
656  {
657  if (_icp != null)
658  {
659  String messages = "";
660  _icp.beginRun(out messages);
661  }
662  }
663 
667  internal static void EndRun()
668  {
669  if (_icp != null)
670  {
671  String messages = "";
672  _icp.endRun(out messages);
673  }
674  }
675 
677  internal static void PauseRun()
678  {
679  if (_icp != null)
680  {
681  String messages = "";
682  _icp.pauseRun(out messages);
683  }
684  }
685 
689  internal static void ResumeRun()
690  {
691  if (_icp != null)
692  {
693  String messages = "";
694  _icp.resumeRun(out messages);
695  }
696  }
697 
701  internal static void AbortRun()
702  {
703  if (_icp != null)
704  {
705  String messages = "";
706  _icp.abortRun(out messages);
707  }
708  }
709 
713  internal static void SaveRun()
714  {
715  if (_icp != null)
716  {
717  String messages = "";
718  _icp.saveRun(out messages);
719  }
720  }
721 
725  internal static void UpdateRun()
726  {
727  //Not implemented in pc-icp yet
728  }
729 
733  internal static void StoreRun()
734  {
735  //Not implemented in pc-icp yet
736  }
737 
741  internal static void BeginWaiting()
742  {
743  if (_icp != null)
744  {
745  String messages = "";
746  _icp.startSEWait(out messages);
747  _inWaitingState = true;
748  }
749  }
750 
754  internal static void EndWaiting()
755  {
756  if (_icp != null)
757  {
758  String messages = "";
759  _icp.endSEWait(out messages);
760  _inWaitingState = false;
761  }
762  }
763 
764  #endregion
765 
766  }
767 }
static String _currentPeriod
Definition: DAE.cs:55
static String _daeMemoryUsed
Definition: DAE.cs:65
static String _daeData
Definition: DAE.cs:33
delegate void DaeEventHandler(DateTime time, String runNumber)
Delegate for handling events triggered from the DAE
static void SetTitleBarVisibility(Boolean visible)
This method shows/hides the DAE VI&#39;s title bar
Definition: DAE.cs:233
static String _runStatus
Definition: DAE.cs:39
static DaeEventHandler RunEnded
The event that is fired when a run ends.
Definition: DAE.cs:121
static String _monitorTo
Definition: DAE.cs:73
static String _numberOfTimeChannels
Definition: DAE.cs:76
static String _monitorSpectrum
Definition: DAE.cs:71
static String _totalDaeCounts
Definition: DAE.cs:66
static DaeEventHandler RunStarted
The event that is fired when a run starts.
Definition: DAE.cs:116
static void KillIcp()
This method will kill the ICP.
Definition: DAE.cs:169
static DateTime _lastUpdated
Definition: DAE.cs:35
static String _numberOfPeriods
Definition: DAE.cs:56
static String _totalUAmps
Definition: DAE.cs:61
static Boolean _inWaitingState
Definition: DAE.cs:30
static String _DaeTimingSource
Definition: DAE.cs:67
static void KillIsisDataSvr()
Definition: DAE.cs:188
static String _filePath
Definition: DAE.cs:28
static void HideDae()
This method hides the DAE VI - it is hidden by default.
Definition: DAE.cs:224
static String _rawFramesTotal
Definition: DAE.cs:51
This class is a wrapper for the ICP programme that is a default part of SECI. The class is set up to ...
Definition: DAE.cs:25
static String _userName
Definition: DAE.cs:42
static String _monitorFrom
Definition: DAE.cs:72
static String _startTime
Definition: DAE.cs:44
static void getValuesFromXML()
This method actually read the XML returned from the ICP and extracts the values.
Definition: DAE.cs:332
static String _numberOfSpectra
Definition: DAE.cs:77
static String _rbNumber
Definition: DAE.cs:41
static void SetTitle(String title)
Sets the run title
Definition: DAE.cs:260
static String _runTitle
Definition: DAE.cs:43
static Boolean _visible
Definition: DAE.cs:29
static String _runDurationTotal
Definition: DAE.cs:45
static String _runDurationPeriod
Definition: DAE.cs:46
static void ShowDae()
This method shows the DAE VI - it is hidden by default.
Definition: DAE.cs:216
static String _periodSequence
Definition: DAE.cs:57
static String _countRate
Definition: DAE.cs:62
static String _rawFramesPeriod
Definition: DAE.cs:52
static isisicpLib.dae _icp
Definition: DAE.cs:31
static String _monitorCounts
Definition: DAE.cs:70
static String _runNumber
Definition: DAE.cs:40
static void LoadDaeVI()
This method loads and starts the DAE VI and creates the DCOM connection to the ICP.
Definition: DAE.cs:142
static void connectToIcp()
Definition: DAE.cs:153
static String _goodFramesTotal
Definition: DAE.cs:49
static String _beamCurrent
Definition: DAE.cs:60
static String _goodFramesPeriod
Definition: DAE.cs:50