/// <summary> /// 关闭连接 /// </summary> /// <param name="commu"></param> /// <returns></returns> public static bool CloseCommu(KRcc.Commu commu) { try { bool result = true; if (commu != null && commu.IsConnected) { commu.disconnect(); commu.Dispose(); } return(result); } catch (Exception ex) { return(false); } }
/// <summary> /// 运行命令 /// </summary> /// <param name="commu"></param> /// <param name="commandString"></param> /// <returns></returns> public static string RunCommand(KRcc.Commu commu, string commandString) { try { //默认回车 ArrayList arrayList = commu.command(commandString + "\n"); int returnStatusCode = 0; int.TryParse(arrayList[0].ToString(), out returnStatusCode); if (returnStatusCode == 0) { return(arrayList[1].ToString()); } return(string.Empty); } catch (Exception ex) { throw ex; } }
/// <summary> /// 检查机器人连接是否已经正常 /// </summary> /// <param name="commu"></param> /// <returns></returns> public static bool CheckCommu(KRcc.Commu commu) { return(commu != null && commu.IsConnected); }
/// <summary> /// 开始监控(设定时间) /// </summary> /// <param name="IntervalMilliseconds"></param> /// <returns></returns> public bool StartTrigger(int IntervalMilliseconds) { try { // 读取指定位置的配置文件 var logger = NLogBuilder.ConfigureNLog("XmlConfig/nlog.config").GetCurrentClassLogger(); if (MC_AllMachineList == null || MC_AllMachineList.Count == 0) { return(false); } //循环所有机器人创建线程进行监控 foreach (var item in MC_AllMachineList) { string robotID = item.id; var t = new Thread(new ThreadStart(() => { //一直运行 while (true) { Thread.Sleep(IntervalMilliseconds); KRcc.Commu commu = null; try { //默认状态 item.Status = Common.Enum.Robot.RobotStatus.Default; commu = Common.RobotOperationHandler.GetCommu(item.IPAddress, "TCP", "23"); if (Common.RobotOperationHandler.CheckCommu(commu)) { //解析状态 string commandResult = Common.RobotOperationHandler.RunCommand(commu, "SWITCH REPEAT,CS,POWER,EMERGENCY,ERROR"); //连接状态 item.Status = Common.Enum.Robot.RobotStatus.Link; //var redisManger = new RedisManagerPool("127.0.0.1:6379"); //var _redis = redisManger.GetClient(); //var redisTodos = _redis.As<Model.RobotStatus>(); //var newTodo = new RobotStatus //{ // id = item.id, // IPAddress = item.IPAddress, // Status = item.Status, // DateTime = DateTime.Now.Date //}; //redisTodos.Store(newTodo); } } catch (Exception ex) { //错误状态 item.Status = Common.Enum.Robot.RobotStatus.Error; //foreach (var intm in MC_AllMachineList) //{ // if (item.Status == Common.Enum.Robot.RobotStatus.Error) // { // item.IsConnect = false; // Common.RobotStatusHelper.UpdateRobotMasterEntity(intm); // } //} logger.Error(ex, "机器人连接错误"); } finally { //关闭连接 if (Common.RobotOperationHandler.CheckCommu(commu)) { commu.disconnect(); } //清除变量 if (commu != null) { commu.Dispose(); } } } })) { Name = robotID }; t.Start(); threads.Add(t); } return(true); } catch (Exception ex) { //Common.LogHandler.WriteLog("机器人状态错误 原因是:" + ex.Message, ex); throw ex; } }