Example #1
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);
            }
        }
Example #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");

                Readingpacket request = new Readingpacket
                {
                    ReadingStatus = ReadingStatus.Success,
                    Notes         = "test test"
                };

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

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

                        var statusMessage = await Client.AddReadingAsync(request, headers : headers);

                        if (statusMessage.ReadingStatus == ReadingStatus.Success)
                        {
                            _logger.LogInformation("successfully saved");
                        }
                        else
                        {
                            _logger.LogInformation($"failed to save -- {statusMessage.Message}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.Message);
                }



                await Task.Delay(_configuration.GetValue <int>("Service:DelayInterval"), stoppingToken);
            }
        }
Example #3
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 stream = Client.SendDiagnostics();
                for (int i = 0; i < 5; i++)
                {
                    await stream.RequestStream.WriteAsync(await _readingFactory.Generate(customerId));
                }
                await stream.RequestStream.CompleteAsync();

                await Task.Delay(_configuration.GetValue <int>("Service:DelayInterval"), stoppingToken);
            }
        }
Example #4
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);
            }
        }
Example #5
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}
            }
        }
        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);
            }
        }
Example #7
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);
            }
        }
Example #8
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("Send Diagnostics");
                    var stream = Client.SendDiagnostics();
                    for (var x = 0; x < 5; x++)
                    {
                        var reading = await _readingFactory.Generate(customerId);

                        await stream.RequestStream.WriteAsync(reading);
                    }

                    await stream.RequestStream.CompleteAsync();
                }

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


                var packet = new ReadingPaket
                {
                    Successful = ReadingStatus.Success,
                    Notes      = "This is our test"
                };

                for (int i = 0; i < 5; i++)
                {
                    packet.Readings.Add(
                        await _readingFactory.Generate(customerId));
                }
                try
                {
                    var result = await Client.AddReadingAsync(packet);

                    if (result.Success == ReadingStatus.Success)
                    {
                        _logger.LogInformation("Successfully send");
                    }
                    else
                    {
                        _logger.LogInformation("Failed to send");
                    }
                }
                catch (RpcException ex)
                {
                    if (ex.StatusCode == StatusCode.OutOfRange)
                    {
                        _logger.LogError($"{ex.Trailers}");
                    }
                    _logger.LogError("Exception thrown: {exception}", ex.ToString());
                }

                await Task.Delay(_config.GetValue <int>("Service:DelayInterval"), stoppingToken);
            }
        }