//public ScheduleTask() { }//空构造函数 public ScheduleTask(DateTime time, ScheduleFrequence frequence) { this.time = time; this.frequence = frequence; result = ScheduleResult.UnExecute; preExecuteTime = DateTime.MinValue; if (time < DateTime.Now) { switch (frequence) { case ScheduleFrequence.Single: this.result = ScheduleResult.Outdate; break; case ScheduleFrequence.EveryDay: this.time = DateTime.Now.AddDays(1); //明天执行 break; case ScheduleFrequence.EveryWeek: int days = (int)((DateTime.Now - time).TotalDays); this.time = time.AddDays(7 * (days / 7 + 1)); break; default: break; } } }
//添加任务 private void AddOK_Click(object sender, EventArgs e) { ScheduleFrequence frequence = 0; ScheduleOffGrid schoffGrid = ScheduleOffGrid.On; RadioButton[] freqs = { radBtnOneDay, radBtnEveDay, radWeekly }; //频率 RadioButton[] Events = { radStartInverter, radCloseInverter, radioVoltage }; //事件 foreach (RadioButton freq in freqs) { if (freq.Checked == true) { switch (Convert.ToInt16(freq.Tag)) { case 0: frequence = ScheduleFrequence.Single; break; case 1: frequence = ScheduleFrequence.EveryDay; break; case 2: frequence = ScheduleFrequence.EveryWeek; break; } } } foreach (RadioButton Event in Events) { if (Event.Checked == true) { schoffGrid = (ScheduleOffGrid)Convert.ToInt16(Event.Tag); break; } } //创建一个任务 ScheduleTask schTask = new ScheduleTask(Convert.ToDateTime(dateTimePicker1.Text), frequence); schTask.tag = (int)schoffGrid; schTask.handle += new TaskHandle(new OffGridTask(schoffGrid).handle); SolarSchedule.ScheduleManager.getInstance().addTask(schTask); //schedule.listBox1.Items.Add(schTask); for (int i = 0; i < schedule.dataGridView1.Rows.Count; i++) { if (schedule.dataGridView1.Rows[i].Cells[0].Value.ToString() == null || schedule.dataGridView1.Rows[i].Cells[0].Value.ToString() == "") { schedule.dataGridView1.Rows[i].Cells[0].Value = ScheduleTask.transTask(schTask.tag); schedule.dataGridView1.Rows[i].Cells[1].Value = schTask.time.ToString("yyyy-MM-dd HH:mm:ss dddd"); schedule.dataGridView1.Rows[i].Cells[2].Value = ScheduleTask.transFreq(schTask.frequence); schedule.dataGridView1.Rows[i].Cells[3].Value = ScheduleTask.transResult(schTask.result); schedule.dataGridView1.Rows[i].Cells[4].Value = ScheduleTask.transPreResult(schTask.preExecuteTime); schedule.dataGridView1.Rows[i].Cells[5].Value = schTask; break; } } this.Close(); }
//频率解析 public static string transFreq(ScheduleFrequence frequence) { string str = string.Empty; switch (frequence) { case ScheduleFrequence.Single: str = Model_Data.Language.ScheduleStr.once; break; case ScheduleFrequence.EveryDay: str = Model_Data.Language.ScheduleStr.everyDay; break; case ScheduleFrequence.EveryWeek: str = Model_Data.Language.ScheduleStr.Weekly; break; } return(str); }
/*------------------- 管理器 ----------------------------------*/ public void loadTask() { //初始化 任务列表 using (FileStream fs = File.Open(path, FileMode.Open)) { XmlDocument document = new XmlDocument(); document.Load(fs); XmlNode root = document.LastChild; foreach (XmlNode item in root.ChildNodes) { ScheduleFrequence freq = (ScheduleFrequence)Enum.Parse(typeof(ScheduleFrequence), item.Attributes["frequence"].Value, true); ScheduleOffGrid type = (ScheduleOffGrid)Enum.Parse(typeof(ScheduleOffGrid), item.Attributes["type"].Value, true); SolarSchedule.ScheduleTask st = new SolarSchedule.ScheduleTask( DateTime.Parse(item.Attributes["time"].Value), SolarSchedule.ScheduleFrequence.Single); st.tag = (int)type; st.handle += new TaskHandle(new OffGridTask(type).handle); _tasks.Add(st); } } }