Black Box
BBtaskScheduler.cs
1 /*
2 * FILE : BBtaskScheduler.cs
3 * PROJECT : BBTasker
4 * PROGRAMMER : Brian Hinds
5 * DATE : March 20, 2019
6 * FIRST VERSION : 1.2.0
7 * DESCRIPTION :Task Scheduler for the Black Box Architecture
8 */
9 
10 using BBSensorConnection;
11 using BBSensorsErrorLog;
12 using System;
13 using System.Diagnostics;
14 using System.IO;
15 using System.Text;
16 using System.Text.RegularExpressions;
17 using System.Threading;
18 using System.Windows.Forms;
19 using Timer = System.Windows.Forms.Timer;
20 
21 namespace BBTasker
22 {
23  public partial class BBtaskScheduler : Form
24  {
25  #region Variables
26 
27  private Process PIRSensorTask = new Process();
28  private Process ThermostatTask = new Process();
29  private Process FitnessTask = new Process();
30  private Process BBNetwork = new Process();
31  private string BBNSetup = @"netSetup.bat";
32  private string BBPpath = @"BBPresenseSensor.exe";
33  private string BBTpath = @"BBThermostatSensor.exe";
34  private string BBFpath = @"MiBand-Heartrate.exe";
35  private BBSensorObjects BBSO = new BBSensorObjects();
36 
37  public static string StartDate = "";
38  public static string StopDate = "";
39 
42  private bool BBPSStatus = false;
43 
46  private bool BBTSStatus = false;
47 
48  private string BBTSname = "";
49  private bool BBTSGetModel = false;
50 
53  private bool BBFSStatus = false;
54 
55  private string fileName = "";
56 
57  private bool ScheduleSet = false;
58  private DateTime startdate;
59  private DateTime enddate;
60  private DateTime today = DateTime.Today;
61 
62  #endregion Variables
63 
64  #region Constructor
65 
68  public BBtaskScheduler()
69  {
70  InitializeComponent();
71 
72  StatusChecker = new Timer();
73  StatusChecker.Tick += new EventHandler(StatusChecker_Tick);
74  StatusChecker.Interval = 5000;
75  StatusChecker.Enabled = true;
76  StatusChecker.Stop();
77  }
78 
79  #endregion Constructor
80 
81  #region PluginProcessCheck
82 
86  private void BBPSS()
87  {
88  fileName = Path.GetFileName(BBPpath);
89 
90  //check if process still running
91  Process[] processName = Process.GetProcessesByName(fileName.Substring(0, fileName.LastIndexOf('.')));
92  if (processName.Length > 0)
93  {
94  MotionStatusIcon.Image = Properties.Resources.trafficlight_green;
95 
96  // Check running condition
97  BBPSStatus = BBSO.isRunning;
98 
99  // Operation status
100  if (BBPSStatus)
101  {
102  MotionStatusIcon.Image = Properties.Resources.trafficlight_green;
103  }
104  if (!BBPSStatus)
105  {
106  MotionStatusIcon.Image = Properties.Resources.trafficlight_yellow;
107  }
108  }
109  else
110  {
111  MotionStatusIcon.Image = Properties.Resources.trafficlight_red;
112  BBPSStatus = false;
113  }
114  }
115 
119  private void BBTSS()
120 
121  {
122  string fileName = Path.GetFileName(BBTpath);
123 
124  //check if process still running
125  Process[] processName = Process.GetProcessesByName(fileName.Substring(0, fileName.LastIndexOf('.')));
126  if (processName.Length > 0)
127  {
128  ThermostatStatusIcon.Image = Properties.Resources.trafficlight_green;
129 
130  // Get thermostat model
131  if (!BBTSGetModel)
132  {
133  BBTSname = BBThermostatConnection.BBThermostatConnect.BBThermostatType();
134 
135  BBTSname = RemoveUnwantedChar(BBTSname);
136 
137  BBTModel.Text = BBTSname;
138  BBTSGetModel = true;
139  }
140 
141  //Checking thermostat data status
143 
144  // Operation status
145  if (BBTSStatus)
146  {
147  ThermostatStatusIcon.Image = Properties.Resources.trafficlight_green;
148  }
149  if (!BBTSStatus)
150  {
151  ThermostatStatusIcon.Image = Properties.Resources.trafficlight_red;
152  }
153  }
154  else
155  {
156  ThermostatStatusIcon.Image = Properties.Resources.trafficlight_red;
157  BBTSStatus = false;
158  }
159  }
160 
166  public string RemoveUnwantedChar(string input)
167  {
168  StringBuilder stringBuilder = new StringBuilder();
169 
170  foreach (object match in Regex.Matches(input, "[0-9nu BRevCTkMGHzVs%\\-.]"))
171  {
172  stringBuilder.Append(match.ToString());
173  }
174 
175  return stringBuilder.ToString();
176  }
177 
181  private void BBFSS()
182  {
183  fileName = Path.GetFileName(BBFpath);
184 
185  //check if process still running
186  Process[] processName = Process.GetProcessesByName(fileName.Substring(0, fileName.LastIndexOf('.')));
187  if (processName.Length > 0)
188  {
189  FitnessStatusIcon.Image = Properties.Resources.trafficlight_green;
190  }
191  else
192  {
193  FitnessStatusIcon.Image = Properties.Resources.trafficlight_red;
194  BBFSStatus = false;
195  }
196  }
197 
198  #endregion PluginProcessCheck
199 
200  #region ApplicationStartup
201 
205  private void StartPIR()
206  {
207  try
208  {
209  PIRSensorTask.StartInfo.FileName = BBPpath;
210  PIRSensorTask.StartInfo.CreateNoWindow = false;
211  PIRSensorTask.Start();
212  }
213  catch (Exception ex)
214  {
215  BBSensorErrorLog.SaveErrorData(ex.Message.ToString());
216  }
217  }
218 
222  private void StartThermostate()
223  {
224  try
225  {
226  ThermostatTask.StartInfo.FileName = BBTpath;
227  ThermostatTask.StartInfo.CreateNoWindow = false;
228  ThermostatTask.Start();
229  }
230  catch (Exception ex)
231  {
232  ThermostatStatusIcon.Image = Properties.Resources.trafficlight_red;
233  BBSensorErrorLog.SaveErrorData(ex.Message.ToString());
234  }
235  }
236 
240  private void StartFitnessTracker()
241  {
242  try
243  {
244  FitnessTask.StartInfo.FileName = BBFpath;
245  FitnessTask.StartInfo.CreateNoWindow = false;
246  FitnessTask.Start();
247  }
248  catch (Exception ex)
249  {
250  BBSensorErrorLog.SaveErrorData(ex.Message.ToString());
251  }
252  }
253 
254  #endregion ApplicationStartup
255 
256  #region PluginStatusCheck
257 
261  private void BBTaskCheck()
262  {
263  StatusIcon.Image = Properties.Resources.trafficlight_green;
264 
265  try
266  {
267  // Checking the status of the sensors
268  if (BBFSStatus && BBPSStatus && BBTSStatus && ScheduleSet)
269  {
270  SchedulerIcon.Image = Properties.Resources.trafficlight_green;
271  }
272  if (BBFSStatus || BBPSStatus || BBTSStatus && ScheduleSet)
273  {
274  SchedulerIcon.Image = Properties.Resources.trafficlight_green;
275  }
276  if (!BBFSStatus && !BBPSStatus && !BBTSStatus && ScheduleSet)
277  {
278  SchedulerIcon.Image = Properties.Resources.trafficlight_yellow;
279  }
280  if (BBFSStatus && BBPSStatus && BBTSStatus && !ScheduleSet)
281  {
282  SchedulerIcon.Image = Properties.Resources.trafficlight_yellow;
283  }
284  }
285  catch (Exception ex)
286  {
287  BBSensorErrorLog.SaveErrorData(ex.Message.ToString());
288  SchedulerIcon.Image = Properties.Resources.trafficlight_red;
289  }
290  }
291 
297  private void StatusChecker_Tick(object sender, EventArgs e)
298  {
299  TSStatusChecker();
300  }
301 
305  private void TSStatusChecker()
306  {
307  if ((startdate <= today) && (today <= enddate) && ScheduleSet)
308  {
310  try
311  {
312  BBTSS();
313  }
314  catch (Exception ex)
315  {
316  StatusIcon.Image = Properties.Resources.trafficlight_red;
317  BBSensorErrorLog.SaveErrorData(ex.Message.ToString());
318  }
319  //Fitness tracker
320  try
321  {
322  BBFSS();
323  }
324  catch (Exception ex)
325  {
326  StatusIcon.Image = Properties.Resources.trafficlight_red;
327  BBSensorErrorLog.SaveErrorData(ex.Message.ToString());
328  }
329  //Presence sensor
330  try
331  {
332  BBPSS();
333 
334  BBTaskCheck();
335  }
336  catch (Exception ex)
337  {
338  MotionStatusIcon.Image = Properties.Resources.trafficlight_red;
339  StatusIcon.Image = Properties.Resources.trafficlight_red;
340  BBSensorErrorLog.SaveErrorData(ex.Message.ToString());
341  }
342 
343  // Update progress bar
344  ResearchSpand.Value = (today).Day / startdate.Day;
345  }
346  if (ResearchSpand.Value == 100)
347  {
348  BBTaskCheck();
349  StatusChecker.Stop();
350  MessageBox.Show("You have come to the end of the\n research period, thank you for participating.");
351  ScheduleSet = false;
352  PIRSensorTask.Kill();
353  ThermostatTask.Kill();
354  FitnessTask.Kill();
355  }
356 
357  //Reset status
358  BBPSStatus = false;
359 
360  //Resetting thermostat data status
361  BBTSStatus = false;
362 
363  //Resetting Fitness tracker status
364  BBFSStatus = false;
365  }
366 
367  #endregion PluginStatusCheck
368 
369  #region ButtonControls
370 
376  private void BBSmartSetup_Click(object sender, EventArgs e)
377  {
378  ProcessStartInfo proc1 = new ProcessStartInfo();
379  string Command;
380  proc1.UseShellExecute = true;
381  Command = @"netsh wlan show hostednetwork";
382  proc1.WorkingDirectory = @"C:\Windows\System32";
383  proc1.FileName = @"C:\Windows\System32\cmd.exe";
384  proc1.Verb = "runas";
385  proc1.Arguments = "/k " + Command;
386  proc1.WindowStyle = ProcessWindowStyle.Normal;
387  Process.Start(proc1);
388  }
389 
395  private void BBAdvSetup_Click(object sender, EventArgs e)
396  {
397  StartFitnessTracker();
398  }
399 
404  private void BBtaskScheduler_Load(object sender, EventArgs e)
405  {
406  }
407 
412  private bool ScheduleChecker()
413  {
414  ResearchSpand.Value = 0;
415  startdate = Convert.ToDateTime(StartDateObj.Value.ToString("MM/dd/yyyy"));
416  enddate = Convert.ToDateTime(StopDateObj.Value.ToString("MM/dd/yyyy"));
417 
418  if (startdate != null && enddate != null)
419  {
420  int value = (int)((enddate - startdate).TotalDays);
421  ResearchSpand.Maximum = value;
422  ResearchSpand.Minimum = 0;
423 
424  StatusChecker.Start();
425  ScheduleSet = true;
426  }
427  else
428  {
429  StatusChecker.Start();
430  ScheduleSet = false;
431  }
432 
433  return ScheduleSet;
434  }
435 
441  private void BBRestart_Click(object sender, EventArgs e)
442  {
443  PIRSensorTask.Kill();
444  ThermostatTask.Kill();
445  FitnessTask.Kill();
446  Thread.Sleep(1000);
447  PIRSensorTask.Start();
448  Thread.Sleep(500);
449  ThermostatTask.Start();
450  Thread.Sleep(500);
451  FitnessTask.Kill();
452  }
453 
454  #endregion ButtonControls
455 
456  #region DashBoardControls
457 
461  private void CheckDate()
462  {
463  DateTime today = DateTime.Today;
464  if ((startdate <= today) && (today <= enddate))
465  {
466  ScheduleSet = ScheduleChecker();
467 
468  if (ScheduleSet)
469  {
470  ScheduleChecker();
471  StatusChecker.Start();
472  Thread.Sleep(500);
473  StartThermostate();
474  Thread.Sleep(500);
475  StartFitnessTracker();
476  Thread.Sleep(500);
477  StartPIR();
478  }
479  }
480  else
481  {
482  StopDate = "";
483  StartDate = "";
484  MessageBox.Show("Schedule date invalid");
485  }
486  }
487 
492  private void submitBtn_Click(object sender, EventArgs e)
493  {
494  CheckDate();
495  }
496 
502  private void offBtn_Click(object sender, EventArgs e)
503  {
504  try
505  {
506  PIRSensorTask.Kill();
507  ThermostatTask.Kill();
508  FitnessTask.Kill();
509  Thread.Sleep(1000);
510  this.Close();
511  }
512  catch
513  {
514  this.Close();
515  }
516  }
517 
523  private void networkDisconnect_Click(object sender, EventArgs e)
524  {
525  ProcessStartInfo proc1 = new ProcessStartInfo();
526  string Command;
527  proc1.UseShellExecute = true;
528  Command = @"netsh wlan stop hostednetwork";
529  proc1.WorkingDirectory = @"C:\Windows\System32";
530  proc1.FileName = @"C:\Windows\System32\cmd.exe";
531  proc1.Verb = "runas";
532  proc1.Arguments = "/k " + Command;
533  proc1.WindowStyle = ProcessWindowStyle.Hidden;
534  Process.Start(proc1);
535  }
536 
542  private void networkSetup_Click(object sender, EventArgs e)
543  {
544  ProcessStartInfo proc1 = new ProcessStartInfo();
545  string Command;
546  proc1.UseShellExecute = true;
547  Command = @"netsh wlan start hostednetwork";
548  proc1.WorkingDirectory = @"C:\Windows\System32";
549  proc1.FileName = @"C:\Windows\System32\cmd.exe";
550  proc1.Verb = "runas";
551  proc1.Arguments = "/k " + Command;
552  proc1.WindowStyle = ProcessWindowStyle.Hidden;
553  Process.Start(proc1);
554  }
555 
561  private void adaptorBtn_Click(object sender, EventArgs e)
562  {
563  ProcessStartInfo proc1 = new ProcessStartInfo();
564  string Command;
565  proc1.UseShellExecute = true;
566  Command = @"ncpa.cpl";
567  proc1.WorkingDirectory = @"C:\Windows\System32";
568  proc1.FileName = @"C:\Windows\System32\cmd.exe";
569  proc1.Arguments = "/k " + Command;
570  proc1.WindowStyle = ProcessWindowStyle.Hidden;
571  Process.Start(proc1);
572  }
573 
579  private void NetSetup_Click(object sender, EventArgs e)
580  {
581  Process proc = null;
582  try
583  {
584  string batDir = string.Format(@"NetSetup\");
585  proc = new Process();
586  proc.StartInfo.WorkingDirectory = batDir;
587  proc.StartInfo.FileName = BBNSetup;
588  proc.StartInfo.CreateNoWindow = false;
589  proc.Start();
590  proc.WaitForExit();
591  MessageBox.Show("Bat file executed !!");
592  }
593  catch (Exception)
594  {
595  MessageBox.Show("Error setting up access point", "Configureation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
596  }
597  }
598 
599  #endregion DashBoardControls
600  }
601 }
static void SaveErrorData(string error)
Log all the error that occurs in the program
string RemoveUnwantedChar(string input)
Parse model info