| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- using SHJX.Service.Common.ReadXML;
- using System.Windows.Threading;
- using SHJX.Service.Control.Extends;
- namespace SHJX.Service.Control.Execute
- {
- public class ExecuteCommand
- {
- private static readonly ILogger logger = LogFactory.BuildLogger(typeof(ExecuteCommand));
- private static IDataManager _dataManager;
- private static ReadConfigUtil _config;
- private static readonly object recode_lock = new();
- private static IEventAggregator _ea;
- public static List<ShowStatusValueArgs> statusValue = new();
- private static string ShowStatusBefore = "";
- public ExecuteCommand(IEventAggregator ea, IDataManager dataManager, TaskExtend taskExtend, ReadConfigUtil config)
- {
- _ea = ea;
- _dataManager = dataManager;
- _config = config;
- }
- /// <summary>
- /// 寄存器操作
- /// </summary>
- /// <param name="typeName">类型</param>
- /// <param name="time">时间</param>
- /// <returns></returns>
- public static bool RegisterExecute(string typeName, double time)
- {
- if (time <= 0)
- {
- return true;
- }
- bool res = typeName.RegisterOpen();
- Thread.Sleep(Convert.ToInt32(Math.Round(time * 1000)));
- return res && typeName.RegisterClose();
- }
- /// <summary>
- /// 电机返回原点
- /// </summary>
- /// <param name="typeName"></param>
- /// <returns></returns>
- public static bool MotorGoBackExecute(string typeName)
- {
- try
- {
- bool res = false;
- if (typeName.In(EquipmentNames.AxisX, EquipmentNames.AxisY)) //这段代码是xy移动距离大于1000前要看z是否回到原点
- {
- char[] judgeRes = EquipmentNames.AxisZ.MotorRead();
- if (judgeRes is null or { Length: 0 })
- {
- return false;
- }
- if (!judgeRes[3 - 1].Equals('0'))
- {
- EquipmentNames.AxisZ.MotorGoBack();
- }
- if (typeName == EquipmentNames.AxisX)
- {
- MotorSpeed speed = _dataManager.Query<MotorSpeed>().Where(it => it.SystemName.Equals(EquipmentNames.AxisX)).First();
- res = speed.SystemName.WriteSpeed(6);
- }
- if (typeName == EquipmentNames.AxisY)
- {
- MotorSpeed speed = _dataManager.Query<MotorSpeed>().Where(it => it.SystemName.Equals(EquipmentNames.AxisY)).First();
- res = speed.SystemName.WriteSpeed(6);
- }
- }
- bool writeResponse = typeName.MotorGoBack();
- if (!writeResponse)
- {
- return false;
- }
- MotorMoveStatus moveStatus = _dataManager.Query<MotorMoveStatus>().Where(it => it.MotorName.Equals(typeName)).First();
- if (moveStatus is null)
- {
- return writeResponse;
- }
- moveStatus.CurrentPosition = 0;
- writeResponse = writeResponse && _dataManager.Update(moveStatus) > 0;
- logger.LogDebug($"下发{typeName}返回原点{(writeResponse ? "成功" : "失败")}");
- if (typeName == EquipmentNames.AxisX)
- {
- MotorSpeed speed = _dataManager.Query<MotorSpeed>().Where(it => it.SystemName.Equals(EquipmentNames.AxisX)).First();
- res = speed.SystemName.WriteSpeed(speed.Speed);
- }
- if (typeName == EquipmentNames.AxisY)
- {
- MotorSpeed speed = _dataManager.Query<MotorSpeed>().Where(it => it.SystemName.Equals(EquipmentNames.AxisY)).First();
- res = speed.SystemName.WriteSpeed(speed.Speed);
- }
- return writeResponse;
- }
- catch (Exception ex)
- {
- logger.LogError(ex.ToString());
- return false;
- }
- }
- /// <summary>
- /// 电机移动
- /// </summary>
- /// <param name="typeName">类型名称</param>
- /// <param name="task">任务</param>
- public static bool MotorMoveExecute(string typeName, EquipmentTask task)
- {
- try
- {
- string movePoint = string.Empty;
- movePoint = task.Target switch
- {
- "From" => task.From,
- "To" => task.To,
- _ => throw new ArgumentNullException(task.Target),
- };
- EquipmentArea area = _dataManager.Query<EquipmentArea>().Where(item => item.PointName.Equals(movePoint)).First();
- if (area is null)
- {
- return false;
- }
- MotorMoveStatus moveStatus = _dataManager.Query<MotorMoveStatus>().Where(item => item.MotorName.Equals(typeName)).First();
- if (moveStatus is null)
- {
- return false;
- }
- MotorConvert motorConvert = _dataManager.Query<MotorConvert>().Where(item => item.MotorName.Equals(typeName)).First();
- if (motorConvert is null)
- {
- return false;
- }
- #region 移动距离
- char[] readRes = EquipmentNames.Tongs.MotorRead(); //这里需要读取
- if (typeName == EquipmentNames.AxisX|| typeName == EquipmentNames.AxisY)//X,Y的话要看z在不在原点
- {
- if (!readRes[2].Equals('0'))
- {
- logger.LogDebug($"X,Y运动前Z轴回原点命令发送");
- ExecuteCommand.MotorGoBackExecute(EquipmentNames.AxisZ);
- }
- }
- bool judgeRes = false;
- if (typeName == EquipmentNames.AxisZ)//抓着杯的时候 抓手不需要太向下
- {
- judgeRes = readRes[4].Equals('0');
- }
- double? localPos = typeName switch
- {
- EquipmentNames.AxisX => area?.LocationX - moveStatus?.CurrentPosition,
- EquipmentNames.AxisY => area?.LocationY - moveStatus?.CurrentPosition,
- EquipmentNames.AxisZ => judgeRes ? area?.LocationZF : area?.LocationZ,
- _ => throw new ArgumentNullException(typeName),
- };
-
- double? distance = typeName switch
- {
- EquipmentNames.AxisX or EquipmentNames.AxisY or EquipmentNames.AxisZ => -1 * localPos * motorConvert?.ConvertRatio,
- _ => throw new ArgumentNullException(typeName)
- };
- #endregion
- bool writeResponse = typeName.MotorMove(Convert.ToInt32(Math.Round(distance ?? 0)));
- logger.LogDebug($"{task.Source} ==> 下发{typeName}{(writeResponse ? "成功" : "失败")}");
- if (!writeResponse)
- {
- return false;
- }
- moveStatus.CurrentPosition += localPos ?? 0;
- return _dataManager.Update(moveStatus) > 0;
- }
- catch (Exception)
- {
- throw;
- }
- }
- public static bool ShowStatus(EquipmentTask task)
- {
- try
- {
- if (task is null||task.SerialKey==0)
- {
- return false;
- }
- lock (recode_lock)
- {
- ShowStatusValueArgs status = new ShowStatusValueArgs();
- if (ShowStatusBefore == task.RouteStep.ToString())
- {
- return true;
- }
- ShowStatusBefore = task.RouteStep.ToString();
- string step = task.RouteStep.ToString() switch
- {
- "AddLiquid" => "加液",
- "AddLiquidGoback" => "加液返回",
- "Dissolve" => "消解",
- "DissolveGoback" => "消解结束",
- "Titration" => "1号滴定",
- "Titration2" => "2号滴定",
- "TitrationGoback" => "1号滴定返回",
- "Titration2Goback" => "2号滴定返回",
- };
- status.CurrentContent = task.Source + ":" + step + "-" + "从(" + task.From + ")到(" + task.To + ")";
- status.Name = task.Source.ToString();
- _ea.GetEvent<UpdateStausEvent>().Publish(status);
- return true;
- }
- }
- catch
- {
- return false;
- }
- }
- public static bool ShowStatusAction(EquipmentTask task, string value)
- {
- try
- {
- if (task is null)
- {
- return false;
- }
- lock (recode_lock)
- {
- ShowStatusValueArgs status = new ShowStatusValueArgs();
- if (ShowStatusBefore == value)
- {
- return true;
- }
- ShowStatusBefore = value;
- string step = value switch
- {
- "Sodium2Hydroxide" => "氢氧化钠",
- "Titration1Vitriol" => "滴定硫酸1",
- "Titration2Vitriol" => "滴定硫酸2",
- "AddLiquidVitriol" => "加液硫酸",
- "Titration1PotassiumPermanganate" => "高锰酸钾1",
- "AddLiquidPotassiumPermanganate" => "加液高锰酸钾2",
- "Sodium1Oxalate" => "草酸钠1",
- "Sodium2Oxalate" => "草酸钠2",
- };
- status.CurrentContent = task.Source + ":加入" + "(" + step + ")溶液";
- if (task.Source is null)
- {
- status.Name = "清洗";
- }
- else
- status.Name = task.Source.ToString();
- _ea.GetEvent<UpdateStausEvent>().Publish(status);
- return true;
- }
- }
- catch
- {
- return false;
- }
- }
- public static bool ShowStatusOpenClose(EquipmentTask task, string value)
- {
- try
- {
- if (task is null)
- {
- return false;
- }
- lock (recode_lock)
- {
- ShowStatusValueArgs status = new ShowStatusValueArgs();
- if (ShowStatusBefore == value)
- {
- return true;
- }
- status.CurrentContent = task.Source + ":" + value;
- if (task.Source is null)
- {
- status.Name = "清洗";
- }
- else
- status.Name = task.Source.ToString();
- _ea.GetEvent<UpdateStausEvent>().Publish(status);
- return true;
- }
- }
- catch
- {
- return false;
- }
- }
- }
- }
|