private static void CreateGridProsumer(ref List <Prosumer> producers, ref List <Prosumer> consumers, float netElectricity) { if (netElectricity > 0) { //Create smartGrid is a producer, nationalGrid is a consumer Prosumer smartGrid = new Prosumer() { Consumed = 0, Name = "SmartGrid", PreferedBuyerName = "", Produced = netElectricity, Difference = netElectricity, Remainder = 0 }; Prosumer nationalGrid = new Prosumer() { Consumed = netElectricity, Name = "NationalGrid", PreferedBuyerName = "SmartGrid", Produced = 0, Difference = 0 - netElectricity, Remainder = 0 - netElectricity }; //Electricity in smart grid is positive, send to global grid ProsumersController pCtrl = new ProsumersController(); pCtrl.UpdateGrids(new ProsumerDTO[] { new ProsumerDTO(smartGrid), new ProsumerDTO(nationalGrid) }); consumers.Add(nationalGrid); } else if (netElectricity < 0) { //Electricity is negative, import power Prosumer smartGrid = new Prosumer() { Consumed = netElectricity, Name = "SmartGrid", PreferedBuyerName = "NationalGrid", Produced = 0, Difference = 0 - netElectricity, Remainder = 0 }; Prosumer nationalGrid = new Prosumer() { Consumed = 0, Name = "NationalGrid", PreferedBuyerName = "", Produced = netElectricity, Difference = netElectricity, Remainder = netElectricity }; //Electricity in smart grid is positive, send to global grid ProsumersController pCtrl = new ProsumersController(); pCtrl.UpdateGrids(new ProsumerDTO[] { new ProsumerDTO(smartGrid), new ProsumerDTO(nationalGrid) }); producers.Add(nationalGrid); } }
public static async Task HandleTransactions() { //Do all transactions here using (var prosumerDb = new SmartGridContext()) { var uow = new UnitOfWork <Prosumer>(prosumerDb); var smartGridUow = new UnitOfWork <SmartGridModel>(prosumerDb); float netElectricity = 0; float produced = 0; float consumed = 0; List <Prosumer> consumers = new List <Prosumer>(); List <Prosumer> producers = new List <Prosumer>(); List <Transaction> transactions = new List <Transaction>(); //Go through all prosumers and get foreach (var prosumer in uow.Repository.ReadAll()) { if (prosumer.Type == "Grid") { continue; } netElectricity += prosumer.Difference; consumed += prosumer.Consumed; produced += prosumer.Produced; if (prosumer.Difference > 0) { //Add to list of producers if more electricity is produced than consumed producers.Add(prosumer); } else if (prosumer.Difference < 0) { //Add to list of consumres if more electricity is consumed rather than produced consumers.Add(prosumer); } //Otherwise do not add to either consumers or producers } //CreateGridProsumer(ref producers, ref consumers, netElectricity); bool profit = DistributePowerBetweenMultiple(ref producers, ref consumers, ref transactions); Prosumer smartGrid = null; Prosumer nationalGrid = null; if (profit) { smartGrid = new Prosumer() { Name = "SmartGrid", PreferedBuyerName = "NationalGrid", Type = "Grid", Produced = netElectricity, Consumed = 0, Difference = netElectricity, Remainder = netElectricity }; nationalGrid = new Prosumer() { Name = "NationalGrid", PreferedBuyerName = "SmartGrid", Type = "Grid", Produced = 0, Consumed = netElectricity, Difference = 0 - netElectricity, Remainder = 0 - netElectricity, }; //More power is produced than consumed, sell the rest to SmartGrid and then National Grid SellPowerToSmartGrid(ref producers, ref transactions, smartGrid); producers.Add(smartGrid); consumers.Add(nationalGrid); DistributePowerBetweenMultiple(ref producers, ref consumers, ref transactions); } else { netElectricity = Math.Abs(netElectricity); float price = BitcoinPriceGetter.GetPrice(); smartGrid = new Prosumer() { Name = "SmartGrid", PreferedBuyerName = "NationalGrid", Type = "Grid", Consumed = netElectricity, Produced = 0, Difference = 0 - netElectricity, Remainder = 0 - netElectricity }; nationalGrid = new Prosumer() { Name = "NationalGrid", PreferedBuyerName = "SmartGrid", Type = "Grid", Consumed = 0, Produced = netElectricity, Difference = netElectricity, Remainder = netElectricity, }; //Save transaction transactions.Add(new Transaction() { Id = string.Format("{0}-{1}-{2}_{3}_{4}", DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year, nationalGrid.Name, smartGrid.Name), Consumer = smartGrid.Name, Producer = nationalGrid.Name, TransactionDate = DateTime.Now, KwhAmount = nationalGrid.Produced, PricePerKwh = price, TotalPrice = nationalGrid.Produced * price }); smartGrid.Remainder = netElectricity; nationalGrid.Remainder = 0; producers.Add(smartGrid); DistributePowerBetweenMultiple(ref producers, ref consumers, ref transactions); //More power is consumed, buy the rest from National grid } SmartGridModelsController smartCtrl = new SmartGridModelsController(); smartCtrl.PostSmartGridModel(new SmartGridModel() { TimeStamp = DateTime.Now, TotalGeneration = produced, TotalForbrug = consumed, Brutto = produced - consumed, }); ProsumersController pCtrl = new ProsumersController(); pCtrl.UpdateGrids(new ProsumerDTO[] { new ProsumerDTO(smartGrid), new ProsumerDTO(nationalGrid) }); TransactionController ctrl = new TransactionController(); await ctrl.Post(transactions); uow.Commit(); //Commit at the end } }