Example #1
0
        public async Task <DevicePingStatusResponse> GetPingRequestStatus(DevicePingLogRequest request)
        {
            PingRequestStatus        pingRequestStatus = new PingRequestStatus();
            DevicePingStatusResponse response;
            List <IErrorInfo>        errorInfos = new List <IErrorInfo>();

            errorInfos.AddRange(await base.Validate(this._validators, request));
            errorInfos.AddRange(await this._pingValidator.Validate(request));

            if (errorInfos.Count <= 0)
            {
                this._loggingService.Info("Started Invoking DevicePingRepository with request : " + JsonConvert.SerializeObject(request), "DevicePingService.Fetch");

                pingRequestStatus = await this._devicePingRepository.Fetch(request);

                if (pingRequestStatus != null)
                {
                    RequestStatus rs = (RequestStatus)pingRequestStatus.RequestStatusID;
                    pingRequestStatus.RequestState = rs.ToString();
                }
                this._loggingService.Info("Ended Invoking DevicePingRepository with response : " + JsonConvert.SerializeObject(pingRequestStatus), "DevicePingService.Fetch");
                response = _mapper.Map <DevicePingStatusResponse>(pingRequestStatus);
            }
            else
            {
                response = new DevicePingStatusResponse
                {
                    AssetUID  = request.AssetUID.ToString(),
                    DeviceUID = request.DeviceUID.ToString(),
                    Errors    = errorInfos
                };
            }

            return(response);
        }
Example #2
0
        public async Task <PingRequestStatus> Insert(DevicePingLogRequest devicePingLogRequest)
        {
            try
            {
                PingRequestStatus response                = null;
                DateTime          currentDateTime         = DateTime.UtcNow;
                string            devicePingRequestExpiry = "0";
                switch (devicePingLogRequest.FamilyName.ToUpper())
                {
                case "PL":
                    devicePingRequestExpiry = _configuration["AppSettings:PL_PingInterval"];
                    break;

                case "MTS":
                    devicePingRequestExpiry = _configuration["AppSettings:MTS_PingInterval"];
                    break;

                case "DATAOUT":
                    devicePingRequestExpiry = _configuration["AppSettings:DataOut_PingInterval"];
                    break;

                default:
                    throw new Exception("Device Family Type Not Supported");
                    break;
                }

                DateTime expiryDate = currentDateTime.AddSeconds(double.Parse(devicePingRequestExpiry));
                this._loggingService.Info("Started executing query", "DevicePingRepository.Insert");

                this._transactions.Upsert <PingRequestStatus>(new PingRequestStatus
                {
                    RequestStatusID      = (int)RequestStatus.Pending,
                    RequestTimeUTC       = currentDateTime,
                    RequestExpiryTimeUTC = expiryDate,
                    DevicePingLogUID     = devicePingLogRequest.DevicePingLogUID,
                    AssetUID             = devicePingLogRequest.AssetUID,
                    DeviceUID            = devicePingLogRequest.DeviceUID
                });

                this._loggingService.Info("Completed query execution", "DevicePingRepository.Insert");

                response                      = new PingRequestStatus();
                response.AssetUID             = devicePingLogRequest.AssetUID;
                response.DeviceUID            = devicePingLogRequest.DeviceUID;
                response.DevicePingLogUID     = devicePingLogRequest.DevicePingLogUID;
                response.RequestState         = RequestStatus.Pending.ToString();
                response.RequestExpiryTimeUTC = expiryDate;
                response.RequestTimeUTC       = DateTime.Parse(currentDateTime.ToString("yyyy-MM-dd HH:mm:ss"));

                return(response);
            }
            catch (Exception ex)
            {
                this._loggingService.Error("Exception occurred while executing query", MethodInfo.GetCurrentMethod().Name, ex);
                throw ex;
            }
        }
Example #3
0
        private async Task <DevicePingLogRequest> ConstructMessage(DevicePingLogRequest devicePingLogRequest)
        {
            bool result = false;

            try
            {
                return(devicePingLogRequest);
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #4
0
        public async Task <ActionResult> GetPingRequestStatus([FromQuery] DevicePingLogRequest request)
        {
            request = await base.ReadRequestContentAsync <DevicePingLogRequest>(request);

            this._loggingService.LogInformation(string.Format("Fetching Ping Request Status for AssetUID {0} and DeviceUID {1}", request.AssetUID, request.DeviceUID), className);

            if (request == null)
            {
                this._loggingService.LogInformation("Invalid Request", "PingController.PostDevicePingRequest");
                IList <IErrorInfo> errors = new List <IErrorInfo>();
                errors.Add(new ErrorInfo()
                {
                    Message = Utils.GetEnumDescription(ErrorCodes.RequestInvalid), ErrorCode = (int)ErrorCodes.RequestInvalid
                });
                return(Ok(new DevicePingStatusResponse()
                {
                    Errors = errors
                }));
            }

            DevicePingStatusResponse devicePingStatusResponse = new DevicePingStatusResponse();

            request.UserUID     = base.GetUserContext(Request);
            request.CustomerUID = base.GetCustomerContext(Request);

            this._loggingService.LogInformation("Started invoking Device Ping Log Service with request : " + JsonConvert.SerializeObject(request), "PingController.GetPingRequestStatus");

            devicePingStatusResponse = await _devicePingService.GetPingRequestStatus(request);

            this._loggingService.LogInformation("Ended invoking Device Ping Log Service with response : " + JsonConvert.SerializeObject(devicePingStatusResponse), "PingController.GetPingRequestStatus");
            if (devicePingStatusResponse == null)
            {
                IList <IErrorInfo> errors = new List <IErrorInfo>();
                errors.Add(new ErrorInfo()
                {
                    Message = Utils.GetEnumDescription(ErrorCodes.PingRequestNotFound), ErrorCode = (int)ErrorCodes.PingRequestNotFound
                });
                return(Ok(new DevicePingStatusResponse()
                {
                    AssetUID = request.AssetUID.ToString(), DeviceUID = request.DeviceUID.ToString(), Errors = errors
                }));
            }
            else
            {
                return(Ok(devicePingStatusResponse));
            }
        }
Example #5
0
        public async Task <ActionResult> PostDevicePingRequest(DevicePingLogRequest devicePingLogRequest)
        {
            devicePingLogRequest = await base.ReadRequestContentAsync <DevicePingLogRequest>(devicePingLogRequest);

            if (devicePingLogRequest == null)
            {
                this._loggingService.LogInformation("Invalid Request", "PingController.PostDevicePingRequest");
                IList <IErrorInfo> errors = new List <IErrorInfo>();
                errors.Add(new ErrorInfo()
                {
                    Message = Utils.GetEnumDescription(ErrorCodes.RequestInvalid), ErrorCode = (int)ErrorCodes.RequestInvalid
                });
                return(Ok(new DevicePingStatusResponse()
                {
                    Errors = errors
                }));
            }

            this._loggingService.LogInformation("Saving Device Ping Request for request : " + JsonConvert.SerializeObject(devicePingLogRequest), "PingController.Post");

            var devicePingStatusResponse = new DevicePingStatusResponse();

            devicePingLogRequest.UserUID     = base.GetUserContext(Request);
            devicePingLogRequest.CustomerUID = base.GetCustomerContext(Request);

            this._loggingService.LogInformation(string.Format("Post Ping Request to Device Ping Log Table for Asset = {0} and Device = {1}", devicePingLogRequest.AssetUID, devicePingLogRequest.DeviceUID), "PingController.Post");
            devicePingStatusResponse = await _devicePingService.PostDevicePingRequest(devicePingLogRequest);

            if (devicePingStatusResponse == null)
            {
                this._loggingService.LogInformation("Not saved Successfully", "PingController.PostDevicePingRequest");
                IList <IErrorInfo> errors = new List <IErrorInfo>();
                errors.Add(new ErrorInfo()
                {
                    Message = Utils.GetEnumDescription(ErrorCodes.PingRequestNotSaved), ErrorCode = (int)ErrorCodes.PingRequestNotSaved
                });
                return(StatusCode((int)HttpStatusCode.InternalServerError, new DevicePingStatusResponse()
                {
                    AssetUID = devicePingLogRequest.AssetUID.ToString(), DeviceUID = devicePingLogRequest.DeviceUID.ToString(), Errors = errors
                }));
            }
            else
            {
                return(Ok(devicePingStatusResponse));
            }
        }
Example #6
0
        public async Task <PingRequestStatus> Fetch(DevicePingLogRequest devicePingLogRequest)
        {
            try
            {
                this._loggingService.Debug("Started executing query", "");
                var response = await this._transactions.GetAsync <PingRequestStatus>(string.Format(Queries.FetchPingRequestStatusQuery,
                                                                                                   devicePingLogRequest.AssetUID.ToStringWithoutHyphens().WrapWithUnhex(), devicePingLogRequest.DeviceUID.ToStringWithoutHyphens().WrapWithUnhex()));

                this._loggingService.Debug("Query Execution Completed", "");
                return(response.ToList().FirstOrDefault());
            }
            catch (Exception ex)
            {
                this._loggingService.Error("Exception occurred while executing query", MethodInfo.GetCurrentMethod().Name, ex);
                throw ex;
            }
        }
Example #7
0
        private async Task <IList <IErrorInfo> > ValidateDuplicateRequest(DevicePingLogRequest devicePingLogRequest)
        {
            var latestRequest = await _devicePingRepository.Fetch(devicePingLogRequest);

            IList <IErrorInfo> errors = new List <IErrorInfo>();

            if (latestRequest != null)
            {
                if (((latestRequest.RequestStatusID == (int)RequestStatus.Pending) || (latestRequest.RequestStatusID == (int)RequestStatus.Acknowledged)) && DateTime.UtcNow <= latestRequest.RequestExpiryTimeUTC)
                {
                    errors.Add(new ErrorInfo()
                    {
                        Message = Utils.GetEnumDescription(ErrorCodes.DuplicatePingRequest), ErrorCode = (int)ErrorCodes.DuplicatePingRequest
                    });
                }
            }
            return(errors);
        }
Example #8
0
        public async Task <DevicePingStatusResponse> PostDevicePingRequest(DevicePingLogRequest request)
        {
            PingRequestStatus        pingRequestStatus = new PingRequestStatus();
            DevicePingStatusResponse response;
            DevicePingLogRequest     validatedRequest = new DevicePingLogRequest();
            Guid devicePingLogUID        = Guid.Empty;
            List <IErrorInfo> errorInfos = new List <IErrorInfo>();

            response = new DevicePingStatusResponse
            {
                AssetUID  = request.AssetUID.ToString(),
                DeviceUID = request.DeviceUID.ToString(),
            };
            errorInfos.AddRange(await base.Validate(this._validators, request));
            errorInfos.AddRange(await this._pingValidator.Validate(request));

            //errorInfos.AddRange(await this.ValidateDuplicateRequest(request));

            var latestRequest = await _devicePingRepository.Fetch(request);

            if (latestRequest != null)
            {
                if (((latestRequest.RequestStatusID == (int)RequestStatus.Pending) || (latestRequest.RequestStatusID == (int)RequestStatus.Acknowledged)) && DateTime.UtcNow <= latestRequest.RequestExpiryTimeUTC)
                {
                    errorInfos.AddRange(new List <IErrorInfo>()
                    {
                        (new ErrorInfo()
                        {
                            Message = Utils.GetEnumDescription(ErrorCodes.DuplicatePingRequest), ErrorCode = (int)ErrorCodes.DuplicatePingRequest
                        })
                    });
                    response.AssetUID             = latestRequest.AssetUID.ToString();
                    response.DeviceUID            = latestRequest.DeviceUID.ToString();
                    response.DevicePingLogUID     = latestRequest.DevicePingLogUID.ToString();
                    response.RequestExpiryTimeUTC = latestRequest.RequestExpiryTimeUTC;
                    response.RequestStatusID      = latestRequest.RequestStatusID;
                    RequestStatus rs = (RequestStatus)latestRequest.RequestStatusID;
                    response.RequestState   = rs.ToString();
                    response.RequestTimeUTC = latestRequest.RequestTimeUTC;
                }
            }

            if (errorInfos.Count <= 0)
            {
                try
                {
                    DeviceTypeFamily deviceTypeFamily = await _devicePingRepository.GetDeviceTypeFamily(request.DeviceUID);

                    if (deviceTypeFamily == null)
                    {
                        errorInfos.Add(new ErrorInfo
                        {
                            ErrorCode = (int)ErrorCodes.DeviceTypeFamilyNotSupported,
                            Message   = Utils.GetEnumDescription(ErrorCodes.DeviceTypeFamilyNotSupported),
                            IsInvalid = true,
                        });
                        response.Errors = errorInfos;
                        return(response);
                    }
                    request.FamilyName = deviceTypeFamily.FamilyName;
                    devicePingLogUID   = _messageConstructor.ProcessMessage(request.AssetUID, request.DeviceUID, deviceTypeFamily.FamilyName);
                    if (devicePingLogUID == Guid.Empty)
                    {
                        errorInfos.Add(new ErrorInfo
                        {
                            ErrorCode = (int)ErrorCodes.UnexpectedError,
                            Message   = Utils.GetEnumDescription(ErrorCodes.UnexpectedError),
                            IsInvalid = true,
                        });
                        response.Errors = errorInfos;
                        return(response);
                    }
                    request.DevicePingLogUID = devicePingLogUID;
                    this._loggingService.Info("Started Invoking DevicePingRepository with request : " + JsonConvert.SerializeObject(request), "DevicePingService.Insert");
                    pingRequestStatus = await this._devicePingRepository.Insert(request);

                    this._loggingService.Info("Ended Invoking DevicePingRepository with response : " + JsonConvert.SerializeObject(pingRequestStatus), "DevicePingService.Insert");
                    response = _mapper.Map <DevicePingStatusResponse>(pingRequestStatus);
                }
                catch (Exception ex)
                {
                    response.Errors.Add(new ErrorInfo
                    {
                        ErrorCode = (int)ErrorCodes.UnexpectedError,
                        Message   = Utils.GetEnumDescription(ErrorCodes.UnexpectedError),
                        IsInvalid = true,
                    });
                    this._loggingService.Error("Exception occurred while saving Ping Request", "PingController.PostDevicePingRequest", ex);
                    throw ex;
                }
            }
            else
            {
                response.Errors = errorInfos;
            }

            return(response);
        }