static void Main(string[] args) { TradersRepoController mTradersRepoController = new TradersRepoController(); List <Trader> GetAllTraders = mTradersRepoController.GetTradersAsync().Result.ToList(); foreach (var v in GetAllTraders) { Console.WriteLine(v); } }
private static void ClearTraders(TradersRepoController tradersRepoController) { List <Trader> GetAllTraders = tradersRepoController.GetAllTradersAsync().Result.ToList(); int counter = 0; foreach (var v in GetAllTraders) { counter++; tradersRepoController.Delete(v.Id).Wait(); Console.WriteLine("Deleted " + counter + "/" + GetAllTraders.Count + " Trader with id = " + v.Id); } }
static void Main(string[] args) { // Variables TradersRepoController mTradersRepoController = new TradersRepoController(); ProsumerController mProsumerController = new ProsumerController(); SmartGridsController mSmartGridsController = new SmartGridsController(); // Clear SmartGrid ClearSmartGrid(mSmartGridsController); // Clear Trades ClearTraders(mTradersRepoController); /// Clear Prosumers ClearProsumers(mProsumerController); /// Populate Prosumers PopulateProsumers(mProsumerController); // Find Producer and Consumer List <Prosumer> mProducerList = mProsumerController.GetProsumerWithProduction("Overproducing").Result.ToList(); List <Prosumer> mConsumerList = mProsumerController.GetProsumerWithProduction("Underproducing").Result.ToList(); // Insert Producers and Consumer into SmartGrid object SmartGrid mSmartGrid = new SmartGrid(); mSmartGrid.Producers = mProducerList; mSmartGrid.Consumers = mConsumerList; // Post SmartGrid to SmartGrid Database mSmartGridsController.Post(mSmartGrid).Wait(); // Get All from SmartGrid var AllSmartGrid = mSmartGridsController.GetAllSmartGridsAsync().Result.LastOrDefault(); var mProducers = AllSmartGrid.Producers; var mConsumers = AllSmartGrid.Consumers; // Calculate Producers- and Consumers kWhAmount var ProducersAmount = 0; var ConsumersAmount = 0; foreach (var v in mProducers) { ProducersAmount += v.KWhAmount; } foreach (var v in mConsumers) { ConsumersAmount += v.KWhAmount; } var difference = Math.Abs(ProducersAmount) - Math.Abs(ConsumersAmount); Console.WriteLine("\nDifference: " + difference); Console.WriteLine("Producers Amount: " + Math.Abs(ProducersAmount)); Console.WriteLine("Consumers Amount: " + Math.Abs(ConsumersAmount)); // Let's Trade int loopCounter = 0; foreach (var consumer in mConsumers) { loopCounter++; Console.WriteLine("\n******************** Consumer " + loopCounter + " *******************\n"); foreach (var producer in mProducers) { if (consumer.KWhAmount == 0) { continue; } if (producer.KWhAmount == 0) { continue; } //Console.WriteLine(">> " + producer.Name + " is selling " + Math.Abs(producer.KWhAmount) + " kWh"); //Console.WriteLine(">> " + consumer.Name + " is buying " + Math.Abs(consumer.KWhAmount) + " kWh\n"); if (Math.Abs(consumer.KWhAmount) < producer.KWhAmount) { producer.KWhAmount += consumer.KWhAmount; Console.WriteLine(">>>> TRADE: " + consumer.Name + " has bought " + Math.Abs(consumer.KWhAmount) + " kWh from " + producer.Name); Trader trader = new Trader() { ProducerId = producer.ProsumerId.ToString(), ConsumerId = consumer.ProsumerId.ToString(), KWhTransferred = Math.Abs(consumer.KWhAmount).ToString(), TransferDate = DateTime.Now }; mTradersRepoController.Post(trader).Wait(); consumer.KWhAmount = 0; mProsumerController.Put(producer.ProsumerId, producer).Wait(); mProsumerController.Put(consumer.ProsumerId, consumer).Wait(); } else if (Math.Abs(consumer.KWhAmount) > producer.KWhAmount) { consumer.KWhAmount += producer.KWhAmount; Console.WriteLine(">>>> TRADE: " + consumer.Name + " has bought " + Math.Abs(producer.KWhAmount) + " kWh from " + producer.Name); Trader trader = new Trader() { ProducerId = producer.ProsumerId.ToString(), ConsumerId = consumer.ProsumerId.ToString(), KWhTransferred = producer.KWhAmount.ToString(), TransferDate = DateTime.Now }; mTradersRepoController.Post(trader).Wait(); producer.KWhAmount = 0; mProsumerController.Put(producer.ProsumerId, producer).Wait(); mProsumerController.Put(consumer.ProsumerId, consumer).Wait(); } Console.WriteLine(producer.Name + " is selling " + producer.KWhAmount + " and " + consumer.Name + " is buying " + Math.Abs(consumer.KWhAmount)); } if (consumer.KWhAmount != 0) { Console.WriteLine("Buyer is missing " + Math.Abs(consumer.KWhAmount) + ", but he somehow manages to get free kWh from the powerplant (happy ending)"); consumer.KWhAmount = 0; } } // Remove Consumers and Producers if their kWhAmount equals 0. foreach (var producer in mProducers.ToList()) { if (producer.KWhAmount == 0) { mProducers.Remove(producer); } } foreach (var consumer in mConsumers.ToList()) { if (consumer.KWhAmount == 0) { mConsumers.Remove(consumer); } } // Post the SmartGrid mSmartGrid.Producers = mProducers; mSmartGrid.Consumers = mConsumers; mSmartGridsController.Post(mSmartGrid).Wait(); // Check difference now and how much involved powerplant was as buy/sell if (difference > 0) { Console.WriteLine("\nSold " + difference + " kWh to powerplant"); } else { Console.WriteLine("\nBought " + Math.Abs(difference) + " kWh from powerplant"); } Console.WriteLine("\n\nProgram has finished ..."); Console.ReadLine(); }