/// <summary> /// 获取选中的计划 /// </summary> /// <param name="var">变量</param> /// <returns>状态</returns> private double PLAN_GETSELECTED(CVariable var) { PlanWindow planWindow = DataCenter.PlanWindow; String id = planWindow.GetSelectedPlanID(); if (id != null && id.Length > 0) { CPlan plan = new CPlan(); DataCenter.PlanService.GetPlan(id, ref plan); CVariable newVar = new CVariable(m_indicator); newVar.m_expression = "'" + id + "'"; m_indicator.SetVariable(var.m_parameters[0], newVar); CVariable newVar2 = new CVariable(m_indicator); newVar2.m_expression = "'" + plan.m_name + "'"; m_indicator.SetVariable(var.m_parameters[1], newVar2); CVariable newVar3 = new CVariable(m_indicator); newVar3.m_expression = "'" + new DateTime(plan.m_nextTime).ToString("yyyy-MM-dd HH:mm:ss") + "'"; m_indicator.SetVariable(var.m_parameters[2], newVar3); CVariable newVar4 = new CVariable(m_indicator); newVar4.m_expression = "'" + plan.m_member + "'"; m_indicator.SetVariable(var.m_parameters[3], newVar4); CVariable newVar5 = new CVariable(m_indicator); newVar5.m_expression = "'" + plan.m_command + "'"; m_indicator.SetVariable(var.m_parameters[4], newVar5); return(1); } else { return(0); } }
/// <summary> /// 设置最后结果 /// </summary> /// <param name="id">ID</param> /// <param name="result">结果</param> public void SetLastResult(String id, String result) { CPlan plan = new CPlan(); if (GetPlan(id, ref plan) > 0) { plan.m_lastResult = result; } }
/// <summary> /// 创建服务 /// </summary> /// <param name="plan">计划</param> public void NewService(CPlan plan) { Log("创建计划:" + plan.m_name); lock (m_plans) { m_plans[plan.m_id] = plan; } SavePlans(); }
/// <summary> /// 禁用服务 /// </summary> /// <param name="id">ID</param> public void StopService(String id) { CPlan plan = new CPlan(); if (GetPlan(id, ref plan) > 0) { plan.m_status = "禁用"; Log("禁用计划:" + plan.m_name); SavePlans(); } }
/// <summary> /// 开始准备运行计划 /// </summary> /// <param name="plan">计划对象</param> public void StartRunPlan(CPlan plan) { object[] parameters = new object[3]; parameters[0] = plan.m_id; parameters[1] = plan.m_command; Log("正在执行计划:" + plan.m_name); //启动线程运行 Thread planThread = new Thread(new ParameterizedThreadStart(Run)); planThread.IsBackground = true; planThread.Start(parameters); }
/// <summary> /// 删除任务 /// </summary> /// <param name="id">ID</param> public void RemoveService(String id) { CPlan plan = new CPlan(); if (GetPlan(id, ref plan) > 0) { lock (m_plans) { m_plans.Remove(id); } Log("删除计划" + plan.m_name); SavePlans(); } }
/// <summary> /// 获取计划 /// </summary> /// <param name="id">ID</param> /// <param name="plan">计划</param> /// <returns>状态</returns> public int GetPlan(String id, ref CPlan plan) { int state = 0; lock (m_plans) { if (m_plans.ContainsKey(id)) { plan = m_plans[id]; state = 1; } } return(state); }
/// <summary> /// 修改计划 /// </summary> /// <param name="var">变量</param> /// <returns>状态</returns> private double PLAN_MODIFY(CVariable var) { String id = m_indicator.GetText(var.m_parameters[0]); if (id != null && id.Length > 0) { CPlan plan = new CPlan(); DataCenter.PlanService.GetPlan(id, ref plan); plan.m_name = m_indicator.GetText(var.m_parameters[1]); plan.m_nextTime = Convert.ToDateTime(m_indicator.GetText(var.m_parameters[2])).Ticks; plan.m_member = m_indicator.GetText(var.m_parameters[3]); plan.m_command = m_indicator.GetText(var.m_parameters[4]); DataCenter.PlanService.SavePlans(); return(1); } return(0); }
/// <summary> /// 初始化方法 /// </summary> public void Init() { UserCookie cookie = new UserCookie(); if (DataCenter.UserCookieService.GetCookie("PLANS", ref cookie) > 0) { String[] strs = cookie.m_value.Split(new String[] { "☀" }, StringSplitOptions.RemoveEmptyEntries); int strsSize = strs.Length; for (int i = 0; i < strsSize; i++) { CPlan plan = new CPlan(); plan.FromString(strs[i]); if (plan.m_id.Length > 0) { m_plans[plan.m_id] = plan; } } } }
/// <summary> /// 保存 /// </summary> public void SavePlans() { String str = ""; List <CPlan> plans = new List <CPlan>(); GetPlans(plans); int plansSize = plans.Count; for (int i = 0; i < plansSize; i++) { CPlan plan = plans[i]; str += plan.ToString(); str += "☀"; } plans.Clear(); UserCookie cookie = new UserCookie(); cookie.m_key = "PLANS"; cookie.m_value = str; DataCenter.UserCookieService.AddCookie(cookie); }
/// <summary> /// 定时检查 /// </summary> public void Check() { m_planService.OnTimer(); Dictionary <int, GridColumn> columnsIndex = new Dictionary <int, GridColumn>(); List <GridColumn> columns = m_gridPlan.GetColumns(); int columnsSize = columns.Count; for (int i = 0; i < columnsSize; i++) { GridColumn column = columns[i]; columnsIndex[CStr.ConvertStrToInt(column.Name.Substring(4))] = column; } Dictionary <String, String> pids = new Dictionary <String, String>(); List <CPlan> plans = new List <CPlan>(); DataCenter.PlanService.GetPlans(plans); int plansSize = plans.Count; for (int i = 0; i < plansSize; i++) { pids[plans[i].m_id] = ""; } GridRow selectedRow = null; Dictionary <String, GridRow> rowsMap = new Dictionary <String, GridRow>(); List <GridRow> rows = m_gridPlan.GetRows(); int rowsSize = rows.Count; for (int i = 0; i < rowsSize; i++) { GridRow row = rows[i]; String id = ""; if (row.GetCell("colP1") != null) { id = row.GetCell("colP1").GetString(); } if (pids.ContainsKey(id)) { rowsMap[id] = row; } else { m_gridPlan.RemoveRow(row); row.Dispose(); rowsSize--; i--; } } m_gridPlan.Update(); m_gridPlan.BeginUpdate(); for (int i = 0; i < plansSize; i++) { CPlan plan = plans[i]; GridRow row = null; bool newData = false; if (rowsMap.ContainsKey(plan.m_id)) { row = rowsMap[plan.m_id]; } else { row = new GridRow(); row.Height = 50; selectedRow = row; m_gridPlan.AddRow(row); newData = true; } foreach (int col in columnsIndex.Keys) { GridCell cell = null; GridColumn column = columnsIndex[col]; if (newData) { if (col == 5) { GridProgressCell progressCell = new GridProgressCell(); cell = progressCell; row.AddCell(column.Index, cell); } else { cell = new GridStringCell(); if (col == 3) { cell.AllowEdit = true; } row.AddCell(column.Index, cell); } } else { cell = row.GetCell(column.Index); } switch (col) { //ID case 1: cell.SetString(plan.m_id); break; //名称 case 2: cell.SetString(plan.m_name); break; //进程 case 3: cell.SetString(plan.m_command); break; //状态 case 4: cell.SetString(plan.m_status); GridCellStyle cellStyle = new GridCellStyle(); if (plan.m_status == "启动") { cellStyle.ForeColor = CDraw.GetPriceColor(1, 2); } else if (plan.m_status == "禁用") { cellStyle.ForeColor = CDraw.GetPriceColor(2, 1); } cell.Style = cellStyle; break; //下次执行时间 case 5: GridProgressCell progressCell = cell as GridProgressCell; if (plan.m_nextTime != 0) { DateTime nowDate = DateTime.Now; long span = (long)plan.m_timeSpan * 1000 * 10000; double rate = 100 - 100 * (plan.m_nextTime - nowDate.Ticks) / span; if (rate < 0) { rate = 100 - 100 * (double)(plan.m_nextTime - nowDate.Ticks) / (plan.m_nextTime - plan.m_createTime); } progressCell.Rate = rate; } else { progressCell.Rate = 0; } cell.SetString(new DateTime(plan.m_nextTime).ToString()); break; //上次执行时间 case 6: cell.SetString(new DateTime(plan.m_lastTime).ToString()); break; //上次结果 case 7: cell.SetString(plan.m_lastResult); break; //间隔 case 8: cell.SetString(plan.m_timeSpan.ToString()); break; //创建时间 case 9: cell.SetString(new DateTime(plan.m_createTime).ToString()); break; //相关人员 case 10: cell.SetString(plan.m_member); break; } } } //修正选中行 if (selectedRow != null) { List <GridRow> selectedRows = new List <GridRow>(); selectedRows.Add(selectedRow); m_gridPlan.SelectedRows = selectedRows; } m_gridPlan.EndUpdate(); Native.Invalidate(); columnsIndex.Clear(); pids.Clear(); plans.Clear(); }
/// <summary> /// 新的计划 /// </summary> /// <param name="var">变量</param> /// <returns>状态</returns> private double PLAN_CREATE(CVariable var) { //拼装行对象 CPlan plan = new CPlan(); String id = System.Guid.NewGuid().ToString(); String name = ""; String sTime = ""; String command = ""; String member = ""; int timeSpan = 60; String runImmediately = "否"; if (var.m_parameters.Length == 1) { String fileName = Application.StartupPath + "\\" + var.m_parameters[0].m_expression.Replace("'", ""); String text = ""; CFileA.Read(fileName, ref text); int index = text.IndexOf("\r\n"); name = text.Substring(0, index); text = text.Substring(index + 2); index = text.IndexOf("\r\n"); sTime = text.Substring(index + 2); index = text.IndexOf("\r\n"); member = text.Substring(0, index); text = text.Substring(index + 2); index = text.IndexOf("\r\n"); command = text.Substring(index + 2); } else { name = m_indicator.GetText(var.m_parameters[0]); sTime = m_indicator.GetText(var.m_parameters[1]); member = m_indicator.GetText(var.m_parameters[2]); command = m_indicator.GetText(var.m_parameters[3]); } plan.m_id = id; plan.m_name = name; plan.m_member = member; plan.m_status = "启动"; if (command != null && command.Length > 0) { plan.m_command = command; } plan.m_createTime = DateTime.Now.Ticks; plan.m_timeSpan = timeSpan; DateTime startTime = new DateTime(new TimeSpan(DateTime.Now.Ticks + (long)plan.m_timeSpan * 1000 * 10000).Ticks); if (sTime != null && sTime.Length > 0) { startTime = Convert.ToDateTime(sTime); } plan.m_startTime = startTime.Ticks; plan.m_runImmediately = runImmediately == "是"; if (plan.m_runImmediately) { plan.m_startTime = DateTime.Now.Ticks; } else { if (startTime < DateTime.Now) { plan.m_runImmediately = true; plan.m_startTime = DateTime.Now.Ticks; } else { plan.m_nextTime = plan.m_startTime; } } DataCenter.PlanService.NewService(plan); return(0); }
/// <summary> /// 秒表事件 /// </summary> public void OnTimer() { DateTime now = DateTime.Now; long nowTicks = now.Ticks; List <CPlan> plans = new List <CPlan>(); GetPlans(plans); int plansSize = plans.Count; for (int i = 0; i < plansSize; i++) { CPlan plan = plans[i]; if (plan.m_runImmediately) { if (plan.m_status == "启动") { //立即执行的任务 plan.m_runImmediately = false; plan.m_lastTime = nowTicks; plan.m_lastResult = "无"; plan.m_nextTime = nowTicks + (long)plan.m_timeSpan * 1000 * 10000; //StartRunPlan(plan); SavePlans(); } } else { if (plan.m_nextTime != 0) { long timeSpan = (long)plan.m_timeSpan * 1000 * 10000; if (plan.m_nextTime < nowTicks) { //满足执行的情况 if (plan.m_nextTime + timeSpan > nowTicks) { if (plan.m_status == "启动") { plan.m_status = "禁止"; //StartRunPlan(plan); plan.m_lastTime = plan.m_nextTime; plan.m_lastResult = "无"; SavePlans(); Console.Beep(1000, 500); Console.Beep(1000, 500); Console.Beep(1000, 500); } //plan.m_nextTime += timeSpan; } else { //计算出最近的执行时间 //long newDate = plan.m_nextTime + timeSpan; //while (newDate < nowTicks) //{ // newDate += timeSpan; //} //plan.m_nextTime = newDate; } } } } } plans.Clear(); }