From 81f06d24e663bea383311b2178e732d5680cea4b Mon Sep 17 00:00:00 2001 From: nxiaoxiao <3247747442@qq.com> Date: Sun, 1 Sep 2024 21:26:24 +0800 Subject: [PATCH] =?UTF-8?q?20240901-=E8=B0=83=E8=AF=95=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/LayerData.cs | 8 +++- Common/ServerConfig.cs | 8 ++++ DbClass/MapData.cs | 18 ++++---- Instructs/AddElementInstruct.cs | 29 ++++++------ Instructs/GetServerConfigInstruct.cs | 3 +- Instructs/GetServerImgInstruct.cs | 66 +++++++++++++++------------ Instructs/UpdateElementNodeInstuct.cs | 10 ++-- init.sql | 2 +- 8 files changed, 87 insertions(+), 57 deletions(-) diff --git a/Common/LayerData.cs b/Common/LayerData.cs index 240e163..48291d0 100644 --- a/Common/LayerData.cs +++ b/Common/LayerData.cs @@ -46,7 +46,13 @@ namespace OMS.NET.Common } if (Members![elementId.ToString()] == null) { - Members![elementId.ToString()] = elementType; + Members![elementId.ToString()] = elementType switch { + "point" => 1, + "line" => 2, + "area" => 3, + "curve" => 4, + _ => throw new Exception("图层添加:不支持的元素类型"), + }; Structure!.AsArray().Add(elementId); HasChange = true; UpdateToDb(); diff --git a/Common/ServerConfig.cs b/Common/ServerConfig.cs index 225f259..c0e6434 100644 --- a/Common/ServerConfig.cs +++ b/Common/ServerConfig.cs @@ -242,6 +242,14 @@ namespace OMS.NET.Common ? value : ""; + /// + /// 底图的默认类型 + /// default empty + /// + public string ServerConfigBaseMapType => _config.TryGetValue("ServerConfigBaseMapType", out string? value) + ? value + : "realistic"; + public ServerConfig() { //do nothing,everything default diff --git a/DbClass/MapData.cs b/DbClass/MapData.cs index eccefaa..9651aa3 100644 --- a/DbClass/MapData.cs +++ b/DbClass/MapData.cs @@ -13,7 +13,7 @@ namespace OMS.NET.DbClass public int Phase { get; set; } public int? Width { get; set; } public string? ChildRelations { get; set; } - public string? FatherRelations { get; set; } + public string? FatherRelation { get; set; } public string? ChildNodes { get; set; } public string? FatherNode { get; set; } public string? Details { get; set; } @@ -33,8 +33,8 @@ namespace OMS.NET.DbClass { using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName); connection.Open(); - var query = @"INSERT INTO map_0_data (type, points, point, color, phase, width, child_relations, father_relations, child_nodes, father_node, details, custom) - VALUES (@Type, @Points, @Point, @Color, @Phase, @Width, @ChildRelations, @FatherRelations, @ChildNodes, @FatherNode, @Details, @Custom)"; + var query = @"INSERT INTO map_0_data (type, points, point, color, phase, width, child_relations, father_relation, child_nodes, father_node, details, custom) + VALUES (@Type, @Points, @Point, @Color, @Phase, @Width, @ChildRelations, @FatherRelation, @ChildNodes, @FatherNode, @Details, @Custom)"; using MySqlCommand command = new(query, connection); command.Parameters.AddWithValue("@Type", mapData.Type); command.Parameters.AddWithValue("@Points", mapData.Points); @@ -43,7 +43,7 @@ namespace OMS.NET.DbClass command.Parameters.AddWithValue("@Phase", mapData.Phase); command.Parameters.AddWithValue("@Width", mapData.Width ?? (object)DBNull.Value); command.Parameters.AddWithValue("@ChildRelations", mapData.ChildRelations ?? (object)DBNull.Value); - command.Parameters.AddWithValue("@FatherRelations", mapData.FatherRelations ?? (object)DBNull.Value); + command.Parameters.AddWithValue("@FatherRelation", mapData.FatherRelation ?? (object)DBNull.Value); command.Parameters.AddWithValue("@ChildNodes", mapData.ChildNodes ?? (object)DBNull.Value); command.Parameters.AddWithValue("@FatherNode", mapData.FatherNode ?? (object)DBNull.Value); command.Parameters.AddWithValue("@Details", mapData.Details ?? (object)DBNull.Value); @@ -57,7 +57,7 @@ namespace OMS.NET.DbClass using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName); connection.Open(); var query = @"UPDATE map_0_data SET type = @Type, points = @Points, point = @Point, color = @Color, phase = @Phase, width = @Width, - child_relations = @ChildRelations, father_relations = @FatherRelations, child_nodes = @ChildNodes, father_node = @FatherNode, + child_relations = @ChildRelations, father_relation = @FatherRelation, child_nodes = @ChildNodes, father_node = @FatherNode, details = @Details, custom = @Custom WHERE id = @Id"; using MySqlCommand command = new(query, connection); command.Parameters.AddWithValue("@Id", mapData.Id); @@ -68,7 +68,7 @@ namespace OMS.NET.DbClass command.Parameters.AddWithValue("@Phase", mapData.Phase); command.Parameters.AddWithValue("@Width", mapData.Width ?? (object)DBNull.Value); command.Parameters.AddWithValue("@ChildRelations", mapData.ChildRelations ?? (object)DBNull.Value); - command.Parameters.AddWithValue("@FatherRelations", mapData.FatherRelations ?? (object)DBNull.Value); + command.Parameters.AddWithValue("@FatherRelation", mapData.FatherRelation ?? (object)DBNull.Value); command.Parameters.AddWithValue("@ChildNodes", mapData.ChildNodes ?? (object)DBNull.Value); command.Parameters.AddWithValue("@FatherNode", mapData.FatherNode ?? (object)DBNull.Value); command.Parameters.AddWithValue("@Details", mapData.Details ?? (object)DBNull.Value); @@ -90,7 +90,7 @@ namespace OMS.NET.DbClass { using MySqlConnection connection = new(GlobalArea.ConnectionStringWithDbName); connection.Open(); - var query = @"SELECT id, type, points, point, color, phase, width, child_relations, father_relations, child_nodes, father_node, details, custom + var query = @"SELECT id, type, points, point, color, phase, width, child_relations, father_relation, child_nodes, father_node, details, custom FROM map_0_data WHERE id = @Id"; using MySqlCommand command = new(query, connection); command.Parameters.AddWithValue("@Id", id); @@ -107,7 +107,7 @@ namespace OMS.NET.DbClass Phase = reader.GetInt32("phase"), Width = reader["width"] as int?, ChildRelations = reader["child_relations"] as string, - FatherRelations = reader["father_relations"] as string, + FatherRelation = reader["father_relation"] as string, ChildNodes = reader["child_nodes"] as string, FatherNode = reader["father_node"] as string, Details = reader["details"] as string, @@ -139,7 +139,7 @@ namespace OMS.NET.DbClass Phase = reader.GetInt32("phase"), Width = reader.IsDBNull(reader.GetOrdinal("width")) ? (int?)null : reader.GetInt32("width"), ChildRelations = reader.IsDBNull(reader.GetOrdinal("child_relations")) ? null : reader.GetString("child_relations"), - FatherRelations = reader.IsDBNull(reader.GetOrdinal("father_relations")) ? null : reader.GetString("father_relations"), + FatherRelation = reader.IsDBNull(reader.GetOrdinal("father_relation")) ? null : reader.GetString("father_relation"), ChildNodes = reader.IsDBNull(reader.GetOrdinal("child_nodes")) ? null : reader.GetString("child_nodes"), FatherNode = reader.IsDBNull(reader.GetOrdinal("father_node")) ? null : reader.GetString("father_node"), Details = reader.IsDBNull(reader.GetOrdinal("details")) ? null : reader.GetString("details"), diff --git a/Instructs/AddElementInstruct.cs b/Instructs/AddElementInstruct.cs index 6ecf988..bcba6be 100644 --- a/Instructs/AddElementInstruct.cs +++ b/Instructs/AddElementInstruct.cs @@ -21,18 +21,19 @@ namespace OMS.NET.Instructs if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查 try { + char[] charsToTrim = { '"' }; long id = Data.GetProperty("id").GetInt64(); - string type = Data.GetProperty("type").GetString(); - string points = Data.GetProperty("points").GetString(); - string point = Data.GetProperty("point").GetString(); - string? color = Data.GetProperty("color").GetString(); + string type = Data.GetProperty("type").GetRawText().Trim(charsToTrim); + string points = Data.GetProperty("points").GetRawText(); + string point = Data.GetProperty("point").GetRawText().Trim(charsToTrim); + string? color = Data.GetProperty("color").GetRawText().Trim(charsToTrim); int? width = Data.GetProperty("width").GetInt32(); - string? childRelations = Data.GetProperty("childRelations").GetString(); - string? fatherRelations = Data.GetProperty("fatherRelations").GetString(); - string? childNodes = Data.GetProperty("childNodes").GetString(); - string? fatherNode = Data.GetProperty("fatherNode").GetString(); - string? details = Data.GetProperty("details").GetString(); - string? custom = Data.GetProperty("custom").GetString(); + // string? childRelations = Data.GetProperty("childRelations").GetString(); + // string? fatherRelation = Data.GetProperty("fatherRelation").GetString(); + // string? childNodes = Data.GetProperty("childNodes").GetString(); + // string? fatherNode = Data.GetProperty("fatherNode").GetString(); + string? details = Data.GetProperty("details").GetRawText().Trim(charsToTrim); + string? custom = Data.GetProperty("custom").GetRawText().Trim(charsToTrim); //validations todo //假设所有数据均合法 MapData mapData = new() @@ -42,10 +43,10 @@ namespace OMS.NET.Instructs Point = Util.JsonToBase64(point), Color = color, Width = width, - ChildRelations = childRelations, - FatherRelations = fatherRelations, - ChildNodes = childNodes, - FatherNode = fatherNode, + // ChildRelations = childRelations, + // FatherRelation = fatherRelation, + // ChildNodes = childNodes, + // FatherNode = fatherNode, Details = Util.JsonToBase64(details), Custom = Util.JsonToBase64(custom) }; diff --git a/Instructs/GetServerConfigInstruct.cs b/Instructs/GetServerConfigInstruct.cs index b314998..6b8a71c 100644 --- a/Instructs/GetServerConfigInstruct.cs +++ b/Instructs/GetServerConfigInstruct.cs @@ -31,7 +31,8 @@ namespace OMS.NET.Instructs max_zoom = GlobalArea.ServerConfig.MaxZoom, min_zoom = GlobalArea.ServerConfig.MinZoom, default_zoom = GlobalArea.ServerConfig.DefaultZoom, - base_map_url = GlobalArea.ServerConfig.BaseMapUrl + base_map_url = GlobalArea.ServerConfig.BaseMapUrl, + base_map_type = GlobalArea.ServerConfig.ServerConfigBaseMapType } }); }); diff --git a/Instructs/GetServerImgInstruct.cs b/Instructs/GetServerImgInstruct.cs index 3ff0566..ddbd5cf 100644 --- a/Instructs/GetServerImgInstruct.cs +++ b/Instructs/GetServerImgInstruct.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Text.Json; using WebSocketSharp; namespace OMS.NET.Instructs @@ -14,43 +15,52 @@ namespace OMS.NET.Instructs { return Task.Run(() => { - if (Time != null) + if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查 + try { - //时间解析 - string format = "yyyy-MM-dd HH:mm:ss"; - DateTime clientDateTime = DateTime.ParseExact(Time, format, CultureInfo.InvariantCulture); - //Console.WriteLine("Converted DateTime: " + clientDateTime); - string serverImgPath = GlobalArea.ServerConfig.Img; - if (File.Exists(serverImgPath)) + string time = Data.GetProperty("time").GetString(); + if (time != null) { - DateTime serverImgLastModified = File.GetLastWriteTime(serverImgPath); - Instruct res = new() + //时间解析 + string format = "yyyy-MM-dd HH:mm:ss"; + DateTime clientDateTime = DateTime.ParseExact(time, format, CultureInfo.InvariantCulture); + //Console.WriteLine("Converted DateTime: " + clientDateTime); + string serverImgPath = GlobalArea.ServerConfig.Img; + if (File.Exists(serverImgPath)) { - IsResponse = true, - Type = "send_serverImg" - }; - if (serverImgLastModified > clientDateTime) - { - byte[] imageBytes = File.ReadAllBytes(serverImgPath); - string imageFileType = GetImageFileType(imageBytes); - string base64String = $"data:image/{imageFileType};base64," + Convert.ToBase64String(imageBytes); - //Console.WriteLine("Base64 Encoded Image: " + base64String); - res.Data = new + DateTime serverImgLastModified = File.GetLastWriteTime(serverImgPath); + Instruct res = new() { - @string = base64String, - time = serverImgLastModified.ToString(format), + IsResponse = true, + Type = "send_serverImg" }; - } - else - { - res.Data = new + if (serverImgLastModified > clientDateTime) { - @string = "" - }; + byte[] imageBytes = File.ReadAllBytes(serverImgPath); + string imageFileType = GetImageFileType(imageBytes); + string base64String = $"data:image/{imageFileType};base64," + Convert.ToBase64String(imageBytes); + res.Data = new + { + @string = base64String, + time = serverImgLastModified.ToString(format), + }; + } + else + { + res.Data = new + { + @string = "" + }; + } + ResponseOrBroadcastInstructs.Add(res); } - ResponseOrBroadcastInstructs.Add(res); } } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + }); } diff --git a/Instructs/UpdateElementNodeInstuct.cs b/Instructs/UpdateElementNodeInstuct.cs index a020837..170e5da 100644 --- a/Instructs/UpdateElementNodeInstuct.cs +++ b/Instructs/UpdateElementNodeInstuct.cs @@ -23,11 +23,15 @@ namespace OMS.NET.Instructs { long id = Data.GetProperty("id").GetInt64(); string updateId = Data.GetProperty("updateId").GetString(); - string points = Data.GetProperty("points").GetString()!; - string point = Data.GetProperty("point").GetString()!; + string points = Data.GetProperty("points").GetRawText();//todo MapData mapData = MapData.Get(id) ?? throw new Exception($"数据库中未能找到id为{id}的元素"); mapData.Points = Util.JsonToBase64(points); - mapData.Point = Util.JsonToBase64(point); + + if (Data.TryGetProperty("point", out JsonElement pointNode)) + { + mapData.Point = Util.JsonToBase64(pointNode.GetRawText()); + } + if (MapData.Update(mapData) == -1) throw new Exception("数据库修改失败"); ResponseOrBroadcastInstructs.Add(new Instruct() diff --git a/init.sql b/init.sql index 06ec3fc..a88abc7 100644 --- a/init.sql +++ b/init.sql @@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS map_0_data ( phase INT(1) NOT NULL, width INT(11), child_relations MEDIUMTEXT, - father_relations VARCHAR(255), + father_relation VARCHAR(255), child_nodes MEDIUMTEXT, father_node VARCHAR(255), details MEDIUMTEXT,