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

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

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

@ -13,40 +13,30 @@ namespace OMS.NET.Instructs
{
return Task.Run(() =>
{
if (GlobalArea.LoginCheckByID(wsid))
if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查,不通过则直接退出
//获取所有在线用户,移除重复
List<string?> emails = GlobalArea.UserConnects.Where(u => u.UserEmail != null)
.Select(u => u.UserEmail).Distinct().ToList();
List<dynamic> allLoginUsers = new();
emails.ForEach(email =>
{
//获取所有在线用户,移除重复
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
{
AccountData accountData = GlobalArea.GetLoginAccountData(email)!;
//由于是已登录用户,默认都能查询到值
allLoginUsers.Add(new
{
userEmail = accountData.UserEmail,
userQq = accountData.UserQQ,
userName = accountData.UserName,
headColor = accountData.HeadColor,
});
userEmail = accountData.UserEmail,
userQq = accountData.UserQQ,
userName = accountData.UserName,
headColor = accountData.HeadColor,
});
this.ResponseOrBroadcastInstructs.Add(new SendPresenceInstruct()
{
IsResponse = true,
Data = allLoginUsers,
});
}
});
this.ResponseOrBroadcastInstructs.Add(new Instruct()
{
IsResponse = true,
Type = "send_presence",
Data = allLoginUsers,
});
});
}
}
public class SendPresenceInstruct : Instruct
{
public SendPresenceInstruct()
{
this.Type = "send_presence";
}
}
}

@ -11,22 +11,14 @@ namespace OMS.NET.Instructs
{
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);//模拟耗时操作
});
}
}
public class PublickeyInstruct : Instruct
{
public PublickeyInstruct()
{
this.Type = "publickey";
}
}
}

@ -11,9 +11,10 @@ namespace OMS.NET.Instructs
{
return Task.Run(() =>
{
this.ResponseOrBroadcastInstructs.Add(new SendServerConfigInstruct
this.ResponseOrBroadcastInstructs.Add(new Instruct()
{
IsResponse = true,
Type = "send_serverConfig",
Data = new
{
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))
{
DateTime serverImgLastModified = File.GetLastWriteTime(serverImgPath);
Instruct res = new SendServerConfigInstruct
Instruct res = new()
{
IsResponse = true
IsResponse = true,
Type = "send_serverImg"
};
if (serverImgLastModified > clientDateTime)
{
@ -73,12 +74,4 @@ namespace OMS.NET.Instructs
return "unknown";
}
}
public class SendServerImgInstruct : Instruct
{
public SendServerImgInstruct()
{
this.Type = "send_serverImg";
}
}
}

@ -13,36 +13,27 @@ namespace OMS.NET.Instructs
{
return Task.Run(() =>
{
if (GlobalArea.LoginCheckByID(wsid))
if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查,不通过则直接退出
string userEmail = GlobalArea.UserConnects.First(u => u.ID == wsid).UserEmail!;
AccountData accountData = GlobalArea.GetLoginAccountData(userEmail)!;
this.ResponseOrBroadcastInstructs.Add(new Instruct()
{
string userEmail = GlobalArea.UserConnects.First(u => u.ID == wsid).UserEmail!;
AccountData accountData = GlobalArea.GetLoginAccountData(userEmail)!;
this.ResponseOrBroadcastInstructs.Add(new SendUserDataInstruct()
IsResponse = true,
Type = "send_userData",
Data = new
{
IsResponse = true,
Data = new
{
user_email = accountData.UserEmail,
user_name = accountData.UserName,
map_layer = accountData.MapLayer,
default_a1 = accountData.DefaultA1,
save_point = accountData.SavePoint,
head_color = accountData.HeadColor,
mode = accountData.Mode,
phase = accountData.Phase,
custom = accountData.Custom,
}
});
}
user_email = accountData.UserEmail,
user_name = accountData.UserName,
map_layer = accountData.MapLayer,
default_a1 = accountData.DefaultA1,
save_point = accountData.SavePoint,
head_color = accountData.HeadColor,
mode = accountData.Mode,
phase = accountData.Phase,
custom = accountData.Custom,
}
});
});
}
}
public class SendUserDataInstruct : Instruct
{
public SendUserDataInstruct()
{
this.Type = "send_userData";
}
}
}

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

@ -11,70 +11,61 @@ namespace OMS.NET.Instructs
{
public LoginInstruct()
{
this.Type = "login";
Type = "login";
}
public override Task Handler(string wsid)
{
return Task.Run(() =>
{
if (this.Data?.GetType() == typeof(JsonElement))
if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查
string email = Data.GetProperty("email").GetString();
string password = Data.GetProperty("password").GetString();
if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
{
string email = this.Data.GetProperty("email").GetString();
string password = this.Data.GetProperty("password").GetString();
if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
{
GlobalArea.Log.Warn("登录信息不能为空!");
return;
}
//能够获取到则说明客户端有发送数据过来
Instruct res1 = new LoginStatusInstuct
{
Data = false,
IsResponse = true
};//默认为false
//Console.WriteLine($"已获取到{email}:{password},解密测试{GlobalArea.DecryptFromBase64String(password)}");
AccountData? accountData = AccountData.Get(email);
GlobalArea.AddLoginAccountData(accountData);
if (accountData != null)
GlobalArea.Log.Warn("登录信息不能为空!");
return;
}
//能够获取到则说明客户端有发送数据过来
Instruct res1 = new()
{
IsResponse = true,
Type = "loginStatus",
Data = false
};//默认为false
//Console.WriteLine($"已获取到{email}:{password},解密测试{GlobalArea.DecryptFromBase64String(password)}");
AccountData? accountData = AccountData.Get(email);
GlobalArea.AddLoginAccountData(accountData);
if (accountData != null)
{
//只能原文比较,密文每次都不一样,涉及随机性填充
if (accountData.Password == GlobalArea.DecryptFromBase64String(password))
{
//只能原文比较,密文每次都不一样,涉及随机性填充
if (accountData.Password == GlobalArea.DecryptFromBase64String(password))
res1.Data = true;//登录成功则修改为true
ResponseOrBroadcastInstructs.Add(res1);
GlobalArea.Log.Info($"{accountData.UserEmail}:登录成功");
GlobalArea.Login(wsid, accountData.UserEmail);
GlobalArea.Log.Info($"当前登录用户数量: {GlobalArea.LoginUserCount}");
ResponseOrBroadcastInstructs.Add(new Instruct
{
res1.Data = true;//登录成功则修改为true
this.ResponseOrBroadcastInstructs.Add(res1);
GlobalArea.Log.Info($"{accountData.UserEmail}:登录成功");
GlobalArea.Login(wsid, accountData.UserEmail);
GlobalArea.Log.Info($"当前登录用户数量: {GlobalArea.LoginUserCount}");
this.ResponseOrBroadcastInstructs.Add(new Instruct
Type = "broadcast",
Class = "logIn",
Data = new
{
Type = "broadcast",
Class = "logIn",
Data = new
{
userEmail = accountData.UserEmail,
userName = accountData.UserName,
headColor = accountData.HeadColor,
userQq = accountData.UserQQ,
},
IsBroadcast = true
});
}
else
{
this.ResponseOrBroadcastInstructs.Add(res1);
}
userEmail = accountData.UserEmail,
userName = accountData.UserName,
headColor = accountData.HeadColor,
userQq = accountData.UserQQ,
},
IsBroadcast = true
});
}
else
{
ResponseOrBroadcastInstructs.Add(res1);
}
}
});
}
}
public class LoginStatusInstuct : Instruct
{
public LoginStatusInstuct()
{
this.Type = "loginStatus";
}
}
}

@ -1,8 +1,5 @@
namespace OMS.NET.Instructs
{
/// <summary>
/// 用于测试和代码复制
/// </summary>
public class PingInstuct : Instruct
{
public PingInstuct()
@ -14,19 +11,12 @@ namespace OMS.NET.Instructs
{
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
{
public class SelectIngElementBroadcastInstruct : BroadcastInstuct
public class SelectIngElementInstruct : Instruct
{
public SelectIngElementBroadcastInstruct()
public SelectIngElementInstruct()
{
this.Type = "broadcast";
this.Class = "selectIngElement";
}
@ -15,8 +16,8 @@ namespace OMS.NET.Instructs
{
return Task.Run(() =>
{
if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查不通过则直接退出
if (Data?.GetType() != typeof(JsonElement)) return;
if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查,不通过则直接退出
if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查
try
{
long id = Data.GetInt64();
@ -48,7 +49,7 @@ namespace OMS.NET.Instructs
});
}
string time = GlobalArea.GetCurrentTime();
ResponseOrBroadcastInstructs.Add(new SelectIngElementBroadcastInstruct()
ResponseOrBroadcastInstructs.Add(new SelectIngElementInstruct()
{
IsBroadcast = true,
Conveyor = conveyor,

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