@ -1,7 +1,12 @@
using Microsoft.Extensions.Logging ;
using Microsoft.Extensions.Hosting ;
using Microsoft.Extensions.Logging ;
using SlnMesnac.Config ;
using SlnMesnac.TouchSocket.Entity ;
using System ;
using System.Collections.Generic ;
using System.Text ;
using System.Threading ;
using System.Threading.Tasks ;
using TouchSocket.Core ;
using TouchSocket.Sockets ;
@ -29,10 +34,13 @@ using TouchSocket.Sockets;
#endregion << 版 本 注 释 >>
namespace SlnMesnac.TouchSocket
{
public class TcpServer
public class TcpServer : BackgroundService
{
private ILogger < TcpServer > _logger ;
private readonly TcpService _service ;
private readonly AppConfig _appConfig ;
/// <summary>
/// 接收客户端指令委托
/// </summary>
@ -44,28 +52,64 @@ namespace SlnMesnac.TouchSocket
public Action < String , bool > RefreshStateAction ;
public TcpServer ( ILogger < TcpServer > logger , TcpService tcpService )
public delegate void GetVisionData ( TcpVisionEntity entity ) ;
/// <summary>
/// 视觉系统反馈给上位机调度系统状态
/// </summary>
public event GetVisionData ? VisionSysStateEvent ;
/// <summary>
/// 视觉系统回复给上位机调度系统开始工作状态
/// </summary>
public event GetVisionData ? VisionStartWorkEvent ;
/// <summary>
/// 一次码垛完成,发送码垛结果
/// </summary>
public event GetVisionData ? StackWorkDoneEvent ;
/// <summary>
/// 视觉系统发送给机器人码垛位置定位结果
/// </summary>
public event GetVisionData ? StackRobotLocationEvent ;
/// <summary>
/// 人工异常处理完成并更新计数
/// </summary>
public event GetVisionData ? ManualExceptionDealDoneEvent ;
private string ClientID = "" ;
public TcpServer ( ILogger < TcpServer > logger , TcpService tcpService , AppConfig appConfig )
{
_logger = logger ;
_service = tcpService ;
_appConfig = appConfig ;
}
protected override Task ExecuteAsync ( CancellationToken stoppingToken )
{
Init ( _appConfig . TCPVisionConfig ) ;
return Task . CompletedTask ;
}
public void Init ( int serverPort )
public void Init ( string serverPort )
{
try
{
_service . Connecting = ( client , e ) = > {
_logger . LogInformation ( $"客户端{client.IP}正在接入服务" ) ;
return EasyTask . CompletedTask ;
} ;
_service . Connected = ( client , e ) = > {
_service . Connected = ( client , e ) = > {
_logger . LogInformation ( $"客户端{client.IP}接入服务成功" ) ;
RefreshClientInfoEvent ? . Invoke ( _service ) ;
RefreshStateAction ? . Invoke ( client . IP , true ) ;
ClientID = client . Id ;
return EasyTask . CompletedTask ;
} ;
_service . Disconnected = ( client , e ) = > {
_service . Disconnected = ( client , e ) = > {
_logger . LogInformation ( $"客户端{client.IP}断开连接" ) ;
RefreshStateAction ? . Invoke ( client . IP , false ) ;
RefreshClientInfoEvent ? . Invoke ( _service ) ;
@ -82,15 +126,18 @@ namespace SlnMesnac.TouchSocket
{
RefreshStateAction ? . Invoke ( client . IP , true ) ;
}
//var mes = Encoding.UTF8.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意: 数据长度是byteBlock.Len
byte [ ] receivedBuffer = new byte [ e . ByteBlock . Len ] ;
Array . Copy ( e . ByteBlock . Buffer , 0 , receivedBuffer , 0 , e . ByteBlock . Len ) ;
ReceivedClientBufferEvent ? . Invoke ( receivedBuffer ) ;
//ReceivedClientBufferEvent?.Invoke(receivedBuffer);
DataClassify ( BufferDataAnalysis . BufferRootAnalysis ( receivedBuffer ) ) ;
return EasyTask . CompletedTask ;
} ;
_service . Setup ( new TouchSocketConfig ( ) //载入配置
. SetListenIPHosts ( new IPHost [ ] { new IPHost ( $"0.0.0.0:{serverPort}" ) } )
. SetListenIPHosts ( new IPHost [ ] { new IPHost ( serverPort ) } )
. ConfigureContainer ( a = > //容器的配置顺序应该在最前面
{
a . AddConsoleLogger ( ) ;
@ -111,15 +158,37 @@ namespace SlnMesnac.TouchSocket
}
/// <summary>
/// 向所有客户端发送心跳
/// 数据类型区分
/// </summary>
public void SendHeartBeat ( )
/// <param name="entity"></param>
public void DataClassify ( TcpVisionEntity entity )
{
var clients = _service . SocketClients . GetClients ( ) ;
foreach ( var item in clients )
if ( entity = = null )
{
_service . Send ( item . Id , "heartbeat" ) ;
_logger . LogError ( "数据格式校验错误!" ) ;
return ;
}
switch ( entity . Command )
{
case 10 : VisionSysStateEvent ? . Invoke ( entity ) ; break ;
case 11 : VisionStartWorkEvent ? . Invoke ( entity ) ; break ;
case 12 : StackWorkDoneEvent ? . Invoke ( entity ) ; break ;
case 13 : StackRobotLocationEvent ? . Invoke ( entity ) ; break ;
case 14 : ManualExceptionDealDoneEvent ? . Invoke ( entity ) ; break ;
default : _logger . LogInformation ( "未知命令字!" ) ; return ;
}
}
///// <summary>
///// 向所有客户端发送心跳
///// </summary>
//public void SendHeartBeat()
//{
// var clients = _service.SocketClients.GetClients();
// foreach (var item in clients)
// {
// _service.Send(item.Id,"heartbeat");
// }
//}
}
}