using System;
using System.Threading;
using SHJX.Service.Common.Extend;
using SHJX.Service.Model.Control;
using SHJX.Service.Common.ReadXML;
using SHJX.Service.ServerClient.Interface;
using SHJX.Service.ServerClient.TempController;
using SHJX.Service.Common.Logging;
using Microsoft.Extensions.Logging;
namespace SHJX.Service.ServerClient.RS485Control
{
///
/// MotorX的通信
///
public class MotorXPort : SerialPortImp, PortControlImp
{
///
/// X轴
///
/// 日志
/// 配置
private static readonly ILogger logger = LogFactory.BuildLogger(typeof(MotorXPort));
public MotorXPort( ReadConfigUtil config)
: base( config)
{
config.GetSpeed.ForEach(item => {
if (item.NodeName == "MotorX")
speedX = item.Speed;
});
}
private double speedX = 0;
public object Read(PortArgs args)
{
object obj = args.ReadWay switch
{
"OriginalPosition" => ReadOriginalPosition(args.NodeId),
_ => JudgeStorage(args.NodeId),
};
return obj;
}
private bool ReadOriginalPosition(byte NodeId)
{
var res = JudgeStorage(NodeId);
if (res is null or { Length: 0 }) return false;
return res[6].Equals('0');
}
public bool Write(PortArgs args)
{
var res = args.WriteWay switch
{
WriteWay.Move => xMove(args),
WriteWay.GoBack => xMotorGoBack(args),
WriteWay.Stop => MotorStop(args.NodeId),
WriteWay.Speed => MotorSpeed(args.NodeId, args.Distance),
WriteWay.AcSpeed => MotorAcSpeed(args.NodeId, args.Distance),
WriteWay.DeSpeed => MotorDeSpeed(args.NodeId, args.Distance),
_ => throw new ArgumentNullException(args.WriteWay.GetEnumDesc()),
};
return res;
}
private bool xMove(PortArgs args)
{
bool res = true;
//var judgeRes = JudgeStorage(3);
//if (judgeRes is null || judgeRes.Length < 1)
// return false;
//if (!judgeRes[3 - 1].Equals('0'))
//{
// res = MotorGoBack(3, 3);
//}
return res && Move(args.NodeId, args.Distance, 60, speedX);
}
private bool xMotorGoBack(PortArgs args)
{
bool res = true;
//var judgeRes = JudgeStorage(3);
//if (judgeRes is null || judgeRes.Length < 1)
// return false;
//if (!judgeRes[3 - 1].Equals('0'))
//{
// res = MotorGoBack(3, 3);
//}
return res && MotorGoBack(args.NodeId, 7);
}
}
}