diff --git a/Common/ActiveDataElement.cs b/Common/ActiveDataElement.cs index bfc993b..63b8437 100644 --- a/Common/ActiveDataElement.cs +++ b/Common/ActiveDataElement.cs @@ -12,7 +12,7 @@ namespace OMS.NET.Common public class ActiveDataElement { - private static readonly JsonSerializerOptions options = new() + public static readonly JsonSerializerOptions options = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, // 属性名为小写 DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, // 忽略 null 值, diff --git a/GlobalArea.cs b/GlobalArea.cs index f2b455b..0e82699 100644 --- a/GlobalArea.cs +++ b/GlobalArea.cs @@ -293,6 +293,17 @@ namespace OMS.NET private static readonly List _ActiveDataList = new(); private static readonly object _ActiveDataListLock = new(); + public static List GetActiveDataList + { + get + { + lock (_ActiveDataListLock) + { + return _ActiveDataList.Where(x => x.Pick != null || x.Select != null).ToList(); + } + } + } + public static bool IsElementExit(long elementId) { lock (_ActiveDataListLock) diff --git a/Instructs/GetActiveDataInstruct.cs b/Instructs/GetActiveDataInstruct.cs new file mode 100644 index 0000000..bd2b07d --- /dev/null +++ b/Instructs/GetActiveDataInstruct.cs @@ -0,0 +1,24 @@ +namespace OMS.NET.Instructs +{ + public class GetActiveDataInstruct : Instruct + { + public GetActiveDataInstruct() + { + this.Type = "get_activeData"; + } + + public override Task Handler(string wsid) + { + return Task.Run(() => + { + if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查,不通过则直接退出 + this.ResponseOrBroadcastInstructs.Add(new Instruct() + { + IsResponse = true, + Type = "send_activeData", + Data = GlobalArea.GetActiveDataList + }); + }); + } + } +} \ No newline at end of file diff --git a/Instructs/Instruct.cs b/Instructs/Instruct.cs index 65ae634..5f6122c 100644 --- a/Instructs/Instruct.cs +++ b/Instructs/Instruct.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using System.Runtime.InteropServices; using System.Text.Json; using System.Text.Json.Serialization; +using OMS.NET.Common; namespace OMS.NET.Instructs { @@ -52,6 +53,7 @@ namespace OMS.NET.Instructs "get_presence" => JsonSerializer.Deserialize(root.GetRawText(), options), "get_mapData" => JsonSerializer.Deserialize(root.GetRawText(), options), "get_mapLayer" => JsonSerializer.Deserialize(root.GetRawText(), options), + "get_activeData" => JsonSerializer.Deserialize(root.GetRawText(), options), "ping" => JsonSerializer.Deserialize(root.GetRawText(), options), //广播指令 @@ -64,6 +66,10 @@ namespace OMS.NET.Instructs "curve" => JsonSerializer.Deserialize(root.GetRawText(), options), "deleteElement" => JsonSerializer.Deserialize(root.GetRawText(), options), "textMessage" => JsonSerializer.Deserialize(root.GetRawText(), options), + "selectIngElement" => JsonSerializer.Deserialize(root.GetRawText(), options), + "selectEndElement" => JsonSerializer.Deserialize(root.GetRawText(), options), + "pickIngElement" => JsonSerializer.Deserialize(root.GetRawText(), options), + "pickEndElement" => JsonSerializer.Deserialize(root.GetRawText(), options), _ => JsonSerializer.Deserialize(root.GetRawText(), options) }, _ => null @@ -96,7 +102,14 @@ namespace OMS.NET.Instructs if (value.Data != null) { writer.WritePropertyName(InstructNamingPolicy.Convert(nameof(Instruct.Data))); - JsonSerializer.Serialize(writer, value.Data, value.Data.GetType(), options); + if (value.Data.GetType() == typeof(List)) + { + JsonSerializer.Serialize(writer, value.Data, value.Data.GetType(), ActiveDataElement.options); + } + else + { + JsonSerializer.Serialize(writer, value.Data, value.Data.GetType(), options); + } } writer.WriteEndObject(); } diff --git a/Instructs/PickEndElementInstruct.cs b/Instructs/PickEndElementInstruct.cs new file mode 100644 index 0000000..06f2933 --- /dev/null +++ b/Instructs/PickEndElementInstruct.cs @@ -0,0 +1,50 @@ + +using System.Text.Json; +using OMS.NET.Common; +using OMS.NET.DbClass; + +namespace OMS.NET.Instructs +{ + public class PickEndElementInstruct : Instruct + { + public PickEndElementInstruct() + { + Type = "broadcast"; + Class = "pickEndElement"; + } + + public override Task Handler(string wsid) + { + return Task.Run(() => + { + if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查,不通过则直接退出 + if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查 + try + { + long id = Data.GetInt64(); + string conveyor = GlobalArea.GetLoginEmailByID(wsid); + if (GlobalArea.IsElementExit(id))//已存在activedata项 + { + ActiveDataElement activeDataElement = GlobalArea.GetActiveDataElement(id)!; + if (activeDataElement.Pick?.Email == conveyor) + { + activeDataElement.Pick = null; + string time = GlobalArea.GetCurrentTime(); + ResponseOrBroadcastInstructs.Add(new PickEndElementInstruct() + { + IsBroadcast = true, + Conveyor = conveyor, + Time = time, + Data = id + }); + } + } + } + catch (Exception ex) + { + GlobalArea.Log.Warn($"处理{this.Class}广播指令出错:" + ex.Message); + } + }); + } + } +} \ No newline at end of file diff --git a/Instructs/PickIngElementInstruct.cs b/Instructs/PickIngElementInstruct.cs new file mode 100644 index 0000000..7e9a33a --- /dev/null +++ b/Instructs/PickIngElementInstruct.cs @@ -0,0 +1,71 @@ +using System.Text.Json; +using OMS.NET.Common; +using OMS.NET.DbClass; + +namespace OMS.NET.Instructs +{ + public class PickIngElementInstruct : Instruct + { + public PickIngElementInstruct() + { + this.Type = "broadcast"; + this.Class = "pickIngElement"; + } + + public override Task Handler(string wsid) + { + return Task.Run(() => + { + if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查,不通过则直接退出 + if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查 + 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.Pick == null) + { + activeDataElement.Pick = userInfo; + } + else + { + throw new Exception($"{id}已被{activeDataElement.Pick.Name}Pick"); + } + } + else + { + GlobalArea.AddActiveDataElement(new ActiveDataElement(id) + { + Pick = userInfo + }); + } + string time = GlobalArea.GetCurrentTime(); + ResponseOrBroadcastInstructs.Add(new PickIngElementInstruct() + { + 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/SelectEndElementInstruct.cs b/Instructs/SelectEndElementInstruct.cs new file mode 100644 index 0000000..5e363cb --- /dev/null +++ b/Instructs/SelectEndElementInstruct.cs @@ -0,0 +1,50 @@ + +using System.Text.Json; +using OMS.NET.Common; +using OMS.NET.DbClass; + +namespace OMS.NET.Instructs +{ + public class SelectEndElementInstruct : Instruct + { + public SelectEndElementInstruct() + { + Type = "broadcast"; + Class = "selectEndElement"; + } + + public override Task Handler(string wsid) + { + return Task.Run(() => + { + if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查,不通过则直接退出 + if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查 + try + { + long id = Data.GetInt64(); + string conveyor = GlobalArea.GetLoginEmailByID(wsid); + if (GlobalArea.IsElementExit(id))//已存在activedata项 + { + ActiveDataElement activeDataElement = GlobalArea.GetActiveDataElement(id)!; + if (activeDataElement.Select?.Email == conveyor) + { + activeDataElement.Select = null; + string time = GlobalArea.GetCurrentTime(); + ResponseOrBroadcastInstructs.Add(new SelectEndElementInstruct() + { + IsBroadcast = true, + Conveyor = conveyor, + Time = time, + Data = id + }); + } + } + } + catch (Exception ex) + { + GlobalArea.Log.Warn($"处理{this.Class}广播指令出错:" + ex.Message); + } + }); + } + } +} \ No newline at end of file diff --git a/Instructs/SelectIngElementInstruct.cs b/Instructs/SelectIngElementInstruct.cs index 92eb855..8db3278 100644 --- a/Instructs/SelectIngElementInstruct.cs +++ b/Instructs/SelectIngElementInstruct.cs @@ -38,7 +38,7 @@ namespace OMS.NET.Instructs } else { - throw new Exception($"{id}已被{activeDataElement.Select.Name}选中"); + throw new Exception($"{id}已被{activeDataElement.Select.Name}Select"); } } else