public ActionResult Add(tb_command_model model)
 {
     return this.Visit(Core.EnumUserRole.Admin, () =>
     {
         tb_command_dal dal = new tb_command_dal();
         using (DbConn PubConn = DbConfig.CreateConn(Config.TaskConnectString))
         {
             PubConn.Open();
             model.commandcreatetime = DateTime.Now;
             dal.Add(PubConn, model);
         }
         return RedirectToAction("index");
     });
 }
 public ActionResult Update(int id)
 {
     return this.Visit(Core.EnumUserRole.Admin, () =>
     {
         using (DbConn PubConn = DbConfig.CreateConn(Config.TaskConnectString))
         {
             PubConn.Open();
             tb_command_dal dal = new tb_command_dal();
             tb_command_model_Ex model = dal.GetOneCommand(PubConn, id);
             List<tb_node_model> Node = new tb_node_dal().GetListAll(PubConn);
             ViewBag.Node = Node;
             return View(model);
         }
     });
 }
        //
        // GET: /Command/

        public ActionResult Index(int taskid = -1, int commandstate = -1, int nodeid = -1, int pagesize = 10, int pageindex = 1)
        {
            return this.Visit(Core.EnumUserRole.Admin, () =>
            {
                ViewBag.taskid = taskid; ViewBag.commandstate = commandstate; ViewBag.nodeid = nodeid; ViewBag.pagesize = pagesize; ViewBag.pageindex = pageindex;
                tb_command_dal dal = new tb_command_dal();
                PagedList<tb_command_model_Ex> pageList = null;
                int count = 0;
                using (DbConn PubConn = DbConfig.CreateConn(Config.TaskConnectString))
                {
                    PubConn.Open();
                    List<tb_command_model_Ex> List = dal.GetList(PubConn, commandstate, taskid, nodeid, pagesize, pageindex, out count);
                    List<tb_task_model> Task = new tb_task_dal().GetListAll(PubConn);
                    List<tb_node_model> Node = new tb_node_dal().GetListAll(PubConn);
                    ViewBag.Node = Node;
                    ViewBag.Task = Task;
                    pageList = new PagedList<tb_command_model_Ex>(List, pageindex, pagesize, count);
                }
                return View(pageList);
            });
        }
 public JsonResult Delete(int id)
 {
     return this.Visit(Core.EnumUserRole.Admin, () =>
     {
         using (DbConn PubConn = DbConfig.CreateConn(Config.TaskConnectString))
         {
             PubConn.Open();
             tb_command_dal dal = new tb_command_dal();
             dal.Delete(PubConn, id);
             return Json(new { code = 1, msg = "Success" });
         }
     });
 }
 /// <summary>
 /// 运行消息循环
 /// </summary>
 static void RuningCommandLoop()
 {
     LogHelper.AddNodeLog("准备接受命令并运行消息循环...");
     while (true)
     {
         System.Threading.Thread.Sleep(1000);
         try
         {
             List<tb_command_model> commands = new List<tb_command_model>();
             try
             {
                 SqlHelper.ExcuteSql(GlobalConfig.TaskDataBaseConnectString, (c) =>
                 {
                     tb_command_dal commanddal = new tb_command_dal();
                     if (lastMaxID < 0)
                         lastMaxID = commanddal.GetMaxCommandID(c);
                     commands = commanddal.GetNodeCommands(c, GlobalConfig.NodeID, lastMaxID);
                 });
             }
             catch (Exception exp2)
             {
                 LogHelper.AddNodeError("获取当前节点命令集错误", exp2);
             }
             if(commands.Count>0)
                 LogHelper.AddNodeLog("当前节点扫描到"+commands.Count+"条命令,并执行中....");
             foreach (var c in commands)
             {
                 try
                 {
                     CommandFactory.Execute(c);
                     SqlHelper.ExcuteSql(GlobalConfig.TaskDataBaseConnectString, (conn) =>
                     {
                         new tb_command_dal().UpdateCommandState(conn, c.id, (int)Core.EnumTaskCommandState.Success);
                     });
                     LogHelper.AddNodeLog(string.Format("当前节点执行命令成功! id:{0},命令名:{1},命令内容:{2}", c.id, c.commandname, c.command));
                 }
                 catch (Exception exp1)
                 {
                     try
                     {
                         SqlHelper.ExcuteSql(GlobalConfig.TaskDataBaseConnectString, (conn) =>
                         {
                             new tb_command_dal().UpdateCommandState(conn, c.id, (int)Core.EnumTaskCommandState.Error);
                         });
                     }
                     catch { }
                     LogHelper.AddTaskError("执行节点命令失败", c.taskid, exp1);
                 }
                 lastMaxID = Math.Max(lastMaxID, c.id);
             }
         }
         catch (Exception exp)
         {
             LogHelper.AddNodeError("系统级不可恢复严重错误", exp);
         }
         System.Threading.Thread.Sleep(3000);
     }
 }