Esempio n. 1
0
        [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())));
        }
Esempio n. 2
0
        public IActionResult GetPerSecondStatsFromTimeInterval([FromBody] PerSecondStatsFromTimeIntervalRequest unverifiedTimeIntervalRequest)
        {
            // Verify device's API Key.
            if (_apiKeyService.VerifyAPIKey(unverifiedTimeIntervalRequest.API_Key) < 0)
            {
                // If API Key does not exist or is deactivated.
                return(Unauthorized());
            }

            if (DataMessageService.CheckTimeIntervalValidity(unverifiedTimeIntervalRequest.TimeInterval) == false)
            {
                return(BadRequest(new JsonResult(new InvalidTimeIntervalResponseBody())));
            }

            DataMessage responseBody = DataMessageService.RetrievePerSecondStatsBetweenInterval(unverifiedTimeIntervalRequest.TimeInterval);

            if (responseBody.IsEmpty())
            {
                return(NoContent());
            }

            return(Ok(new JsonResult(responseBody)));
        }