调整Instruct的命名和继承逻辑,减少非服务器接收Instruct的实现,并且减少代码的if嵌套。

dev
nxiaoxiao 1 year ago
parent e2c85517cc
commit 670856fb58

@ -1,134 +0,0 @@
using System.Text;
using System.Text.Json;
using OMS.NET.Common;
using OMS.NET.DbClass;
namespace OMS.NET.Instructs
{
public class AddElementBroadcastInstruct : BroadcastInstuct
{
public AddElementBroadcastInstruct()
{
Type = "broadcast";
}
public override Task Handler(string wsid)
{
return Task.Run(() =>
{
if (GlobalArea.LoginCheckByID(wsid))
{
//从Data中解析数据收到的是json文本类型
if (Data?.GetType() == typeof(JsonElement))
{
try
{
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();
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();
//validations todo
//假设所有数据均合法
MapData mapData = new()
{
Type = type,
Points = Util.JsonToBase64(points),
Point = Util.JsonToBase64(point),
Color = color,
Width = width,
ChildRelations = childRelations,
FatherRelations = fatherRelations,
ChildNodes = childNodes,
FatherNode = fatherNode,
Details = Util.JsonToBase64(details),
Custom = Util.JsonToBase64(custom)
};
//保存点到数据库
MapData.Add(mapData);
//获取图层id
if (Data.GetProperty("custom").TryGetProperty("tmpId", out JsonElement tmpIdNode))
{
string tmpId = tmpIdNode.GetString() ?? throw new Exception("tmpId不能为空");
LayerData? layer = GlobalArea.GetLayerDataByTemplateId(tmpId);
if (layer != null && mapData.Id != -1)
{
layer.AppendElement(mapData.Id, mapData.Type);//包括数据库操作
//准备回复广播
string conveyor = GlobalArea.GetLoginEmailByID(wsid);
string time = GlobalArea.GetCurrentTime();
ResponseOrBroadcastInstructs.Add(new BroadcastInstuct()
{
IsBroadcast = true,
Class = this.Class,
Conveyor = conveyor,
Time = time,
Data = mapData,
});
ResponseOrBroadcastInstructs.Add(new BroadcastInstuct()
{
IsBroadcast = true,
Class = "updateLayerData",
Conveyor = conveyor,
Time = time,
Data = new
{
id = layer.LayerId,
members = layer.Members!.ToString(),
structure = layer.Structure!.ToString(),
}
});
ResponseOrBroadcastInstructs.Add(new SendErrorInstuct()
{
IsResponse = true,
Class = "upload",
Conveyor = conveyor,
Time = time,
Data = new Dictionary<string, object>()
{
{"vid",id},
{"rid",mapData.Id}
},
});
}
}
else
{
throw new Exception("未找到 tmpId");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
ResponseOrBroadcastInstructs.Add(new SendErrorInstuct()
{
IsResponse = true,
Class = "upload",
Conveyor = GlobalArea.GetLoginEmailByID(wsid),
Time = GlobalArea.GetCurrentTime(),
Data = new
{
source = Data,
message = ex.Message,
},
});
}
}
}
});
}
}
}

@ -0,0 +1,132 @@
using System.Text;
using System.Text.Json;
using OMS.NET.Common;
using OMS.NET.DbClass;
namespace OMS.NET.Instructs
{
public class AddElementInstruct : Instruct
{
public AddElementInstruct()
{
Type = "broadcast";
}
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.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();
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();
//validations todo
//假设所有数据均合法
MapData mapData = new()
{
Type = type,
Points = Util.JsonToBase64(points),
Point = Util.JsonToBase64(point),
Color = color,
Width = width,
ChildRelations = childRelations,
FatherRelations = fatherRelations,
ChildNodes = childNodes,
FatherNode = fatherNode,
Details = Util.JsonToBase64(details),
Custom = Util.JsonToBase64(custom)
};
//保存点到数据库
MapData.Add(mapData);
//获取图层id
if (Data.GetProperty("custom").TryGetProperty("tmpId", out JsonElement tmpIdNode))
{
string tmpId = tmpIdNode.GetString() ?? throw new Exception("tmpId不能为空");
LayerData? layer = GlobalArea.GetLayerDataByTemplateId(tmpId);
if (layer != null && mapData.Id != -1)
{
layer.AppendElement(mapData.Id, mapData.Type);//包括数据库操作
//准备回复广播
string conveyor = GlobalArea.GetLoginEmailByID(wsid);
string time = GlobalArea.GetCurrentTime();
ResponseOrBroadcastInstructs.Add(new Instruct()
{
IsBroadcast = true,
Type = "broadcast",
Class = this.Class,
Conveyor = conveyor,
Time = time,
Data = mapData,
});
ResponseOrBroadcastInstructs.Add(new Instruct()
{
IsBroadcast = true,
Type = "broadcast",
Class = "updateLayerData",
Conveyor = conveyor,
Time = time,
Data = new
{
id = layer.LayerId,
members = layer.Members!.ToString(),
structure = layer.Structure!.ToString(),
}
});
ResponseOrBroadcastInstructs.Add(new Instruct()
{
IsResponse = true,
Type = "send_correct",
Class = "upload",
Conveyor = conveyor,
Time = time,
Data = new Dictionary<string, object>()
{
{"vid",id},
{"rid",mapData.Id}
},
});
}
}
else
{
throw new Exception("未找到 tmpId");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
ResponseOrBroadcastInstructs.Add(new Instruct()
{
IsResponse = true,
Type = "send_error",
Class = "upload",
Conveyor = GlobalArea.GetLoginEmailByID(wsid),
Time = GlobalArea.GetCurrentTime(),
Data = new
{
source = Data,
message = ex.Message
}
});
}
});
}
}
}

@ -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";
}
}
}

@ -2,9 +2,9 @@ using System.Text.Json;
namespace OMS.NET.Instructs namespace OMS.NET.Instructs
{ {
public class DeleteElementBroadcastInstruct : BroadcastInstuct public class DeleteElementInstruct : Instruct
{ {
public DeleteElementBroadcastInstruct() public DeleteElementInstruct()
{ {
this.Class = "deleteElement"; this.Class = "deleteElement";
} }
@ -14,7 +14,7 @@ namespace OMS.NET.Instructs
return Task.Run(() => return Task.Run(() =>
{ {
if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查不通过则直接退出 if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查不通过则直接退出
if (Data?.GetType() != typeof(JsonElement)) return; if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查
try try
{ {
string conveyor = GlobalArea.GetLoginEmailByID(wsid); string conveyor = GlobalArea.GetLoginEmailByID(wsid);

@ -13,24 +13,15 @@ namespace OMS.NET.Instructs
{ {
return Task.Run(() => return Task.Run(() =>
{ {
if (GlobalArea.LoginCheckByID(wsid)) if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查,不通过则直接退出
{
List<MapData> mapDatas = MapData.GetMapDataList().Where(m => m.Phase != 2).ToList(); List<MapData> mapDatas = MapData.GetMapDataList().Where(m => m.Phase != 2).ToList();
this.ResponseOrBroadcastInstructs.Add(new SendMapDataInstruct() this.ResponseOrBroadcastInstructs.Add(new Instruct()
{ {
IsResponse = true, IsResponse = true,
Type = "send_mapData",
Data = mapDatas Data = mapDatas
}); });
}
}); });
} }
} }
public class SendMapDataInstruct : Instruct
{
public SendMapDataInstruct()
{
this.Type = "send_mapData";
}
}
} }

@ -13,24 +13,15 @@ namespace OMS.NET.Instructs
{ {
return Task.Run(() => return Task.Run(() =>
{ {
if (GlobalArea.LoginCheckByID(wsid)) if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查,不通过则直接退出
{
List<MapLayer> mapLayers = MapLayer.GetMapLayerList().Where(m => m.Phase != 2).ToList(); List<MapLayer> mapLayers = MapLayer.GetMapLayerList().Where(m => m.Phase != 2).ToList();
this.ResponseOrBroadcastInstructs.Add(new SendMapLayerInstruct() this.ResponseOrBroadcastInstructs.Add(new Instruct()
{ {
IsResponse = true, IsResponse = true,
Type = "send_mapLayer",
Data = mapLayers Data = mapLayers
}); });
}
}); });
} }
} }
public class SendMapLayerInstruct : Instruct
{
public SendMapLayerInstruct()
{
this.Type = "send_mapLayer";
}
}
} }

@ -13,8 +13,7 @@ namespace OMS.NET.Instructs
{ {
return Task.Run(() => return Task.Run(() =>
{ {
if (GlobalArea.LoginCheckByID(wsid)) if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查,不通过则直接退出
{
//获取所有在线用户,移除重复 //获取所有在线用户,移除重复
List<string?> emails = GlobalArea.UserConnects.Where(u => u.UserEmail != null) List<string?> emails = GlobalArea.UserConnects.Where(u => u.UserEmail != null)
.Select(u => u.UserEmail).Distinct().ToList(); .Select(u => u.UserEmail).Distinct().ToList();
@ -31,22 +30,13 @@ namespace OMS.NET.Instructs
headColor = accountData.HeadColor, headColor = accountData.HeadColor,
}); });
}); });
this.ResponseOrBroadcastInstructs.Add(new SendPresenceInstruct() this.ResponseOrBroadcastInstructs.Add(new Instruct()
{ {
IsResponse = true, IsResponse = true,
Type = "send_presence",
Data = allLoginUsers, Data = allLoginUsers,
}); });
}
}); });
} }
} }
public class SendPresenceInstruct : Instruct
{
public SendPresenceInstruct()
{
this.Type = "send_presence";
}
}
} }

@ -11,22 +11,14 @@ namespace OMS.NET.Instructs
{ {
return Task.Run(() => return Task.Run(() =>
{ {
this.ResponseOrBroadcastInstructs.Add(new PublickeyInstruct() this.ResponseOrBroadcastInstructs.Add(new Instruct()
{ {
Data = GlobalArea.GetRsaPublickKey(), IsResponse = true,
IsResponse = true Type = "publickey",
Data = GlobalArea.GetRsaPublickKey()
}); });
//Thread.Sleep(9000);//模拟耗时操作 //Thread.Sleep(9000);//模拟耗时操作
}); });
} }
} }
public class PublickeyInstruct : Instruct
{
public PublickeyInstruct()
{
this.Type = "publickey";
}
}
} }

@ -11,9 +11,10 @@ namespace OMS.NET.Instructs
{ {
return Task.Run(() => return Task.Run(() =>
{ {
this.ResponseOrBroadcastInstructs.Add(new SendServerConfigInstruct this.ResponseOrBroadcastInstructs.Add(new Instruct()
{ {
IsResponse = true, IsResponse = true,
Type = "send_serverConfig",
Data = new Data = new
{ {
anonymous_login = bool.TryParse(GlobalArea.ServerConfig.AnonymousLogin, out bool booleanValue) && booleanValue, anonymous_login = bool.TryParse(GlobalArea.ServerConfig.AnonymousLogin, out bool booleanValue) && booleanValue,
@ -36,12 +37,4 @@ namespace OMS.NET.Instructs
}); });
} }
} }
public class SendServerConfigInstruct : Instruct
{
public SendServerConfigInstruct()
{
this.Type = "send_serverConfig";
}
}
} }

@ -24,9 +24,10 @@ namespace OMS.NET.Instructs
if (File.Exists(serverImgPath)) if (File.Exists(serverImgPath))
{ {
DateTime serverImgLastModified = File.GetLastWriteTime(serverImgPath); DateTime serverImgLastModified = File.GetLastWriteTime(serverImgPath);
Instruct res = new SendServerConfigInstruct Instruct res = new()
{ {
IsResponse = true IsResponse = true,
Type = "send_serverImg"
}; };
if (serverImgLastModified > clientDateTime) if (serverImgLastModified > clientDateTime)
{ {
@ -73,12 +74,4 @@ namespace OMS.NET.Instructs
return "unknown"; return "unknown";
} }
} }
public class SendServerImgInstruct : Instruct
{
public SendServerImgInstruct()
{
this.Type = "send_serverImg";
}
}
} }

@ -13,13 +13,13 @@ namespace OMS.NET.Instructs
{ {
return Task.Run(() => return Task.Run(() =>
{ {
if (GlobalArea.LoginCheckByID(wsid)) if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查,不通过则直接退出
{
string userEmail = GlobalArea.UserConnects.First(u => u.ID == wsid).UserEmail!; string userEmail = GlobalArea.UserConnects.First(u => u.ID == wsid).UserEmail!;
AccountData accountData = GlobalArea.GetLoginAccountData(userEmail)!; AccountData accountData = GlobalArea.GetLoginAccountData(userEmail)!;
this.ResponseOrBroadcastInstructs.Add(new SendUserDataInstruct() this.ResponseOrBroadcastInstructs.Add(new Instruct()
{ {
IsResponse = true, IsResponse = true,
Type = "send_userData",
Data = new Data = new
{ {
user_email = accountData.UserEmail, user_email = accountData.UserEmail,
@ -33,16 +33,7 @@ namespace OMS.NET.Instructs
custom = accountData.Custom, custom = accountData.Custom,
} }
}); });
}
}); });
} }
} }
public class SendUserDataInstruct : Instruct
{
public SendUserDataInstruct()
{
this.Type = "send_userData";
}
}
} }

@ -58,12 +58,12 @@ namespace OMS.NET.Instructs
"broadcast" => classValue switch "broadcast" => classValue switch
{ {
//广播指令继承结构 //广播指令继承结构
"point" => JsonSerializer.Deserialize<AddElementBroadcastInstruct>(root.GetRawText(), options), "point" => JsonSerializer.Deserialize<AddElementInstruct>(root.GetRawText(), options),
"line" => JsonSerializer.Deserialize<AddElementBroadcastInstruct>(root.GetRawText(), options), "line" => JsonSerializer.Deserialize<AddElementInstruct>(root.GetRawText(), options),
"area" => JsonSerializer.Deserialize<AddElementBroadcastInstruct>(root.GetRawText(), options), "area" => JsonSerializer.Deserialize<AddElementInstruct>(root.GetRawText(), options),
"curve" => JsonSerializer.Deserialize<AddElementBroadcastInstruct>(root.GetRawText(), options), "curve" => JsonSerializer.Deserialize<AddElementInstruct>(root.GetRawText(), options),
"deleteElement" => JsonSerializer.Deserialize<DeleteElementBroadcastInstruct>(root.GetRawText(), options), "deleteElement" => JsonSerializer.Deserialize<DeleteElementInstruct>(root.GetRawText(), options),
"textMessage" => JsonSerializer.Deserialize<TextMessageBroadcastInstruct>(root.GetRawText(), options), "textMessage" => JsonSerializer.Deserialize<TextMessageInstruct>(root.GetRawText(), options),
_ => JsonSerializer.Deserialize<Instruct>(root.GetRawText(), options) _ => JsonSerializer.Deserialize<Instruct>(root.GetRawText(), options)
}, },
_ => null _ => null

@ -11,27 +11,27 @@ namespace OMS.NET.Instructs
{ {
public LoginInstruct() public LoginInstruct()
{ {
this.Type = "login"; Type = "login";
} }
public override Task Handler(string wsid) public override Task Handler(string wsid)
{ {
return Task.Run(() => return Task.Run(() =>
{ {
if (this.Data?.GetType() == typeof(JsonElement)) if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查
{ string email = Data.GetProperty("email").GetString();
string email = this.Data.GetProperty("email").GetString(); string password = Data.GetProperty("password").GetString();
string password = this.Data.GetProperty("password").GetString();
if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
{ {
GlobalArea.Log.Warn("登录信息不能为空!"); GlobalArea.Log.Warn("登录信息不能为空!");
return; return;
} }
//能够获取到则说明客户端有发送数据过来 //能够获取到则说明客户端有发送数据过来
Instruct res1 = new LoginStatusInstuct Instruct res1 = new()
{ {
Data = false, IsResponse = true,
IsResponse = true Type = "loginStatus",
Data = false
};//默认为false };//默认为false
//Console.WriteLine($"已获取到{email}:{password},解密测试{GlobalArea.DecryptFromBase64String(password)}"); //Console.WriteLine($"已获取到{email}:{password},解密测试{GlobalArea.DecryptFromBase64String(password)}");
AccountData? accountData = AccountData.Get(email); AccountData? accountData = AccountData.Get(email);
@ -42,11 +42,11 @@ namespace OMS.NET.Instructs
if (accountData.Password == GlobalArea.DecryptFromBase64String(password)) if (accountData.Password == GlobalArea.DecryptFromBase64String(password))
{ {
res1.Data = true;//登录成功则修改为true res1.Data = true;//登录成功则修改为true
this.ResponseOrBroadcastInstructs.Add(res1); ResponseOrBroadcastInstructs.Add(res1);
GlobalArea.Log.Info($"{accountData.UserEmail}:登录成功"); GlobalArea.Log.Info($"{accountData.UserEmail}:登录成功");
GlobalArea.Login(wsid, accountData.UserEmail); GlobalArea.Login(wsid, accountData.UserEmail);
GlobalArea.Log.Info($"当前登录用户数量: {GlobalArea.LoginUserCount}"); GlobalArea.Log.Info($"当前登录用户数量: {GlobalArea.LoginUserCount}");
this.ResponseOrBroadcastInstructs.Add(new Instruct ResponseOrBroadcastInstructs.Add(new Instruct
{ {
Type = "broadcast", Type = "broadcast",
Class = "logIn", Class = "logIn",
@ -62,19 +62,10 @@ namespace OMS.NET.Instructs
} }
else else
{ {
this.ResponseOrBroadcastInstructs.Add(res1); ResponseOrBroadcastInstructs.Add(res1);
}
} }
} }
}); });
} }
} }
public class LoginStatusInstuct : Instruct
{
public LoginStatusInstuct()
{
this.Type = "loginStatus";
}
}
} }

@ -1,8 +1,5 @@
namespace OMS.NET.Instructs namespace OMS.NET.Instructs
{ {
/// <summary>
/// 用于测试和代码复制
/// </summary>
public class PingInstuct : Instruct public class PingInstuct : Instruct
{ {
public PingInstuct() public PingInstuct()
@ -14,19 +11,12 @@ namespace OMS.NET.Instructs
{ {
return Task.Run(() => return Task.Run(() =>
{ {
this.ResponseOrBroadcastInstructs.Add(new PongInstuct() this.ResponseOrBroadcastInstructs.Add(new Instruct()
{ {
IsResponse = true IsResponse = true,
Type = "pong"
}); });
}); });
} }
} }
public class PongInstuct : Instruct
{
public PongInstuct()
{
this.Type = "pong";
}
}
} }

@ -4,10 +4,11 @@ using OMS.NET.DbClass;
namespace OMS.NET.Instructs namespace OMS.NET.Instructs
{ {
public class SelectIngElementBroadcastInstruct : BroadcastInstuct public class SelectIngElementInstruct : Instruct
{ {
public SelectIngElementBroadcastInstruct() public SelectIngElementInstruct()
{ {
this.Type = "broadcast";
this.Class = "selectIngElement"; this.Class = "selectIngElement";
} }
@ -15,8 +16,8 @@ namespace OMS.NET.Instructs
{ {
return Task.Run(() => return Task.Run(() =>
{ {
if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查不通过则直接退出 if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查,不通过则直接退出
if (Data?.GetType() != typeof(JsonElement)) return; if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查
try try
{ {
long id = Data.GetInt64(); long id = Data.GetInt64();
@ -48,7 +49,7 @@ namespace OMS.NET.Instructs
}); });
} }
string time = GlobalArea.GetCurrentTime(); string time = GlobalArea.GetCurrentTime();
ResponseOrBroadcastInstructs.Add(new SelectIngElementBroadcastInstruct() ResponseOrBroadcastInstructs.Add(new SelectIngElementInstruct()
{ {
IsBroadcast = true, IsBroadcast = true,
Conveyor = conveyor, Conveyor = conveyor,

@ -2,10 +2,11 @@ using System.Text.Json;
namespace OMS.NET.Instructs namespace OMS.NET.Instructs
{ {
public class TextMessageBroadcastInstruct : BroadcastInstuct public class TextMessageInstruct : Instruct
{ {
public TextMessageBroadcastInstruct() public TextMessageInstruct()
{ {
this.Type = "broadcast";
this.Class = "textMessage"; this.Class = "textMessage";
} }
@ -14,14 +15,14 @@ namespace OMS.NET.Instructs
return Task.Run(() => return Task.Run(() =>
{ {
if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查不通过则直接退出 if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查不通过则直接退出
if (Data?.GetType() != typeof(JsonElement)) return; if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查
try try
{ {
string message = Data.GetProperty("message").GetString(); string message = Data.GetProperty("message").GetString();
string conveyor = GlobalArea.GetLoginEmailByID(wsid); string conveyor = GlobalArea.GetLoginEmailByID(wsid);
string time = GlobalArea.GetCurrentTime(); string time = GlobalArea.GetCurrentTime();
GlobalArea.Log.Info($"[{time}] {conveyor}:{message}"); GlobalArea.Log.Info($"[{time}] {conveyor}:{message}");
ResponseOrBroadcastInstructs.Add(new TextMessageBroadcastInstruct() ResponseOrBroadcastInstructs.Add(new TextMessageInstruct()
{ {
IsBroadcast = true, IsBroadcast = true,
Conveyor = conveyor, Conveyor = conveyor,
Loading…
Cancel
Save