实现更多的指令

dev
nxiaoxiao 1 year ago
parent 0082549e0b
commit e2c85517cc

@ -12,14 +12,39 @@ namespace OMS.NET.Common
public class ActiveDataElement
{
private static readonly JsonSerializerOptions options = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, // 属性名为小写
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, // 忽略 null 值,
WriteIndented = true, // 美化输出
Converters = { new ActiveDataElementConverter() }
};
public string EID { get; set; }
public UserInfo? Pick { get; set; }
public UserInfo? Select { get; set; }
public ActiveDataElement(string id)
public ActiveDataElement(long id)
{
EID = "E" + id;
}
public string ToJsonString()
{
return JsonSerializer.Serialize(this, options);
}
public static ActiveDataElement? JsonParse(string json)
{
try
{
return JsonSerializer.Deserialize<ActiveDataElement>(json, options);
}
catch (Exception ex)
{
GlobalArea.Log.Error(ex.Message);
return null;
}
}
}
public class ActiveDataElementConverter : JsonConverter<ActiveDataElement>
@ -32,7 +57,7 @@ namespace OMS.NET.Common
}
reader.Read();
string? eid = (reader.GetString()?[1..]) ?? throw new JsonException(); // Removing 'E' prefix to get the ID
var element = new ActiveDataElement(eid);
var element = new ActiveDataElement(Convert.ToInt64(eid));
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)

@ -290,6 +290,34 @@ namespace OMS.NET
private static readonly List<LayerData> _LayerDataList = new();
private static readonly object _LayerDataListLock = new();
private static readonly List<ActiveDataElement> _ActiveDataList = new();
private static readonly object _ActiveDataListLock = new();
public static bool IsElementExit(long elementId)
{
lock (_ActiveDataListLock)
{
ActiveDataElement? ad = _ActiveDataList.Find(u => u.EID == ("E" + elementId));
return ad != null;
}
}
public static ActiveDataElement? GetActiveDataElement(long elementId)
{
lock (_ActiveDataListLock)
{
return _ActiveDataList.Find(u => u.EID == ("E" + elementId));
}
}
public static void AddActiveDataElement(ActiveDataElement activeDataElement)
{
lock (_ActiveDataListLock)
{
_ActiveDataList.Add(activeDataElement);
}
}
/// <summary>
/// 从数据库中构建layerid 和 templateid的对应关系
/// </summary>
@ -332,7 +360,7 @@ namespace OMS.NET
/// <summary>
/// 默认loglevel为3发布版本需要改成2
/// </summary>
private static Logger _Logger = new(3);
private static readonly Logger _Logger = new(3);
private static readonly object _LoggerLock = new();
public static Logger Log

@ -0,0 +1,33 @@
using System.Text.Json;
namespace OMS.NET.Instructs
{
public class DeleteElementBroadcastInstruct : BroadcastInstuct
{
public DeleteElementBroadcastInstruct()
{
this.Class = "deleteElement";
}
public override Task Handler(string wsid)
{
return Task.Run(() =>
{
if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查不通过则直接退出
if (Data?.GetType() != typeof(JsonElement)) return;
try
{
string conveyor = GlobalArea.GetLoginEmailByID(wsid);
//解析id和tmpId
long id = Data.GetProperty("id").GetInt64();
string tmpId = Data.GetProperty("tmpId").GetString();
//需要先实现activeData相关
}
catch
{
}
});
}
}
}

@ -62,6 +62,8 @@ namespace OMS.NET.Instructs
"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),
_ => JsonSerializer.Deserialize<Instruct>(root.GetRawText(), options)
},
_ => null

@ -0,0 +1,70 @@
using System.Text.Json;
using OMS.NET.Common;
using OMS.NET.DbClass;
namespace OMS.NET.Instructs
{
public class SelectIngElementBroadcastInstruct : BroadcastInstuct
{
public SelectIngElementBroadcastInstruct()
{
this.Class = "selectIngElement";
}
public override Task Handler(string wsid)
{
return Task.Run(() =>
{
if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查不通过则直接退出
if (Data?.GetType() != typeof(JsonElement)) return;
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.Select == null)
{
activeDataElement.Select = userInfo;
}
else
{
throw new Exception($"{id}已被{activeDataElement.Select.Name}选中");
}
}
else
{
GlobalArea.AddActiveDataElement(new ActiveDataElement(id)
{
Select = userInfo
});
}
string time = GlobalArea.GetCurrentTime();
ResponseOrBroadcastInstructs.Add(new SelectIngElementBroadcastInstruct()
{
IsBroadcast = true,
Conveyor = conveyor,
Time = time,
Data = new
{
id = id,
color = accountData.HeadColor
}
});
}
catch (Exception ex)
{
GlobalArea.Log.Warn($"处理{this.Class}广播指令出错:" + ex.Message);
}
});
}
}
}

@ -0,0 +1,39 @@
using System.Text.Json;
namespace OMS.NET.Instructs
{
public class TextMessageBroadcastInstruct : BroadcastInstuct
{
public TextMessageBroadcastInstruct()
{
this.Class = "textMessage";
}
public override Task Handler(string wsid)
{
return Task.Run(() =>
{
if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查不通过则直接退出
if (Data?.GetType() != typeof(JsonElement)) return;
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()
{
IsBroadcast = true,
Conveyor = conveyor,
Time = time,
Data = Data
});
}
catch (Exception ex)
{
GlobalArea.Log.Warn("处理textMessage发生问题" + ex.Message);
}
});
}
}
}
Loading…
Cancel
Save