SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
SeciMgr.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Runtime.Remoting;
5 using System.Runtime.Remoting.Channels;
6 using System.Runtime.Remoting.Channels.Http;
7 using System.Runtime.Remoting.Channels.Tcp;
8 using Seci.Definitions;
9 using System.IO;
10 [assembly: CLSCompliant(true)]
11 
12 namespace Seci.Managers
13 {
17  public static class SeciMgr
18  {
19  private static int _maxLVFileAge = 7; //In days (for example: 7 = a week)
21  private static Boolean _valuesUpdaterEnabled;
23  private static Boolean _lvmqUpdaterEnabled;
24  public delegate void RemotingEventHandler(String[] vars);
26 
27  public static Boolean WebDashboardEnabled { get; set; }
28  public static Boolean WebDashboardShowTitle { get; set; }
29 
30  //Indicates whether something significant is going on
31  //For example: changing configuration, editing blocks etc.
32  public static Boolean IsBusy { get; set; }
33 
37  public delegate void ValuesUpdatedEventHandler(Boolean timeout);
38  public static event ValuesUpdatedEventHandler OnValuesUpdated;
39 
43  public static event EventHandler OnNewLVMessage;
44  public static event EventHandler OnLVNotThere;
45 
50  public static void InitialiseSeci(String exeDir)
51  {
52  Status.ReadAppSettings();
53  //Set up directories
54  Status.ExecutionDir = exeDir;
55  Status.ErrorDir = Status.BaseLogDir + "\\Seci messages\\";
56  Status.ConfigDir = exeDir + "\\Configurations\\";
57  Status.LabViewErrorDir = Status.BaseLogDir + "\\LabVIEW messages\\";
58  Status.HelpDir = exeDir + "\\Help\\";
59 
60  Status.CreateDirectories();
61 
62  //Clear out any old log files
63  clearOutLabViewLogs();
64 
65  //Set event handlers
66  _valuesUpdater = new Tools.ValuesUpdater(Status.BlockUpdateRate);
67  _valuesUpdater.OnValuesUpdated += new Tools.ValuesUpdater.ValuesUpdatedEventHandler(daeValuesUpdated);
68 
69  _lvmqUpdater = new LabView.LvmqUpdater(3000);
70  _lvmqUpdater.OnChangeToLVMessages += new EventHandler(messageQNewLVMessage);
71  _lvmqUpdater.OnLabviewNotThere +=new EventHandler(messageQLabviewNotThere);
72 
73  //Enable remoting
74  enableDotNetRemoting();
75  Seci.Helpers.MessageLogger.WriteMessage("SeciGui", "User started SECI");
76 
77  if (Seci.Definitions.Status.LogToDatabase)
78  {
79  //Enable database logging
80  Seci.Helpers.DatabaseLogger.StartLogger();
81  }
82  }
83 
87  private static void clearOutLabViewLogs()
88  {
89  try
90  {
91  //Work out the valid log file names
92  DateTime today = DateTime.Today;
93  List<String> validFiles = new List<String>();
94 
95  for (int i = 0; i < _maxLVFileAge; ++i)
96  {
97  DateTime pastDate = today.Subtract(new TimeSpan(i, 0, 0, 0));
98  validFiles.Add(pastDate.Day + " " + pastDate.ToString("MMMM") + " - " + pastDate.DayOfWeek + ".txt");
99  }
100 
101  DirectoryInfo di = new DirectoryInfo(Status.LabViewErrorDir);
102  if (di.Exists)
103  {
104  List<FileInfo> filist = new List<FileInfo>(di.GetFiles("*.txt"));
105 
106  foreach (FileInfo fi in filist)
107  {
108  if (!validFiles.Contains(fi.Name))
109  {
110  fi.Delete();
111  }
112  }
113  }
114  }
115  catch
116  {
117  //Just in case - it is not the end of the world if this fails
118  }
119  }
120 
124  public static void CloseSeci()
125  {
126  if (Seci.Definitions.Status.LogToDatabase)
127  {
128  Seci.Helpers.DatabaseLogger.StopDatabaseLoggerThread();
129  System.Threading.Thread.Sleep(1000);
130  Seci.Helpers.DatabaseLogger.CloseDatabase();
131  }
132  }
133 
140  private static void daeValuesUpdated(Boolean timeout)
141  {
142  //Raise an event so the GUI can tell when the values have been updated.
143  //This is forwarding the event from the valuesUpdater.
144  if (OnValuesUpdated != null)
145  {
146  OnValuesUpdated(timeout);
147  }
148  }
149 
156  private static void messageQNewLVMessage(object sender, EventArgs e)
157  {
158  //Raise an event so the GUI can tell when new LabVIEW messages are received.
159  //This is forwarding the event from the LVMQUpdater.
160  if (OnNewLVMessage != null)
161  {
162  OnNewLVMessage(sender, e);
163  }
164  }
165 
166  private static void messageQLabviewNotThere(object sender, EventArgs e)
167  {
168  //Raise an event so the GUI can tell when new LabVIEW messages are received.
169  //This is forwarding the event from the LVMQUpdater.
170  if (OnLVNotThere != null)
171  {
172  OnLVNotThere(sender, e);
173  }
174  }
175 
180  private static Boolean enableDotNetRemoting()
181  {
182  //Create two channels
183  try
184  {
185  //SECURE - TCP
186  //Setup the configuration parameters through a dictionary
187  IDictionary properties = new Hashtable();
188  properties.Add("port", 8084);
189  properties.Add("secure", true);
190  properties.Add("impersonate", true);
191 
192  //Create an instance of a channel
193  TcpServerChannel secureChannel = new TcpServerChannel(properties, null);
194  ChannelServices.RegisterChannel(secureChannel, true);
195 
196  //Register as an available service with the name SECISecure.rem
197  RemotingConfiguration.RegisterWellKnownServiceType(typeof(Remoting.ReadWriteRemoting), "SECISecure.rem", WellKnownObjectMode.Singleton);
198 
199  //UNSECURE - HTTP
200  //Create an instance of a channel
201  HttpChannel Channel = new HttpChannel(8085);
202  ChannelServices.RegisterChannel(Channel, false);
203 
204  //Register as an available service with the name SECI.rem
205  RemotingConfiguration.RegisterWellKnownServiceType(typeof(Remoting.ReadOnlyRemoting), "SECI.rem", WellKnownObjectMode.Singleton);
206 
207  return true;
208  }
209  catch
210  {
211  return false;
212  }
213  }
214 
220  public static Boolean InitialiseLabView(Boolean loadStandardVIs)
221  {
222  //Start LabVIEW and initialise the DAE VI
223  if (LabView.LabViewApp.StartLabView(Status.ExecutionDir))
224  {
225  //Initialise LabVIEW Message Queue
226  Standard.MessageQueue.InitialiseMessQVI();
227 
228  if (loadStandardVIs)
229  {
230  //Load DAE
231  initialiseDae();
232 
233  //Initialise BeamLogger
234  Standard.BeamLogger.InitialiseBeamlogger();
235 
236  //Initialise SampleParameters, BeamlineParameters and UserDetails
237  Standard.BeamlineParameters.Initialise();
238  Standard.SampleParameters.Initialise();
239  Standard.UserDetails.Initialise();
240 
241  //Load the DAEMonitor VI, if required
243  {
244  Standard.DaeMonitor.Initialise();
245  }
246  }
247  return true;
248  }
249  else
250  {
251  return false;
252  }
253  }
254 
259  private static void initialiseDae()
260  {
261  Standard.Dae.Initialise(Status.LabViewDir + Status.DaeFile);
262 
263  //Make sure add handler only once
264  Standard.Dae.RunEnded -= new Standard.DaeEventHandler(runStartedOrEnded);
265  Standard.Dae.RunEnded += new Standard.DaeEventHandler(runStartedOrEnded);
266 
267  Standard.Dae.RunStarted -= new Seci.Standard.DaeEventHandler(runStartedOrEnded);
268  Standard.Dae.RunStarted += new Seci.Standard.DaeEventHandler(runStartedOrEnded);
269  }
270 
276  static void runStartedOrEnded(DateTime time, string runNumber)
277  {
278  Tools.ValuesUpdater.LogValuesAtStartOrEndRun(time, runNumber);
279  }
280 
281  #region Background Threads
282 
288  public static void StartThreads(Boolean startBlockUpdater, Boolean startLVMQUpdater)
289  {
290  if (startBlockUpdater)
291  {
292  _valuesUpdater.StartUpdater();
293  _valuesUpdaterEnabled = true;
294  }
295 
296  if (startLVMQUpdater)
297  {
298  _lvmqUpdater.StartUpdater();
299  _lvmqUpdaterEnabled = true;
300  }
301  }
302 
306  public static void PauseThreads()
307  {
308  if (_valuesUpdaterEnabled)
309  {
310  _valuesUpdater.PauseUpdater();
311  }
312 
313  if (_lvmqUpdaterEnabled)
314  {
315  _lvmqUpdater.PauseUpdater();
316  }
317  }
318 
322  public static void ResumeThreads()
323  {
324  if (_valuesUpdaterEnabled)
325  {
326  _valuesUpdater.ResumeUpdater();
327  }
328 
329  if (_lvmqUpdaterEnabled)
330  {
331  _lvmqUpdater.ResumeUpdater();
332  }
333  }
334 
335  public static void ClearDatabaseQueues()
336  {
337  Seci.Helpers.DatabaseLogger.ClearQueues();
338  }
339 
340  #endregion
341 
348  public static Boolean LogInAsManager(String password)
349  {
350  if (Status.CheckManagerPassword(password))
351  {
352  Status.ManagerLoggedIn = true;
353  return true;
354  }
355  else
356  {
357  return false;
358  }
359  }
360 
361  public static void ClearGraphs()
362  {
363  if (ClearGraphRequested != null)
364  {
365  ClearGraphRequested(null);
366  }
367  }
368  }
369 }
static void ClearDatabaseQueues()
Definition: SeciMgr.cs:335
This class is used for checking to see if the LabVIEW messages received from the message panel VI hav...
Definition: LVMQUpdater.cs:17
static Boolean InitialiseLabView(Boolean loadStandardVIs)
Initialise LabVIEW and load any default VIs.
Definition: SeciMgr.cs:220
This class is used for updating the values SECI has stored for the blocks and from the DAE...
static void daeValuesUpdated(Boolean timeout)
This method is run when the ValuesUpdater raises a OnValuesUpdated event. The event is just passed on...
Definition: SeciMgr.cs:140
static Boolean enableDotNetRemoting()
Enables Dot Net remoting for web access etc.
Definition: SeciMgr.cs:180
static void InitialiseSeci(String exeDir)
Initialises SECI. Needs to be called before SECI does anything else.
Definition: SeciMgr.cs:50
static EventHandler OnNewLVMessage
Delegate for passing &quot;new LabVIEW message&quot; event to the GUI level.
Definition: SeciMgr.cs:43
static void ResumeThreads()
Resume the ValuesUpdater and LVMQUpdater after pausing.
Definition: SeciMgr.cs:322
Class for holding the standard run-time settings for SECI. A large percentage of the information held...
Definition: Status.cs:12
static String LabViewErrorDir
Definition: Status.cs:85
static Boolean LogInAsManager(String password)
Method for logging in as manager. Tests password against that of the manager account and returns true...
Definition: SeciMgr.cs:348
static Boolean _lvmqUpdaterEnabled
Definition: SeciMgr.cs:23
static Tools.ValuesUpdater _valuesUpdater
Definition: SeciMgr.cs:20
static String ExecutionDir
Definition: Status.cs:87
static void clearOutLabViewLogs()
This clears out any logfiles that are older than a certain amount
Definition: SeciMgr.cs:87
static void initialiseDae()
Initialise the DAE. This loads the DAE VI and starts it running. Also adds a event handler for watchi...
Definition: SeciMgr.cs:259
static void messageQLabviewNotThere(object sender, EventArgs e)
Definition: SeciMgr.cs:166
static Boolean _valuesUpdaterEnabled
Definition: SeciMgr.cs:21
static void CloseSeci()
Closes SECI by making sure everything is closed etc.
Definition: SeciMgr.cs:124
static ValuesUpdatedEventHandler OnValuesUpdated
Definition: SeciMgr.cs:38
static EventHandler OnLVNotThere
Definition: SeciMgr.cs:44
delegate void RemotingEventHandler(String[] vars)
This delegate handles any events that come in via the remoting interface that need to be handled by t...
A general manager class for SECI. Contains the stuff that does not fit into any of the other manager ...
Definition: SeciMgr.cs:17
static void messageQNewLVMessage(object sender, EventArgs e)
This method is run when the LVMQUpdater raises a OnNewLVMessage event. The event is just passed on to...
Definition: SeciMgr.cs:156
static void StartThreads(Boolean startBlockUpdater, Boolean startLVMQUpdater)
Start the ValuesUpdater and LVMQUpdater running.
Definition: SeciMgr.cs:288
static void PauseThreads()
Pause the ValuesUpdater and LVMQUpdater.
Definition: SeciMgr.cs:306
static void runStartedOrEnded(DateTime time, string runNumber)
Logs the block values at the start or end of the run.
Definition: SeciMgr.cs:276
static LabView.LvmqUpdater _lvmqUpdater
Definition: SeciMgr.cs:22
static RemotingEventHandler ClearGraphRequested
Definition: SeciMgr.cs:25
static void ClearGraphs()
Definition: SeciMgr.cs:361
static Boolean UseDaeMonitorVI
Definition: Status.cs:99