Esempio n. 1
0
        public async Task UpdateConsumer(long consumer, long outageId)
        {
            while (!ReliableDictionariesInitialized)
            {
                await Task.Delay(1000);
            }

            using (var unitOfWork = new UnitOfWork())
            {
                try
                {
                    var consumerHistoricals = new List <ConsumerHistorical>(1);

                    if (await UnenergizedConsumers.ContainsKeyAsync(consumer))
                    {
                        consumerHistoricals.Add(new ConsumerHistorical()
                        {
                            OutageId = outageId, ConsumerId = consumer, OperationTime = DateTime.Now, DatabaseOperation = DatabaseOperation.UPDATE
                        });
                        //await UnenergizedConsumers.SetAsync(consumer, 0);
                    }

                    unitOfWork.ConsumerHistoricalRepository.AddRange(consumerHistoricals);
                    unitOfWork.Complete();
                }
                catch (Exception e)
                {
                    string message = $"{baseLogString} UpdateConsumer => Exception: {e.Message}";
                    Logger.LogError(message, e);
                }
            }
        }
        public async Task OnConsumerBlackedOut(List <long> consumers, long?outageId)
        {
            while (!ReliableDictionariesInitialized)
            {
                await Task.Delay(1000);
            }

            using (var unitOfWork = new UnitOfWork())
            {
                try
                {
                    var consumerHistoricals = new List <ConsumerHistorical>();

                    foreach (var consumer in consumers)
                    {
                        if (!await UnenergizedConsumers.ContainsKeyAsync(consumer))
                        {
                            consumerHistoricals.Add(new ConsumerHistorical()
                            {
                                OutageId = outageId, ConsumerId = consumer, OperationTime = DateTime.Now, DatabaseOperation = DatabaseOperation.INSERT
                            });
                            await UnenergizedConsumers.SetAsync(consumer, 0);
                        }
                    }

                    unitOfWork.ConsumerHistoricalRepository.AddRange(consumerHistoricals);
                    unitOfWork.Complete();
                }
                catch (Exception e)
                {
                    string message = $"{baseLogString} OnConsumersBlackedOut => Exception: {e.Message}";
                    Logger.LogError(message, e);
                }
            }
        }
Esempio n. 3
0
        public async Task OnConsumersEnergized(HashSet <long> consumers)
        {
            while (!ReliableDictionariesInitialized)
            {
                await Task.Delay(1000);
            }

            using (var unitOfWork = new UnitOfWork())
            {
                try
                {
                    var consumerHistoricals = new List <ConsumerHistorical>();
                    var copy             = (await UnenergizedConsumers.GetDataCopyAsync()).Keys.ToList();
                    var changedConsumers = copy.Intersect(consumers).ToList();

                    foreach (var consumer in changedConsumers)
                    {
                        var consumerHistorical = new ConsumerHistorical()
                        {
                            ConsumerId        = consumer,
                            OperationTime     = DateTime.Now,
                            DatabaseOperation = DatabaseOperation.DELETE,
                        };

                        consumerHistoricals.Add(consumerHistorical);
                    }

                    foreach (var changed in changedConsumers)
                    {
                        if (await UnenergizedConsumers.ContainsKeyAsync(changed))
                        {
                            await UnenergizedConsumers.TryRemoveAsync(changed);
                        }
                    }

                    unitOfWork.ConsumerHistoricalRepository.AddRange(consumerHistoricals);
                    unitOfWork.Complete();
                }
                catch (Exception e)
                {
                    string message = $"{baseLogString} OnConsumersEnergized => Exception: {e.Message}";
                    Logger.LogError(message, e);
                }
            }
        }