using System.Text.Json.Nodes; using Autofac; using AutoMapper; using Entity.Api.Resp; using Entity.Constant; using Entity.DbModel.Station; using HybirdFrameworkCore.Autofac; using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Entity; using HybirdFrameworkDriver.Session; using HybirdFrameworkDriver.TcpClient; using log4net; using Newtonsoft.Json; using Repository.Station; using Service.Charger.Client; using Service.Charger.Codec; using Service.Charger.Handler; using Service.Charger.Msg.Host.Req; using Service.Execute; using Service.Plc.Msg.Host.Req; namespace Service.Charger.Server; [Scope] public class PlcClient : TcpClient { //0 待机 1:换电任务 2:移仓任务 3:消防任务 #region 属性 private long TaskNo = 0; /// /// Plc编号 /// public string Sn { get; set; } /// /// /// public bool SwapDone { get; set; } /// /// 换电开始标记 /// public bool SwapStart { get; set; } /// /// 电池搬运结束 /// public bool BatteryMoveDone { get; set; } public bool DisassembleDone { get; set; } //自动模式 public bool Auto { get; set; } //远程模式 public bool Remote { get; set; } //初始化完成 public bool Init { get; set; } //9号仓是否有电池 public bool Is9Exist { get; set; } private readonly BinInfoRepository _binInfoRepository; public PlcClient(BinInfoRepository binInfoRepository) { _binInfoRepository = binInfoRepository; } #endregion //修改当前任务 public void ExChangeTaskNo(long taskNo) { Interlocked.Exchange(ref TaskNo, taskNo); } //重置当前任务 public void ResetTaskNo() { Interlocked.Exchange(ref TaskNo, 0); } public bool IsTaskFree() { return ReadTaskNo() == 0l; } //读取当前任务 public long ReadTaskNo() { return Interlocked.Read(ref TaskNo); } #region send /// /// 发送初始化命令 /// /// Plc编号 /// 消息 public Result SendInitializeCommandReq() { if (!Connected) { return Result.Fail($"Plc{Sn}未连接"); } InitializeCommandReq req = new InitializeCommandReq(); this.Channel.WriteAndFlushAsync(req); StationSoftMgr.PutDeviceLog((int)StationConstant.DeviceCode.Plc,SwapConstant.PlcProtocol.Init, JsonConvert.SerializeObject(req),(int)SwapConstant.CommunicationType.Send); return Result.Success(); } /// /// 发送出库命令 /// /// 出库方式 /// 取仓位号 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(); } /// /// 发送红绿灯 /// /// 出库方式 public Result SendTrafficLightReq(byte trafficLight) { if (!Connected) { return Result.Fail($"Plc{Sn}未连接"); } TrafficLightReq req = new TrafficLightReq(trafficLight) { TrafficLight = trafficLight, }; this.Channel.WriteAndFlushAsync(req); return Result.Success(); } /// /// 发送开始换电命令 /// /// 取仓位号 /// 存仓位号 /// 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 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 SendWhetherChargedReq() { if (!Connected) { return Result.Fail($"Plc{Sn}未连接"); } List binInfos = _binInfoRepository.Query(); var configuration = new MapperConfiguration(cfg => cfg.CreateMap()); var mapper = configuration.CreateMapper(); List binInfoList = mapper.Map>(binInfos); byte[] granaries = new byte[8]; for (int i = 0; i < binInfoList.Count && i < granaries.Length; i++) { granaries[i] = (binInfoList[i].ChargeStatus == 0 || binInfoList[i].ChargeStatus.Value == 2 || binInfoList[i].ChargeStatus.Value == 4) ? (byte)0 : (byte)1; } WhetherChargedReq req=new WhetherChargedReq(granaries[0], granaries[1], granaries[2], granaries[3], granaries[4], granaries[5], granaries[6], granaries[7]); this.Channel.WriteAndFlushAsync(req); return Result.Success(); } ///// ///// 发送电池充电状态 ///// ///// Plc编号 ///// 消息 //public Result SendWhetherChargedResq() //{ // if (!Connected) // { // return Result.Fail($"Plc{Sn}未连接"); // } // List binInfos = _binInfoRepository.Query(); // var configuration = new MapperConfiguration(cfg => cfg.CreateMap()); // var mapper = configuration.CreateMapper(); // List binInfoList = mapper.Map>(binInfos); // byte[] granaries = new byte[8]; // for (int i = 0; i < binInfoList.Count && i < granaries.Length; i++) // { // granaries[i] = (binInfoList[i].ChargeStatus == 0 || binInfoList[i].ChargeStatus.Value == 2 || binInfoList[i].ChargeStatus.Value == 4) ? (byte)0 : (byte)1; // } // WhetherChargedResq req = new WhetherChargedResq(granaries[0], granaries[1], granaries[2], granaries[3], granaries[4], granaries[5], granaries[6], granaries[7]); // 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(); } public EquipAlarmDefineRepository _equipAlarmDefineRepository = AppInfo.Container.Resolve(); public List GetAlarmDeinneList() { return _equipAlarmDefineRepository.QueryByClauseToList(i => i.EquipCode == "plc"); } /// /// 发送准备电池开启 /// /// 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; SwapStart = false; ResetTaskNo(); } public override ILog Logger() { return Log(); } private ILog Log() { var name = "Plc" + 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.PlcSn, sn); ChannelUtils.AddAttr(Channel, PlcConst.EqmTypeNo, sn); ChannelUtils.AddAttr(Channel, PlcConst.EqmCode, sn); ChannelUtils.AddAttr(Channel, PlcConst.DestAddr, destAddr); } }