Example #1
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);
        }
Example #2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
                var packet = new ReadingPackets()
                {
                    Notes     = "This is called from Worker",
                    Succesful = ReadingStatus.Success,
                };
                for (int index = 0; index < 5; index++)
                {
                    packet.Readings.Add(_readingFactory.Generate());
                }

                try
                {
                    var headers = new Metadata
                    {
                        { "Authorization", $"Bearer {await GenerateToken()}" }
                    };
                    var result = Client.AddReading(packet, headers: headers);
                    if (result.Success == ReadingStatus.Success)
                    {
                        _logger.LogInformation($"Success: {result.Message}");
                    }
                    else
                    {
                        _logger.LogInformation($"Failed: {result.Message}");
                    }
                }
                catch (RpcException exception)
                {
                    _logger.LogError($"RPC Exception - Status Code: {exception.StatusCode}");
                    _logger.LogError($"{exception.Trailers}");
                    _logger.LogError(exception.Message);
                }
                await Task.Delay(_config.GetValue <int>("Service:DelayInternal"), stoppingToken);
            }
        }