Esempio n. 1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

                var pkt = new ReadingPacket()
                {
                    Successful = ReadingStatus.Success,
                    Notes      = "This is a nice test"
                };

                for (int i = 0; i < _config.GetValue <int>("Service:Batch"); i++)
                {
                    pkt.Readings.Add(await _factory.Generate(new Random().Next(1, 10000)));
                }

                var result = await Client.AddReadingAsync(pkt);

                if (result.Success == ReadingStatus.Success)
                {
                    _logger.LogInformation("Succesfully sent");
                }
                else
                {
                    _logger.LogInformation("Failed to send");
                }

                await Task.Delay(_config.GetValue <int>("Service:DelayInterval"), stoppingToken);
            }
        }
Esempio n. 2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
                var customerId = _configuration.GetValue <int>("Service:CustomerId");

                var pkt = new ReadingPacket()
                {
                    Successful = MessageStatus.Success,
                    Notes      = "This is our test"
                };

                for (int i = 0; i < 5; ++i)
                {
                    pkt.Readings.Add(await _messageFactory.Generate(customerId));
                }

                var result = await Client.AddReadingAsync(pkt);

                _logger.LogInformation(result.Success == MessageStatus.Success
                    ? "Successfully sent"
                    : "Failed to send");

                await Task.Delay(_configuration.GetValue <int>("Service:DelayInterval"), stoppingToken);
            }
        }
Esempio n. 3
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
        });
    }
Esempio n. 4
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var counter    = 0;
            var customerId = _config.GetValue <int>("Service:CustomerId");

            while (!stoppingToken.IsCancellationRequested)
            {
                counter++;
                //if (counter % 10 == 0)
                //{
                //    Console.WriteLine("Sending Diagnostics");
                //    var stream = Client.SendDiagnostics();
                //    for (var i = 0; i < 3; i++)
                //    {
                //        var reading = await readingFactory.Generate(customerId);
                //        await stream.RequestStream.WriteAsync(reading);
                //    }
                //    await stream.RequestStream.CompleteAsync();
                //}
                var pkt = new ReadingPacket()
                {
                    Successful = ReadingStatus.Success,
                    Notes      = "This is our test"
                };

                for (var x = 0; x < 2; x++)
                {
                    pkt.Readings.Add(await readingFactory.Generate(customerId));
                }
                try
                {
                    if (!NeedsLogin() || await GenerateToken())
                    {
                        var headers = new Metadata();
                        headers.Add("Authorization", $"Bearer {_token}");

                        var result = await Client.AddReadingAsync(pkt, headers : headers);

                        if (result.Success == ReadingStatus.Success)
                        {
                            _logger.LogInformation("Successful sent");
                        }
                        else
                        {
                            _logger.LogInformation("Failed to send");
                        }
                    }
                }
                catch (RpcException ex)
                {
                    _logger.LogError($"Exception thrown: {ex}");
                }

                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
                await Task.Delay(_config.GetValue <int>("Service:DelayInterval"), stoppingToken);
            }
        }
Esempio n. 5
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var counter    = 0;
            var customerId = this.config.GetValue <int>("Service:CustomerId");

            while (!stoppingToken.IsCancellationRequested)
            {
                counter++;

                if (counter % 10 == 0)
                {
                    Console.WriteLine("Sending Diagostics");
                    var stream = Client.SendDiagnostic();

                    for (int i = 0; i < 5; i++)
                    {
                        var reading = await this.readingFactory.Generate(customerId);

                        await stream.RequestStream.WriteAsync(reading);
                    }

                    await stream.RequestStream.CompleteAsync();
                }
                logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);


                var pkt = new ReadingPacket()
                {
                    Successful = ReadingStatus.Success,
                    Notes      = "This is a test"
                };

                for (int i = 0; i < 5; i++)
                {
                    var reading = await this.readingFactory.Generate(customerId);

                    pkt.Readings.Add(reading);
                }

                var result = await Client.AddReadingAsync(pkt);

                if (result.Success == ReadingStatus.Success)
                {
                    logger.LogInformation("Successfully sent");
                }
                else
                {
                    logger.LogInformation("Failed to sent");
                }

                await Task.Delay(this.config.GetValue <int>("Service:DelayInterval"), stoppingToken);
            }
        }
Esempio n. 6
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 readingPack in request.Readings)
                    {
                        if (readingPack.ReadingValue < 1000)
                        {
                            _logger.LogDebug("Reading value below acceptable level");
                            var trailer = new Metadata()
                            {
                                { "BadValue", readingPack.ReadingValue.ToString() },
                                { "Field", "ReadingValue" },
                                { "Message", "Readings are invalid" }
                            };

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

                        var model = new MeterReading
                        {
                            CustomerId  = readingPack.CustomerId,
                            Value       = readingPack.ReadingValue,
                            ReadingDate = readingPack.ReadingTime.ToDateTime()
                        };
                        _repositoryContext.AddEntity(model);
                    }

                    if (await _repositoryContext.SaveAllAsync())
                    {
                        _logger.LogInformation($"Stored {request.Readings.Count} New Readings...");
                        result.Success = ReadingStatus.Success;
                    }
                }
                catch (RpcException)
                {
                    throw;
                }
                catch (Exception e)
                {
                    _logger.LogError($"Exception thrown during the save meter readings: {e}");
                    throw new RpcException(Status.DefaultCancelled, "Exception thrown during the meter reading process");
                }
            }
            return(result);
        }
Esempio n. 7
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

                var customerId = _config.GetValue <int>("Service:CustomerId");

                var packet = new ReadingPacket
                {
                    Successful = ReadingStatus.Success,
                    Notes      = "This is a test"
                };
                //var reading = new ReadingMessage
                //{
                //    CustomerId = customerId,
                //    ReadingValue = 10000,
                //    ReadingTime = Timestamp.FromDateTime(DateTime.UtcNow)
                //};

                for (int x = 0; x < 5; ++x)
                {
                    packet.Readings.Add(await _readingFactory.GenerateAsync(customerId));
                }

                try
                {
                    var result = await client.AddReadingAsync(packet);

                    if (result.Success == ReadingStatus.Success)
                    {
                        _logger.LogInformation("Successfully sent");
                    }
                    else
                    {
                        _logger.LogInformation("Failed to send");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

                await Task.Delay(_config.GetValue <int>("Service:DelayInterval"), stoppingToken);
            }
        }
Esempio n. 8
0
        public override Task <StatusMessage> AddReading(ReadingPacket request, ServerCallContext context)
        {
            var result = new StatusMessage {
                Status = ReadingStatus.Failure
            };

            if (request.Status != ReadingStatus.Success)
            {
                return(Task.FromResult(result));
            }

            try
            {
                foreach (var reading in request.Readings)
                {
                    if (reading.ReadingValue < 1000)
                    {
                        _logger.LogDebug("Reading value is below acceptable value");
                        var trailer = new Metadata
                        {
                            { "BadValue", reading.ReadingValue.ToString() },
                            { "Field", nameof(reading.ReadingValue) },
                            { "Message", "Reading is invalid" }
                        };
                        throw new RpcException(new Status(StatusCode.OutOfRange, "Value is too low"), trailer);
                    }

                    //save to db
                    result.Status = ReadingStatus.Success;
                    _logger.LogInformation($"Reading saved {JsonSerializer.Serialize(reading)}");
                }
            }
            catch (RpcException)
            {
                throw;
            }
            catch (Exception e)
            {
                _logger.LogError($"Exception thrown during saving of readings: {e}");
                throw new RpcException(Status.DefaultCancelled, e.Message);
            }

            return(Task.FromResult(result));
        }
Esempio n. 9
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
                //await Task.Delay(1000, stoppingToken); // do something every second

                var customerId = _config.GetValue <int>("Service:CustomerId");
                var pkt        = new ReadingPacket()
                {
                    Successful = ReadingStatus.Success,
                    Notes      = "This is our test"
                };

                for (var x = 0; x < 5; ++x)
                {
                    pkt.Readings.Add(await _factory.Generate(customerId));
                }

                //moved to ReadingFactory class
                //var reading = new ReadingMessage();
                //reading.CustomerId = customerId;
                //reading.ReadingValue = 10000;
                //reading.ReadingTime = Timestamp.FromDateTime(DateTime.UtcNow);


                var result = await Client.AddReadingAsync(pkt);

                if (result.Success == ReadingStatus.Success)
                {
                    _logger.LogInformation("Successfully sent");
                }
                else
                {
                    _logger.LogInformation("Failed to sent");
                }

                await Task.Delay(_config.GetValue <int>("Service:DelayInterval"), stoppingToken); // do something every second}
            }
        }
Esempio n. 10
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));
        }
Esempio n. 11
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var counter    = 0;
            var customerId = new Random();

            while (!stoppingToken.IsCancellationRequested)
            {
                counter++;

                if (counter % 10 == 0)
                {
                    Console.WriteLine("Sending Diagnostics..");
                    var stream = Client.SendDiagnostics();
                    for (int i = 0; i < 5; i++)
                    {
                        var reading = await _factory.Generate(customerId.Next(1, 10000));

                        await stream.RequestStream.WriteAsync(reading);
                    }

                    await stream.RequestStream.CompleteAsync();
                }

                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

                var pkt = new ReadingPacket()
                {
                    Successful = ReadingStatus.Success,
                    Notes      = "This is a nice test"
                };

                for (int i = 0; i < _config.GetValue <int>("Service:Batch"); i++)
                {
                    pkt.Readings.Add(await _factory.Generate(customerId.Next(1, 10000)));
                }

                try
                {
                    var result = await Client.AddReadingAsync(pkt);

                    if (result.Success == ReadingStatus.Success)
                    {
                        _logger.LogInformation("Succesfully sent");
                    }
                    else
                    {
                        _logger.LogInformation("Failed to send");
                    }
                }
                catch (RpcException ex)
                {
                    //_logger.LogError($"Exception thrown: {ex}"); // basic status error

                    if (ex.StatusCode == StatusCode.OutOfRange)
                    {
                        _logger.LogError($"{ex.Trailers}");
                    }
                    _logger.LogError($"Exception thrown: {ex}"); // custom status error
                }

                await Task.Delay(_config.GetValue <int>("Service:DelayInterval"), stoppingToken);
            }
        }
Esempio n. 12
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var counter    = 0;
            var customerId = _configuration.GetValue <int>("Service:CustomerId");

            while (!stoppingToken.IsCancellationRequested)
            {
                counter++;
                if (counter % 10 == 0)
                {
                    _logger.LogInformation("Sending diagnostics");
                    var stream = Client.SendDiagnostics();
                    for (var i = 0; i < 5; i++)
                    {
                        var reading = await _readingFactory.Generate(customerId);

                        await stream.RequestStream.WriteAsync(reading);
                    }

                    await stream.RequestStream.CompleteAsync();
                }
                var readingPacket = new ReadingPacket
                {
                    Status = ReadingStatus.Success,
                    Notes  = "test"
                };
                for (var i = 0; i < 5; i++)
                {
                    readingPacket.Readings.Add(await _readingFactory.Generate(customerId));
                }

                try
                {
                    //for jwt
                    //if (!NeedsLogin || await GenerateToken())
                    //{
                    //    var headers = new Metadata {{"Authorization", $"Bearer {_token}"}};
                    //    var result = await Client.AddReadingAsync(readingPacket, headers);

                    //    _logger.LogInformation(result.Status == ReadingStatus.Success
                    //        ? "Successfully sent"
                    //        : "Failed to send");
                    //}


                    var result = await Client.AddReadingAsync(readingPacket);

                    _logger.LogInformation(result.Status == ReadingStatus.Success
                        ? "Successfully sent"
                        : "Failed to send");
                }
                catch (RpcException e)
                {
                    if (e.StatusCode == StatusCode.OutOfRange)
                    {
                        _logger.LogError($" {e.Trailers}");
                    }
                    _logger.LogError($"Exception thrown {e}");
                }

                await Task.Delay(_configuration.GetValue <int>("Service:DelayInterval"), stoppingToken);
            }
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var counter    = 0;
            var customerId = _configuration.GetValue <int>("Service:CustomerId");


            while (!stoppingToken.IsCancellationRequested)
            {
                counter++;

                //if (counter % 10 == 0)
                //{
                //    Console.WriteLine("Sending Diagnostics");
                //    var stream = Client.SendDiagnostics();
                //    for (int i = 0; i < 5; i++)
                //    {
                //        var readingRes = await _factory.Generate(customerId);
                //        await stream.RequestStream.WriteAsync(readingRes);
                //    }

                //    await stream.RequestStream.CompleteAsync();
                //}

                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);


                var pkt = new ReadingPacket()
                {
                    Successful = ReadingStatus.Success,
                    Notes      = "This is our test"
                };

                var reading = new ReadingMessage();
                reading.CustomerId   = customerId;
                reading.ReadingValue = 10000;
                reading.ReadingTime  = Timestamp.FromDateTime(DateTime.UtcNow);

                for (int i = 0; i < 5; i++)
                {
                    pkt.Readings.Add(await _factory.Generate(customerId));
                }

                try
                {
                    //if (!NeedsLogin() || await GenerateToken())
                    //{
                    //    var headers = new Metadata();
                    //    headers.Add("Authorization", $"Bearer {_token}");

                    var result = await Client.AddReadingAsync(pkt); // headers: headers);

                    if (result.Successful == ReadingStatus.Success)
                    {
                        _logger.LogInformation("Successfully sent");
                    }
                    else
                    {
                        _logger.LogInformation("Failed to send");
                    }
                    //}
                }
                catch (RpcException ex)
                {
                    if (ex.StatusCode == StatusCode.OutOfRange)
                    {
                        _logger.LogError($"{ex.Trailers}");
                    }
                    _logger.LogError($"Exception Thrown: {ex}");
                }

                await Task.Delay(_configuration.GetValue <int>("Service:DelayInterval"), stoppingToken);
            }
        }
Esempio n. 14
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var counter    = 0;
            var customerId = _configuration.GetValue <int>("Service:CustomerId");

            while (!stoppingToken.IsCancellationRequested)
            {
                counter++;
                //if (counter % 10 == 0)
                //{
                //    Console.WriteLine("Sending Diagnostics");
                //    var stream = Client.SendDiagnostics();
                //    for (int x = 0; x < 5; x++)
                //    {
                //        var reading = await _factory.Generate(customerId);
                //        await stream.RequestStream.WriteAsync(reading);
                //    }

                //    //we finish writing to you
                //    await stream.RequestStream.CompleteAsync();
                //}
                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);



                var pkt = new ReadingPacket()
                {
                    Successful = ReadingStatus.Success,
                    Notes      = "this a test"
                };


                for (int x = 0; x < 5; x++)
                {
                    pkt.Readings.Add(await _factory.Generate(customerId));
                }

                try
                {
                    ////used only for JWT authentication
                    //if (!NeedsLogin() || await GenerateToken())
                    //{
                    //    var headers = new Metadata();
                    //    headers.Add("Authorization", $"Bearer {_token}");

                    //var result = await Client.AddReadingAsync(pkt, headers: headers);

                    var result = await Client.AddReadingAsync(pkt);

                    if (result.Success == ReadingStatus.Success)
                    {
                        _logger.LogInformation("Successfully sent");
                    }
                    else
                    {
                        _logger.LogError("Failed to send");
                    }
                    //}
                }
                catch (RpcException e)
                {
                    if (e.StatusCode == StatusCode.OutOfRange)
                    {
                        _logger.LogError($"{e.Trailers}");
                    }
                    Console.WriteLine(e);
                    throw;
                }


                await Task.Delay(_configuration.GetValue <int>("Service:DelayInterval"), stoppingToken);
            }
        }
Esempio n. 15
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));
        }