You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

362 lines
9.8 KiB

using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Entity;
using HybirdFrameworkDriver.Session;
using HybirdFrameworkDriver.TcpClient;
6 months ago
using log4net;
using Service.Charger.Client;
using Service.Charger.Codec;
using Service.Charger.Handler;
using Service.Charger.Msg.Host.Req;
namespace Service.Charger.Server;
[Scope]
public class PlcClient : TcpClient<IBaseHandler, Decoder, Encoder>
{
#region 属性
6 months ago
/// <summary>
/// Plc编号
/// </summary>
public string Sn { get; set; }
/// <summary>
///
/// </summary>
public bool SwapDone { get; set; }
/// <summary>
/// 换电开始标记
/// </summary>
public bool SwapStart { get; set; }
public bool DisassembleDone { get; set; }
//自动模式
public bool Auto { get; set; }
//远程模式
public bool Remote { get; set; }
//初始化完成
public bool Init { get; set; }
#endregion
#region send
/// <summary>
/// 发送初始化命令
/// </summary>
/// <param name="sn">Plc编号</param>
/// <param name="msg">消息</param>
public Result<bool> SendInitializeCommandReq()
{
if (!Connected)
{
return Result<bool>.Fail($"Plc{Sn}未连接");
}
InitializeCommandReq req = new InitializeCommandReq();
this.Channel.WriteAndFlushAsync(req);
return Result<bool>.Success();
}
/// <summary>
/// 发送出库命令
/// </summary>
/// <param name="sn">Plc编号</param>
/// <param name="msg">消息</param>
public Result<bool> SendOutboundCommandReq(byte outboundMode,byte takePositionNumber)
{
if (!Connected)
{
return Result<bool>.Fail($"Plc{Sn}未连接");
}
OutboundCommandReq req = new OutboundCommandReq(outboundMode, takePositionNumber)
{
OutboundMode = outboundMode,
TakePositionNumber = takePositionNumber,
};
this.Channel.WriteAndFlushAsync(req);
return Result<bool>.Success();
}
/// <summary>
/// 发送开始换电命令
/// </summary>
/// <param name="sn">Plc编号</param>
/// <param name="msg">消息</param>
public Result<bool> SendStartBatterySwapReq(byte takePositionNumber,byte positionNumber)
{
if (!Connected)
{
return Result<bool>.Fail($"Plc{Sn}未连接");
}
StartBatterySwapReq req = new StartBatterySwapReq(takePositionNumber, positionNumber)
{
TakePositionNumber = takePositionNumber,
PositionNumber = positionNumber,
};
this.Channel.WriteAndFlushAsync(req);
return Result<bool>.Success();
}
/// <summary>
/// 发送开始搬电命令
/// </summary>
/// <param name="sn">Plc编号</param>
/// <param name="msg">消息</param>
public Result<bool> SendStartMovElectricityReq(byte takeBatteryShelfNumber,byte saveBatteryShelfNumber)
{
if (!Connected)
{
return Result<bool>.Fail($"Plc{Sn}未连接");
}
StartMovElectricityReq req = new StartMovElectricityReq(takeBatteryShelfNumber, saveBatteryShelfNumber)
{
TakeBatteryShelfNumber = takeBatteryShelfNumber,
SaveBatteryShelfNumber = saveBatteryShelfNumber,
};
this.Channel.WriteAndFlushAsync(req);
return Result<bool>.Success();
}
/// <summary>
/// 发送车辆型号
/// </summary>
/// <param name="sn">Plc编号</param>
/// <param name="msg">消息</param>
public Result<bool> SendVehicleModelReq(byte cartNo)
{
if (!Connected)
{
return Result<bool>.Fail($"Plc{Sn}未连接");
}
VehicleModelReq req = new VehicleModelReq(cartNo)
{
CartNo = cartNo,
};
this.Channel.WriteAndFlushAsync(req);
return Result<bool>.Success();
}
/// <summary>
/// 发送移库命令
/// </summary>
/// <param name="sn">Plc编号</param>
/// <param name="msg">消息</param>
public Result<bool> SendMoveCommandReq(byte takePositionNumber,byte positionNumber)
{
if (!Connected)
{
return Result<bool>.Fail($"Plc{Sn}未连接");
}
MoveCommandReq req = new MoveCommandReq(takePositionNumber, positionNumber)
{
TakePositionNumber = takePositionNumber,
PositionNumber = positionNumber,
};
this.Channel.WriteAndFlushAsync(req);
return Result<bool>.Success();
}
/// <summary>
/// 发送继续命令
/// </summary>
/// <param name="sn">Plc编号</param>
/// <param name="msg">消息</param>
public Result<bool> SendContinueCommandReq( )
{
if (!Connected)
{
return Result<bool>.Fail($"Plc{Sn}未连接");
}
ContinueCommandReq req = new ContinueCommandReq();
this.Channel.WriteAndFlushAsync(req);
return Result<bool>.Success();
}
/// <summary>
/// 发送暂停命令
/// </summary>
/// <param name="sn">Plc编号</param>
/// <param name="msg">消息</param>
public Result<bool> SendPauseCommandReq()
{
if (!Connected)
{
return Result<bool>.Fail($"Plc{Sn}未连接");
}
PauseCommandReq req = new PauseCommandReq();
this.Channel.WriteAndFlushAsync(req);
return Result<bool>.Success();
}
/// <summary>
/// 发送入库命令
/// </summary>
/// <param name="sn">Plc编号</param>
/// <param name="msg">消息</param>
public Result<bool> SendInboundCommandsReq(byte positionNumber)
{
if (!Connected)
{
return Result<bool>.Fail($"Plc{Sn}未连接");
}
InboundCommandsReq req = new InboundCommandsReq(positionNumber)
{
PositionNumber = positionNumber,
};
this.Channel.WriteAndFlushAsync(req);
return Result<bool>.Success();
}
/// <summary>
/// 发送终止命令
/// </summary>
/// <param name="sn">Plc编号</param>
/// <param name="msg">消息</param>
public Result<bool> SendTerminationOrderReq( )
{
if (!Connected)
{
return Result<bool>.Fail($"Plc{Sn}未连接");
6 months ago
}
TerminationOrderReq req = new TerminationOrderReq();
this.Channel.WriteAndFlushAsync(req);
return Result<bool>.Success();
6 months ago
}
/// <summary>
/// 发送故障复位命令
/// </summary>
/// <param name="sn">Plc编号</param>
/// <param name="msg">消息</param>
public Result<bool> SendFaultResetReq()
6 months ago
{
if (!Connected)
6 months ago
{
return Result<bool>.Fail($"Plc{Sn}未连接");
}
FaultResetReq req = new FaultResetReq();
this.Channel.WriteAndFlushAsync(req);
return Result<bool>.Success();
}
///// <summary>
///// 发送参数设置
///// </summary>
///// <param name="sn">Plc编号</param>
///// <param name="msg">消息</param>
//public Result<bool> SendParameterSettingsReq(byte[] messageBodyAddress,byte parameterType,byte parameter)
//{
// if (!Connected)
// {
// return Result<bool>.Fail($"Plc{Sn}未连接");
// }
// ParameterSettingsReq req = new ParameterSettingsReq( parameterType, parameter)
// {
// MessageBodyAddress = messageBodyAddress,
// ParameterType = parameterType,
// Parameter = parameter,
// };
// this.Channel.WriteAndFlushAsync(req);
// return Result<bool>.Success();
//}
6 months ago
/// <summary>
/// 发送参数设置
/// </summary>
/// <param name="sn">Plc编号</param>
/// <param name="msg">消息</param>
public Result<bool> SendParameterSettingsReq( byte parameterType, byte parameter)
{
if (!Connected)
{
return Result<bool>.Fail($"Plc{Sn}未连接");
6 months ago
}
ParameterSettingsReq req = new ParameterSettingsReq(parameterType, parameter)
{
ParameterType = parameterType,
Parameter = parameter,
};
this.Channel.WriteAndFlushAsync(req);
return Result<bool>.Success();
}
/// <summary>
/// 发送准备电池开启
/// </summary>
/// <param name="sn">Plc编号</param>
/// <param name="msg">消息</param>
public Result<bool> SendPrepareBatteryOnReq(byte readyBatterySign,byte takePositionNumber)
{
if (!Connected)
{
return Result<bool>.Fail($"Plc{Sn}未连接");
6 months ago
}
PrepareBatteryOnReq req = new PrepareBatteryOnReq(readyBatterySign, takePositionNumber)
{
ReadyBatterySign = readyBatterySign,
TakePositionNumber = takePositionNumber,
};
this.Channel.WriteAndFlushAsync(req);
return Result<bool>.Success();
}
6 months ago
#endregion
public void Reset()
{
SwapDone = false;
DisassembleDone = false;
SwapStart = false;
}
private ILog Log()
{
var name = "Plc" + this.Sn;
ILog logger = LogManager.GetLogger(name);
Console.WriteLine(name + "-" + logger.GetHashCode());
return logger;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool Connect()
{
base.BaseConnect();
Log().Info($"charger {Sn} connect succeed");
return Connected;
}
/// <summary>
///
/// </summary>
/// <param name="sn"></param>
/// <param name="destAddr"></param>
public void SessionAttr(string sn, string destAddr)
{
ChannelUtils.AddAttr(Channel, PlcConst.ChargerSn, sn);
ChannelUtils.AddAttr(Channel, PlcConst.EqmTypeNo, sn);
ChannelUtils.AddAttr(Channel, PlcConst.EqmCode, sn);
ChannelUtils.AddAttr(Channel, PlcConst.DestAddr, destAddr);
}
6 months ago
}