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 { #region 属性 /// /// Plc编号 /// public string Sn { get; set; } public bool SwapDone { get; set; } public bool DisassembleDone { get; set; } #endregion #region send /// /// 发送初始化命令 /// /// Plc编号 /// 消息 public Result SendInitializeCommandReq() { if (!Connected) { return Result.Fail($"Plc{Sn}未连接"); } InitializeCommandReq req = new InitializeCommandReq(); this.Channel.WriteAndFlushAsync(req); return Result.Success(); } /// /// 发送出库命令 /// /// Plc编号 /// 消息 public Result SendOutboundCommandReq(byte outboundMode,byte takePositionNumber) { if (!Connected) { return Result.Fail($"Plc{Sn}未连接"); } OutboundCommandReq req = new OutboundCommandReq(outboundMode, takePositionNumber) { OutboundMode = outboundMode, TakePositionNumber = takePositionNumber, }; this.Channel.WriteAndFlushAsync(req); return Result.Success(); } /// /// 发送开始换电命令 /// /// Plc编号 /// 消息 public Result SendStartBatterySwapReq(byte takePositionNumber,byte positionNumber) { if (!Connected) { return Result.Fail($"Plc{Sn}未连接"); } StartBatterySwapReq req = new StartBatterySwapReq(takePositionNumber, positionNumber) { TakePositionNumber = takePositionNumber, PositionNumber = positionNumber, }; this.Channel.WriteAndFlushAsync(req); return Result.Success(); } /// /// 发送开始搬电命令 /// /// Plc编号 /// 消息 public Result SendStartMovElectricityReq(byte takeBatteryShelfNumber,byte saveBatteryShelfNumber) { if (!Connected) { return Result.Fail($"Plc{Sn}未连接"); } StartMovElectricityReq req = new StartMovElectricityReq(takeBatteryShelfNumber, saveBatteryShelfNumber) { TakeBatteryShelfNumber = takeBatteryShelfNumber, SaveBatteryShelfNumber = saveBatteryShelfNumber, }; this.Channel.WriteAndFlushAsync(req); return Result.Success(); } /// /// 发送车辆型号 /// /// Plc编号 /// 消息 public Result SendVehicleModelReq(byte cartNo) { if (!Connected) { return Result.Fail($"Plc{Sn}未连接"); } VehicleModelReq req = new VehicleModelReq(cartNo) { CartNo = cartNo, }; this.Channel.WriteAndFlushAsync(req); return Result.Success(); } /// /// 发送移库命令 /// /// Plc编号 /// 消息 public Result SendMoveCommandReq(byte takePositionNumber,byte positionNumber) { if (!Connected) { return Result.Fail($"Plc{Sn}未连接"); } MoveCommandReq req = new MoveCommandReq(takePositionNumber, positionNumber) { TakePositionNumber = takePositionNumber, PositionNumber = positionNumber, }; this.Channel.WriteAndFlushAsync(req); return Result.Success(); } /// /// 发送继续命令 /// /// Plc编号 /// 消息 public Result SendContinueCommandReq( ) { if (!Connected) { return Result.Fail($"Plc{Sn}未连接"); } ContinueCommandReq req = new ContinueCommandReq(); this.Channel.WriteAndFlushAsync(req); return Result.Success(); } /// /// 发送暂停命令 /// /// Plc编号 /// 消息 public Result SendPauseCommandReq() { if (!Connected) { return Result.Fail($"Plc{Sn}未连接"); } PauseCommandReq req = new PauseCommandReq(); this.Channel.WriteAndFlushAsync(req); return Result.Success(); } /// /// 发送入库命令 /// /// Plc编号 /// 消息 public Result SendInboundCommandsReq(byte positionNumber) { if (!Connected) { return Result.Fail($"Plc{Sn}未连接"); } InboundCommandsReq req = new InboundCommandsReq(positionNumber) { PositionNumber = positionNumber, }; this.Channel.WriteAndFlushAsync(req); return Result.Success(); } /// /// 发送终止命令 /// /// Plc编号 /// 消息 public Result SendTerminationOrderReq( ) { if (!Connected) { return Result.Fail($"Plc{Sn}未连接"); } TerminationOrderReq req = new TerminationOrderReq(); this.Channel.WriteAndFlushAsync(req); return Result.Success(); } /// /// 发送故障复位命令 /// /// Plc编号 /// 消息 public Result SendFaultResetReq() { if (!Connected) { return Result.Fail($"Plc{Sn}未连接"); } FaultResetReq req = new FaultResetReq(); this.Channel.WriteAndFlushAsync(req); return Result.Success(); } ///// ///// 发送参数设置 ///// ///// Plc编号 ///// 消息 //public Result SendParameterSettingsReq(byte[] messageBodyAddress,byte parameterType,byte parameter) //{ // if (!Connected) // { // return Result.Fail($"Plc{Sn}未连接"); // } // ParameterSettingsReq req = new ParameterSettingsReq( parameterType, parameter) // { // MessageBodyAddress = messageBodyAddress, // ParameterType = parameterType, // Parameter = parameter, // }; // this.Channel.WriteAndFlushAsync(req); // return Result.Success(); //} /// /// 发送参数设置 /// /// Plc编号 /// 消息 public Result SendParameterSettingsReq( byte parameterType, byte parameter) { if (!Connected) { return Result.Fail($"Plc{Sn}未连接"); } ParameterSettingsReq req = new ParameterSettingsReq(parameterType, parameter) { ParameterType = parameterType, Parameter = parameter, }; this.Channel.WriteAndFlushAsync(req); return Result.Success(); } /// /// 发送准备电池开启 /// /// Plc编号 /// 消息 public Result SendPrepareBatteryOnReq(byte readyBatterySign,byte takePositionNumber) { if (!Connected) { return Result.Fail($"Plc{Sn}未连接"); } PrepareBatteryOnReq req = new PrepareBatteryOnReq(readyBatterySign, takePositionNumber) { ReadyBatterySign = readyBatterySign, TakePositionNumber = takePositionNumber, }; this.Channel.WriteAndFlushAsync(req); return Result.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; } /// /// /// /// public bool Connect() { base.BaseConnect(); Log().Info($"charger {Sn} connect succeed"); return Connected; } /// /// /// /// /// 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); } }