|
|
|
using HybirdFrameworkCore.Autofac.Attribute;
|
|
|
|
using HybirdFrameworkCore.Entity;
|
|
|
|
using HybirdFrameworkDriver.Session;
|
|
|
|
using HybirdFrameworkDriver.TcpClient;
|
|
|
|
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 属性
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Plc编号
|
|
|
|
/// </summary>
|
|
|
|
public string Sn { get; set; }
|
|
|
|
|
|
|
|
public bool SwapDone { get; set; }
|
|
|
|
|
|
|
|
public bool DisassembleDone { 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}未连接");
|
|
|
|
}
|
|
|
|
TerminationOrderReq req = new TerminationOrderReq();
|
|
|
|
this.Channel.WriteAndFlushAsync(req);
|
|
|
|
return Result<bool>.Success();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 发送故障复位命令
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sn">Plc编号</param>
|
|
|
|
/// <param name="msg">消息</param>
|
|
|
|
public Result<bool> SendFaultResetReq()
|
|
|
|
{
|
|
|
|
if (!Connected)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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}未连接");
|
|
|
|
}
|
|
|
|
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}未连接");
|
|
|
|
}
|
|
|
|
PrepareBatteryOnReq req = new PrepareBatteryOnReq(readyBatterySign, takePositionNumber)
|
|
|
|
{
|
|
|
|
ReadyBatterySign = readyBatterySign,
|
|
|
|
TakePositionNumber = takePositionNumber,
|
|
|
|
};
|
|
|
|
this.Channel.WriteAndFlushAsync(req);
|
|
|
|
return Result<bool>.Success();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
{
|
|
|
|
SwapDone = false;
|
|
|
|
DisassembleDone = false;
|
|
|
|
}
|
|
|
|
private ILog Log()
|
|
|
|
{
|
|
|
|
var name = "Charger" + 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);
|
|
|
|
}
|
|
|
|
}
|