‪Black Box
BBSensorConnectionManager.cs
Go to the documentation of this file.
1 /*
2 * FILE : BBSensorConnectionManager.cs
3 * PROJECT : Black Box
4 * PROGRAMMER : BRIAN HINDS
5 * FIRST VERSION : 1.0.0.0
6 * DATE : March 20, 2019
7 * DESCRIPTION : This file manages the connection for the Presence sensor.
8 */
9 
11 using System;
12 using System.Net;
13 using System.Net.Sockets;
14 
16 {
18  {
19  #region Variables
20 
21  private static int ‪counter;
22  private static string ‪sensorIP;
23  private static short ‪sensorPort;
24 
25  #endregion Variables
26 
27  #region Connection Properties
28 
29  public static IPAddress ‪LocalIPAddress => IPAddress.Parse(‪sensorIP);
30  public static int ‪Port => ‪sensorPort;
31  public static IPEndPoint ‪EndPoint => new IPEndPoint(‪LocalIPAddress, ‪Port);
32 
33  #endregion Connection Properties
34 
35  #region Connection String
36 
41  public static void ‪parseConnectionData(string connection)
42  {
43  ‪counter = 0;
44 
45  string[] connectionData = connection.Split(',');
46  foreach (string connectionDataDigits in connectionData)
47  {
48  if (‪counter == 0) { ‪sensorIP = connectionDataDigits; }
49  if (‪counter == 1) { ‪sensorPort = Convert.ToInt16(connectionDataDigits); }
50 
51  ‪counter++;
52  }
53  }
54 
55  #endregion Connection String
56 
57  #region Create Listener and Socket
58 
63  public static Socket ‪CreateListener()
64  {
65  Socket socket = null;
66  try
67  {
68  // Create a TCP/IP socket.
69  socket = ‪CreateSocket();
70  socket.Bind(‪EndPoint);
71  socket.Listen(10);
72  }
73  catch (Exception ex)
74  {
75  ‪BBSensorErrorLog.‪SaveErrorData("PIR Sensor " + ex.Message.ToString());
76  }
77 
78  return socket;
79  }
80 
81  public static Socket ‪CreateSocket()
82  {
83  return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
84  }
85 
86  #endregion Create Listener and Socket
87  }
88 }
‪static Socket CreateListener()
‪Create the connection needed for the TCP connection
‪static void SaveErrorData(string error)
‪Log all the error that occurs in the program
‪static void parseConnectionData(string connection)
‪Parse connection data