重构中,先删除多余代码和修改部分逻辑
parent
0082549e0b
commit
2a0fa4e7a0
@ -1,63 +0,0 @@
|
|||||||
using System.Text.Json;
|
|
||||||
using System.Text.Json.Nodes;
|
|
||||||
using OMS.NET.DbClass;
|
|
||||||
|
|
||||||
namespace OMS.NET.Common
|
|
||||||
{
|
|
||||||
public class LayerData
|
|
||||||
{
|
|
||||||
public string? TemplateId { get; set; }
|
|
||||||
public long LayerId { get; set; }
|
|
||||||
public bool HasChange { get; set; } = false;
|
|
||||||
public string Type { get; set; }
|
|
||||||
public JsonNode? Members { get; set; }
|
|
||||||
public JsonNode? Structure { get; set; }
|
|
||||||
public int Phase { get; set; } = 1;
|
|
||||||
|
|
||||||
public LayerData(long layerId, string type)
|
|
||||||
{
|
|
||||||
LayerId = layerId;
|
|
||||||
Type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public LayerData(string? templateId, MapLayer layer)
|
|
||||||
{
|
|
||||||
TemplateId = templateId;
|
|
||||||
LayerId = layer.Id;
|
|
||||||
Type = layer.Type;
|
|
||||||
Members = (layer.Type == "order") ? JsonNode.Parse(layer.Members) : JsonNode.Parse(Util.Base64ToJson(layer.Members));
|
|
||||||
Structure = string.IsNullOrEmpty(layer.Structure) ? null : JsonNode.Parse(Util.Base64ToJson(layer.Structure));
|
|
||||||
Phase = layer.Phase;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AppendElement(long itemId, string ItemType)
|
|
||||||
{
|
|
||||||
if (this.Type == "order")
|
|
||||||
{
|
|
||||||
GlobalArea.Log.Error($"order图层不能添加{ItemType}元素");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (this.Members![itemId.ToString()] == null)
|
|
||||||
{
|
|
||||||
this.Members![itemId.ToString()] = ItemType;
|
|
||||||
this.Structure!.AsArray().Add(itemId);
|
|
||||||
this.HasChange = true;
|
|
||||||
//上传图层到数据库
|
|
||||||
MapLayer mapLayer = ConvertToMapLayer();
|
|
||||||
MapLayer.Update(mapLayer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public MapLayer ConvertToMapLayer()
|
|
||||||
{
|
|
||||||
return new MapLayer()
|
|
||||||
{
|
|
||||||
Id = this.LayerId,
|
|
||||||
Type = this.Type,
|
|
||||||
Members = (this.Type == "order") ? this.Members!.ToString() : Util.JsonToBase64(this.Members!.ToString()),
|
|
||||||
Structure = (this.Structure == null) ? "" : Util.JsonToBase64(this.Structure!.ToString()),
|
|
||||||
Phase = this.Phase
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,71 +0,0 @@
|
|||||||
using System.Text;
|
|
||||||
using System.Text.Json;
|
|
||||||
using System.Text.Json.Nodes;
|
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
namespace OMS.NET.Common
|
|
||||||
{
|
|
||||||
public class Point
|
|
||||||
{
|
|
||||||
public double X { get; set; }
|
|
||||||
public double Y { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Util
|
|
||||||
{
|
|
||||||
public static readonly JsonSerializerOptions options = new()
|
|
||||||
{
|
|
||||||
ReadCommentHandling = JsonCommentHandling.Skip, //允许注释
|
|
||||||
AllowTrailingCommas = true,//允许尾随逗号
|
|
||||||
//PropertyNamingPolicy = new InstructNamingPolicy(), // 属性名为定制转换
|
|
||||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, // 忽略 null 值
|
|
||||||
WriteIndented = true, // 美化输出
|
|
||||||
PropertyNameCaseInsensitive = true,//属性名忽略大小写
|
|
||||||
};
|
|
||||||
|
|
||||||
public static string ObjToBase64(object obj)
|
|
||||||
{
|
|
||||||
string utf8 = JsonSerializer.Serialize(obj, options);
|
|
||||||
return Convert.ToBase64String(Encoding.UTF8.GetBytes(utf8));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string JsonToBase64(string json)
|
|
||||||
{
|
|
||||||
return Convert.ToBase64String(Encoding.UTF8.GetBytes(json));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string Base64ToJson(string base64)
|
|
||||||
{
|
|
||||||
return Encoding.UTF8.GetString(Convert.FromBase64String(base64));
|
|
||||||
}
|
|
||||||
|
|
||||||
// public static JsonNode? JsonNodeFromJson(string json)
|
|
||||||
// {
|
|
||||||
// return JsonNode.Parse(json);
|
|
||||||
// }
|
|
||||||
|
|
||||||
//===========================================================
|
|
||||||
|
|
||||||
public static List<Point> PointsFromBase64(string base64)
|
|
||||||
{
|
|
||||||
string utf8 = Base64ToJson(base64);
|
|
||||||
return PointsFromJson(utf8);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<Point> PointsFromJson(string json)
|
|
||||||
{
|
|
||||||
return JsonSerializer.Deserialize<List<Point>>(json, options) ?? new List<Point>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Point PointFromBase64(string base64)
|
|
||||||
{
|
|
||||||
string utf8 = Base64ToJson(base64);
|
|
||||||
return PointFromJson(utf8);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Point PointFromJson(string json)
|
|
||||||
{
|
|
||||||
return JsonSerializer.Deserialize<Point>(json, options) ?? throw new Exception("转化point类型失败:" + json);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,19 +0,0 @@
|
|||||||
using System.Text.RegularExpressions;
|
|
||||||
|
|
||||||
namespace OMS.NET.Common
|
|
||||||
{
|
|
||||||
public static class Validations
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 验证颜色值是否有效
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="color"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static bool IsHexColor(string color)
|
|
||||||
{
|
|
||||||
// Regex for hex color code (with or without #)
|
|
||||||
var hexColorRegex = new Regex(@"^#?[0-9A-Fa-f]{6}$");
|
|
||||||
return hexColorRegex.IsMatch(color);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,31 +0,0 @@
|
|||||||
using System.Text.Json;
|
|
||||||
|
|
||||||
namespace OMS.NET.Instructs
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 广播指令的基类
|
|
||||||
/// </summary>
|
|
||||||
public class BroadcastInstuct : Instruct
|
|
||||||
{
|
|
||||||
public BroadcastInstuct()
|
|
||||||
{
|
|
||||||
this.Type = "broadcast";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class SendCorrectInstuct : Instruct
|
|
||||||
{
|
|
||||||
public SendCorrectInstuct()
|
|
||||||
{
|
|
||||||
this.Type = "send_error";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class SendErrorInstuct : Instruct
|
|
||||||
{
|
|
||||||
public SendErrorInstuct()
|
|
||||||
{
|
|
||||||
this.Type = "send_correct";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,52 +0,0 @@
|
|||||||
using OMS.NET.DbClass;
|
|
||||||
|
|
||||||
namespace OMS.NET.Instructs
|
|
||||||
{
|
|
||||||
public class GetPresenceInstruct : Instruct
|
|
||||||
{
|
|
||||||
public GetPresenceInstruct()
|
|
||||||
{
|
|
||||||
this.Type = "get_presence";
|
|
||||||
}
|
|
||||||
|
|
||||||
public override Task Handler(string wsid)
|
|
||||||
{
|
|
||||||
return Task.Run(() =>
|
|
||||||
{
|
|
||||||
if (GlobalArea.LoginCheckByID(wsid))
|
|
||||||
{
|
|
||||||
//获取所有在线用户,移除重复
|
|
||||||
List<string?> emails = GlobalArea.UserConnects.Where(u => u.UserEmail != null)
|
|
||||||
.Select(u => u.UserEmail).Distinct().ToList();
|
|
||||||
List<dynamic> allLoginUsers = new();
|
|
||||||
emails.ForEach(email =>
|
|
||||||
{
|
|
||||||
AccountData accountData = GlobalArea.GetLoginAccountData(email)!;
|
|
||||||
//由于是已登录用户,默认都能查询到值
|
|
||||||
allLoginUsers.Add(new
|
|
||||||
{
|
|
||||||
userEmail = accountData.UserEmail,
|
|
||||||
userQq = accountData.UserQQ,
|
|
||||||
userName = accountData.UserName,
|
|
||||||
headColor = accountData.HeadColor,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
this.ResponseOrBroadcastInstructs.Add(new SendPresenceInstruct()
|
|
||||||
{
|
|
||||||
IsResponse = true,
|
|
||||||
Data = allLoginUsers,
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class SendPresenceInstruct : Instruct
|
|
||||||
{
|
|
||||||
public SendPresenceInstruct()
|
|
||||||
{
|
|
||||||
this.Type = "send_presence";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,47 +0,0 @@
|
|||||||
namespace OMS.NET.Instructs
|
|
||||||
{
|
|
||||||
public class GetServerConfigInstruct : Instruct
|
|
||||||
{
|
|
||||||
public GetServerConfigInstruct()
|
|
||||||
{
|
|
||||||
this.Type = "get_serverConfig";
|
|
||||||
}
|
|
||||||
|
|
||||||
public override Task Handler(string wsid)
|
|
||||||
{
|
|
||||||
return Task.Run(() =>
|
|
||||||
{
|
|
||||||
this.ResponseOrBroadcastInstructs.Add(new SendServerConfigInstruct
|
|
||||||
{
|
|
||||||
IsResponse = true,
|
|
||||||
Data = new
|
|
||||||
{
|
|
||||||
anonymous_login = bool.TryParse(GlobalArea.ServerConfig.AnonymousLogin, out bool booleanValue) && booleanValue,
|
|
||||||
key = GlobalArea.ServerConfig.Key,
|
|
||||||
url = GlobalArea.ServerConfig.Url,
|
|
||||||
name = GlobalArea.ServerConfig.Name,
|
|
||||||
online_number = GlobalArea.ConnectClientsCount,
|
|
||||||
max_online = GlobalArea.ServerConfig.MaxUser,
|
|
||||||
default_x = GlobalArea.ServerConfig.DefaultX,
|
|
||||||
default_y = GlobalArea.ServerConfig.DefaultY,
|
|
||||||
//以下在0.7会删除
|
|
||||||
enable_base_map = GlobalArea.ServerConfig.EnableBase,
|
|
||||||
//base_map_type= GlobalArea.ServerConfig.ServerConfigEnableBase,
|
|
||||||
max_zoom = GlobalArea.ServerConfig.MaxZoom,
|
|
||||||
min_zoom = GlobalArea.ServerConfig.MinZoom,
|
|
||||||
default_zoom = GlobalArea.ServerConfig.DefaultZoom,
|
|
||||||
base_map_url = GlobalArea.ServerConfig.BaseMapUrl
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class SendServerConfigInstruct : Instruct
|
|
||||||
{
|
|
||||||
public SendServerConfigInstruct()
|
|
||||||
{
|
|
||||||
this.Type = "send_serverConfig";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,84 +0,0 @@
|
|||||||
using System.Globalization;
|
|
||||||
using WebSocketSharp;
|
|
||||||
|
|
||||||
namespace OMS.NET.Instructs
|
|
||||||
{
|
|
||||||
public class GetServerImgInstruct : Instruct
|
|
||||||
{
|
|
||||||
public GetServerImgInstruct()
|
|
||||||
{
|
|
||||||
this.Type = "get_serverImg";
|
|
||||||
}
|
|
||||||
|
|
||||||
public override Task Handler(string wsid)
|
|
||||||
{
|
|
||||||
return Task.Run(() =>
|
|
||||||
{
|
|
||||||
if (this.Time != null)
|
|
||||||
{
|
|
||||||
//时间解析
|
|
||||||
string format = "yyyy-MM-dd HH:mm:ss";
|
|
||||||
DateTime clientDateTime = DateTime.ParseExact(this.Time, format, CultureInfo.InvariantCulture);
|
|
||||||
//Console.WriteLine("Converted DateTime: " + clientDateTime);
|
|
||||||
string serverImgPath = GlobalArea.ServerConfig.Img;
|
|
||||||
if (File.Exists(serverImgPath))
|
|
||||||
{
|
|
||||||
DateTime serverImgLastModified = File.GetLastWriteTime(serverImgPath);
|
|
||||||
Instruct res = new SendServerConfigInstruct
|
|
||||||
{
|
|
||||||
IsResponse = true
|
|
||||||
};
|
|
||||||
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
|
|
||||||
{
|
|
||||||
@string = base64String,
|
|
||||||
time = serverImgLastModified.ToString(format),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
res.Data = new
|
|
||||||
{
|
|
||||||
@string = ""
|
|
||||||
};
|
|
||||||
}
|
|
||||||
this.ResponseOrBroadcastInstructs.Add(res);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
static string GetImageFileType(byte[] imageBytes)
|
|
||||||
{
|
|
||||||
byte[] fileHeader = imageBytes.SubArray(0, 8);
|
|
||||||
|
|
||||||
// PNG 文件的文件头字节为 89 50 4E 47 0D 0A 1A 0A
|
|
||||||
if (fileHeader[0] == 0x89 && fileHeader[1] == 0x50 && fileHeader[2] == 0x4E && fileHeader[3] == 0x47 &&
|
|
||||||
fileHeader[4] == 0x0D && fileHeader[5] == 0x0A && fileHeader[6] == 0x1A && fileHeader[7] == 0x0A)
|
|
||||||
{
|
|
||||||
return "png";
|
|
||||||
}
|
|
||||||
|
|
||||||
// JPEG 文件的文件头字节为 FF D8 FF
|
|
||||||
if (fileHeader[0] == 0xFF && fileHeader[1] == 0xD8 && fileHeader[2] == 0xFF)
|
|
||||||
{
|
|
||||||
return "jpeg";
|
|
||||||
}
|
|
||||||
|
|
||||||
return "unknown";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class SendServerImgInstruct : Instruct
|
|
||||||
{
|
|
||||||
public SendServerImgInstruct()
|
|
||||||
{
|
|
||||||
this.Type = "send_serverImg";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,32 +0,0 @@
|
|||||||
namespace OMS.NET.Instructs
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 用于测试和代码复制
|
|
||||||
/// </summary>
|
|
||||||
public class PingInstuct : Instruct
|
|
||||||
{
|
|
||||||
public PingInstuct()
|
|
||||||
{
|
|
||||||
this.Type = "ping";
|
|
||||||
}
|
|
||||||
|
|
||||||
public override Task Handler(string wsid)
|
|
||||||
{
|
|
||||||
return Task.Run(() =>
|
|
||||||
{
|
|
||||||
this.ResponseOrBroadcastInstructs.Add(new PongInstuct()
|
|
||||||
{
|
|
||||||
IsResponse = true
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class PongInstuct : Instruct
|
|
||||||
{
|
|
||||||
public PongInstuct()
|
|
||||||
{
|
|
||||||
this.Type = "pong";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue