From e2c85517cc86ae5585a4e21272e37a1bf51eaee6 Mon Sep 17 00:00:00 2001 From: nxiaoxiao <3247747442@qq.com> Date: Fri, 16 Aug 2024 22:27:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=9B=B4=E5=A4=9A=E7=9A=84?= =?UTF-8?q?=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{ActiveData.cs => ActiveDataElement.cs} | 29 +++++++- GlobalArea.cs | 30 +++++++- Instructs/DeleteElementBroadcastInstuct.cs | 33 +++++++++ Instructs/Instruct.cs | 2 + .../SelectIngElementBroadcastInstruct.cs | 70 +++++++++++++++++++ Instructs/TextMessageBroadcastInstruct.cs | 39 +++++++++++ 6 files changed, 200 insertions(+), 3 deletions(-) rename Common/{ActiveData.cs => ActiveDataElement.cs} (82%) create mode 100644 Instructs/DeleteElementBroadcastInstuct.cs create mode 100644 Instructs/SelectIngElementBroadcastInstruct.cs create mode 100644 Instructs/TextMessageBroadcastInstruct.cs diff --git a/Common/ActiveData.cs b/Common/ActiveDataElement.cs similarity index 82% rename from Common/ActiveData.cs rename to Common/ActiveDataElement.cs index 34d8a46..bfc993b 100644 --- a/Common/ActiveData.cs +++ b/Common/ActiveDataElement.cs @@ -12,14 +12,39 @@ namespace OMS.NET.Common public class ActiveDataElement { + private static readonly JsonSerializerOptions options = new() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, // 属性名为小写 + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, // 忽略 null 值, + WriteIndented = true, // 美化输出 + Converters = { new ActiveDataElementConverter() } + }; public string EID { get; set; } public UserInfo? Pick { get; set; } public UserInfo? Select { get; set; } - public ActiveDataElement(string id) + public ActiveDataElement(long id) { EID = "E" + id; } + + public string ToJsonString() + { + return JsonSerializer.Serialize(this, options); + } + + public static ActiveDataElement? JsonParse(string json) + { + try + { + return JsonSerializer.Deserialize(json, options); + } + catch (Exception ex) + { + GlobalArea.Log.Error(ex.Message); + return null; + } + } } public class ActiveDataElementConverter : JsonConverter @@ -32,7 +57,7 @@ namespace OMS.NET.Common } reader.Read(); string? eid = (reader.GetString()?[1..]) ?? throw new JsonException(); // Removing 'E' prefix to get the ID - var element = new ActiveDataElement(eid); + var element = new ActiveDataElement(Convert.ToInt64(eid)); while (reader.Read()) { if (reader.TokenType == JsonTokenType.EndObject) diff --git a/GlobalArea.cs b/GlobalArea.cs index 3a1c6af..f2b455b 100644 --- a/GlobalArea.cs +++ b/GlobalArea.cs @@ -290,6 +290,34 @@ namespace OMS.NET private static readonly List _LayerDataList = new(); private static readonly object _LayerDataListLock = new(); + private static readonly List _ActiveDataList = new(); + private static readonly object _ActiveDataListLock = new(); + + public static bool IsElementExit(long elementId) + { + lock (_ActiveDataListLock) + { + ActiveDataElement? ad = _ActiveDataList.Find(u => u.EID == ("E" + elementId)); + return ad != null; + } + } + + public static ActiveDataElement? GetActiveDataElement(long elementId) + { + lock (_ActiveDataListLock) + { + return _ActiveDataList.Find(u => u.EID == ("E" + elementId)); + } + } + + public static void AddActiveDataElement(ActiveDataElement activeDataElement) + { + lock (_ActiveDataListLock) + { + _ActiveDataList.Add(activeDataElement); + } + } + /// /// 从数据库中构建layerid 和 templateid的对应关系 /// @@ -332,7 +360,7 @@ namespace OMS.NET /// /// 默认loglevel为3,发布版本需要改成2 /// - private static Logger _Logger = new(3); + private static readonly Logger _Logger = new(3); private static readonly object _LoggerLock = new(); public static Logger Log diff --git a/Instructs/DeleteElementBroadcastInstuct.cs b/Instructs/DeleteElementBroadcastInstuct.cs new file mode 100644 index 0000000..bfc2270 --- /dev/null +++ b/Instructs/DeleteElementBroadcastInstuct.cs @@ -0,0 +1,33 @@ +using System.Text.Json; + +namespace OMS.NET.Instructs +{ + public class DeleteElementBroadcastInstruct : BroadcastInstuct + { + public DeleteElementBroadcastInstruct() + { + this.Class = "deleteElement"; + } + + public override Task Handler(string wsid) + { + return Task.Run(() => + { + if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查不通过则直接退出 + if (Data?.GetType() != typeof(JsonElement)) return; + try + { + string conveyor = GlobalArea.GetLoginEmailByID(wsid); + //解析id和tmpId + long id = Data.GetProperty("id").GetInt64(); + string tmpId = Data.GetProperty("tmpId").GetString(); + //需要先实现activeData相关 + } + catch + { + + } + }); + } + } +} \ No newline at end of file diff --git a/Instructs/Instruct.cs b/Instructs/Instruct.cs index 0f25cfb..5173568 100644 --- a/Instructs/Instruct.cs +++ b/Instructs/Instruct.cs @@ -62,6 +62,8 @@ namespace OMS.NET.Instructs "line" => JsonSerializer.Deserialize(root.GetRawText(), options), "area" => JsonSerializer.Deserialize(root.GetRawText(), options), "curve" => JsonSerializer.Deserialize(root.GetRawText(), options), + "deleteElement" => JsonSerializer.Deserialize(root.GetRawText(), options), + "textMessage" => JsonSerializer.Deserialize(root.GetRawText(), options), _ => JsonSerializer.Deserialize(root.GetRawText(), options) }, _ => null diff --git a/Instructs/SelectIngElementBroadcastInstruct.cs b/Instructs/SelectIngElementBroadcastInstruct.cs new file mode 100644 index 0000000..17f5bf4 --- /dev/null +++ b/Instructs/SelectIngElementBroadcastInstruct.cs @@ -0,0 +1,70 @@ +using System.Text.Json; +using OMS.NET.Common; +using OMS.NET.DbClass; + +namespace OMS.NET.Instructs +{ + public class SelectIngElementBroadcastInstruct : BroadcastInstuct + { + public SelectIngElementBroadcastInstruct() + { + this.Class = "selectIngElement"; + } + + public override Task Handler(string wsid) + { + return Task.Run(() => + { + if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查不通过则直接退出 + if (Data?.GetType() != typeof(JsonElement)) return; + try + { + long id = Data.GetInt64(); + string conveyor = GlobalArea.GetLoginEmailByID(wsid); + AccountData accountData = GlobalArea.GetLoginAccountData(conveyor)!; + UserInfo userInfo = new() + { + Email = conveyor, + Color = accountData.HeadColor, + Name = accountData.UserName, + }; + if (GlobalArea.IsElementExit(id))//已存在activedata项 + { + ActiveDataElement activeDataElement = GlobalArea.GetActiveDataElement(id)!; + if (activeDataElement.Select == null) + { + activeDataElement.Select = userInfo; + } + else + { + throw new Exception($"{id}已被{activeDataElement.Select.Name}选中"); + } + } + else + { + GlobalArea.AddActiveDataElement(new ActiveDataElement(id) + { + Select = userInfo + }); + } + string time = GlobalArea.GetCurrentTime(); + ResponseOrBroadcastInstructs.Add(new SelectIngElementBroadcastInstruct() + { + IsBroadcast = true, + Conveyor = conveyor, + Time = time, + Data = new + { + id = id, + color = accountData.HeadColor + } + }); + } + catch (Exception ex) + { + GlobalArea.Log.Warn($"处理{this.Class}广播指令出错:" + ex.Message); + } + }); + } + } +} \ No newline at end of file diff --git a/Instructs/TextMessageBroadcastInstruct.cs b/Instructs/TextMessageBroadcastInstruct.cs new file mode 100644 index 0000000..b7b1d0b --- /dev/null +++ b/Instructs/TextMessageBroadcastInstruct.cs @@ -0,0 +1,39 @@ +using System.Text.Json; + +namespace OMS.NET.Instructs +{ + public class TextMessageBroadcastInstruct : BroadcastInstuct + { + public TextMessageBroadcastInstruct() + { + this.Class = "textMessage"; + } + + public override Task Handler(string wsid) + { + return Task.Run(() => + { + if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查不通过则直接退出 + if (Data?.GetType() != typeof(JsonElement)) return; + try + { + string message = Data.GetProperty("message").GetString(); + string conveyor = GlobalArea.GetLoginEmailByID(wsid); + string time = GlobalArea.GetCurrentTime(); + GlobalArea.Log.Info($"[{time}] {conveyor}:{message}"); + ResponseOrBroadcastInstructs.Add(new TextMessageBroadcastInstruct() + { + IsBroadcast = true, + Conveyor = conveyor, + Time = time, + Data = Data + }); + } + catch (Exception ex) + { + GlobalArea.Log.Warn("处理textMessage发生问题:" + ex.Message); + } + }); + } + } +} \ No newline at end of file