//手動設定需量反應事件 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"; }
} //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; }