sc 11 months ago
parent
commit
4a63a33e44
  1. 32
      UserClass/AsyncTcpClient.cs
  2. 3
      View/info.xaml
  3. 49
      ViewModel/MainWindowViewModel.cs

32
UserClass/AsyncTcpClient.cs

@ -30,31 +30,49 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
/// </summary> /// </summary>
public class AsyncTcpClient public class AsyncTcpClient
{ {
public static async Task TcpClient(TcpClient tcpClient, string ip,string port) public static async Task<bool> TcpClient(TcpClient tcpClient, string ip,string port)
{ {
//TcpClient tcpClient = new TcpClient(); //TcpClient tcpClient = new TcpClient();
tcpClient.Connecting = (client, e) => { return EasyTask.CompletedTask; };//即将连接到服务器,此时已经创建socket,但是还未建立tcp tcpClient.Connecting = (client, e) => { return EasyTask.CompletedTask; };//即将连接到服务器,此时已经创建socket,但是还未建立tcp
tcpClient.Connected = (client, e) => { return EasyTask.CompletedTask; };//成功连接到服务器 tcpClient.Connected = (client, e) =>
{
return EasyTask.CompletedTask;
};//成功连接到服务器
tcpClient.Closing = (client, e) => { return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。 tcpClient.Closing = (client, e) => { return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。
tcpClient.Closed = (client, e) => { return EasyTask.CompletedTask; };//从服务器断开连接,当连接不成功时不会触发。 tcpClient.Closed = (client, e) => { return EasyTask.CompletedTask; };//从服务器断开连接,当连接不成功时不会触发。
tcpClient.Received = (client, e) => tcpClient.Received = (client, e) =>
{ {
//从服务器收到信息。但是一般byteBlock和requestInfo会根据适配器呈现不同的值。 //从服务器收到信息。但是一般byteBlock和requestInfo会根据适配器呈现不同的值。
var mes = e.ByteBlock.Span.ToString(Encoding.UTF8); var mes = e.ByteBlock.Span.ToString(Encoding.UTF8);
tcpClient.Logger.Info($"客户端接收到信息:{mes}");
return EasyTask.CompletedTask;
// tcpClient.Logger.Info($"客户端接收到信息:{mes}");
return EasyTask.CompletedTask;
}; };
//载入配置 //载入配置
await tcpClient.SetupAsync(new TouchSocketConfig() await tcpClient.SetupAsync(new TouchSocketConfig()
.SetRemoteIPHost(ip+":"+port) .SetRemoteIPHost(ip+":"+port)
.ConfigurePlugins(a =>
{
a.UseTcpReconnection();//触发型重连
})
.ConfigureContainer(a => .ConfigureContainer(a =>
{ {
// a.AddConsoleLogger();//添加一个日志注入 // a.AddConsoleLogger();//添加一个日志注入
})); }));
try
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。 {
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
return true;
}
catch
{
return false;
}
} }
} }

3
View/info.xaml

@ -6,7 +6,7 @@
xmlns:local="clr-namespace:SunlightCentralizedControlManagement_SCCM_.View" xmlns:local="clr-namespace:SunlightCentralizedControlManagement_SCCM_.View"
xmlns:lang="clr-namespace:SunlightCentralizedControlManagement_SCCM_.Properties" xmlns:lang="clr-namespace:SunlightCentralizedControlManagement_SCCM_.Properties"
mc:Ignorable="d" mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Background="White"> d:DesignHeight="400" d:DesignWidth="300" Background="White">
<Grid> <Grid>
<TextBlock x:Name="name" VerticalAlignment="Top" Height="50" Background="#FF646464" Text="-----" FontWeight="Bold" FontSize="36" /> <TextBlock x:Name="name" VerticalAlignment="Top" Height="50" Background="#FF646464" Text="-----" FontWeight="Bold" FontSize="36" />
<TextBlock x:Name="temp" VerticalAlignment="Top" Height="50" Text="---.-°C" FontWeight="Bold" FontSize="36" HorizontalAlignment="Right" Padding="5,0,5,0"/> <TextBlock x:Name="temp" VerticalAlignment="Top" Height="50" Text="---.-°C" FontWeight="Bold" FontSize="36" HorizontalAlignment="Right" Padding="5,0,5,0"/>
@ -19,5 +19,6 @@
<local:RoilingTextBlock Foreground="Black" x:Name="Step" Text="NO Information" FontSize="21" Height="30" Margin="120,100,0,0" VerticalAlignment="Top"/> <local:RoilingTextBlock Foreground="Black" x:Name="Step" Text="NO Information" FontSize="21" Height="30" Margin="120,100,0,0" VerticalAlignment="Top"/>
<local:RoilingTextBlock Foreground="Black" x:Name="Message" Text="NO Information" FontSize="21" Height="30" Margin="120,150,0,0" VerticalAlignment="Top"/> <local:RoilingTextBlock Foreground="Black" x:Name="Message" Text="NO Information" FontSize="21" Height="30" Margin="120,150,0,0" VerticalAlignment="Top"/>
<local:RoilingTextBlock Foreground="Black" x:Name="Orders" Text="NO Information" FontSize="21" Height="30" Margin="120,200,0,0" VerticalAlignment="Top"/> <local:RoilingTextBlock Foreground="Black" x:Name="Orders" Text="NO Information" FontSize="21" Height="30" Margin="120,200,0,0" VerticalAlignment="Top"/>
<StackPanel x:Name="log" Margin="0,300,0,0"/>
</Grid> </Grid>
</UserControl> </UserControl>

49
ViewModel/MainWindowViewModel.cs

@ -1,4 +1,5 @@
using OpenTK.Graphics.ES11; using Newtonsoft.Json.Linq;
using OpenTK.Graphics.ES11;
using SunlightCentralizedControlManagement_SCCM_.Properties; using SunlightCentralizedControlManagement_SCCM_.Properties;
using SunlightCentralizedControlManagement_SCCM_.UserClass; using SunlightCentralizedControlManagement_SCCM_.UserClass;
using System; using System;
@ -75,13 +76,13 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel
Sys_Time = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"); Sys_Time = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
} }
void Tick_Event_5S(object sender, EventArgs e)//Tick_Event周期执行事件5S void Tick_Event_5S(object sender, EventArgs e)//Tick_Event周期执行事件5S
{ {
for (int i = 0; i < Machinesdata_Count; i++) for (int i = 0; i < Machinesdata_Count; i++)
{ {
MachiensTcpClient[i].SendAsync("SC800"); // MachiensTcpClient[i].SendAsync("SC800");
} }
} }
void DisTimer_500MS(object sender, EventArgs e)//Tick_Event周期执行事件500MS void DisTimer_500MS(object sender, EventArgs e)//Tick_Event周期执行事件500MS
@ -96,34 +97,33 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel
{ {
SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径 SQLiteHelpers = new SQLiteHelper(DBAddress); //数据库连接路径
SQLiteHelpers.Open(); //打开数据库 SQLiteHelpers.Open(); //打开数据库
Machines = SQLiteHelpers.ExecuteDataSet("select * from Machines ", null).Tables[0]; //读取表写入缓存 Machines = SQLiteHelpers.ExecuteDataSet("select * from Machines Order by id", null).Tables[0]; //读取表写入缓存
SQLiteHelpers.Close(); SQLiteHelpers.Close();
CountDown(); CountDown();
TcpClientNEW(); TcpClientNEW();
} }
private readonly TcpClient[] MachiensTcpClient = new TcpClient[999]; private readonly TcpClient[] MachiensTcpClient = new TcpClient[999];
public int Machinesdata_Count; public int Machinesdata_Count;
public void TcpClientNEW() public async void TcpClientNEW()
{ {
DataRow[] machinesdata = Machines.Select("PORT>0 AND IP<>''");//获取连接有效的组 bool Machine_ = false;
DataRow[] machinesdata = Machines.Select("PORT>0 AND IP<>''", "id asc");//获取连接有效的组
Machinesdata_Count = machinesdata.Count(); Machinesdata_Count = machinesdata.Count();
for (int i = 0; i < Machinesdata_Count; i++) for (int i = 0; i < Machinesdata_Count; i++)
{ {
MachiensTcpClient[i] = new TcpClient(); MachiensTcpClient[i] = new TcpClient();
DataRow dt= machinesdata[i]; DataRow dt = machinesdata[i];
_= AsyncTcpClient.TcpClient(MachiensTcpClient[i] //建立tcp连接 Machine_ = await AsyncTcpClient.TcpClient(MachiensTcpClient[i] //建立tcp连接
, Selet_Machines(MainWindowViewModel.Machines, "IP", Convert.ToInt16( dt["ID"])).ToString() , Selet_Machines(MainWindowViewModel.Machines, "IP", Convert.ToInt16(dt["ID"])).ToString()
, Selet_Machines(MainWindowViewModel.Machines, "PORT", Convert.ToInt16(dt["ID"])).ToString()); , Selet_Machines(MainWindowViewModel.Machines, "PORT", Convert.ToInt16(dt["ID"])).ToString());
}
}
if (!Machine_) Updata_Machines(Machines , Convert.ToInt16(dt["ID"]), "State" ,"309" );
}
}
public static string SYS_WorkNumder; //工单号 public static string SYS_WorkNumder; //工单号
public static int SYS_AT1; //附缸1-3 public static int SYS_AT1; //附缸1-3
@ -152,5 +152,24 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel
} }
} }
public static void Updata_Machines(DataTable DB, int key, string name, string Value)//更新数据
{
try
{
lock (DB)
{
DataRow drEmployee = DB.Select("ID='" + key + "'").First();
drEmployee.BeginEdit();
drEmployee[name] = Value;
drEmployee.EndEdit();
drEmployee.AcceptChanges();
drEmployee.ClearErrors();
}
}
catch (Exception)
{
// LogGing.LogGingDATA("SDTD:" + ex.ToString());
}
}
} }
} }

Loading…
Cancel
Save