Exemple #1
0
        public async override Task <StatusMessage> AddReading(ReadingPacket request, ServerCallContext context)
        {
            var result = new StatusMessage()
            {
                Success = ReadingStatus.Failure
            };

            try
            {
                if (request.Successful == ReadingStatus.Success)
                {
                    foreach (var reading in request.Readings)
                    {
                        var read = new MeterReading
                        {
                            CustomerId  = reading.CustomerId,
                            Value       = reading.ReadingValue,
                            ReadingDate = reading.ReadingTime.ToDateTime()
                        };
                        readingRepository.AddEntity(read);
                    }
                    if (await readingRepository.SaveAllAsync())
                    {
                        result.Success = ReadingStatus.Success;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = "Exception throwing during process";
                logger.LogError($"Error occurred while processing : {ex}");
            }

            return(result);
        }
        public override async Task <StatusMessage> AddReading(ReadingPacket request, ServerCallContext context)
        {
            var result = new StatusMessage
            {
                Success = MessageStatus.Failure
            };

            if (request.Successful == MessageStatus.Success)
            {
                try
                {
                    foreach (var reading in request.Readings)
                    {
                        _readingRepository.AddEntity(new MeterReading
                        {
                            CustomerId  = reading.CustomerId,
                            ReadingDate = reading.ReadingTime.ToDateTime(),
                            Value       = reading.ReadingValue
                        });

                        if (await _readingRepository.SaveAllAsync())
                        {
                            result.Success = MessageStatus.Success;
                        }
                    }
                }
                catch (Exception e)
                {
                    result.Message = "Exception throw during process";
                    _logger.LogError($"Exception throw during process {e.Message}");
                }
            }

            return(result);
        }
Exemple #3
0
        public override async Task <StatusMessage> AddReading(ReadingPacket request, ServerCallContext context)
        {
            var result = new StatusMessage()
            {
                Success = ReadingStatus.Failure
            };

            if (request.Successful == ReadingStatus.Success)
            {
                try
                {
                    foreach (var r in request.Readings)
                    {
                        if (r.ReadingValue < 1000)
                        {
                            _logger.LogDebug("Reading value below acceptable level");
                            //throw new RpcException(Status.DefaultCancelled, "Value too low");
                            var trailer = new Metadata()
                            {
                                { "BadValue", r.ReadingValue.ToString() },
                                { "Field", "ReadingValue" },
                                { "Message", "Readings are invalid" }
                            };
                            throw new RpcException(new Status(StatusCode.OutOfRange, "Value too low"));
                        }

                        //Save into db
                        var reading = new MeterReading()
                        {
                            Value       = r.ReadingValue,
                            ReadingDate = r.ReadingTime.ToDateTime(),
                            CustomerId  = r.CustomerId
                        };

                        _repository.AddEntity(reading);

                        if (await _repository.SaveAllAsync())
                        {
                            _logger.LogInformation($"{request.Readings.Count} New Readings ...");
                            result.Success = ReadingStatus.Success;
                        }
                    }
                }
                catch (RpcException)
                {
                    throw;
                }
                catch (Exception e)
                {
                    //result.Message = e.Message;
                    _logger.LogError($"Exception thrown during saving of readings:{e}");
                    throw  new RpcException(Status.DefaultCancelled, "Exception thrown during process");
                }
            }


            //return base.AddReading(request, context);
            return(result);
        }
Exemple #4
0
        public override async Task <StatusMessage> AddReading(ReadingPacket request, ServerCallContext context)
        {
            _logger.LogInformation($"AddReading function executed....");

            var result = new StatusMessage
            {
                Successful = ReadingStatus.Failure
            };

            if (request.Successful == ReadingStatus.Success)
            {
                try
                {
                    foreach (var item in request.Readings)
                    {
                        if (item.ReadingValue < 1000)
                        {
                            var trailer = new Metadata
                            {
                                { "bad_value", item.ReadingValue.ToString().ToLower() },
                                { "message", "values are too low" }
                            };
                            _logger.LogDebug("reading value below acceptable level");
                            throw new RpcException(new Status(StatusCode.OutOfRange, "Value too low"), trailer);
                        }


                        //save to the database
                        var reading = new MeterReading()
                        {
                            Value       = item.ReadingValue,
                            ReadingDate = item.ReadingTime.ToDateTime(),
                            CustomerId  = item.CustomerId,
                        };
                        repository.AddEntity(reading);
                        _logger.LogInformation($"Adding Entity....{reading}");
                    }

                    if (await repository.SaveAllAsync())
                    {
                        _logger.LogInformation($"Stored {request.Readings.Count} New Readings....");
                        result.Successful = ReadingStatus.Success;
                    }
                }
                catch (RpcException)
                {
                    throw;
                }
                catch (System.Exception exception)
                {
                    _logger.LogError($"Exception thrown during saving of readings: {exception}");
                    throw new RpcException(Status.DefaultCancelled, $"exception thrown during process {exception}");
                }
            }

            return(result);
        }
Exemple #5
0
        public async override Task <StatusMessage> AddReading(ReadingPacket request,
                                                              ServerCallContext context)
        {
            var result = new StatusMessage()
            {
                Success = ReadingStatus.Failure
            };

            if (request.Successful == ReadingStatus.Success)
            {
                try
                {
                    foreach (var r in request.Readings)
                    {
                        if (r.ReadingValue < 1000)
                        {
                            _logger.LogDebug("Reading value below acceptable level");

                            var trailer = new Metadata()
                            {
                                { "BadValue", r.ReadingValue.ToString() },
                                { "Field", "ReadingValue" },
                                { "Message", "Readings are invalid" }
                            };

                            throw new RpcException(new Status(StatusCode.OutOfRange, "Value too low"), trailer);
                        }

                        var reading = new MeterReading()
                        {
                            Value       = r.ReadingValue,
                            ReadingDate = r.ReadingTime.ToDateTime(),
                            CustomerId  = r.CustomerId
                        };

                        _repository.AddEntity(reading);
                    }

                    if (await _repository.SaveAllAsync())
                    {
                        result.Success = ReadingStatus.Success;
                    }
                }
                catch (RpcException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    _logger.LogError($"Exception: {ex.Message}");
                    throw new RpcException(Status.DefaultCancelled, $"Exception occured: {ex.Message}");
                }
            }

            return(result);
        }
        public async override Task <StatusMessage> AddReading(ReadingPacket request,
                                                              ServerCallContext context)
        {
            var result = new StatusMessage()
            {
                Success = ReadingStatus.Failure
            };

            if (request.Successful == ReadingStatus.Success)
            {
                try
                {
                    foreach (var r in request.Readings)
                    {
                        var reading = new MeterReading()
                        {
                            Value       = r.ReadingValue,
                            ReadingDate = r.ReadingTime.ToDateTime(),
                            CustomerId  = r.CustomerId
                        };

                        if (r.ReadingValue < 1000)
                        {
                            logger.LogDebug("Reading Value Below Acceptable Level");
                            var trailer = new Metadata()
                            {
                                { "bad-value", r.ReadingValue.ToString() },
                                { "field", "ReadingValue" },
                                { "message", "Readings are Invalid" }
                            };
                            throw new RpcException(new Status(StatusCode.OutOfRange, "Value Too Low"));
                        }
                        repository.AddEntity(reading);
                    }
                    if (await repository.SaveAllAsync())
                    {
                        logger.LogInformation($"Stored {request.Readings.Count} New Readings....");
                        result.Success = ReadingStatus.Success;
                    }
                }
                catch (RpcException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    logger.LogError($"Exception thrown during saving of reading: {ex}");
                    throw new RpcException(Status.DefaultCancelled, "Exception thrown during process");
                }
            }

            return(result);
        }
Exemple #7
0
        public override async Task <StatusMessage> AddReading(ReadingPacket request, ServerCallContext context)
        {
            var result = new StatusMessage
            {
                Successful = ReadingStatus.Failure
            };

            if (request.Successful == ReadingStatus.Success)
            {
                try
                {
                    foreach (var r in request.Readings)
                    {
                        if (r.ReadingValue < 1000)
                        {
                            _logger.LogDebug("Reading Value below acceptable level");
                            var trailer = new Metadata
                            {
                                { "BadValue", r.ReadingValue.ToString() },
                                { "Field", "ReadingValue" },
                                { "Message", "Readings are invalid" }
                            };
                            throw new RpcException(Status.DefaultCancelled, trailer, "Value too low");
                        }

                        //save to the database
                        var reading = new MeterReading
                        {
                            Value       = r.ReadingValue,
                            ReadingDate = r.ReadingTime.ToDateTime(),
                            CustomerId  = r.CustomerId
                        };

                        _repository.AddEntity(reading);
                    }

                    if (await _repository.SaveAllAsync())
                    {
                        _logger.LogInformation($"Stored {request.Readings.Count} New Readings...");
                        result.Successful = ReadingStatus.Success;
                    }
                }
                catch (Exception ex)
                {
                    result.Message = "Exception thrown during process";
                    _logger.LogError($"Exception thrown during saving of readings: {ex}");
                }
            }

            return(result);
        }
Exemple #8
0
        public async override Task <StatusMessage> AddReading(ReadingPacket request, ServerCallContext context)
        {
            var result = new StatusMessage()
            {
                Success = ReadingStatus.Failure
            };

            if (request.Successful == ReadingStatus.Success)
            {
                try
                {
                    foreach (var r in request.Readings)
                    {
                        //save to the DB
                        var reading = new MeterReading()
                        {
                            Value       = r.ReadingValue,
                            ReadingDate = r.ReadingTime.ToDateTime(),
                            CustomerId  = r.CustomerId
                        };

                        _repository.AddEntity(reading);
                    }

                    if (await _repository.SaveAllAsync())
                    {
                        _logger.LogInformation($"Stored {request.Readings.Count} New Readings...");
                        result.Success = ReadingStatus.Success;
                    }
                }
                catch (Exception ex)
                {
                    result.Message = "Exception thrown during process";
                    _logger.LogError($"Exception thrown during saving of readings:{ex}");
                }
            }

            //return Task.FromResult(result); since using async, doesn't need to Task.FromResult
            return(result);
        }
        public async override Task <StatusMessage> AddReading(ReadingPacket request, ServerCallContext context)
        {
            var result = new StatusMessage()
            {
                Success = ReadingStatus.Failure
            };

            if (request.Success == ReadingStatus.Success)
            {
                try
                {
                    foreach (var r in request.Readings)
                    {
                        //Save to the database
                        var reading = new MeterReading()
                        {
                            Value       = r.ReadingValue,
                            ReadingDate = r.ReadingTime.ToDateTime(),
                            CustomerId  = r.CustomerId
                        };

                        _repository.AddEntity(reading);
                    }

                    if (await _repository.SaveAllAsync())
                    {
                        result.Success = ReadingStatus.Success;
                    }
                }
                catch (Exception ex)
                {
                    result.Message = "Exception thrown during process";
                    _logger.LogError($"Exception thrown during saving of readings: {ex}");
                }
            }

            return(result);
        }
Exemple #10
0
        public override Task <StatusMessage> AddReading(ReadingPacket request, ServerCallContext context)
        {
            var result = new StatusMessage()
            {
                Success = ReadingStatus.Failure
            };

            if (request.Successful == ReadingStatus.Success)
            {
                try
                {
                    foreach (var reading in request.Readings)
                    {
                        // Save to database
                    }
                }
                catch (Exception ex)
                {
                    result.Message = $"Exception: {ex.Message}";
                }
            }

            return(Task.FromResult(result));
        }
Exemple #11
0
        public override async Task <StatusMessage> AddReading(ReadingPacket request, ServerCallContext context)
        {
            if (request.Successful == ReadingStatus.Success)
            {
                foreach (var reading in request.Readings)
                {
                    var readingValue = new MeterReading()
                    {
                        CustomerId  = reading.CustomerId,
                        Value       = reading.ReadingValue,
                        ReadingDate = reading.ReadingTime.ToDateTime()
                    };

                    _logger.LogInformation($"Adding {reading.ReadingValue}");
                    _repository.AddEntity(readingValue);
                }

                if (await _repository.SaveAllAsync())
                {
                    _logger.LogInformation("Successfully Saved new Readings...");
                    return(new StatusMessage()
                    {
                        Message = "Successfully added to the database.",
                        Status = ReadingStatus.Success
                    });
                }
            }

            _logger.LogError("Failed to Saved new Readings...");
            return(new StatusMessage()
            {
                Message = "Failed to store readings in Database",
                Status = ReadingStatus.Success
            });
            //var result = new StatusMessage {
            //    Success = ReadingStatus.Success
            //};

            //if (request.Successful == ReadingStatus.Success)
            //{
            //    try
            //    {
            //        foreach(var reading in request.Readings)
            //        {
            //            var data = new MeterReading
            //            {
            //                Value = reading.ReadingValue,
            //                ReadingDate = reading.ReadingTime.ToDateTime(),
            //                CustomerId = reading.CustomerId
            //            };

            //            _repository.AddEntity(data);
            //        }

            //        if (await _repository.SaveAllAsync())
            //        {
            //            result.Success = ReadingStatus.Success;
            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //        result.Message = "Exception being thrown during process";
            //        _logger.LogError($"Exception thrown during saving of readings: {ex}");
            //    }
            //}

            //return result;
        }