//手動設定需量反應事件 private void button2_Click(object sender, EventArgs e) { string currentTimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");// 2018-04-17 01:20:26 string start_at = dtp.Text + " " + dtpTime.Text + ":00";; string end_at = dtp2.Text + " " + dtpTime2.Text + ":00";; DateJob.getTimeDiff(start_at, end_at); //取得時間差 double Start_Now = DateJob.getStart_Now(); //開始 減 現在 double End_Start = DateJob.getEnd_Start(); //結束 減 開始 if (Start_Now > 0 && End_Start > 0) { //判斷 需量時間合理 (現在-開始 >0 && 結束-開始>0 dbName = "hems"; dtName = "dr"; drJob.dr_IfRepeat_Insert(dbName, dtName, start_at, end_at, 0); dbName = null; dtName = null; label13.Visible = true; label13.Text = "需量反應事件 \n 設定完成"; drdone = "done"; } else if (Start_Now < 0) { MessageBox.Show("請設定開始時間於未來"); } else if (Start_Now > 0 && End_Start < 0) { MessageBox.Show("請設定結束時間大於開始時間"); } //label13.Text = "Start_Now : " + Start_Now + "\n" +"End_Start : " + End_Start + " \n"; }
public Form1() { InitializeComponent(); timer1.Enabled = true; inital_DataTime(); //DR開始 預設現在+2分鐘 ~~~DR結束 預設現在+3分鐘 string current1TimeStr = DateTime.Now.AddMinutes(2).ToString("yyyy-MM-dd HH:mm:ss"); string current2TimeStr = DateTime.Now.AddMinutes(3).ToString("yyyy-MM-dd HH:mm:ss"); DateJob.getString1(current1TimeStr); dtp.Value = Convert.ToDateTime(DateJob.getYMD()); dtpTime.Value = Convert.ToDateTime(DateJob.getHMS()); DateJob.getString1(current2TimeStr); dtp2.Value = Convert.ToDateTime(DateJob.getYMD()); dtpTime2.Value = Convert.ToDateTime(DateJob.getHMS()); }
} //9燈*30W public void eq_work(string eqStart, string eqFinish, string eqName, int eqPower) { //name=03 P=2200 reality =>ac DateJob.getTimeDiff(eqStart, eqFinish); //計算時間差 開始 現在 結束 int Start_Now = (int)DateJob.getStart_Now(); int End_Now = (int)DateJob.getEnd_Now(); drJob.IfDRTime("hems", "dr", "work1"); if (Start_Now <= 0 && End_Now >= 0 && !((drJob.getdrstate()).Equals("drstart"))) { Random rnd = new Random(Guid.NewGuid().GetHashCode()); //真亂數 double randNum1 = ((rnd.NextDouble() * 6) - 3); //+- 3 randNum1 = Math.Round(randNum1, 5); //小數點五位 randNum1 = eqPower * (randNum1 / 100) + eqPower; // P +-3% randNum1 = randNum1 / 1000; //(Wh --> kWh) //上傳 name / energy /inserted_at eqHistory_insert(eqName, (float)randNum1, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); string sql = "UPDATE `equipment` SET `status` = '1',`power` = '" + (float)(randNum1 * 1000) + "' WHERE `name`='" + eqName + "'"; update(sql); randNum1 = 0; //P } else { Random rnd = new Random(Guid.NewGuid().GetHashCode()); //真亂數 double randNum1 = (rnd.NextDouble() * 3); //+- 3 randNum1 = Math.Round(randNum1, 5); //小數點五位 randNum1 = randNum1 / 100; // +-3 randNum1 = randNum1 / 1000; // //(Wh --> kWh) //insert eqHistory_insert(eqName, (float)randNum1, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); string sql = "UPDATE `equipment` SET `status` = '0',`power` = '" + (float)(randNum1 * 1000) + "' WHERE `name`='" + eqName + "'"; update(sql); randNum1 = 0; } }
//判斷是否為dr開始或結束 並定義start_at ,duration , work 可藉由get取得 public static void IfDRTime(string dbName, string dtName, string work) { string currentTimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); // now DateTime currentTime = System.DateTime.Now; //now string[] today = currentTimeStr.Split(' '); //tomorrow年月日 //建立 DataTable DataTable dt = new DataTable(); int datarow_num; string Qrequest = "SELECT COUNT(*) FROM " + dtName + " WHERE start_at LIKE " + "'" + today[0] + "%" + "'"; datarow_num = DataRowNumber(dbName, Qrequest); //Allow Zero Datetime = true 讓Datetime可為 0000-00-00 00:00:00 string config = "server=" + dbHost + ";uid=" + dbUser + ";pwd=" + dbPass + ";database=" + dbName + "; Allow Zero Datetime = true"; MySqlConnection connection = new MySqlConnection(config); try { //使用 MySqlDataAdapter 查詢資料,並將結果存回 DataSet 當做名為 test1 的 DataTable string query = "SELECT * FROM " + dtName + " WHERE start_at LIKE " + "'" + today[0] + "%" + "'"; MySqlDataAdapter dataAdapter1 = new MySqlDataAdapter(query, connection); //MySqlDataAdapter自動 open &close dataAdapter1.Fill(dt); string[] id = new string[datarow_num]; string[] start_at = new string[datarow_num]; string[] end_at = new string[datarow_num]; int[] drworkArr = new int[datarow_num]; int[] duration = new int[datarow_num]; for (int i = 0; i < datarow_num; i++) { id[i] = dt.Rows[i]["id"].ToString(); start_at[i] = Convert.ToDateTime(dt.Rows[i]["start_at"]).ToString("yyyy-MM-dd HH:mm:ss"); end_at[i] = Convert.ToDateTime(dt.Rows[i]["end_at"]).ToString("yyyy-MM-dd HH:mm:ss"); drworkArr[i] = (int)dt.Rows[i][work]; duration[i] = (int)dt.Rows[i]["duration"]; DateJob.getTimeDiff(start_at[i], end_at[i]); int Start_Now = (int)DateJob.getStart_Now(); int End_Now = (int)DateJob.getEnd_Now(); //可能抓到兩筆 正在發生DR 並且留下最後抓到的資料 有錯該修正180507 if ((Start_Now <= 0) && (End_Now >= 0)) { drid = id[i]; // 當前DR事件 id值 drstart_at = start_at[i]; drend_at = end_at[i]; drduration = duration[i]; drstate = "drstart"; drwork = drworkArr[i]; //如drwork = 0 執行dr on 令 drwork = 1 }//需量反應開始 dr_on work else if (End_Now <= 0) //&& (drworkArr[i]).Equals(1) { drid = id[i]; // 剛結束DR事件 id值 drstate = "drend"; drwork = drworkArr[i]; //如 drwork = 1 執行dr finish 令 drwork = 2 }//需量反應結束 dr_on work } } catch (MySqlException me) { } catch (MySqlConversionException ee) { MessageBox.Show("MySqlConversionException : \n" + ee); } catch (IndexOutOfRangeException rangeE) { MessageBox.Show("IndexOutOfRangeException : " + rangeE); } dbName = null; dtName = null; }
//--------------------------------------------------------------------------------------------------- ////(自動手動)新增dr事件(每小時) 並判斷是否有重複 auto = 1 自動 / auto = 0 手動 public static void dr_IfRepeat_Insert(string dbName, string dtName, string startTime, string endTime, int auto) { //currentTimeStr = tomorrow string currentTimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); // now string current1TimeStr = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss"); //tomorrow DateTime currentTime = System.DateTime.Now; //now string YMD = ""; if (auto == 1) { string[] tomorrow = current1TimeStr.Split(' '); //tomorrow年月日 YMD = tomorrow[0]; } else if (auto == 0) { // string[] NonAuto = startTime.Split(' '); //startTime年月日 //YMD = NonAuto[0]; } DateJob.getTimeDiff(startTime, endTime); int duration = (int)DateJob.getEnd_Start(); //duration //建立 DataSet DataTable dt = new DataTable(); int datarow_num; string Qrequest = "SELECT COUNT(*) FROM " + dtName + " WHERE start_at LIKE " + "'" + YMD + "%" + "'"; datarow_num = DataRowNumber(dbName, Qrequest); //Allow Zero Datetime = true 讓Datetime可為 0000-00-00 00:00:00 string config = "server=" + dbHost + ";uid=" + dbUser + ";pwd=" + dbPass + ";database=" + dbName + "; Allow Zero Datetime = true"; MySqlConnection connection = new MySqlConnection(config); try { //使用 MySqlDataAdapter 查詢資料,並將結果存回 DataSet 當做名為 test1 的 DataTable string query = "SELECT * FROM dr WHERE start_at LIKE " + "'" + YMD + "%" + "'"; MySqlDataAdapter dataAdapter1 = new MySqlDataAdapter(query, connection); //MySqlDataAdapter自動 open &close dataAdapter1.Fill(dt); string[] Lstart_at = new string[datarow_num]; int checkDRinsert = 0; for (int i = 0; i < datarow_num; i++) //檢查資料庫是否已有dr事件 避免重複 { Lstart_at[i] = Convert.ToDateTime(dt.Rows[i]["start_at"]).ToString("yyyy-MM-dd HH:mm:ss"); if (Lstart_at[i].Equals(startTime)) { checkDRinsert = checkDRinsert + 1; } //已新增過資料 else { } //新增Dr事件 } // MessageBox.Show(Lstart_at[0] + "\n" + dt.Rows[0]["start_at"]); //insert(dr事件時間); if (checkDRinsert == 0) { dr_set(startTime, endTime, duration, currentTimeStr, dbName); } datarow_num = 0; checkDRinsert = 0; } catch (MySqlConversionException ee) { MessageBox.Show("MySqlConversionException : \n" + ee); } catch (IndexOutOfRangeException rangeE) { MessageBox.Show("IndexOutOfRangeException : " + rangeE); } catch (FormatException Fe) { MessageBox.Show("FormatException : " + Fe); } //開始 結束 現在 持續 }
//判斷三段電價 public double getFeeNum(string date1) { double price; DateJob.getString1(date1); string Year = DateJob.getYear(); string Month = DateJob.getMonth(); string Day = DateJob.getDay(); string Hour = DateJob.getHour(); string Min = DateJob.getMin(); string Sec = DateJob.getSec(); /* * MessageBox.Show("年 : " + DateJob.getYear() + "\n" +"月 : " + DateJob.getMonth() + "\n" +"日 : " + DateJob.getDay() + "\n" + * "時 : " + DateJob.getHour() + "\n" +"分 : " + DateJob.getMin() + "\n" +"秒 : " + DateJob.getSec() + "\n"); */ DateTime dt = new DateTime(Convert.ToInt32(Year), Convert.ToInt32(Month), Convert.ToInt32(Day)); //判斷夏月 做出 三段式電價表 if (Month.Equals("06") || Month.Equals("07") || Month.Equals("08") || Month.Equals("09")) { //夏月 if (GetDayName(dt).Equals("6") || GetDayName(dt).Equals("7")) { price = 1.71; } //假日_全日離峰 else { //非假日 if (Hour.Equals("10") || Hour.Equals("11") || Hour.Equals("13") || Hour.Equals("14") || Hour.Equals("15") || Hour.Equals("16")) { price = 5.84; } //夏日平日_尖峰10-11.59 +13-16.59 else if ((Hour.Equals("07") && (Convert.ToInt32(Min) >= 30)) || Hour.Equals("08") || Hour.Equals("09") || Hour.Equals("12") || Hour.Equals("17") || Hour.Equals("18") || Hour.Equals("19") || Hour.Equals("20") || Hour.Equals("21") || (Hour.Equals("22") && (Convert.ToInt32(Min) < 30))) { price = 3.85; } //夏日平日_半尖峰 else { price = 1.71; } //夏日平日_離峰 } } else { //非夏月 if (GetDayName(dt).Equals("6") || GetDayName(dt).Equals("7")) { price = 1.65; } //假日_全日離峰 else { //非假日 if ((Hour.Equals("07") && (Convert.ToInt32(Min) < 30)) || Hour.Equals("00") || Hour.Equals("01") || Hour.Equals("02") || Hour.Equals("03") || Hour.Equals("04") || Hour.Equals("05") || Hour.Equals("06") || Hour.Equals("11") || (Hour.Equals("22") && (Convert.ToInt32(Min) >= 30))) { price = 1.65; } //非夏月平日_離峰0-7.30 + 22.30-24.00 else { price = 3.69; } //非夏月平日_半尖峰 } } return(price); }