public void TestDeleteMethod() { DoorsTrackingController controller = new DoorsTrackingController(); controller.Delete(1); DoorTracking doorTrace = controller.Get(1); Assert.AreEqual(null, doorTrace); }
public static async Task <DoorTracking> GetOneDoorTrackingAsync(int id) { string requestUri = DoorsTrackingUri + "/" + id; using (HttpClient client = new HttpClient()) { string content = await client.GetStringAsync(requestUri); DoorTracking b = JsonConvert.DeserializeObject <DoorTracking>(content); return(b); } }
/// <summary> /// Method that creates a new DB row which resets Occupancy to zero when called. /// </summary> public static async void ResetCounter() { var dateTimeNow = DateTime.Now; dateTimeNow = Truncate(dateTimeNow, TimeSpan.FromMilliseconds(10)); // Create new DoorTracking object with the newly created properties DoorTracking resetDoorTracking = new DoorTracking(dateTimeNow, 0, false); // This object is the result of the HTTP POST method that is used in this very line DoorTracking doorTracking = await TaskController.AddDoorTrackingAsync(resetDoorTracking); }
public void TestPostMethod() { DoorsTrackingController controller = new DoorsTrackingController(); var door = new System.DateTime(2020, 12, 10, 20, 13, 50); DoorTracking postDoor = new DoorTracking(3480, door, 6, true); controller.Post(postDoor); DoorTracking doors = controller.Get(3480); Assert.AreEqual(true, doors.Equals(postDoor) != null); }
public void TestPutMethod() { var controller = new DoorsTrackingController(); DoorTracking door = controller.Get(2); door.DateTime = new System.DateTime(2020, 12, 5, 00, 31, 39); controller.Put(2, door); DoorTracking doorPut = controller.Get(2); Assert.AreEqual(door.DateTime, doorPut.DateTime); }
public static async Task <DoorTracking> GetOneDoorTrackingAsync1(int id) { string requestUri = DoorsTrackingUri + "/" + id; using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(requestUri); if (response.StatusCode == HttpStatusCode.NotFound) { throw new Exception("DoorTracking not found. Try another id"); } response.EnsureSuccessStatusCode(); string str = await response.Content.ReadAsStringAsync(); DoorTracking b = JsonConvert.DeserializeObject <DoorTracking>(str); return(b); } }
public static async Task <DoorTracking> DeleteOneDoorTrackingAsync(int id) { string requestUri = DoorsTrackingUri + "/" + id; using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.DeleteAsync(requestUri); Console.WriteLine("StatusCode" + response.StatusCode); if (response.StatusCode == HttpStatusCode.NotFound) { throw new Exception("DoorTracking not found or DoorTracking is persistent. Try another id"); } response.EnsureSuccessStatusCode(); string str = await response.Content.ReadAsStringAsync(); DoorTracking deletedDoorTracking = JsonConvert.DeserializeObject <DoorTracking>(str); return(deletedDoorTracking); } }
public static async Task <DoorTracking> UpdateDoorTrackingAsync(DoorTracking newDoorTracking, int id) { using (HttpClient client = new HttpClient()) { string requestUri = DoorsTrackingUri + "/" + id; var jsonString = JsonConvert.SerializeObject(newDoorTracking); Console.WriteLine("JSON: " + jsonString); StringContent content = new StringContent(jsonString, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PutAsync(requestUri, content); if (response.StatusCode == HttpStatusCode.NotFound) { throw new Exception("DoorTracking not found. Try another id"); } string str = await response.Content.ReadAsStringAsync(); DoorTracking updateDoorTracking = JsonConvert.DeserializeObject <DoorTracking>(str); return(updateDoorTracking); } }
public static async Task <DoorTracking> AddDoorTrackingAsync(DoorTracking newDoorTracking) { using (HttpClient client = new HttpClient()) { var jsonString = JsonConvert.SerializeObject(newDoorTracking); Console.WriteLine("JSON: " + jsonString); StringContent content = new StringContent(jsonString, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync(DoorsTrackingUri, content); if (response.StatusCode == HttpStatusCode.Conflict) { throw new Exception("DoorTracking already exists. Try another id"); } response.EnsureSuccessStatusCode(); string str = await response.Content.ReadAsStringAsync(); DoorTracking copyOfNewDoorTracking = JsonConvert.DeserializeObject <DoorTracking>(str); return(copyOfNewDoorTracking); } }
/// <summary> /// Main method that receives broadcast UDP messages and creates new DoorTracking object /// </summary> /// <param name="args"></param> static void Main(string[] args) { // Create a socket using (UdpClient socket = new UdpClient(new IPEndPoint(IPAddress.Any, Port))) { // Listen to all IPs and ports in the network IPEndPoint remoteEndPoint = new IPEndPoint(0, 0); // RESET TIMER var DailyTime = "00:00:00"; var timePartsFirstRun = DailyTime.Split(new char[1] { ':' }); var dateFirstRun = DateTime.Now; var nextTaskExecutionDate = new DateTime(dateFirstRun.Year, dateFirstRun.Month, dateFirstRun.Day, int.Parse(timePartsFirstRun[0]), int.Parse(timePartsFirstRun[1]), int.Parse(timePartsFirstRun[2])); bool shouldIssueTaskToday = true; while (true) { var dateNow = DateTime.Now; if (shouldIssueTaskToday) { if (nextTaskExecutionDate > dateNow) { TimeSpan ts = nextTaskExecutionDate - dateNow; //waits certain time and run the code Task.Delay(ts).ContinueWith((x) => ResetCounter()); shouldIssueTaskToday = false; } } else if (nextTaskExecutionDate.Day != dateNow.Day) { nextTaskExecutionDate = nextTaskExecutionDate.AddDays(1); shouldIssueTaskToday = true; } // Listen to Broadcast Console.WriteLine("Waiting for broadcast {0}", socket.Client.LocalEndPoint); byte[] datagramReceived = socket.Receive(ref remoteEndPoint); string message = Encoding.ASCII.GetString(datagramReceived, 0, datagramReceived.Length); Console.WriteLine("Receives {0} bytes from {1} port {2} message {3}", datagramReceived.Length, remoteEndPoint.Address, remoteEndPoint.Port, message); // Call Parse function that splits received message to parts Parse(message); Console.WriteLine(dateTimeLine); Console.WriteLine(isEntranceLine); // Convert string dateTimeLine message part to DateTime type DateTime dateTime = DateTime.Parse(dateTimeLine); bool isEntrance = true; // Convert string isEntranceLine message part to boolean type if (isEntranceLine == "1") { isEntrance = true; } else { isEntrance = false; } // Get last value of Occupancy column using HTTP GET method int lastOccupancyValueValue = TaskController.GetLastDoorTrackingAsync().Result; int occupancyValue = 0; // Increase or decrease occupancyValue according to the value of isEntrance if (isEntrance == true) { occupancyValue = lastOccupancyValueValue + 1; } else { // Prevent occupancyValue from becoming negative by assigning value of zero if (lastOccupancyValueValue == 0) { occupancyValue = 0; } else { occupancyValue = lastOccupancyValueValue - 1; } } // Create new DoorTracking object with the newly created properties DoorTracking newDoorTracking = new DoorTracking(dateTime, occupancyValue, isEntrance); // This object is the result of the HTTP POST method that is used in this very line DoorTracking doorTracking = TaskController.AddDoorTrackingAsync(newDoorTracking).Result; } } }