public void Start() { if (IsRuning) { return; } queue = new BlockingCollection <RequestCommand>(); task = new Task(() => { foreach (RequestCommand item in queue.GetConsumingEnumerable()) { try { Excute(item); } catch (Exception e) { TraceManagerForCommand.AppendErrMsg("命令消费器消费过程中出错:" + e.Message + "堆栈:" + e.StackTrace); } } }, TaskCreationOptions.LongRunning); task.Start(); IsRuning = true; }
public void Stop() { if (!IsRuning) { return; } // 先完成添加 queue.CompleteAdding(); DateTime time1 = DateTime.Now; while (queue.Count > 0) { Thread.Sleep(1); // 最多等待10秒避免关不掉 if (DateTime.Now - time1 > TimeSpan.FromSeconds(10)) { TraceManagerForCommand.AppendErrMsg("命令消费器关闭超时丢弃了" + queue.Count.ToString() + "条任务"); break; } } while (queue.Count > 0) { // 等了十秒还没听,队列全部元素废弃 queue.Take(); } queue = null; Task.WaitAll(task); task.Dispose(); task = null; IsRuning = false; }
private void ExcuteCommand(RequestCommand command) { if (command.sonServerType == CommandServerType.YL_WEB && command.operType == CommandOperType.ReLoadData) { if (ExcuteDoing) // 正在采集,等这次采集结束,在采集一次 { DateTime time1 = DateTime.Now; while (true) { Thread.Sleep(1); if (DateTime.Now - time1 > TimeSpan.FromSeconds(command.timeoutSeconds)) // 超时 { CommandManager.MakeTimeout("Scada-WEB-压力监测点 数据更新超时", ref command); CommandManager.CompleteCommand(command); TraceManagerForCommand.AppendInfo(command.message); return; } if (!ExcuteDoing) { break; } } } // 调取之前先重新加载一次缓存 Excute(); CommandManager.MakeSuccess("Scada-WEB-压力监测点 数据已更新", ref command); CommandManager.CompleteCommand(command); TraceManagerForCommand.AppendInfo("Scada-WEB-压力监测点 数据已更新"); return; } CommandManager.MakeFail("错误的请求服务类型", ref command); CommandManager.CompleteCommand(command); TraceManagerForCommand.AppendErrMsg(command.message); return; }
private void ExcuteCommand(RequestCommand command) { CommandManager.MakeFail("暂时不支持控制服务", ref command); CommandManager.CompleteCommand(command); TraceManagerForCommand.AppendErrMsg(command.message); return; }
// 调度过来的命令 public void ReceiveCommand(RequestCommand command) { // TraceManagerForOPC.AppendInfo("PUMP-OPC子服务当前任务条目数:"+ this.queue.Count.ToString()); //写值操作 if (command.sonServerType == CommandServerType.Pump_OPC && command.operType == CommandOperType.Write) { TakeOutCollectCommand(); this.Append(PumpCommand.CreateWriteCommand(this.opcClientManager, command)); //命令记录 TraceManagerForCommand.AppendDebug(string.Format("命令ID:{0}开始执行JZID:{1},业务地址{2},设定值{3}操作", command.ID, command.jzID, command.fDBAddress, command.value)); return; } // OPC_Pump--重载请求 if (command.sonServerType == CommandServerType.Pump_OPC && command.operType == CommandOperType.ReLoadData) { TakeOutCollectCommand(); this.Append(PumpCommand.CreateReloadCommand(this.opcClientManager, command)); //命令记录 TraceManagerForCommand.AppendDebug(string.Format("命令ID:{0}开始执行PUMP-OPC重载机组数据操作:", command.ID)); return; } // 错误请求 CommandManager.MakeFail("错误的请求类型", ref command); CommandManager.CompleteCommand(command); TraceManagerForCommand.AppendErrMsg(command.message); return; }
private void ExcuteHandle(RequestCommand command) { if (actExcute == null) { CommandManager.MakeFail("命令执行体未注册", ref command); CommandManager.CompleteCommand(command); TraceManagerForCommand.AppendErrMsg(command.message); return; } actExcute(command); }
public void ReceiveCommand(RequestCommand command) { // 已经在入口验证过命令对象 if (!IsRuning || this.commandCustomer == null || !this.commandCustomer.IsRuning) { CommandManager.MakeFail("Scada-WEB-压力监测点命令消费器运行异常", ref command); CommandManager.CompleteCommand(command); TraceManagerForCommand.AppendErrMsg("Scada-WEB-压力监测点命令消费器运行异常"); return; } this.commandCustomer.Append(command); }
/// <summary> /// 完成命令 /// </summary> /// <param name="command"></param> public static void CompleteCommand(RequestCommand command) { command.endTime = DateTime.Now; command.timeConsuming = RecCommand.GetTimeSpanStr(command.beginTime, command.endTime); // 命令记录 CommandManager.AppendRecCommand(RecCommand.ToRecCommand(command)); // 命令回执 ResponseCommand responseCommand; switch (command.state) { case CommandState.Succeed: { responseCommand = new ResponseCommand() { info = "命令执行成功", errMsg = "", statusCode = "200" }; } break; case CommandState.Timeout: { responseCommand = new ResponseCommand() { info = "", errMsg = "命令执行超时", statusCode = "490" }; } break; default: { responseCommand = new ResponseCommand() { info = "", errMsg = "命令执行失败:" + command.message, statusCode = "491" }; } break; } string data = ByteUtil.ToSerializeObject(responseCommand); string err = command.FinshCallBack(command.sessionID, data, true); if (!string.IsNullOrWhiteSpace(err)) { TraceManagerForCommand.AppendErrMsg("命令ID:" + command.ID + "发送回执命令失败"); } }
// 执行调度命令 private void ConsumerCommand(RequestCommand command) { try { ExcuteCommand(command); } catch (Exception e) { CommandManager.MakeFail("Scada-WEB-压力监测点 定时任务执行失败:" + e.Message, ref command); CommandManager.CompleteCommand(command); TraceManagerForCommand.AppendErrMsg(command.message); } }
public void Append(ReciveData reciveData) { if (!IsRuning) return; if (queue.IsAddingCompleted) return; if (queue.Count > 4096) { TraceManagerForCommand.AppendErrMsg( "命令消费队列到达上限无法插入"); return; } queue.Add(reciveData); }
// 丢掉采集命令 public void TakeOutCollectCommand() { try { foreach (PumpCommand command in queue) { command.CollectTaskWriteCancleToken(); } } catch (Exception e) { TraceManagerForCommand.AppendErrMsg("优先执行Command,采集任务移除失败" + e.Message + "堆栈" + e.StackTrace); } }
private void Excute(RecCommand command) { command.UpdateDateTime = DataUtil.ToDateString(DateTime.Now); string sql = string.Format(@"INSERT INTO IoTCommand (会话ID,服务类型, 操作类型,SensorID,PumpJZID,数据业务地址,预设值,最后更新时间,用户ID,状态,开始时间,结束时间,耗时,消息) VALUES ('{0}','{1}','{2}','{3}',{4},'{5}',{6},'{7}',{8},'{9}','{10}','{11}','{12}','{13}');", DataUtil.ToString(command.SessionID), DataUtil.ToString(command.ServerType), DataUtil.ToString(command.OperType), DataUtil.ToString(command.SensorID), DataUtil.ToInt(command.PumpJZID), DataUtil.ToString(command.FDBAddress), DataUtil.ToDouble(command.SetValue), DataUtil.ToDateString(command.UpdateDateTime), DataUtil.ToInt(command.UserID), DataUtil.ToString(command.State), DataUtil.ToDateString(command.BeginTime), DataUtil.ToDateString(command.EndTime), DataUtil.ToString(command.TimeConsuming), DataUtil.ToString(command.Message)); DBUtil.ExecuteNonQuery(sql, out string errMsg); if (!string.IsNullOrWhiteSpace(errMsg)) { TraceManagerForCommand.AppendErrMsg("命令记录器记录数据库失败" + errMsg); } }
// 命令消费-设定超时功能 private void Excute(RequestCommand command) { // 设定超时功能 ActionTimeout <RequestCommand> timeout = new ActionTimeout <RequestCommand>(); timeout.Do = ExcuteHandle; bool bo = timeout.DoWithTimeout(command, new TimeSpan(0, 0, 0, command.timeoutSeconds + 3));//设定基础多等待3秒,防止调用着也做了超时功能 if (bo) { CommandManager.MakeTimeout("会话ID:" + command.sensorID + "执行命令超时", ref command); CommandManager.CompleteCommand(command); TraceManagerForCommand.AppendErrMsg(command.message); GC.Collect(); } }
public void Append(RequestCommand command) { if (!IsRuning) { return; } if (queue.IsAddingCompleted) { return; } if (queue.Count > 4096) { TraceManagerForCommand.AppendErrMsg("命令消费队列到达上限无法插入"); return; } queue.Add(command); }
public void Append(RecCommand data) { if (!IsRuning) { return; } if (queue.IsAddingCompleted) { return; } if (queue.Count > 4096) { TraceManagerForCommand.AppendErrMsg("命令记录器队列到达上限无法插入"); return; } queue.Add(data); }
/// <summary> /// 命令消费 /// </summary> /// <param name="reciveData"></param> private void Excute(ReciveData reciveData) { //将接受到数据反序列化成命令对象 RequestCommand request; try { request = ByteUtil.ToDeserializeObject<RequestCommand>(reciveData.data.body); } catch (Exception e) { ResponseCommand response = new ResponseCommand() { errMsg = "序列化请求对象失败," + e.Message + "堆栈:" + e.StackTrace, statusCode = "440", info = "" }; string data = ByteUtil.ToSerializeObject(response); reciveData.FinshCallBack(reciveData.sessionID, data,true); TraceManagerForCommand.AppendErrMsg(response.errMsg); return; } // 序列化检查 if (request == null) { ResponseCommand response = new ResponseCommand() { errMsg = "序列化请求对象失败,", statusCode = "440", info = "" }; string data = ByteUtil.ToSerializeObject(response); reciveData.FinshCallBack(reciveData.sessionID, data, true); TraceManagerForCommand.AppendErrMsg(response.errMsg); return; } // 将委托和会话ID传递给命令对象 request.FinshCallBack = reciveData.FinshCallBack; request.sessionID = reciveData.sessionID; //命令记录 TraceManagerForCommand.AppendDebug("已获取命令ID:"+ request.ID); // 让调度开始分发给对应的子服务 this.DoRequestCommand(request); }
public void ExcuteWrite() { try { if (!ExcuteWriteHandle(out string errMsg, out string info)) { CommandManager.MakeFail(errMsg, ref requestCommand); CommandManager.CompleteCommand(requestCommand); TraceManagerForCommand.AppendErrMsg(requestCommand.message); return; } CommandManager.MakeSuccess(info, ref requestCommand); CommandManager.CompleteCommand(requestCommand); TraceManagerForCommand.AppendInfo(info); } catch (Exception e) { TraceManagerForOPC.AppendErrMsg("站点写值任务执行失败--" + e.Message + "堆栈:" + e.StackTrace); SendError("站点写值任务执行失败--" + e.Message); } }
public void ReceiveCommand(RequestCommand command) { if (command == null) { CommandManager.MakeFail("接受命令为空请求对象", ref command); CommandManager.CompleteCommand(command); TraceManagerForCommand.AppendErrMsg("接受命令为空请求对象"); return; } if (!IsRuning) { MakeFailCommand(command, "子服务器管理器未运行"); return; } switch (command.sonServerType) { case CommandServerType.Pump_OPC: { if (this.opcPumpService != null && this.opcPumpService.IsRuning) { this.opcPumpService.ReceiveCommand(command); } else { MakeFailCommand(command, "Pump-OPC 子服务器管理器未运行"); return; } } break; case CommandServerType.Pump_WEB: { if (this.webPandaPumpService != null && this.webPandaPumpService.IsRuning) { this.webPandaPumpService.ReceiveCommand(command); } else { MakeFailCommand(command, "Pump-WEB 子服务器管理器未运行"); return; } } break; case CommandServerType.SCADA_OPC: { if (this.opcScadaService != null && this.opcScadaService.IsRuning) { this.opcScadaService.ReceiveCommand(command); } else { MakeFailCommand(command, "SCADA-OPC 子服务器管理器未运行"); return; } } break; case CommandServerType.YL_WEB: { if (this.webPandaYLScadaService != null && this.webPandaYLScadaService.IsRuning) { this.webPandaYLScadaService.ReceiveCommand(command); } else { MakeFailCommand(command, "YL_WEB 子服务器管理器未运行"); return; } } break; case CommandServerType.ZHCD_WEB: { if (this.webPandaZHCDScadaServcice != null && this.webPandaZHCDScadaServcice.IsRuning) { this.webPandaZHCDScadaServcice.ReceiveCommand(command); } else { MakeFailCommand(command, "ZHCD_WEB 子服务器管理器未运行"); return; } } break; case CommandServerType.SpecialProject: { if (this.caseManagerInjection != null && this.caseManagerInjection.IsRuning) { this.caseManagerInjection.ReceiveCommand(command); } else { MakeFailCommand(command, "项目子服务器管理器未运行"); return; } } break; default: { MakeFailCommand(command, "错误的子服务类型"); return; } } }
public static void Stop() { //端口监听服务 try { if (tcpServer != null) { tcpServer.evtReciveHandle -= CommandManager.commandConsumer.Append; //注册接受处理体 tcpServer.Stop(); } tcpServer = null; } catch (Exception e) { TraceManagerForCommand.AppendErrMsg("命令端口监听服务停止失败" + "堆栈:" + e.StackTrace); } // 命令消费者服务 try { if (commandConsumer != null) { commandConsumer.Stop(); if (!commandConsumer.IsRuning) { CommandManager.commandConsumer = null; } else { TraceManagerForCommand.AppendErrMsg("命令消费者服务停止失败"); } } } catch (Exception e) { TraceManagerForCommand.AppendErrMsg("命令消费者服务停止失败:" + e.Message + "堆栈:" + e.StackTrace); } // 命令记录服务 try { if (commandRecorder != null) { commandRecorder.Stop(); if (!commandRecorder.IsRuning) { CommandManager.commandRecorder = null; } else { TraceManagerForCommand.AppendErrMsg("命令记录服务停止失败"); } } } catch (Exception e) { TraceManagerForCommand.AppendErrMsg("命令记录服务停止失败:" + e.Message + "堆栈:" + e.StackTrace); } IsRuning = false; }
public static void Start(Action <RequestCommand> actionRequestCommand, ConfCommandServiceInfo confCommandServiceInfo, out string errMsg) { errMsg = ""; if (IsRuning) { return; } config = confCommandServiceInfo; DoRequestCommand = actionRequestCommand; // 加载服务所需配置文件信息 if (!CheckConfig(config, out errMsg)) { return; } // 命令记录服务 if (commandRecorder != null) { commandRecorder.Stop(); } commandRecorder = new CommandRecorder(); commandRecorder.Start(); if (!commandRecorder.IsRuning) { TraceManagerForCommand.AppendErrMsg("命令记录服务打开失败"); Stop(); return; } // 命令消费者服务 if (commandConsumer != null) { commandConsumer.Stop(); } commandConsumer = new SocketCommandConsumer(DoRequestCommand); commandConsumer.Start(); if (!commandConsumer.IsRuning) { TraceManagerForCommand.AppendErrMsg("命令消费者服务打开失败"); Stop(); return; } //端口监听服务 if (tcpServer != null) { tcpServer.Stop(); } tcpServer = new SuperSocketTcpServerOper(CommandManager.ip, CommandManager.port); tcpServer.evtReciveHandle += CommandManager.commandConsumer.Append; //注册接受处理体 if (!tcpServer.Start(out errMsg)) { TraceManagerForCommand.AppendErrMsg("命令端口监听服务打开失败"); Stop(); return; } IsRuning = true; }
private void MakeFailCommand(RequestCommand command, string errMsg) { CommandManager.MakeFail(errMsg, ref command); CommandManager.CompleteCommand(command); TraceManagerForCommand.AppendErrMsg(errMsg); }