private void IncreaseTaskNextTime(RobotTask task) { switch (task.Trigger) { case TaskTrigger.Daily: task.NextTaskTime = task.NextTaskTime.AddDays(task.Interval); break; case TaskTrigger.Hourly: task.NextTaskTime = task.NextTaskTime.AddHours(task.Interval); break; case TaskTrigger.Minutely: task.NextTaskTime = task.NextTaskTime.AddMinutes(task.Interval); break; case TaskTrigger.Once: Delete(task.TaskID); break; default: break; } Update(task); }
public void Execute(RobotTask task) { IRobotAnalyzer taskAnalyzer = new RobotTaskAnalyzer(task.TargetNum); taskAnalyzer.ParseMsg(task.TaskInfo); if (task.Trigger == TaskTrigger.Once) { //如果是执行一次的任务,执行完毕删除 Delete(task.TaskID); } else { IncreaseTaskNextTime(task); } }
private bool CanExecute(RobotTask task) { bool result = false; DateTime now = DateTime.Now; DateTime currentTime = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second); DateTime nextTime = new DateTime(task.NextTaskTime.Year, task.NextTaskTime.Month, task.NextTaskTime.Day, task.NextTaskTime.Hour, task.NextTaskTime.Minute, task.NextTaskTime.Second); if (nextTime < currentTime) { task.NextTaskTime = GetNextTime(task); } if (task.NextTaskTime == currentTime) { result = true; } return(result); }
public void Execute(int taskID) { RobotTask currentTask = null; foreach (RobotTask task in this.taskList) { if (taskID == task.TaskID) { currentTask = task; break; } } if (currentTask != null) { Execute(currentTask); } }
private DateTime GetNextTime(RobotTask task) { int interval = task.Interval; DateTime old = task.NextTaskTime; DateTime temp = DateTime.Now; DateTime now = new DateTime(temp.Year, temp.Month, temp.Day, temp.Hour, temp.Minute, temp.Second); DateTime correctTime = DateTime.MaxValue; switch (task.Trigger) { case TaskTrigger.Daily: int diff1 = (int)(now - task.NextTaskTime).TotalDays; int span1 = diff1 / interval; task.NextTaskTime = task.NextTaskTime.AddDays(span1 * interval); if (task.NextTaskTime < now) { correctTime = task.NextTaskTime.AddDays(interval); } else { correctTime = now; } break; case TaskTrigger.Hourly: int diff2 = (int)(now - task.NextTaskTime).TotalHours; int span2 = diff2 / interval; task.NextTaskTime = task.NextTaskTime.AddHours(span2 * interval); if (task.NextTaskTime < now) { correctTime = task.NextTaskTime.AddHours(interval); } else { correctTime = now; } break; case TaskTrigger.Minutely: int diff3 = (int)(now - task.NextTaskTime).TotalMinutes; int span3 = diff3 / interval; task.NextTaskTime = task.NextTaskTime.AddMinutes(span3 * interval); if (task.NextTaskTime < now) { correctTime = task.NextTaskTime.AddMinutes(interval); } else { correctTime = now; } break; case TaskTrigger.Once: correctTime = task.TaskTime; break; default: break; } return(correctTime); }
public void Add(RobotTask task) { RobotCore.DBHelper.ExecuteNonQuery("INSERT INTO [Task] ([TaskInfo], [TargetNum],[TargetName],[TaskTime],[NextTaskTime],[Remark],[Trigger],[Interval]) VALUES (@TaskInfo, @TargetNum, @TargetName,@TaskTime,@NextTaskTime,@Remark,@Trigger,@Interval)", new object[] { task.TaskInfo, task.TargetNum, task.TargetName, task.TaskTime, task.NextTaskTime, task.Remark, task.Trigger.ToString(), task.Interval }); this.Refresh(); }
public void Update(RobotTask task) { RobotCore.DBHelper.ExecuteNonQuery("UPDATE Task set NextTaskTime=@NextTime WHERE TaskID=@TaskID", new object[] { task.TaskID, task.NextTaskTime }); this.Refresh(); }
private void btnAddTask_Click(object sender, EventArgs e) { try { int interval = 0; if (!int.TryParse(dudInterval.Text.Trim(), out interval) || interval > 60) { MessageBox.Show("请输入小于等于60的整数", "输入参数有误", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } RobotTask task = new RobotTask(); string date = dateTimePicker.Value.ToShortDateString(); string time = timePicker.Value.ToLongTimeString(); string taskTimeStr = string.Concat(date, " ", time); DateTime taskTime = DateTime.Parse(taskTimeStr); task.TaskTime = taskTime; task.NextTaskTime = taskTime; string cmd = cmbTask.SelectedValue.ToString(); string para = txtPara.Text.Trim(); task.TaskInfo = string.Format("{0}:{1}", cmd, para); if (chkLoop.Checked) { task.Interval = interval; if (cmbTrigger.SelectedIndex == 0) { task.Trigger = TaskTrigger.Daily; } else if (cmbTrigger.SelectedIndex == 1) { task.Trigger = TaskTrigger.Hourly; } else { task.Trigger = TaskTrigger.Minutely; } } else { task.Interval = -1; task.Trigger = TaskTrigger.Once; } if (chkBoxSelf.Checked) { task.TargetName = "自己"; task.TargetNum = RobotCore.Host.Uri.Raw.ToString(); } else { if (chkAllContact.Checked) { foreach (var c in contactList) { task.TargetNum += c.Uri.Raw.ToString() + "|"; task.TargetName += c.DisplayName + "|"; } } else { task.TargetNum += ((Contact)cmbTarget.SelectedValue).Uri.Raw.ToString(); task.TargetName += ((Contact)cmbTarget.SelectedValue).DisplayName; } } task.TargetNum = task.TargetNum.TrimEnd('|'); task.TargetName = task.TargetName.TrimEnd('|'); RobotCore.TaskMgr.Add(task); MessageBox.Show("添加成功!"); RefreshTaskList(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); RobotCore.Log.Error(ex.ToString()); } }