|
|
|
using DyeingComputer.UserClass;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Data;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Net.Sockets;
|
|
|
|
using System.Reflection.Emit;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using TouchSocket.Core;
|
|
|
|
using TouchSocket.Sockets;
|
|
|
|
using DyeingComputer.ViewModel;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using ScottPlot.Colormaps;
|
|
|
|
using static System.Windows.Forms.AxHost;
|
|
|
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
|
|
|
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using static DyeingComputer.UserClass.SqliteHelper;
|
|
|
|
using DyeingComputer.View;
|
|
|
|
using System.Windows;
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
|
|
namespace DyeingComputer.UserClass
|
|
|
|
{/// <summary>
|
|
|
|
/// 异步TCP服务器
|
|
|
|
/// </summary>
|
|
|
|
public class AsyncTcpServer
|
|
|
|
{
|
|
|
|
//设置系统时间的API函数
|
|
|
|
[DllImport("kernel32.dll")]
|
|
|
|
private static extern bool SetLocalTime(ref SYSTEMTIME time);
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
private struct SYSTEMTIME
|
|
|
|
{
|
|
|
|
public short year;
|
|
|
|
public short month;
|
|
|
|
public short dayOfWeek;
|
|
|
|
public short day;
|
|
|
|
public short hour;
|
|
|
|
public short minute;
|
|
|
|
public short second;
|
|
|
|
public short milliseconds;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// 设置系统时间
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="dt">需要设置的时间</param>
|
|
|
|
/// <returns>返回系统时间设置状态,true为成功,false为失败</returns>
|
|
|
|
private static bool SetLocalDateTime(DateTime dt)
|
|
|
|
{
|
|
|
|
SYSTEMTIME st;
|
|
|
|
st.year = (short)dt.Year;
|
|
|
|
st.month = (short)dt.Month;
|
|
|
|
st.dayOfWeek = (short)dt.DayOfWeek;
|
|
|
|
st.day = (short)dt.Day;
|
|
|
|
st.hour = (short)dt.Hour;
|
|
|
|
st.minute = (short)dt.Minute;
|
|
|
|
st.second = (short)dt.Second;
|
|
|
|
st.milliseconds = (short)dt.Millisecond;
|
|
|
|
bool rt = SetLocalTime(ref st);
|
|
|
|
return rt;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async Task Main()
|
|
|
|
{
|
|
|
|
NetFwManger.AllowPort(7790,"TCP");//开放7790端口
|
|
|
|
TcpService service = new TcpService();
|
|
|
|
service.Connecting = (client, e) => { return EasyTask.CompletedTask; };//有客户端正在连接
|
|
|
|
service.Connected = (client, e) => { return EasyTask.CompletedTask; };//有客户端成功连接
|
|
|
|
service.Closing = (client, e) => { return EasyTask.CompletedTask; };//有客户端正在断开连接,只有当主动断开时才有效。
|
|
|
|
service.Closed = (client, e) => { return EasyTask.CompletedTask; };//有客户端断开连接
|
|
|
|
service.Received = (client, e) =>
|
|
|
|
{
|
|
|
|
string SYSAPI = e.ByteBlock.Span.ToString(Encoding.ASCII).Substring(0,5);
|
|
|
|
string DAT = e.ByteBlock.Span.ToString(Encoding.ASCII).Substring(5);
|
|
|
|
string SYSDAT ="";// = e.ByteBlock.Span.ToString(Encoding.ASCII).Substring(5);
|
|
|
|
string SYSKEY ="";
|
|
|
|
if (DAT.Length >= 16) SYSKEY = DAT.Substring(0,16);
|
|
|
|
if (DAT.Length > 16) SYSDAT = DAT.Substring(16);
|
|
|
|
//LogGing.LogSQLDATA("800", "TcpServer", "API:"+ SYSAPI);
|
|
|
|
if (SYSAPI == "SC800")
|
|
|
|
{
|
|
|
|
Dictionary<string, object> Chart_new = new Dictionary<string, object>();//缓存函数
|
|
|
|
Chart_new.Add("MACHINE", MainWindowViewModel.S01);
|
|
|
|
Chart_new.Add("GROUP", MainWindowViewModel.S05);
|
|
|
|
Chart_new.Add("SYSKEY", MainWindowViewModel.SYSKEY);
|
|
|
|
Chart_new.Add("TIME", MainWindowViewModel.SYSTime);
|
|
|
|
client.SendAsync("SC800" + "[" + MainWindowViewModel.S01 + "]" + Chart_new.ToJsonString());
|
|
|
|
}
|
|
|
|
else if (SYSAPI == "SC810")
|
|
|
|
{
|
|
|
|
if (SYSKEY == MainWindowViewModel.SYSKEY)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Dictionary<string, object> WorkOrder_dat;
|
|
|
|
WorkOrder_dat = SerializeConvert.JsonDeserializeFromString<Dictionary<string, object>>(SYSDAT);
|
|
|
|
//WorkOrder_dat.GetValue("WorkOrder");
|
|
|
|
bool dat_w= SQLDATA.WorkOrder(
|
|
|
|
WorkOrder_dat.GetValue("WorkOrder").ToString(),
|
|
|
|
WorkOrder_dat.GetValue("ProcessName").ToString(),
|
|
|
|
WorkOrder_dat.GetValue("StartTime").ToString(),
|
|
|
|
WorkOrder_dat.GetValue("EndTime").ToString(),
|
|
|
|
WorkOrder_dat.GetValue("Time").ToString(),
|
|
|
|
WorkOrder_dat.GetValue("Remark").ToString(),
|
|
|
|
WorkOrder_dat.GetValue("lock").ToString(),
|
|
|
|
WorkOrder_dat.GetValue("State").ToString(),
|
|
|
|
WorkOrder_dat.GetValue("ProcessID").ToString());
|
|
|
|
if (!dat_w) { client.SendAsync("SC910"); }
|
|
|
|
else { client.SendAsync("SC810" + SYSKEY +SYSDAT); }
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
client.SendAsync("SC990");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { client.SendAsync("SC999"); }
|
|
|
|
}//WorkOrder表信息检查写入
|
|
|
|
else if (SYSAPI == "SC811")
|
|
|
|
{
|
|
|
|
if (SYSKEY == MainWindowViewModel.SYSKEY)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
DataTable WorkOrder_dat;
|
|
|
|
WorkOrder_dat = SerializeConvert.JsonDeserializeFromString<DataTable>(SYSDAT);
|
|
|
|
bool dat_w = SQLDATA.WorkOderStep(WorkOrder_dat);
|
|
|
|
if (!dat_w) { client.SendAsync("SC911"); }
|
|
|
|
else { client.SendAsync("SC811" + SYSKEY + SYSDAT); }
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
client.SendAsync("SC991");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { client.SendAsync("SC999"); }
|
|
|
|
}//WorkOrderstep表信息检查写入
|
|
|
|
else if (SYSAPI == "SC820")
|
|
|
|
{
|
|
|
|
if (SYSKEY == MainWindowViewModel.SYSKEY)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (SYSDAT.Length == 21)
|
|
|
|
{
|
|
|
|
SYSDAT = SYSDAT.Substring(1, 19);
|
|
|
|
DateTime dt;
|
|
|
|
if (DateTime.TryParse(SYSDAT, out dt))
|
|
|
|
SetLocalDateTime(dt);
|
|
|
|
}
|
|
|
|
client.SendAsync("SC820" + "[" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "]");
|
|
|
|
}
|
|
|
|
catch { client.SendAsync("SC920"); }
|
|
|
|
}
|
|
|
|
else { client.SendAsync("SC999"); }
|
|
|
|
}//设置系统时间
|
|
|
|
else if (SYSAPI == "SC821")
|
|
|
|
{
|
|
|
|
if (SYSKEY == MainWindowViewModel.SYSKEY)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Dictionary<string, object> dat_821;
|
|
|
|
dat_821 = SerializeConvert.JsonDeserializeFromString<Dictionary<string, object>>(SYSDAT);
|
|
|
|
if (dat_821.GetValue("INSTRUCTION").ToString() == "START")
|
|
|
|
{
|
|
|
|
SQLDATA.TechnologicalProcess_START(dat_821.GetValue("ProgramID").ToString());
|
|
|
|
}
|
|
|
|
else if (dat_821.GetValue("INSTRUCTION").ToString() == "STOP")
|
|
|
|
{
|
|
|
|
MainWindowViewModel.WORK_RUN = 0;//停止
|
|
|
|
MainWindowViewModel.DIDETime = 0;
|
|
|
|
}
|
|
|
|
else if (dat_821.GetValue("INSTRUCTION").ToString() == "PAUSE")
|
|
|
|
{
|
|
|
|
MainWindowViewModel.WORK_RUN = 1;//暂停
|
|
|
|
}
|
|
|
|
else if (dat_821.GetValue("INSTRUCTION").ToString() == "CONTINUE")
|
|
|
|
{
|
|
|
|
MainWindowViewModel.WORK_RUN = 2;//暂停
|
|
|
|
}
|
|
|
|
else if (dat_821.GetValue("INSTRUCTION").ToString() == "JUMP")
|
|
|
|
{
|
|
|
|
MainWindowViewModel.RUN_DATATABLE = TechnologicalProcessView.sql.Tables[0];//缓存表
|
|
|
|
MainWindowViewModel.RUN_STEPID = Convert.ToInt16(dat_821.GetValue("ID").ToString());//插入步骤号
|
|
|
|
MainWindowViewModel.STEP_START(dat_821.GetValue("Numder").ToString(), Convert.ToDouble(dat_821.GetValue("P1")),
|
|
|
|
Convert.ToDouble(dat_821.GetValue("P2")), Convert.ToDouble(dat_821.GetValue("P3")),
|
|
|
|
Convert.ToDouble(dat_821.GetValue("P4")), Convert.ToDouble(dat_821.GetValue("P5")));
|
|
|
|
}
|
|
|
|
else if (dat_821.GetValue("INSTRUCTION").ToString() == "INSERT")
|
|
|
|
{
|
|
|
|
int d = Convert.ToInt16( dat_821.GetValue("ID"));
|
|
|
|
TechnologicalProcessView.SQLiteHelpers = new SQLiteHelper(TechnologicalProcessView.DBAddress); //数据库连接路径
|
|
|
|
TechnologicalProcessView.SQLiteHelpers.Open(); //打开数据库
|
|
|
|
|
|
|
|
string ProgramID = TechnologicalProcessView.SQLiteHelpers.ExecuteScalar("select ProgramID from RUN where Step = '1'", null).ToString();
|
|
|
|
string ProgramNAME = TechnologicalProcessView.SQLiteHelpers.ExecuteScalar("select Program from RUN where Step = '1'", null).ToString();
|
|
|
|
TechnologicalProcessView.SQLiteHelpers.Close();
|
|
|
|
DataRow dr = TechnologicalProcessView.sql.Tables[0].NewRow();//添加表数据
|
|
|
|
dr["ProgramID"] = ProgramID;
|
|
|
|
dr["Program"] = ProgramNAME;
|
|
|
|
TechnologicalProcessView.sql.Tables[0].Rows.InsertAt(dr, d - 1);
|
|
|
|
|
|
|
|
int a = TechnologicalProcessView.sql.Tables[0].Rows.Count;
|
|
|
|
for (int i = 0; i < a; i++)
|
|
|
|
{
|
|
|
|
DataRow drt = TechnologicalProcessView.sql.Tables[0].Rows[i];
|
|
|
|
drt.BeginEdit();
|
|
|
|
drt["Step"] = i + 1;
|
|
|
|
drt.EndEdit();
|
|
|
|
}
|
|
|
|
|
|
|
|
DataRow drtt = TechnologicalProcessView.sql.Tables[0].Rows[d - 1];
|
|
|
|
drtt.BeginEdit();
|
|
|
|
drtt["StepID"] = dat_821.GetValue("STEPID").ToString();
|
|
|
|
drtt["StepName"] = dat_821.GetValue("STEPNAME").ToString();
|
|
|
|
drtt["ParameterName"] = dat_821.GetValue("PNAME").ToString();
|
|
|
|
drtt["Parameter1"] = dat_821.GetValue("P1").ToString();
|
|
|
|
drtt["Parameter2"] = dat_821.GetValue("P2").ToString();
|
|
|
|
drtt["Parameter3"] = dat_821.GetValue("P3").ToString();
|
|
|
|
drtt["Parameter4"] = dat_821.GetValue("P4").ToString();
|
|
|
|
drtt["Parameter5"] = dat_821.GetValue("P5").ToString();
|
|
|
|
drtt.EndEdit();
|
|
|
|
|
|
|
|
var _TechnologicalProcessView = Application.Current.Windows.Cast<UserControl>().FirstOrDefault(window => window is TechnologicalProcessView) as TechnologicalProcessView;//跨页面
|
|
|
|
_TechnologicalProcessView.Grid.ItemsSource = TechnologicalProcessView.sql.Tables[0].DefaultView;
|
|
|
|
_TechnologicalProcessView.Grid.SelectedIndex = -1;
|
|
|
|
|
|
|
|
TechnologicalProcessView.SQLiteHelpers = new SQLiteHelper(TechnologicalProcessView.DBAddress); //数据库连接路径
|
|
|
|
TechnologicalProcessView.SQLiteHelpers.Open(); //打开数据库
|
|
|
|
TechnologicalProcessView.SQLiteHelpers.Delete("RUN", null, null);
|
|
|
|
DataTable data_t = new DataTable();
|
|
|
|
data_t.Columns.Add("DYELOT", typeof(string)); //添加列
|
|
|
|
data_t = TechnologicalProcessView.sql.Tables[0].Clone();
|
|
|
|
int a = TechnologicalProcessView.sql.Tables[0].Rows.Count;
|
|
|
|
for (int i = 0; i < a; i++)
|
|
|
|
{
|
|
|
|
data_t.Clear();//清空
|
|
|
|
|
|
|
|
DataRow dt = TechnologicalProcessView.sql.Tables[0].Rows[i];//行转换
|
|
|
|
DataRow drT = data_t.NewRow();
|
|
|
|
drT.ItemArray = dt.ItemArray;
|
|
|
|
data_t.Rows.InsertAt(drT, 0);
|
|
|
|
dr.BeginEdit(); //添加订单号
|
|
|
|
dr["DYELOT"] = MainWindowViewModel.WorkNumder;
|
|
|
|
dr.EndEdit();
|
|
|
|
|
|
|
|
TechnologicalProcessView.SQLiteHelpers.InsertData("RUN", TechnologicalProcessView.SQLiteHelpers.DataTableToDictionary(data_t));//行插入
|
|
|
|
}
|
|
|
|
|
|
|
|
TechnologicalProcessView.SQLiteHelpers.Close(); //关闭连接
|
|
|
|
}//插入
|
|
|
|
else if (dat_821.GetValue("INSTRUCTION").ToString() == "EDIT")
|
|
|
|
{
|
|
|
|
int n = Convert.ToInt16(dat_821.GetValue("ID").ToString());
|
|
|
|
|
|
|
|
DataRow dr = TechnologicalProcessView.sql.Tables[0].Rows[n - 1];
|
|
|
|
dr.BeginEdit();
|
|
|
|
dr["StepID"] = dat_821.GetValue("STEPID").ToString();
|
|
|
|
dr["StepName"] = dat_821.GetValue("STEPNAME").ToString();
|
|
|
|
dr["ParameterName"] = dat_821.GetValue("PNAME").ToString();
|
|
|
|
dr["Parameter1"] = dat_821.GetValue("P1").ToString();
|
|
|
|
dr["Parameter2"] = dat_821.GetValue("P2").ToString();
|
|
|
|
dr["Parameter3"] = dat_821.GetValue("P3").ToString();
|
|
|
|
dr["Parameter4"] = dat_821.GetValue("P4").ToString();
|
|
|
|
dr["Parameter5"] = dat_821.GetValue("P5").ToString();
|
|
|
|
dr.EndEdit();
|
|
|
|
|
|
|
|
var _TechnologicalProcessView = Application.Current.Windows.Cast<UserControl>().FirstOrDefault(window => window is TechnologicalProcessView) as TechnologicalProcessView;//跨页面
|
|
|
|
_TechnologicalProcessView.Grid.ItemsSource = TechnologicalProcessView.sql.Tables[0].DefaultView;
|
|
|
|
_TechnologicalProcessView.Grid.SelectedIndex = -1;
|
|
|
|
|
|
|
|
TechnologicalProcessView.SQLiteHelpers = new SQLiteHelper(TechnologicalProcessView.DBAddress); //数据库连接路径
|
|
|
|
TechnologicalProcessView.SQLiteHelpers.Open(); //打开数据库
|
|
|
|
TechnologicalProcessView.SQLiteHelpers.Delete("RUN", null, null);
|
|
|
|
DataTable data_t = new DataTable();
|
|
|
|
data_t.Columns.Add("DYELOT", typeof(string)); //添加列
|
|
|
|
data_t = TechnologicalProcessView.sql.Tables[0].Clone();
|
|
|
|
int a = TechnologicalProcessView.sql.Tables[0].Rows.Count;
|
|
|
|
for (int i = 0; i < a; i++)
|
|
|
|
{
|
|
|
|
data_t.Clear();//清空
|
|
|
|
|
|
|
|
DataRow dt = TechnologicalProcessView.sql.Tables[0].Rows[i];//行转换
|
|
|
|
DataRow drT = data_t.NewRow();
|
|
|
|
drT.ItemArray = dt.ItemArray;
|
|
|
|
data_t.Rows.InsertAt(drT, 0);
|
|
|
|
dr.BeginEdit(); //添加订单号
|
|
|
|
dr["DYELOT"] = MainWindowViewModel.WorkNumder;
|
|
|
|
dr.EndEdit();
|
|
|
|
|
|
|
|
TechnologicalProcessView.SQLiteHelpers.InsertData("RUN", TechnologicalProcessView.SQLiteHelpers.DataTableToDictionary(data_t));//行插入
|
|
|
|
}
|
|
|
|
|
|
|
|
TechnologicalProcessView.SQLiteHelpers.Close(); //关闭连接
|
|
|
|
}//编辑
|
|
|
|
else if (dat_821.GetValue("INSTRUCTION").ToString() == "DELETE")
|
|
|
|
{
|
|
|
|
int d = Convert.ToInt16(dat_821.GetValue("ID"));
|
|
|
|
|
|
|
|
int a = TechnologicalProcessView.sql.Tables[0].Rows.Count;
|
|
|
|
if (a != d)//如果删除不是最后一行执行
|
|
|
|
{
|
|
|
|
TechnologicalProcessView.sql.Tables[0].Rows.RemoveAt(d - 1);
|
|
|
|
|
|
|
|
for (int i = 0; i < a; i++)
|
|
|
|
{
|
|
|
|
DataRow drt = TechnologicalProcessView.sql.Tables[0].Rows[i];
|
|
|
|
drt.BeginEdit();
|
|
|
|
drt["Step"] = i + 1;
|
|
|
|
drt.EndEdit();
|
|
|
|
}
|
|
|
|
var _TechnologicalProcessView = Application.Current.Windows.Cast<UserControl>().FirstOrDefault(window => window is TechnologicalProcessView) as TechnologicalProcessView;//跨页面
|
|
|
|
_TechnologicalProcessView.Grid.ItemsSource = TechnologicalProcessView.sql.Tables[0].DefaultView; //转换显示计划表
|
|
|
|
}
|
|
|
|
}
|
|
|
|
client.SendAsync("SC821" + SYSKEY + SYSDAT);
|
|
|
|
}
|
|
|
|
catch { client.SendAsync("SC921"); }
|
|
|
|
}
|
|
|
|
else { client.SendAsync("SC999"); }
|
|
|
|
}//启停跳步指令
|
|
|
|
else if (SYSAPI == "SC830")
|
|
|
|
{
|
|
|
|
if (SYSKEY == MainWindowViewModel.SYSKEY)
|
|
|
|
{
|
|
|
|
Dictionary<string, object> Chart_new = new Dictionary<string, object>();//缓存函数
|
|
|
|
Chart_new.Add("Status", MainWindowViewModel.StatusStr);
|
|
|
|
Chart_new.Add("WorkNumder", MainWindowViewModel.WorkNumder);
|
|
|
|
Chart_new.Add("Time", MainWindowViewModel.SYSTime);
|
|
|
|
Chart_new.Add("MST", MainWindowViewModel.TEMP_co);
|
|
|
|
Chart_new.Add("MTT", MainWindowViewModel.Selet_dtm("1010"));
|
|
|
|
Chart_new.Add("MTL", MainWindowViewModel.Selet_dtm("1015"));
|
|
|
|
Chart_new.Add("MTH", MainWindowViewModel.Selet_dtm("1009"));
|
|
|
|
Chart_new.Add("MUT", MainWindowViewModel.Selet_dtm("1011"));
|
|
|
|
Chart_new.Add("STTA", MainWindowViewModel.Selet_dtm("1012"));
|
|
|
|
Chart_new.Add("STLA", MainWindowViewModel.Selet_dtm("1017"));
|
|
|
|
Chart_new.Add("STTB", MainWindowViewModel.Selet_dtm("1013"));
|
|
|
|
Chart_new.Add("STLB", MainWindowViewModel.Selet_dtm("1018"));
|
|
|
|
Chart_new.Add("STTC", MainWindowViewModel.Selet_dtm("1014"));
|
|
|
|
Chart_new.Add("STLC", MainWindowViewModel.Selet_dtm("1019"));
|
|
|
|
client.SendAsync("SC830" + "[" + MainWindowViewModel.S01 + "]" + Chart_new.ToJsonString());
|
|
|
|
}
|
|
|
|
else { client.SendAsync("SC999"); }
|
|
|
|
}//当前信息
|
|
|
|
else if (SYSAPI == "SC831")
|
|
|
|
{
|
|
|
|
if (SYSKEY == MainWindowViewModel.SYSKEY)
|
|
|
|
{
|
|
|
|
|
|
|
|
client.SendAsync("SC831" + "[" + MainWindowViewModel.S01 + "]" +TechnologicalProcessView.sql.Tables[0].ToJsonString());
|
|
|
|
}
|
|
|
|
else { client.SendAsync("SC999"); }
|
|
|
|
}//当前工作表
|
|
|
|
else if (SYSAPI == "SC832")
|
|
|
|
{
|
|
|
|
if (SYSKEY == MainWindowViewModel.SYSKEY)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{ client.SendAsync("SC832" + "[" + MainWindowViewModel.S01 + "]" + MainWindowViewModel.SYSlog);
|
|
|
|
} catch (Exception) { } }
|
|
|
|
else { client.SendAsync("SC999"); }
|
|
|
|
}//当前细节信息
|
|
|
|
else if (SYSAPI == "SC833")
|
|
|
|
{
|
|
|
|
if (SYSKEY == MainWindowViewModel.SYSKEY)
|
|
|
|
{
|
|
|
|
|
|
|
|
client.SendAsync("SC832" + "[" + MainWindowViewModel.S01 + "]" + "sc833");
|
|
|
|
}
|
|
|
|
else { client.SendAsync("SC999"); }
|
|
|
|
}//当前领料单
|
|
|
|
else if (SYSAPI == "SC851")
|
|
|
|
{
|
|
|
|
if (SYSKEY == MainWindowViewModel.SYSKEY)
|
|
|
|
{
|
|
|
|
client.SendAsync("SC851" + "[" + MainWindowViewModel.S01 + "]" + JsonConvert.SerializeObject(MainWindowViewModel.dt_d));//数字开关信息
|
|
|
|
}
|
|
|
|
else { client.SendAsync("SC999"); }
|
|
|
|
}//数字开关表
|
|
|
|
else if (SYSAPI == "SC852")
|
|
|
|
{
|
|
|
|
if (SYSKEY == MainWindowViewModel.SYSKEY)
|
|
|
|
{
|
|
|
|
client.SendAsync("SC852" + "[" + MainWindowViewModel.S01 + "]" + MainWindowViewModel.dt_a.ToJsonString());//寄存器信息
|
|
|
|
}
|
|
|
|
else { client.SendAsync("SC999"); }
|
|
|
|
}//寄存器表
|
|
|
|
else if (SYSAPI == "SC853")
|
|
|
|
{
|
|
|
|
if (SYSKEY == MainWindowViewModel.SYSKEY)
|
|
|
|
{
|
|
|
|
client.SendAsync("SC853" + "[" + MainWindowViewModel.S01 + "]" + MainWindowViewModel.dt_m.ToJsonString());//缓存信息
|
|
|
|
}
|
|
|
|
else { client.SendAsync("SC999"); }
|
|
|
|
}//缓存表
|
|
|
|
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
|
};
|
|
|
|
|
|
|
|
await service.SetupAsync(new TouchSocketConfig()//载入配置
|
|
|
|
.SetMaxBufferSize(1024 * 1024)
|
|
|
|
.SetMinBufferSize(1024 * 64)
|
|
|
|
.SetListenIPHosts(new IPHost[] { new IPHost("tcp://127.0.0.1:7789"), new IPHost(7790) })//同时监听两个地址
|
|
|
|
.ConfigureContainer(a =>//容器的配置顺序应该在最前面
|
|
|
|
{
|
|
|
|
//a.AddConsoleLogger();//添加一个控制台日志注入(注意:在maui中控制台日志不可用)
|
|
|
|
})
|
|
|
|
.ConfigurePlugins(a =>
|
|
|
|
{
|
|
|
|
//a.Add<DifferentProtocolPlugin>();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
await service.StartAsync();//启动
|
|
|
|
|
|
|
|
LogGing.LogGingDATA("800SREVER:START");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.ByteBlock}");
|
|
|
|
}
|
|
|
|
|
|
|
|
await e.InvokeNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|