‪Black Box
BBSensorObjects.cs
Go to the documentation of this file.
1 /*
2 * FILE : BBSensorObjects.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 object, incoming message and outgoing message.
8 */
9 
11 using System;
12 using System.Net.Sockets;
13 using System.Text;
14 using System.Text.RegularExpressions;
15 
16 namespace ‪BBSensorConnection
17 {
18  public class ‪BBSensorObjects
19  {
20  #region Properties
21 
22  // boolean for movement
23  private static bool ‪isMovement = true;
24 
25  public bool ‪move;
26 
27  // boolean for no movement
28  private static bool ‪noMovement = false;
29 
30  // Raw data from presence data
31  private static string ‪presenseData = "";
32 
33  // Location of presence sensor
34  private static string ‪location = "";
35 
36  // Status of presence sensor
37  private static string ‪status = "";
38 
39  // current temperature upon movement
40  private static string ‪temperature = "";
41 
42  // current humidity upon movement
43  private static string ‪humidity = "";
44 
45  // current temperature upon movement
46  private static string ‪temperatureBuffer = "0.0";
47 
48  // current humidity upon movement
49  private static string ‪humidityBuffer = "0.0";
50 
51  // Client socket
52  public ‪Socket ‪Socket { get; set; }
53 
54  // Size of receive buffer
55  public int ‪BufferSize { get; set; } = 256;
56 
57  // Receive buffer
58  public byte[] ‪Buffer { get; set; }
59 
60  // Received data string
61  private StringBuilder ‪IncomingMessage { get; set; }
62 
63  // Message to be sent
64  private StringBuilder ‪OutgoingMessage { get; set; }
65 
66  // Terminator for each message
67  public string ‪MessageTerminator { get; set; } = "<END>";
68 
69  public bool ‪isRunning { get; set; }
70 
71  #endregion Properties
72 
73  #region Constructors
74 
76  {
77  ‪Buffer = new byte[‪BufferSize];
78  ‪IncomingMessage = new StringBuilder();
79  ‪OutgoingMessage = new StringBuilder();
80  ‪isRunning = true;
81  }
82 
83  #endregion Constructors
84 
85  #region Outgoing Message Methods
86 
91  public byte[] ‪OutgoingMessageToBytes()
92  {
93  if (‪OutgoingMessage.ToString().IndexOf(‪MessageTerminator) < 0)
94  {
96  }
97  return Encoding.ASCII.GetBytes(‪OutgoingMessage.ToString());
98  }
99 
104  public void ‪CreateOutgoingMessage(string msg)
105  {
106  ‪OutgoingMessage.Clear();
107  ‪OutgoingMessage.Append(msg);
109  }
110 
111  #endregion Outgoing Message Methods
112 
113  #region Incoming Message Methods
114 
118  public void ‪BuildIncomingMessage(int bytesRead)
119  {
120  ‪IncomingMessage.Append(Encoding.ASCII.GetString(‪Buffer, 0, bytesRead));
121  //Console.WriteLine(IncomingMessage.Append(Encoding.ASCII.GetString(Buffer, 0, bytesRead)));
122  }
123 
128  public bool ‪MessageReceived()
129  {
130  return ‪IncomingMessage.ToString().IndexOf(‪MessageTerminator) > -1;
131  }
132 
137  {
138  ‪IncomingMessage.Clear();
139  }
140 
146  {
147  return ‪IncomingMessage.Length;
148  }
149 
150  #endregion Incoming Message Methods
151 
152  #region Connected Object Methods
153 
157  public void ‪Close()
158  {
159  try
160  {
161  ‪Socket.Shutdown(SocketShutdown.Both);
162  ‪Socket.Close();
163  }
164  catch (Exception ex)
165  {
166  ‪BBSensorErrorLog.‪SaveErrorData("PIR Sensor connection already closed " + ex.Message.ToString());
167  }
168  }
169 
170  public string ‪GetRemoteEndPoint()
171  {
172  return ‪Socket.RemoteEndPoint.ToString();
173  }
174 
178  public void ‪PrintMessage()
179  {
180  if (‪IncomingMessage.ToString().Contains("START") && ‪isMovement)
181  {
182  ‪parseData(‪IncomingMessage.ToString());
183 
184  ‪isMovement = false;
185  ‪noMovement = true;
186  }
187  if (‪IncomingMessage.ToString().Contains("STOP") && ‪noMovement)
188  {
189  ‪parseData(‪IncomingMessage.ToString());
190  ‪isMovement = true;
191  ‪noMovement = false;
192  }
193  }
194 
195  #endregion Connected Object Methods
196 
197  #region Sensor Data Processing
198 
202  private void ‪parseData(string data)
203  {
204  try
205  {
206  ‪presenseData = Regex.Replace(data, @"<[^>]+>|&nbsp;", "");
207 
208  int count = 0;
209 
210  string[] words = ‪presenseData.Split(',');
211  foreach (string word in words)
212  {
213  if (count == 0) { ‪status = word; }
214  if (count == 1) { ‪location = word; }
215  if (count == 2) { ‪temperature = word; }
216  if (count == 3) { ‪humidity = word; }
217 
218  count++;
219  }
220 
223  if (‪humidity == "nan") { ‪humidity = ‪humidityBuffer; }
224  else { ‪humidityBuffer = ‪humidity; }
225 
226  // Debug window
227  Console.Write(‪location + "," + ‪status + "," + ‪temperature + "," + ‪humidity + DateTime.Now.ToString(" HH:mm:ss") + "," + DateTime.Now.ToString("yyyy-MM-dd") + Environment.NewLine);
228 
229  // Encrypt data
230  ‪BBSensorDataEncryptor.‪BBSensorDataEncryption.‪EncryptData(‪location + "," + ‪status + "," + ‪temperature + "," + ‪humidity + "," + DateTime.Now.ToString("HH:mm:ss") + "," + DateTime.Now.ToString("yyyy-MM-dd"));
231  }
232  catch (Exception ex)
233  {
234  ‪isRunning = false;
235  ‪BBSensorErrorLog.‪SaveErrorData("PIR Sensor " + ex.Message.ToString());
236  }
237  }
238 
239  #endregion Sensor Data Processing
240  }
241 }
‪int IncomingMessageLength()
‪Gets the length of the incoming message
‪void CreateOutgoingMessage(string msg)
‪Creates a new outgoing message
‪void BuildIncomingMessage(int bytesRead)
‪Converts the buffer to a string ans stores it
‪void ClearIncomingMessage()
‪Clears the current incoming message so that we can start building for the next message
‪static void SaveErrorData(string error)
‪Log all the error that occurs in the program
‪void Close()
‪Closes the connection
‪static void EncryptData(string SensorData)
‪This method encrypts the sensor data
‪byte [] OutgoingMessageToBytes()
‪Converts the outgoing message to bytes
‪void PrintMessage()
‪Print the details of the current incoming message
‪bool MessageReceived()
‪Determines if the message was fully received