public static double Get_Demand_Slope() { double slope = 0.0; List <BECDemand.BECDemand_Value> Demand_History_Copy = Demand_History.ToList(); try { if (Demand_History_Copy.Count > 1) { int current_index = Demand_History_Copy.Count - 1; int previous_index = current_index - 1; DateTime current_date = Demand_History_Copy[current_index]._date; DateTime previous_date = Demand_History_Copy[previous_index]._date; double current_value = Demand_History_Copy[current_index]._value; double previous_value = Demand_History_Copy[previous_index]._value; slope = (current_value - previous_value) / (double)(current_date - previous_date).Minutes; //kVA per minute Slope_History.Add(slope); Console.WriteLine("\nROC History Count = " + Slope_History.Count + "\nCurrent ROC = " + slope + "\nROC Average = " + Slope_History.Average() + "\n"); //ROC = Rate Of Change } } catch (Exception es) { SystemLogs_DB.Add(DateTime.Now + " Error while retrieving demand slope, Error: " + es); } Demand_History_Copy.Clear(); return(slope); }
public static void Add_to_Demand_History(double Value, DateTime date) { List <BECDemand.BECDemand_Value> Demand_History_Copy = Demand_History.ToList(); try { if (Demand_History_Copy.Count > 0) { if (!Demand_History_Copy.Exists(x => x._date == date) && date.Ticks > 0) { Demand_History.Add(new BECDemand.BECDemand_Value(Value, date)); // Demand_History_2.Add(new BECDemand.BECDemand_Value(Value, date)); current_slope = Get_Demand_Slope(); } } else { Demand_History.Add(new BECDemand.BECDemand_Value(Value, date)); // Demand_History_2.Add(new BECDemand.BECDemand_Value(Value, date)); current_slope = Get_Demand_Slope(); } } catch (Exception es) { SystemLogs_DB.Add(DateTime.Now + " Error while Adding data point to history, Error: " + es); } Demand_History_Copy.Clear(); }
public static void Get_Device_List() { List <string> from_Database = LoadData(RelayStatusdbconnection, "SELECT DeviceID, DeviceName, R1 FROM Status"); try { if (from_Database.Count > 0) { foreach (string str in from_Database) { string[] strs = str.Split(','); try { Console.WriteLine(strs[0] + "\t" + strs[1]); R1_Status_List.Add(new Devices(Convert.ToInt32(strs[0]), strs[1], Convert.ToInt32(strs[2]))); } catch (Exception) { } } } } catch (Exception es) { SystemLogs_DB.Add(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + "Failed to get Device List,Error: " + es); } }
public static void Full_Control_ON(double S) { //foreach device send query try { full_Control_ON = true; control_OFF = false; if (R1_Status_List.Count > 0) { List <string> update_queries = new List <string>(); foreach (Devices device in R1_Status_List) { if (device._Device_id == 12 || device._Device_id == 20 || device._Device_id == 21) { update_queries.Add(UpdateStatus(device._Device_id, 1, 0)); } else { update_queries.Add(UpdateStatus(device._Device_id, 1, 1)); } } bool success = false; DateTime date_now = DateTime.Now; while (!success && (DateTime.Now - date_now).Seconds < 30) { success = SendStatusUpdateQueries(RelayStatusdbconnection, update_queries); //Send queries } update_queries.Clear(); InsertControlLog(1, DateTime.Now, "Full Control", "ON", S); } } catch (Exception es) { SystemLogs_DB.Add(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + "Failed to apply Full Control,Error: " + es); } }
public static void Run() { int i = 0; int check = 0; while (i < 100) { DateTime now_ = DateTime.Now; TimeSpan time = new TimeSpan(now_.Ticks); double running_Remainder = (double)time.TotalSeconds % 60; if (running_Remainder < 15) { check++; if (check == 1) { Clear_Console(console_top); Console.SetCursorPosition(0, console_top); Set_Remaining_Minutes(DateTime.Now); //Set value of minutes remaining until next interval double latest_demand = 0; try { BECDemand.LoadBECData(); //load demand to BECDemand.Demandstorage latest_demand = BECDemand.Demandstorage.Last(); if ((now_ - BECDemand.BEC_Demand_latest_Date).Minutes < 15) //Check if Demand was updated recently { Set_Defended_Peak_to_Demand(BECDemand.BEC_Demand_latest_Date, latest_demand); } Add_to_Demand_History(latest_demand, BECDemand.BEC_Demand_latest_Date); //add latest demand value to demand history Console.WriteLine("Peak to defend is set to: " + S_0.ToString("####0.00 kVA")); Console.WriteLine("DateTime = " + BECDemand.BEC_Demand_latest_Date + "\nDemand value = " + latest_demand.ToString("####0.00 kVA")); } catch (Exception es) { Console.WriteLine("Failed to load demand, Error: " + es); } double predicted_Ongoing = ((current_slope * Remaining_minutes) + latest_demand); //predicted peek for next interval based on ongoing ROC double predicted_Average = 0.0; try { Console.WriteLine(Remaining_minutes + " minutes are remaining until next interval"); predicted_Ongoing = ((current_slope * Remaining_minutes) + latest_demand); //predicted peek for next interval based on ongoing ROC predicted_Average = -1; //predicted peek for next interval based on average ROC Console.WriteLine("\nOngoing Rate of change of Demand [kVA per minute] = " + current_slope.ToString("####0.00")); Console.WriteLine("With current Ongoing [kVA per minute], at the end of this " + Demand_interval + " minutes interval, Peak will be " + predicted_Ongoing.ToString("####0.00 kVA")); if (Slope_History.Count > 0) { predicted_Average = ((Slope_History.Average() * Remaining_minutes) + latest_demand); Console.WriteLine("\nAverage Rate of change of Demand [kVA per minute] = " + Slope_History.Average().ToString("####0.00")); Console.WriteLine("With current Average [kVA per minute], at the end of this " + Demand_interval + " minutes interval, Peak will be " + predicted_Average.ToString("####0.00 kVA")); } } catch (Exception es) { Console.WriteLine("Failed to update predicted values, Error: " + es); } DateTime date_now = DateTime.Now; try { if (predicted_Average >= 0) { InsertDemandLog(date_now, BECDemand.BEC_Demand_latest_Date, S_0, latest_demand, Demand_interval, current_slope, Slope_History.Average(), predicted_Ongoing, predicted_Average); } else { InsertDemandLog(date_now, BECDemand.BEC_Demand_latest_Date, S_0, latest_demand, Demand_interval, current_slope, predicted_Ongoing); } } catch (Exception es) { Console.WriteLine("Failed to write Demand Logs to Database, Error: " + es); } try { SendLogQueries(SystemLogs_DB); } catch (Exception es) { Console.WriteLine("Failed to write error Logs to Database, Error: " + es); } finally { SystemLogs_DB.Clear(); } try { if ((date_now.Minute + LC_mins_before_interval) % Demand_interval == 0 || date_now.Minute == 0) //at the end of interval { count2++; if (count2 == 1) { if ((date_now - BECDemand.BEC_Demand_latest_Date).Minutes < 15) //Check if Demand was updated recently { double average = BECDemand.Demandstorage.Average(); BECDemand.Demandstorage.Clear(); Console.WriteLine("DateTime = " + BECDemand.BEC_Demand_latest_Date + "\nAverage Demand value = " + average.ToString("####0.00")); if (average + Defence_Range >= S_0) { Console.WriteLine("Demand + Defence range = " + average + Defence_Range); Full_Control_ON(average); if (average > S_0) { S_0 = average; } if (first_start) { control_started_at = DateTime.Now; //update control start time control_removed_at = control_started_at.AddMinutes(1); //update control remove time } if (control_removed_at > control_started_at) //if control was removed earlier { control_started_at = DateTime.Now; //update control start time } } else if (average + (Defence_Range * 2) <= S_0) { Console.WriteLine("Demand + Defence range = " + average + Defence_Range); Control_OFF(average); if (first_start) { control_removed_at = DateTime.Now; //update control remove time control_started_at = control_removed_at.AddMinutes(1); //update control start time } if (control_started_at > control_removed_at) { control_removed_at = DateTime.Now; //update control remove time } if ((DateTime.Now - control_removed_at).TotalMinutes > control_timeout_mins) //apply control if it has been disabled for previous "control_timeout_mins" minutes { can_apply_control = true; } } first_start = false; /* * Defended - 50kVA = BEC demand -> apply load * S0, S (BEC demand at the end of interval) * * if S + 50 >= Defended -> apply full load control * if S + 100 <= Defended -> remove control */ } else { Console.WriteLine(date_now + ". BEC demand is not being updated. Please check server collecting data for BEC"); SystemLogs_DB.Add(date_now + ". BEC demand is not being updated. Please check server collecting data for BEC"); } Slope_History.Clear(); Clear_Demand_History(); } } else { count2 = 0; } } catch (Exception es) { Console.WriteLine("Failed to update Control status, Error: " + es); } Console.WriteLine("Control was started at : " + control_started_at); Console.WriteLine("Control was removed at : " + control_removed_at); if (full_Control_ON) { Console.WriteLine(DateTime.Now + ": Full Control is ON."); } else if (control_OFF) { Console.WriteLine(DateTime.Now + ": Control is OFF."); } } } else { check = 0; } Thread.Sleep(1000); i = 1; } }