From 2682bd343dc5cfc03199d4b86f2203567ac6945a Mon Sep 17 00:00:00 2001
From: rszn <645583145@qq.com>
Date: Thu, 4 Jul 2024 11:29:05 +0800
Subject: [PATCH] =?UTF-8?q?=E6=8D=A2=E7=82=B9=E8=AE=A2=E5=8D=95=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0=E5=90=AF=E5=8A=A8=E6=96=B9=E5=BC=8F=20=E5=AE=9E?=
=?UTF-8?q?=E6=97=B6=E9=80=9A=E8=AE=AF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Entity/Api/Resp/ChargeOrderResp.cs | 12 +++--
Entity/DbModel/Station/ChargeOrder.cs | 6 +++
Service/RealTime/MyHub.cs | 77 +++++++++++++++++++++++++++
Service/RealTime/RtMsg.cs | 11 ++++
WebStarter/Program.cs | 18 +++++--
WebStarter/appsettings.prod.json | 3 ++
WebStarter/db/0702.sql | 1 +
7 files changed, 121 insertions(+), 7 deletions(-)
create mode 100644 Service/RealTime/MyHub.cs
create mode 100644 Service/RealTime/RtMsg.cs
create mode 100644 WebStarter/db/0702.sql
diff --git a/Entity/Api/Resp/ChargeOrderResp.cs b/Entity/Api/Resp/ChargeOrderResp.cs
index 6b9e545..d972e6d 100644
--- a/Entity/Api/Resp/ChargeOrderResp.cs
+++ b/Entity/Api/Resp/ChargeOrderResp.cs
@@ -12,7 +12,7 @@ namespace Entity.Api.Resp
/// Desc:id
/// Default:
/// Nullable:False
- ///
+ ///
public int Id { get; set; }
///
@@ -253,8 +253,14 @@ namespace Entity.Api.Resp
/// Nullable:True
///
public int? CanUpload { get; set; }
-
-
+
+ ///
+ /// 启动方式:0-站控自动启动,1-站控手动启动,2-充电机启动
+ /// Default:
+ /// Nullable:True
+ ///
+ public int StartType { get; set; }
+
///
/// Desc:交流电表量
/// Default:
diff --git a/Entity/DbModel/Station/ChargeOrder.cs b/Entity/DbModel/Station/ChargeOrder.cs
index a6c9922..b98d1b1 100644
--- a/Entity/DbModel/Station/ChargeOrder.cs
+++ b/Entity/DbModel/Station/ChargeOrder.cs
@@ -282,5 +282,11 @@ namespace Entity.DbModel.Station
///
[SugarColumn(ColumnName = "can_upload")]
public int CanUpload { get; set; }
+
+ ///
+ /// 启动方式:0-站控自动启动,1-站控手动启动,2-充电机启动
+ ///
+ [SugarColumn(ColumnName = "start_type")]
+ public int StartType { get; set; }
}
}
diff --git a/Service/RealTime/MyHub.cs b/Service/RealTime/MyHub.cs
new file mode 100644
index 0000000..47f7dfd
--- /dev/null
+++ b/Service/RealTime/MyHub.cs
@@ -0,0 +1,77 @@
+using System.Collections.Concurrent;
+using HybirdFrameworkCore.Utils;
+using log4net;
+using Microsoft.AspNetCore.SignalR;
+using Newtonsoft.Json;
+
+namespace Service.RealTime;
+
+public class MyHub : Hub
+{
+ private static readonly JsonSerializerSettings settings = new JsonSerializerSettings()
+ {
+ ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
+ DateFormatString = "yyyy-MM-dd HH:mm:ss",
+ NullValueHandling = NullValueHandling.Ignore
+ };
+
+ private static readonly ILog Log = LogManager.GetLogger(typeof(MyHub));
+
+ private static readonly ConcurrentDictionary
+ Dictionary = new ConcurrentDictionary();
+
+
+ #region rpc
+
+ public async Task SendMsg(string toUser, string msg)
+ {
+ Log.Info($"receive {msg}");
+ await Clients.All.SendAsync("ReceiveMsg", toUser +":"+msg);
+ }
+
+ public void Login(string uid)
+ {
+ if (!Dictionary.ContainsKey(uid))
+ {
+ Dictionary[uid] = Context.ConnectionId;
+ }
+ }
+
+ public void Logout(string uid)
+ {
+ if (ObjUtils.IsNotNullOrWhiteSpace(uid))
+ {
+ Dictionary.Remove(uid);
+ }
+ }
+
+ public void InvokeWithoutResult(string s)
+ {
+ RtMsg? rtMsg = JsonConvert.DeserializeObject(s, settings);
+ if (rtMsg != null)
+ {
+ }
+ }
+
+ public RtMsg? Invoke(RtMsg rtMsg)
+ {
+ if (rtMsg != null)
+ {
+ object result = new object();
+ RtMsg msg = new RtMsg()
+ {
+ Id = rtMsg.Id,
+ Cmd = rtMsg.Cmd,
+ Datetime = DateTime.Now,
+ FromUser = "swap",
+ Msg = JsonConvert.SerializeObject(result, settings)
+ };
+ //TODO 根据cmd执行业务
+ return msg;
+ }
+
+ return null;
+ }
+
+ #endregion
+}
diff --git a/Service/RealTime/RtMsg.cs b/Service/RealTime/RtMsg.cs
new file mode 100644
index 0000000..5aac8b7
--- /dev/null
+++ b/Service/RealTime/RtMsg.cs
@@ -0,0 +1,11 @@
+namespace Service.RealTime;
+
+public class RtMsg
+{
+ public int Id { get; set; }
+ public string FromUser { get; set; }
+ public string ToUser { get; set; }
+ public DateTime Datetime { get; set; }
+ public string Cmd { get; set; }
+ public string Msg { get; set; }
+}
diff --git a/WebStarter/Program.cs b/WebStarter/Program.cs
index 499ab70..8e297f9 100644
--- a/WebStarter/Program.cs
+++ b/WebStarter/Program.cs
@@ -15,6 +15,7 @@ using Microsoft.IdentityModel.Tokens;
using Service.Cloud.Client;
using Service.Execute;
using Service.Plc.Client;
+using Service.RealTime;
using SqlSugar;
using SqlSugar.IOC;
@@ -97,10 +98,16 @@ builder.Services.AddSwaggerGen(c =>
c.IncludeXmlComments(Path.Combine(basePath, "HybirdFrameworkCore.xml"), true);
});
-builder.Services.AddControllers().AddJsonOptions(configure =>
+bool.TryParse(AppSettingsHelper.GetContent("SignalR", "Enabled"), out var signalrEnabled);
+if (signalrEnabled)
{
- configure.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
-});
+ builder.Services.AddSignalR(options =>
+ {
+ options.EnableDetailedErrors = true;
+ options.KeepAliveInterval = TimeSpan.FromMinutes(1);
+ });
+}
+
// 添加jwt验证
@@ -135,6 +142,10 @@ app.UseStaticFiles();
app.UseAuthorization();
app.UseCors("myCors");
app.MapControllers();
+if (signalrEnabled)
+{
+ app.MapHub("/realtime");
+}
var list = AppSettingsHelper.GetContent("Kestrel", "Endpoints", "http", "Url");
foreach (var s in list.Split(";"))
@@ -164,7 +175,6 @@ if (AppSettingsHelper.GetBool("swap", "enable"))
StationSoftMgr.SwappingStateMachineStart();
}
-
TaskInit.Init();
QuartzSchedulerFactory.Init();
diff --git a/WebStarter/appsettings.prod.json b/WebStarter/appsettings.prod.json
index c187389..ecbc6b0 100644
--- a/WebStarter/appsettings.prod.json
+++ b/WebStarter/appsettings.prod.json
@@ -129,5 +129,8 @@
},
"Job": {
"Enabled": true
+ },
+ "SignalR": {
+ "Enabled": false
}
}
diff --git a/WebStarter/db/0702.sql b/WebStarter/db/0702.sql
new file mode 100644
index 0000000..f07a93e
--- /dev/null
+++ b/WebStarter/db/0702.sql
@@ -0,0 +1 @@
+alter table charge_order add COLUMN start_type int comment '0-站控自启;1-站控手动启动;2-充电机自启';