From 0a61c7d888952a5feeb2966de20dcdb25db04e06 Mon Sep 17 00:00:00 2001 From: zby <24947@USER> Date: Mon, 9 Sep 2024 19:29:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=BB=99plc=E4=B8=8B?= =?UTF-8?q?=E5=8F=91=E7=BA=A2=E7=BB=BF=E7=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Service/Plc/Client/PlcClient.cs | 19 ++++++++++ Service/Plc/Codec/Decoder.cs | 2 ++ .../Plc/Handler/TrafficLightResqHandler.cs | 35 +++++++++++++++++++ Service/Plc/Msg/Host/Req/TrafficLightReq.cs | 27 ++++++++++++++ Service/Plc/Msg/Plc/Resp/TrafficLightResq.cs | 17 +++++++++ WebStarter/Controllers/PlcController.cs | 19 +++++++++- 6 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 Service/Plc/Handler/TrafficLightResqHandler.cs create mode 100644 Service/Plc/Msg/Host/Req/TrafficLightReq.cs create mode 100644 Service/Plc/Msg/Plc/Resp/TrafficLightResq.cs diff --git a/Service/Plc/Client/PlcClient.cs b/Service/Plc/Client/PlcClient.cs index d92799a..f31bbcc 100644 --- a/Service/Plc/Client/PlcClient.cs +++ b/Service/Plc/Client/PlcClient.cs @@ -84,6 +84,25 @@ public class PlcClient : TcpClient } + /// + /// 发送红绿灯 + /// + /// 出库方式 + 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(); + } + + /// /// 发送开始换电命令 /// diff --git a/Service/Plc/Codec/Decoder.cs b/Service/Plc/Codec/Decoder.cs index ef8dd62..e2a23ac 100644 --- a/Service/Plc/Codec/Decoder.cs +++ b/Service/Plc/Codec/Decoder.cs @@ -196,6 +196,8 @@ public class Decoder : ByteToMessageDecoder 117 => ModelConvert.Decode(bytes), /*0x*/ 94 => ModelConvert.Decode(bytes), + + 119=> ModelConvert.Decode < TrafficLightResq>(bytes), _ => new ASDU() }; diff --git a/Service/Plc/Handler/TrafficLightResqHandler.cs b/Service/Plc/Handler/TrafficLightResqHandler.cs new file mode 100644 index 0000000..5c808db --- /dev/null +++ b/Service/Plc/Handler/TrafficLightResqHandler.cs @@ -0,0 +1,35 @@ +using DotNetty.Transport.Channels; +using HybirdFrameworkCore.Autofac.Attribute; +using HybirdFrameworkCore.Utils; +using log4net; +using Newtonsoft.Json; +using Service.Charger.Client; +using Service.Charger.Msg.Charger.Req; +using Service.Charger.Msg.Charger.Resp; + + +namespace Service.Charger.Handler; + +/// +/// 红绿灯命令回复 处理器 +/// +[Order(8)] +[Scope("InstancePerDependency")] +public class TrafficLightResqHandler : SimpleChannelInboundHandler, IBaseHandler +{ + + /// + /// + /// + private static readonly ILog Log = LogManager.GetLogger(typeof(TrafficLightResqHandler)); + + + /// + /// + /// + protected override void ChannelRead0(IChannelHandlerContext ctx, TrafficLightResq msg) + { + Log.Info("TrafficLightResq Order Reply"); + + } +} diff --git a/Service/Plc/Msg/Host/Req/TrafficLightReq.cs b/Service/Plc/Msg/Host/Req/TrafficLightReq.cs new file mode 100644 index 0000000..87035b4 --- /dev/null +++ b/Service/Plc/Msg/Host/Req/TrafficLightReq.cs @@ -0,0 +1,27 @@ +using System; +using HybirdFrameworkCore.Autofac.Attribute; + +namespace Service.Charger.Msg.Host.Req; + +/// +/// 红绿灯命令 +/// +public class TrafficLightReq: ASDU { + + /// + /// 红绿灯 0x00-红灯, 0x01-绿灯 + /// + [Property(0, 8 )] + public byte TrafficLight { get;set;} + + public TrafficLightReq(byte trafficLight) + { + FrameTypeNo = 118; + MsgBodyCount = 1; + TransReason = 3; + PublicAddr = 0; + MsgBodyAddr = new byte[] { 0, 0, 0 }; + TrafficLight = trafficLight; + } + +} diff --git a/Service/Plc/Msg/Plc/Resp/TrafficLightResq.cs b/Service/Plc/Msg/Plc/Resp/TrafficLightResq.cs new file mode 100644 index 0000000..5559633 --- /dev/null +++ b/Service/Plc/Msg/Plc/Resp/TrafficLightResq.cs @@ -0,0 +1,17 @@ +using System; +using HybirdFrameworkCore.Autofac.Attribute; + +namespace Service.Charger.Msg.Charger.Resp; + +/// +/// 红绿灯命令回复 +/// +public class TrafficLightResq : ASDU +{ + /// + /// 应答结果 + /// + [Property(0, 8 )] + public byte AnswerResult {get;set;} + +} diff --git a/WebStarter/Controllers/PlcController.cs b/WebStarter/Controllers/PlcController.cs index df03150..7a223bd 100644 --- a/WebStarter/Controllers/PlcController.cs +++ b/WebStarter/Controllers/PlcController.cs @@ -120,6 +120,23 @@ public class PlcController : ControllerBase return Result.Fail("充电机未连接"); } + /// + /// 发送红绿灯命令 + /// + /// 充电机编号 + /// 消息 + [HttpGet] + [Route("SendTrafficLightReq/{trafficLight}")] + public Result SendTrafficLightReq(byte trafficLight) + { + Service.Charger.Server.PlcClient? chargerClient = ClientMgr.PlcClient; + if (chargerClient != null) + { + return chargerClient.SendTrafficLightReq(trafficLight); + } + return Result.Fail("充电机未连接"); + } + /// /// 发送继续命令 /// @@ -144,7 +161,7 @@ public class PlcController : ControllerBase /// 消息 [HttpGet] [Route("SendPauseCommandReq")] - public Result SendPauseCommandReq( ) + public Result SendPauseCommandReq() { Service.Charger.Server.PlcClient? chargerClient = ClientMgr.PlcClient; if (chargerClient != null)