Example #1
0
        public IHttpActionResult Post([FromBody] List <MonitorValue> commandValues)
        {
            _logger.Info("-----------------------------------------");
            _logger.Info("MonitorValuesController...");
            _logger.Info(ObjectDumper.Dump(commandValues));
            var headers    = Request.Headers;
            var serverGuid = headers.GetValues("server_guid").First();

            if (!Util.IsServerGuidValid(serverGuid))
            {
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }
            try
            {
                var guid  = headers.GetValues("agent_guid").First();
                var agent = monitorDb.GetAgentByGuid(guid);

                monitorDb.DeleteOldHistory(DateTime.UtcNow);
                foreach (var commandValue in commandValues)
                {
                    var MonitorValue = new BusinessLayer.MonitorValue()
                    {
                        AgentId          = agent.AgentId, // This should be derived from header
                        MonitorCommandId = commandValue.Id,
                        ReturnCode       = commandValue.ReturnCode,
                        Value            = commandValue.Value,
                        ErrorMessage     = commandValue.ErrorMessage
                    };
                    _logger.Debug($"MonitorValuesController : UpsertMonitorValue");
                    monitorDb.UpsertMonitorValue(MonitorValue);
                    _logger.Debug($"MonitorValuesController : UpdateLastReceivedReply");
                    monitorDb.UpdateLastReceivedReply(agent.AgentId);
                }
                return(Ok());
            }
            catch (Exception e)
            {
                _logger.Error($"Post method Exception:{e.Message}");
                return(BadRequest());
            }
        }