| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- using Polly;
- using System;
- using System.Threading;
- using SHJX.Service.ServerClient;
- using SHJX.Service.Model.Control;
- namespace SHJX.Service.Control.PortOperate.Interface
- {
- /// <summary>
- /// 泵操作
- /// </summary>
- public abstract class PortOperateImp
- {
- protected readonly OptClient _client;
- protected string OpName { get; set; }
- public PortOperateImp(OptClient optClient)
- {
- _client = optClient;
- }
- /// <summary>
- /// 操作液体的方式
- /// </summary>
- /// <param name="quantity">体积</param>
- /// <param name="pipeName"></param>
- public virtual bool LiquidOperation(string pipeName, long quantity)
- {
- CutPipe(pipeName);
- return MotorWrite(quantity); //吸液
- }
- /// <summary>
- /// 返回原点
- /// </summary>
- /// <param name="pipeName">管道名称</param>
- public virtual void GoBackOriginalPosition(string pipeName)
- {
- CutPipe(pipeName);
- GoBack();
- }
- /// <summary>
- /// 返回原点
- /// </summary>
- private void GoBack()
- {
- PortArgs gobackArg = new()
- {
- TypeName = OpName,
- WriteWay = WriteWay.GoBack
- };
- bool gb_write;
- do
- {
- gb_write = _client.Factory(OpName).Write(gobackArg);
- Thread.Sleep(2000);
- } while (!gb_write);
- bool res;
- do
- {
- char[] readRes = Read();
- if (readRes is null or { Length: 0 })
- {
- Thread.Sleep(1000);
- res = false;
- continue;
- };
- res = readRes[2].Equals('0');
- } while (!res);
- }
- /// <summary>
- /// 到达限位
- /// </summary>
- /// <returns></returns>
- public bool ArriveEndPoint()
- {
- var readRes = Read();
- if (readRes is null or { Length: 0 })
- return false;
- return readRes[2].Equals('0');
- }
- /// <summary>
- /// 读取寄存器
- /// </summary>
- /// <returns></returns>
- protected char[] Read()
- {
- PortArgs readArgs = new()
- {
- TypeName = OpName
- };
- var readRes = _client.Factory(OpName).Read(readArgs) as char[];
- return readRes;
- }
- /// <summary>
- /// 切换管道
- /// </summary>
- /// <param name="pipeName"></param>
- protected virtual void CutPipe(string pipeName)
- {
- var readRes = Read();
- if (readRes is null or { Length: 0 }) return;
- OptPipe(() =>
- {
- var readRes = Read();
- if (readRes is null or { Length: 0 })
- {
- Thread.Sleep(1000);
- return false;
- }
- var res = pipeName switch
- {
- "In" => readRes[5].Equals('0'),
- "Out" => readRes[6].Equals('0'),
- _ => false,
- };
- return res;
- });
- }
- /// <summary>
- /// 选择管道(出 or 吸)
- /// </summary>
- /// <param name="func"></param>
- protected void OptPipe(Func<bool> func)
- {
- var res = func.Invoke();
- if (res) return;
- #region 打开寄存器
- PortArgs switchArg = new()
- {
- TypeName = OpName,
- WriteWay = WriteWay.Normotopia
- };
- bool openRes;
- do
- {
- openRes = _client.Factory(OpName).Write(switchArg);
- Thread.Sleep(1000);
- } while (!openRes);
- #endregion
- Policy.HandleResult<bool>(arg => arg.Equals(false))
- .RetryForever(_ => { MotorWrite(-100); })
- .Execute(func);
- #region 关闭该寄存器
- switchArg.WriteWay = WriteWay.Antiposition;
- do
- {
- openRes = _client.Factory(OpName).Write(switchArg);
- Thread.Sleep(1000);
- } while (!openRes);
- #endregion
- }
- /// <summary>
- /// 写入脉冲
- /// </summary>
- /// <param name="distance"></param>
- protected bool MotorWrite(long distance)
- {
- PortArgs args = new()
- {
- TypeName = OpName,
- WriteWay = WriteWay.Move,
- Distance = distance
- };
- var res = _client.Factory(OpName).Write(args);
- int speedWaitTime = OpName switch
- {
- "SilverSulfate" => 300,
- _ => 500,
- };
- Thread.Sleep((int)(Math.Abs(distance) / 20000 * speedWaitTime));
- return res;
- }
- }
- }
|