// Validates a received DataMessage object's contents (all PerSecondStat objects contained). public bool CheckDataMessageValidity(DataMessage message) { if (message == null || message.IsEmpty()) { return(false); } // Obtain a list of all valid Camera Keys currently in the system. List <string> validCameraKeyList = _dbQueryService.GetAllClaimedCameraKeys(); for (int z = 0; z < message.GetLength(); z++) { // Verify the attributes of the PerSecondStat object, except for the CameraKey. if (message.RealTimeStats[z].isValidSecondStat() == false) { return(false); } // Verify its CameraKey. string cameraKeyToVerify = message.RealTimeStats[z].CameraKey; bool cameraKeyIsValid = validCameraKeyList.Where(o => string.Equals(cameraKeyToVerify, o, StringComparison.OrdinalIgnoreCase)).Any(); if (cameraKeyIsValid == false) { return(false); } } return(true); }
[Route("persist")] // "api/datareceival/persist" public IActionResult Persist([FromBody] DataMessage receivedMessage) { // Verify device's API Key. if (_apiKeyService.VerifyAPIKey(receivedMessage.API_Key) < 0) { // If API Key does not exist or is deactivated. return(Unauthorized()); } if (DataMessageService.CheckDataMessageValidity(receivedMessage) == false) { return(BadRequest(new JsonResult(DataMessageService.CreateInvalidDataMessageResponseBody(receivedMessage)))); } else if (DataMessageService.StoreStatsFromDataMessage(receivedMessage) == true) { // Asynchronously check hourly stats are ready for calculation and perform if needed. Task hourlyStatsCheck = Task.Factory.StartNew( () => _hourlyStatsService.AutoCalculateHourlyStats(receivedMessage)); // Return HTTP OK, without waiting on asynchronous Task. return(Ok("Received datamessage with " + receivedMessage.GetLength() + "per second stats.")); } return(StatusCode(500, new JsonResult(new FailedPersistResponseBody()))); }
// Creates an error response body custom to the characteristics of the received DataMessage object. public InvalidDataMessageResponseBody CreateInvalidDataMessageResponseBody(DataMessage message) { if (message == null || message.IsEmpty()) { return(new InvalidDataMessageResponseBody(true, null)); } else { List <string> temp = new List <string>(); for (int c = 0; c < message.GetLength(); c++) { if (String.IsNullOrWhiteSpace(message.RealTimeStats[c].CameraKey)) { temp.Add("CameraKey"); } // Does not query the camera keys again in order to avoid server slow down. if (message.RealTimeStats[c].NumTrackedPeople < 0) { temp.Add("NumTrackedPeople"); } if (MySqlDateTimeConverter.CheckIfSQLFormat(message.RealTimeStats[c].DateTime) == false) { temp.Add("DateTime"); } else if (message.RealTimeStats[c].DateTime.ToDateTime().ValidateDateTime() == false) { temp.Add("DateTime"); } } // Removes all duplicates from the list of failed attributes. temp = temp.Distinct().ToList <string>(); // Counter for next step. int x = 0; string[] failedAttributes = new string[temp.Count]; // Store values in an array of strings. foreach (string distinctAttribute in temp) { failedAttributes[x] = distinctAttribute; x++; } return(new InvalidDataMessageResponseBody(false, failedAttributes)); } }
// Once a DataMessage is verified, this method allows persisting all contents (PerSecondStat objects) into the database. public bool StoreStatsFromDataMessage(DataMessage verifiedMessage) { List <PerSecondStat> temp = new List <PerSecondStat>(); for (int y = 0; y < verifiedMessage.GetLength(); y++) { temp.Add(verifiedMessage.RealTimeStats[y]); } // Remove any possible duplicates. List <PerSecondStat> distinctStats = temp.Distinct().ToList(); return(_dbQueryService.PersistNewPerSecondStats(distinctStats)); }
// Validates a received DataMessage object's contents (all PerSecondStat objects contained). public bool CheckDataMessageValidity(DataMessage message) { if (message.IsEmpty()) { return(false); } for (int z = 0; z < message.GetLength(); z++) { if (message.RealTimeStats[z].isValidSecondStat() == false) { return(false); } } return(true); }
public DataReceivalResponse GetReceivalResponse(DataMessage receivedMessage) { DataReceivalResponse response = new DataReceivalResponse(); response.NumberOfReceivedStats = receivedMessage.GetLength(); bool sendAlertSummaryValue; lock (SendAlertSummaryLock) { sendAlertSummaryValue = SendAlertSummary; SendAlertSummary = false; } if (sendAlertSummaryValue && receivedMessage.RealTimeStats.Length > 0) { //Assumes the entire message is only reporting for one camera response.ActiveAlertsForCamera = _alertService.GetAllActiveAlertsForCameraKey(receivedMessage.RealTimeStats.First().CameraKey); } return(response); }