Merge branch 'master' of http://www.scserverddns.top:13000/sc/SunlightAggregationManager
This commit is contained in:
commit
3840701756
4 files changed
+221
-62
No files matched your search
@@ -15,7 +15,8 @@
|
||||
<PackageReference Include="Net.Codecrete.QrCodeGenerator" Version="2.1.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||
<PackageReference Include="System.Data.SqlClient" Version="4.9.1" />
|
||||
<PackageReference Include="TouchSocket" Version="4.2.3" />
|
||||
<PackageReference Include="TouchSocket" Version="4.2.10" />
|
||||
<PackageReference Include="TouchSocket.Http" Version="4.2.10" />
|
||||
<PackageReference Include="vocaluxe.dependencies.netfwtypelib" Version="1.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -0,0 +1,205 @@
|
||||
using SunlightAggregationManager.ViewModel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using TouchSocket.Core;
|
||||
using TouchSocket.Http;
|
||||
using TouchSocket.Http.WebSockets;
|
||||
using TouchSocket.Sockets;
|
||||
using static SunlightAggregationManager.UserClass.AsyncTcpServer;
|
||||
|
||||
|
||||
namespace SunlightAggregationManager.UserClass
|
||||
{
|
||||
public class AsyncHttpServer
|
||||
{
|
||||
public static HttpService httpservice = new HttpService();
|
||||
|
||||
public static async Task HttpMain(int port1)
|
||||
{
|
||||
NetFwManger.AllowPort(port1, "TCP");//开放端口
|
||||
await httpservice.SetupAsync(new TouchSocketConfig()//加载配置
|
||||
.SetListenIPHosts(port1)
|
||||
.ConfigureContainer(a =>
|
||||
{
|
||||
a.AddConsoleLogger();
|
||||
})
|
||||
.ConfigurePlugins(a =>
|
||||
{
|
||||
a.Add<MyHttpPlug1>();
|
||||
//default插件应该最后添加,其作用是
|
||||
//1、为找不到的路由返回404
|
||||
//2、处理 header 为Option的探视跨域请求。
|
||||
a.UseDefaultHttpServicePlugin();
|
||||
}));
|
||||
await httpservice.StartAsync();
|
||||
Console.WriteLine("HTTP Port:" + port1.ToString());
|
||||
}
|
||||
|
||||
public class MyHttpPlug1 : PluginBase, IHttpPlugin
|
||||
{
|
||||
public async Task OnHttpRequest(IHttpSessionClient client, HttpContextEventArgs e)
|
||||
{
|
||||
#region HttpContext生命周期
|
||||
//var request = e.Context.Request;//http请求体
|
||||
var response = e.Context.Response;//http响应体
|
||||
#endregion
|
||||
|
||||
var bodyString = await e.Context.Request.GetBodyAsync();
|
||||
try
|
||||
{
|
||||
Person? deserializedPerson = JsonSerializer.Deserialize<Person>(bodyString);
|
||||
|
||||
var _user = MainWindowViewModel.Selet_Memory(MainWindowViewModel._userData, "State", "User='" +
|
||||
deserializedPerson?.User + "' and Password ='" + deserializedPerson?.Password + "'");
|
||||
if (_user != null)
|
||||
{
|
||||
string _dat = _user?.ToString() ?? "0";
|
||||
if (_dat != "99")
|
||||
{
|
||||
//行为
|
||||
Dictionary<string, object> myDict = new Dictionary<string, object>();
|
||||
myDict.Add("LinkID", client.Id);
|
||||
myDict.Add("LastLoginIP", deserializedPerson?.IP ?? client.IP);
|
||||
myDict.Add("LastActionTime", DateTime.Now);
|
||||
myDict.Add("Note", deserializedPerson?.Dat ?? "unknown");
|
||||
MainWindowViewModel.Updata_Memory(MainWindowViewModel._userData, "User='" + deserializedPerson?.User + "'", myDict);
|
||||
|
||||
Console.WriteLine("[Log on]User[" + deserializedPerson?.User + "]Note[" + (deserializedPerson?.Dat ?? "unknown") + "]");
|
||||
|
||||
var dt = await Command(deserializedPerson!);
|
||||
|
||||
e.Context.Response.Headers.Add("SUNLIGHT", "SUNLIGHT");
|
||||
e.Context.Response.SetStatus(200, "success");
|
||||
e.Context.Response.SetContent(Encoding.UTF8.GetBytes(dt.ToCharArray()));
|
||||
await e.Context.Response.AnswerAsync();
|
||||
}
|
||||
else
|
||||
{//停用账号关闭连接
|
||||
//client.SendAsync("Account Deactivated");
|
||||
e.Context.Response.Headers.Add("SUNLIGHT", "SUNLIGHT");
|
||||
e.Context.Response.SetStatus(200, "success");
|
||||
e.Context.Response.SetContent("Account Deactivated");
|
||||
await e.Context.Response.AnswerAsync();
|
||||
}
|
||||
}
|
||||
else
|
||||
{ //无效账号关闭连接
|
||||
//client.SendAsync("Invalid Account");
|
||||
e.Context.Response.Headers.Add("SUNLIGHT", "SUNLIGHT");
|
||||
e.Context.Response.SetStatus(200, "success");
|
||||
e.Context.Response.SetContent("Invalid Account");
|
||||
await e.Context.Response.AnswerAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{//错误
|
||||
Console.WriteLine("Http:" + ex.ToString());
|
||||
//client.SendAsync("Target instruction parsing error manager will close connection");
|
||||
}
|
||||
|
||||
|
||||
/* if (request.IsGet() && request.UrlEquals("/success"))
|
||||
{
|
||||
//直接响应文字
|
||||
await response
|
||||
.SetStatus(200, "success")
|
||||
.FromText("Success")
|
||||
.AnswerAsync();//直接回应
|
||||
Console.WriteLine("处理/success");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//无法处理,调用下一个插件
|
||||
await e.InvokeNext();*/
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<string> Command(Person deserializedPerson)//
|
||||
{
|
||||
string Command = "", ret = "";
|
||||
|
||||
Dictionary<string, object> data_ = new Dictionary<string, object>();
|
||||
var deserialized = JsonSerializer.Deserialize<Dictionary<string, object>>(deserializedPerson.Dat)!;
|
||||
if (deserialized.TryGetValue("Command", out var val) && val != null) Command = val.ToString()!;
|
||||
if (Command == "Log")//回复请求的信息
|
||||
{
|
||||
DataRow? _user = MainWindowViewModel.Selet_Row(MainWindowViewModel._userData, "User='" +
|
||||
deserializedPerson?.User + "' and Password ='" + deserializedPerson?.Password + "'");
|
||||
|
||||
data_.Add("Enterprise", _user?.Field<object>("Enterprise") ?? "");
|
||||
data_.Add("Department", _user?.Field<object>("Department") ?? "");
|
||||
data_.Add("Groups", _user?.Field<object>("Groups") ?? "");
|
||||
data_.Add("UserId", _user?.Field<object>("UserID") ?? "");
|
||||
data_.Add("UserName", _user?.Field<object>("Name") ?? "");
|
||||
ret = JsonSerializer.Serialize(data_);
|
||||
}
|
||||
if (Command == "Info")//回复info请求的信息
|
||||
{
|
||||
var t = ToList(MainWindowViewModel._machines).ToJsonString();
|
||||
//格式化返回信息
|
||||
data_.Add("Info", t);
|
||||
ret = JsonSerializer.Serialize(data_);
|
||||
}
|
||||
if (Command == "Query")//回复Query请求的信息
|
||||
{
|
||||
string s_ = deserialized.GetValue("StartTime").ToString()!;
|
||||
string e_ = deserialized.GetValue("EndTime").ToString()!;
|
||||
string f_ = deserialized.GetValue("Field").ToString()!;
|
||||
|
||||
DataTable resultTable = await MainWindowViewModel.dataBase.ExecuteQueryAsync("SELECT " +
|
||||
f_ + " FROM [dbo].[Dyelots] " + "WHERE CreationTime>'" + s_ + "'and CreationTime<'" + e_ + "'");
|
||||
//格式化返回信息
|
||||
data_.Add("Query", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
||||
ret = JsonSerializer.Serialize(data_);
|
||||
}
|
||||
if (Command == "QueryDyelot")//回复QueryDyelot请求的信息
|
||||
{
|
||||
string d_ = deserialized.GetValue("DyelotName").ToString()!;
|
||||
string f_ = deserialized.GetValue("Field").ToString()!;
|
||||
|
||||
DataTable resultTable = await MainWindowViewModel.dataBase.ExecuteQueryAsync("SELECT " +
|
||||
f_ + " FROM [dbo].[Dyelots] " + "WHERE Dyelot LIKE '" + d_ + "%'");
|
||||
//格式化返回信息
|
||||
data_.Add("Query", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
||||
ret = JsonSerializer.Serialize(data_);
|
||||
}
|
||||
if (Command == "QueryDetail")//回复info请求的信息
|
||||
{
|
||||
string d_ = deserialized.GetValue("Dyelot").ToString()!;
|
||||
string r_ = deserialized.GetValue("ReDye").ToString()!;
|
||||
string f_ = deserialized.GetValue("Field").ToString()!;
|
||||
|
||||
DataTable resultTable = await MainWindowViewModel.dataBase.ExecuteQueryAsync("SELECT " +
|
||||
f_ + " FROM [dbo].[DyelotsBulkedRecipe] " + "WHERE Dyelot='" + d_ + "'and ReDye='" + r_ + "'");
|
||||
//格式化返回信息
|
||||
data_.Add("QueryDetail", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
||||
ret = JsonSerializer.Serialize(data_);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static List<Dictionary<string, object?>>? ToList(DataTable table)//datatable转list
|
||||
{
|
||||
if (table == null) return null;
|
||||
|
||||
// 使用 AsEnumerable() 将 DataTable 转换为 IEnumerable<DataRow>
|
||||
// 然后投影为 Dictionary
|
||||
return table.AsEnumerable().Select(row =>
|
||||
table.Columns.Cast<DataColumn>().ToDictionary(
|
||||
col => col.ColumnName,
|
||||
col => {
|
||||
var value = row[col];
|
||||
// 核心修改:如果是数据库空值 DBNull.Value,则转为 C# 的 null,否则保持原值
|
||||
return value == DBNull.Value ? null : value;
|
||||
}
|
||||
)
|
||||
).ToList();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,6 @@ using System.Windows.Markup;
|
||||
using System.Xml.Linq;
|
||||
using TouchSocket.Core;
|
||||
using TouchSocket.Sockets;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
namespace SunlightAggregationManager.UserClass
|
||||
{
|
||||
@@ -236,64 +235,7 @@ namespace SunlightAggregationManager.UserClass
|
||||
)
|
||||
).ToList();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
class MyTcpService : TcpService<MyTcpSessionClient>
|
||||
{
|
||||
protected override MyTcpSessionClient NewClient()
|
||||
{
|
||||
return new MyTcpSessionClient();
|
||||
}
|
||||
}
|
||||
|
||||
class MyTcpSessionClient : TcpSessionClient
|
||||
{
|
||||
internal void SetDataHandlingAdapter(SingleStreamDataHandlingAdapter adapter)
|
||||
{
|
||||
base.SetAdapter(adapter);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 此插件实现,按照不同端口,使用不同适配器。
|
||||
/// <list type="bullet">
|
||||
/// <item>7789端口:使用"**"结尾的数据</item>
|
||||
/// <item>7790端口:使用"##"结尾的数据</item>
|
||||
/// </list>
|
||||
/// </summary>
|
||||
internal class DifferentProtocolPlugin : PluginBase, ITcpConnectingPlugin, ITcpReceivedPlugin
|
||||
{
|
||||
public async Task OnTcpConnecting(ITcpSession client, ConnectingEventArgs e)
|
||||
{
|
||||
if (client is MyTcpSessionClient sessionClient)
|
||||
{
|
||||
if (sessionClient.ServicePort == 7789)
|
||||
{
|
||||
sessionClient.SetDataHandlingAdapter(new TerminatorPackageAdapter("**"));
|
||||
}
|
||||
else
|
||||
{
|
||||
sessionClient.SetDataHandlingAdapter(new TerminatorPackageAdapter("##"));
|
||||
}
|
||||
}
|
||||
|
||||
await e.InvokeNext();
|
||||
}
|
||||
|
||||
public async Task OnTcpReceived(ITcpSession client, ReceivedDataEventArgs e)
|
||||
{
|
||||
//如果是自定义适配器,此处解析时,可以判断e.RequestInfo的类型
|
||||
|
||||
if (client is ITcpSessionClient sessionClient)
|
||||
{
|
||||
sessionClient.Logger.Info($"{sessionClient.GetIPPort()}收到数据,服务器端口:{sessionClient.ServicePort},数据:{e.Memory.Span.ToString(Encoding.UTF8)}");
|
||||
}
|
||||
|
||||
await e.InvokeNext();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -80,8 +80,10 @@ namespace SunlightAggregationManager.ViewModel
|
||||
}
|
||||
|
||||
//运行数据库
|
||||
dataBase.Config(SQLIP, SQLNAME, SQMOD, SQLUSER, SQLPASWORD);
|
||||
dataBase.Config(SQLIP, SQLNAME, SQMOD, SQLUSER, SQLPASWORD);
|
||||
|
||||
|
||||
Console.WriteLine(GetLocalIP.GetLocalIPv4Address().ToString());
|
||||
if (TCP_E == "1")//启动tcp(内网直连)
|
||||
{
|
||||
int P1, P2;
|
||||
@@ -96,9 +98,18 @@ namespace SunlightAggregationManager.ViewModel
|
||||
}
|
||||
catch (Exception) { P2 = 7790; }
|
||||
|
||||
Console.WriteLine(GetLocalIP.GetLocalIPv4Address().ToString());
|
||||
_ = AsyncTcpServer.TcpMain(P1,P2);
|
||||
}
|
||||
if (HTTP_E == "1")//启动tcp(内网直连)
|
||||
{
|
||||
int P1;
|
||||
try
|
||||
{
|
||||
P1 = int.Parse(Configini.IniReadvalue("NETWORK", "HTTP_PORT"));
|
||||
}
|
||||
catch (Exception) { P1 = 7791; }
|
||||
_ = AsyncHttpServer.HttpMain(P1);
|
||||
}
|
||||
}
|
||||
|
||||
private void Machines_Updata(object sender, DataRowChangeEventArgs e)
|
||||
|
||||
Reference in new issue
Block a user