‪Black Box
BBtaskScheduler.cs
Go to the documentation of this file.
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 
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";
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 
69  {
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
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  {
134 
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
269  {
270  ‪SchedulerIcon.Image = Properties.Resources.trafficlight_green;
271  }
273  {
274  ‪SchedulerIcon.Image = Properties.Resources.trafficlight_green;
275  }
277  {
278  ‪SchedulerIcon.Image = Properties.Resources.trafficlight_yellow;
279  }
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  {
300  }
301 
305  private void ‪TSStatusChecker()
306  {
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  ‪BBAccessPad AccessPad = new ‪BBAccessPad();
379  AccessPad.ShowDialog();
380 
382  {
383  ProcessStartInfo proc1 = new ProcessStartInfo();
384  string Command;
385  proc1.UseShellExecute = true;
386  Command = @"netsh wlan show hostednetwork";
387  proc1.WorkingDirectory = @"C:\Windows\System32";
388  proc1.FileName = @"C:\Windows\System32\cmd.exe";
389  proc1.Verb = "runas";
390  proc1.Arguments = "/k " + Command;
391  proc1.WindowStyle = ProcessWindowStyle.Normal;
392  Process.Start(proc1);
393  }
394  }
395 
401  private void ‪BBAdvSetup_Click(object sender, EventArgs e)
402  {
403  ‪BBAccessPad AccessPad = new ‪BBAccessPad();
404  AccessPad.ShowDialog();
405 
407  {
409  }
410  }
411 
416  private void ‪BBtaskScheduler_Load(object sender, EventArgs e)
417  {
418  }
419 
424  private bool ‪ScheduleChecker()
425  {
426  ‪ResearchSpand.Value = 0;
427  ‪startdate = Convert.ToDateTime(‪StartDateObj.Value.ToString("MM/dd/yyyy"));
428  ‪enddate = Convert.ToDateTime(‪StopDateObj.Value.ToString("MM/dd/yyyy"));
429 
430  if (‪startdate != null && ‪enddate != null)
431  {
432  int value = (int)((‪enddate - ‪startdate).TotalDays);
433  ‪ResearchSpand.Maximum = value;
434  ‪ResearchSpand.Minimum = 0;
435 
436  ‪StatusChecker.Start();
437  ‪ScheduleSet = true;
438  }
439  else
440  {
441  ‪StatusChecker.Start();
442  ‪ScheduleSet = false;
443  }
444 
445  return ‪ScheduleSet;
446  }
447 
453  private void ‪BBRestart_Click(object sender, EventArgs e)
454  {
455  ‪BBAccessPad AccessPad = new ‪BBAccessPad();
456  AccessPad.ShowDialog();
457 
459  {
460  ‪PIRSensorTask.Kill();
461  ‪ThermostatTask.Kill();
462  ‪FitnessTask.Kill();
463  Thread.Sleep(1000);
464  ‪PIRSensorTask.Start();
465  Thread.Sleep(500);
466  ‪ThermostatTask.Start();
467  Thread.Sleep(500);
468  ‪FitnessTask.Kill();
469  }
470  }
471 
472  #endregion ButtonControls
473 
474  #region DashBoardControls
475 
479  private void ‪CheckDate()
480  {
481  DateTime ‪today = DateTime.Today;
482  if ((‪startdate <= ‪today) && (‪today <= ‪enddate))
483  {
485 
486  if (‪ScheduleSet)
487  {
489  ‪StatusChecker.Start();
490  Thread.Sleep(500);
492  Thread.Sleep(500);
494  Thread.Sleep(500);
495  ‪StartPIR();
496  }
497  }
498  else
499  {
500  ‪StopDate = "";
501  ‪StartDate = "";
502  MessageBox.Show("Schedule date invalid");
503  }
504  }
505 
510  private void ‪submitBtn_Click(object sender, EventArgs e)
511  {
512  ‪BBAccessPad AccessPad = new ‪BBAccessPad();
513  AccessPad.ShowDialog();
515  {
517  ‪CheckDate();
518  }
519  }
520 
526  private void ‪offBtn_Click(object sender, EventArgs e)
527  {
528  ‪BBAccessPad AccessPad = new ‪BBAccessPad();
529  AccessPad.ShowDialog();
530 
532 
533  {
534  try
535  {
536  ‪PIRSensorTask.Kill();
537  ‪ThermostatTask.Kill();
538  ‪FitnessTask.Kill();
539  Thread.Sleep(1000);
540  this.Close();
541  }
542  catch
543  {
544  this.Close();
545  }
546  }
547  }
548 
554  private void ‪networkDisconnect_Click(object sender, EventArgs e)
555  {
556  ‪BBAccessPad AccessPad = new ‪BBAccessPad();
557  AccessPad.ShowDialog();
558 
560  {
561  ProcessStartInfo proc1 = new ProcessStartInfo();
562  string Command;
563  proc1.UseShellExecute = true;
564  Command = @"netsh wlan stop hostednetwork";
565  proc1.WorkingDirectory = @"C:\Windows\System32";
566  proc1.FileName = @"C:\Windows\System32\cmd.exe";
567  proc1.Verb = "runas";
568  proc1.Arguments = "/k " + Command;
569  proc1.WindowStyle = ProcessWindowStyle.Hidden;
570  Process.Start(proc1);
571  }
572  }
573 
579  private void ‪networkSetup_Click(object sender, EventArgs e)
580  {
581  ‪BBAccessPad AccessPad = new ‪BBAccessPad();
582  AccessPad.ShowDialog();
583 
585  {
586  ProcessStartInfo proc1 = new ProcessStartInfo();
587  string Command;
588  proc1.UseShellExecute = true;
589  Command = @"netsh wlan start hostednetwork";
590  proc1.WorkingDirectory = @"C:\Windows\System32";
591  proc1.FileName = @"C:\Windows\System32\cmd.exe";
592  proc1.Verb = "runas";
593  proc1.Arguments = "/k " + Command;
594  proc1.WindowStyle = ProcessWindowStyle.Hidden;
595  Process.Start(proc1);
596  }
597  }
598 
604  private void ‪adaptorBtn_Click(object sender, EventArgs e)
605  {
606  ‪BBAccessPad AccessPad = new ‪BBAccessPad();
607  AccessPad.ShowDialog();
608 
610  {
611  ProcessStartInfo proc1 = new ProcessStartInfo();
612  string Command;
613  proc1.UseShellExecute = true;
614  Command = @"ncpa.cpl";
615  proc1.WorkingDirectory = @"C:\Windows\System32";
616  proc1.FileName = @"C:\Windows\System32\cmd.exe";
617  proc1.Arguments = "/k " + Command;
618  proc1.WindowStyle = ProcessWindowStyle.Hidden;
619  Process.Start(proc1);
620  }
621  }
622 
628  private void ‪NetSetup_Click(object sender, EventArgs e)
629  {
630  ‪BBAccessPad AccessPad = new ‪BBAccessPad();
631  AccessPad.ShowDialog();
632 
634  {
635  Process proc = null;
636  try
637  {
638  string batDir = string.Format(@"NetSetup\");
639  proc = new Process();
640  proc.StartInfo.WorkingDirectory = batDir;
641  proc.StartInfo.FileName = ‪BBNSetup;
642  proc.StartInfo.CreateNoWindow = false;
643  proc.Start();
644  proc.WaitForExit();
645  MessageBox.Show("Bat file executed !!");
646  }
647  catch (Exception)
648  {
649  MessageBox.Show("Error setting up access point", "Configureation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
650  }
651  }
652  }
653 
654  #endregion DashBoardControls
655  }
656 }
‪static bool access
Definition: BBAccessPad.cs:10
‪void StartPIR()
‪Presence sensor file location
‪void BBTSS()
‪Black Box thermostat sensor status
‪void BBFSS()
‪Black Box fitness tracker status
‪System.Windows.Forms.PictureBox StatusIcon
‪System.Windows.Forms.DateTimePicker StartDateObj
‪void StartThermostate()
‪thermostat file location
‪bool ScheduleChecker()
‪Get and check schedule dates
‪System.Windows.Forms.ProgressBar ResearchSpand
‪void BBSmartSetup_Click(object sender, EventArgs e)
‪Check network status
‪static void SaveErrorData(string error)
‪Log all the error that occurs in the program
‪void networkSetup_Click(object sender, EventArgs e)
‪Start network connection
‪void BBRestart_Click(object sender, EventArgs e)
‪Restart task scheduler
‪void StatusChecker_Tick(object sender, EventArgs e)
‪Intervals which status are checked
‪void BBtaskScheduler_Load(object sender, EventArgs e)
‪void BBPSS()
‪Black Box presence sensor status
‪string RemoveUnwantedChar(string input)
‪Parse model info
‪void TSStatusChecker()
‪Check operation status
‪void BBTaskCheck()
‪Checking the operation status
‪void adaptorBtn_Click(object sender, EventArgs e)
‪Network connection window
‪System.Windows.Forms.PictureBox SchedulerIcon
‪System.Windows.Forms.DateTimePicker StopDateObj
‪void networkDisconnect_Click(object sender, EventArgs e)
‪Stop network connection
‪void submitBtn_Click(object sender, EventArgs e)
‪void NetSetup_Click(object sender, EventArgs e)
‪Setup Black Box network
‪void CheckDate()
‪validate selected dates
‪System.Windows.Forms.PictureBox MotionStatusIcon
‪void BBAdvSetup_Click(object sender, EventArgs e)
‪Open the advance dialog box
‪void offBtn_Click(object sender, EventArgs e)
‪power down button
‪System.Windows.Forms.Timer StatusChecker
‪static string BBThermostatType()
‪This method retrieve thermostat model
‪void StartFitnessTracker()
‪Fitness tracker file location
‪System.Windows.Forms.Label BBTModel
‪System.Windows.Forms.PictureBox ThermostatStatusIcon
‪void InitializeComponent()
‪Required method for Designer support - do not modify the contents of this method with the code editor...
‪System.Windows.Forms.Timer Timer
‪System.Windows.Forms.PictureBox FitnessStatusIcon