From b87337b8d930d0d7ee7874f7356362b03992002b Mon Sep 17 00:00:00 2001 From: smartwyy <645583145@qq.com> Date: Thu, 30 May 2024 15:50:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=85=E7=94=B5=E6=9C=BA=E9=81=A5=E6=B5=8B?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=B8=8A=E6=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HybirdFrameworkCore/Redis/RedisHelper.cs | 16 +++++++++++++++- .../Handler/UploadTelemetryDataHandler.cs | 14 +++++++------- .../Msg/Charger/Req/UploadTelemetryData.cs | 5 +++++ .../Test/WeatherForecastController.cs | 10 +++++----- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/HybirdFrameworkCore/Redis/RedisHelper.cs b/HybirdFrameworkCore/Redis/RedisHelper.cs index 0f93318..cb2f136 100644 --- a/HybirdFrameworkCore/Redis/RedisHelper.cs +++ b/HybirdFrameworkCore/Redis/RedisHelper.cs @@ -89,6 +89,19 @@ public class RedisHelper : IDisposable return db.StringGet(key); } + /// + /// 先进先出队列 + /// + /// + /// + /// + public void PublishAsync(string topic, string value) + { + ISubscriber sub = GetConnect().GetSubscriber(); + sub.PublishAsync(topic, value); + } + + /// /// 获取Redis中的值(异步版本) /// @@ -107,7 +120,7 @@ public class RedisHelper : IDisposable /// 值 /// 值 /// 操作是否成功 - public bool SetKeyValueStr(string key, string value, TimeSpan? expiry = default(TimeSpan?)) + public bool SetKeyValueStr(string key, string value, TimeSpan? expiry = default(TimeSpan?)) { var db = GetConnect().GetDatabase(_defaultDB); return db.StringSet(key, value, expiry); @@ -124,6 +137,7 @@ public class RedisHelper : IDisposable var db = GetConnect().GetDatabase(_defaultDB); return await db.StringSetAsync(key, value); } + public bool Remove(string key) { var db = GetConnect().GetDatabase(_defaultDB); diff --git a/Service/Charger/Handler/UploadTelemetryDataHandler.cs b/Service/Charger/Handler/UploadTelemetryDataHandler.cs index 02909ce..69b7456 100644 --- a/Service/Charger/Handler/UploadTelemetryDataHandler.cs +++ b/Service/Charger/Handler/UploadTelemetryDataHandler.cs @@ -1,14 +1,10 @@ using DotNetty.Transport.Channels; using HybirdFrameworkCore.Autofac.Attribute; +using HybirdFrameworkCore.Redis; using log4net; +using Newtonsoft.Json; using Service.Charger.Client; -using Service.Charger.Common; using Service.Charger.Msg.Charger.Req; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Service.Charger.Handler { @@ -21,13 +17,17 @@ namespace Service.Charger.Handler public class UploadTelemetryDataHandler : SimpleChannelInboundHandler, IBaseHandler { private static readonly ILog Log = LogManager.GetLogger(typeof(UploadTelemetryDataHandler)); + + public RedisHelper RedisHelper { get; set; } protected override void ChannelRead0(IChannelHandlerContext ctx, UploadTelemetryData msg) { if (ClientMgr.TryGetClient(ctx.Channel, out var sn, out var client)) { - //存储日志 + msg.ChargerNo = sn; Log.Info($"receive {msg} from {sn}"); + RedisHelper.PublishAsync("UploadTelemetryData", JsonConvert.SerializeObject(msg)); + client.UploadTelemetryData = msg; //充电机实时充电功率 client.RealTimeChargePower = msg.HighVoltageAcquisitionCurrent * msg.HighVoltageAcquisitionVoltage; diff --git a/Service/Charger/Msg/Charger/Req/UploadTelemetryData.cs b/Service/Charger/Msg/Charger/Req/UploadTelemetryData.cs index c15aaf4..be59fdf 100644 --- a/Service/Charger/Msg/Charger/Req/UploadTelemetryData.cs +++ b/Service/Charger/Msg/Charger/Req/UploadTelemetryData.cs @@ -193,6 +193,11 @@ namespace Service.Charger.Msg.Charger.Req /// [Property(392 + 8, 32, PropertyReadConstant.Bit, 0.01, 2)] public float ACMeterCurrentBatteryValue { get; set; } + + /// + /// 设备编号 + /// + public string ChargerNo { get; set; } } } \ No newline at end of file diff --git a/WebStarter/Controllers/Test/WeatherForecastController.cs b/WebStarter/Controllers/Test/WeatherForecastController.cs index 8400c0f..a7ff4ac 100644 --- a/WebStarter/Controllers/Test/WeatherForecastController.cs +++ b/WebStarter/Controllers/Test/WeatherForecastController.cs @@ -1,8 +1,11 @@ using System.Text; using Autofac; using HybirdFrameworkCore.Autofac; +using HybirdFrameworkCore.Redis; using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; using Service.Charger.Client; +using Service.Charger.Msg.Charger.Req; using Service.System; namespace WebStarter.Controllers.Test; @@ -31,12 +34,9 @@ public class WeatherForecastController : ControllerBase { _logger.LogInformation("this is a hello world"); + RedisHelper redisHelper = AppInfo.Container.Resolve(); + redisHelper.PublishAsync("UploadTelemetryData", JsonConvert.SerializeObject(new UploadTelemetryData())); - ChargerClient client = AppInfo.Container.Resolve(); - client.InitBootstrap("127.0.0.1", 9998); - client.BaseConnect(); - client.SessionAttr("1", "3"); - client.Channel.WriteAndFlushAsync(Encoding.ASCII.GetBytes("ddddddddddd")); _logger.LogInformation("this is two hello world"); return Enumerable.Range(1, 5).Select(index => new WeatherForecast