Example #1
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()
                {
                    Notes = "Successfully added to the database.",
                    Status = ReadingStatus.Success
                });
            }
        }

        _logger.LogError("Failed to Saved new Readings...");
        return(new StatusMessage()
        {
            Notes = "Failed to store readings in Database",
            Status = ReadingStatus.Success
        });
    }
Example #2
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);
        }
Example #3
0
        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);
        }
Example #4
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);
        }
Example #5
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);
        }
Example #6
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(ReadingPacketMessage request, ServerCallContext context)
        {
            var result = new StatusMessage
            {
                Success = ReadingStatus.Failure
            };

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

                            var trailer = new Metadata()
                            {
                                { "ReadingValue", reading.ReadingValue.ToString() },
                                { "Message", "Reading Value Invalid" }
                            };

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

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

                        _readingRepository.AddEntity(readingEntity);
                    }

                    if (await _readingRepository.SaveAllAsync())
                    {
                        result.Success = ReadingStatus.Success;
                        _logger.LogInformation($"Added {request.Readings.Count} for {request.Readings.First().CustomerId}");
                    }
                }
                catch (RpcException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    _logger.LogError($"An error occurred while saving meter readings: {ex}");
                    throw new RpcException(Status.DefaultCancelled, "An error occurred while saving meter readings.");
                }
            }

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

            try
            {
                if (request.Succesful == ReadingStatus.Success)
                {
                    foreach (var r in request.Readings)
                    {
                        if (r.ReadinValue < 1000)
                        {
                            _logger.LogError("RPC Exception - Out of range");
                            // Keys must not contain spaces
                            var trailer = new Metadata()
                            {
                                { "BadValue", r.ReadinValue.ToString() },
                                { "Field", "ReadingValue" },
                                { "CustomerId", r.CustomerId.ToString() }
                            };
                            throw new RpcException(new Status(StatusCode.OutOfRange, "Value too low"), trailer);
                        }

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

                        _repository.AddEntity(reading);
                    }

                    if (await _repository.SaveAllAsync())
                    {
                        result.Success = ReadingStatus.Success;
                    }
                }
            }
            catch (RpcException)
            {
                throw;
            }
            catch (Exception error)
            {
                _logger.LogError($"Exception thrown: {error.Message}");
                throw new RpcException(Status.DefaultCancelled, $"Exception thrown: {error.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);
        }
Example #10
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);
        }
Example #11
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);
        }
Example #12
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)
                    {
                        var reading = new MeterReading()
                        {
                            CustomerId = r.CustomerId,
                            Value      = r.ReadingValue,
                            Time       = r.ReadingTime.ToDateTime()
                        };
                        _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($"Expecion thrown during of reading {ex}");
                }
            }


            return(await Task.FromResult(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);
        }
Example #14
0
        public override async Task <StatusMessage> AddReading(Readingpacket request, ServerCallContext context)
        {
            var result = new StatusMessage
            {
                ReadingStatus = ReadingStatus.Failure
            };

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

                    if (await _readingRepository.SaveAllAsync())
                    {
                        result.ReadingStatus = ReadingStatus.Success;
                    }
                }
                catch (Exception ex)
                {
                    result.Message = "An error happened while importing readings";
                    _logger.LogError($"An error happened while importing readings: {ex}");
                }
            }

            return(result);
        }
Example #15
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;
        }
Example #16
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)
                    {
                        //---------test
                        if (r.ReadingValue < 1000)
                        {
                            _logger.LogDebug("Reading Value below acceptable level");
                            //throw new RpcException(Status.DefaultCancelled, "Value too low.."); => basic status error

                            #region custom status error
                            var trailer = new Metadata()
                            {
                                { "BadValue", r.ReadingValue.ToString() },
                                { "Field", "ReadingValue" },
                                { "Message", "Readings are invalid" }
                            };
                            throw new RpcException(new Status(StatusCode.OutOfRange, "Value too low")); // => custom status error
                            #endregion
                        }
                        //---------test

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

                    if (await _repository.SaveAllAsync())
                    {
                        _logger.LogInformation($"Stored {request.Readings.Count} new readings...");
                        result.Success = ReadingStatus.Success;
                    }
                }
                //test
                catch (RpcException)
                {
                    throw;
                }
                //
                catch (Exception ex)
                {
                    _logger.LogError($"Expecion thrown during of reading {ex}");
                    throw new RpcException(Status.DefaultCancelled, "Exception thrown during process");
                }
            }


            return(await Task.FromResult(result));
        }