You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Aucma.Scada/HighWayIot.TouchSocket/TouchSocketBusiness.cs

144 lines
5.4 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using HighWayIot.Config;
using HighWayIot.TouchSocket.Common;
using System;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Sockets;
namespace HighWayIot.TouchSocket
{
public class TouchSocketBusiness
{
private static readonly Lazy<TouchSocketBusiness> lazy = new Lazy<TouchSocketBusiness>(() => new TouchSocketBusiness());
public static TouchSocketBusiness Instance
{
get { return lazy.Value; }
}
#region 委托事件
/// <summary>
/// 刷新扫码器状态
/// </summary>
/// <param name="materialCodeStr"></param>
/// <param name="ip"></param>
public delegate void RefreshState(string ip, bool flag);
public static event RefreshState RefreshStateEvent;
//NoRead事件通知
public delegate void MessageNoRead();
public static event MessageNoRead MessageNoReadEvent;
//入库扫码
public delegate void RefreshMaterialCodeStr(string materialCodeStr);
public static event RefreshMaterialCodeStr RefreshMaterialCodeStrEvent;
//出库扫码
public delegate void OutMaterialCodeStr(string materialCodeStr);
public static event OutMaterialCodeStr OutMaterialCodeStrEvent;
#endregion
private AppConfig appConfig = AppConfig.Instance;
TcpService service;
public TouchSocketBusiness()
{
}
#region Socket 初始化
public MessageModel<string> StartTouchSocket()
{
MessageModel<string> messageModel = new MessageModel<string>();
service = new TcpService();
service.Connecting = (client, e) => {
client.Logger.Info($"{client.IP}:{client.Port} 客户端正在连接...");
return EasyTask.CompletedTask;
};//有客户端正在连接
service.Connected = (client, e) => {
client.Logger.Info($"{client.IP}:{client.Port} 客户端连接成功!目前客户端连接数{service.Count}");
return EasyTask.CompletedTask;
};//有客户端成功连接
service.Disconnected += (client, e) => { //有客户端断开连接
client.Logger.Info($"{client.IP}:{client.Port}客户端断开!");
RefreshStateEvent?.Invoke(client.IP, false);
return EasyTask.CompletedTask;
};
service.Received = (client, byteBlock) =>
{
try
{
var mes = Encoding.UTF8.GetString(byteBlock.ByteBlock.Buffer, 0, byteBlock.ByteBlock.Len);//注意数据长度是byteBlock.Len
//心跳包
if (mes == "heartbeat")
{
//扫码器心跳连接
client.Logger.Info($"心跳{client.IP}:{client.Port}》接收到心跳信息:{mes}");
RefreshStateEvent?.Invoke(client.IP, true);
}
else if (mes == "NoRead")
{
if (client.IP == appConfig.foamHikRobotIp)
{
client.Logger.Info($"入库扫码器{client.IP}:{client.Port}》NoRead事件{mes}");
MessageNoReadEvent?.Invoke();
}
}
else
{
// ReceiveCodeDelegateEvent?.Invoke(client.IP, mes);
// 入库扫码器
if(client.IP == appConfig.foamHikRobotIp)
{
client.Logger.Info($"入库扫码器{client.IP}:{client.Port}》接收到信息:{mes}");
RefreshMaterialCodeStrEvent?.Invoke(mes.Trim().TrimEnd('\0'));
}else if(client.IP == appConfig.foamOutHikRobotIp)
{
client.Logger.Info($"出库扫码器{client.IP}:{client.Port}》接收到信息:{mes}");
OutMaterialCodeStrEvent?.Invoke(mes.Trim().TrimEnd('\0'));
}
}
}
catch (Exception ex)
{
client.Logger.Error($"异常:{ex.Message}");
}
return EasyTask.CompletedTask;
};
service.Setup(new TouchSocketConfig()//载入配置
.SetListenIPHosts(new IPHost[] { new IPHost($"0.0.0.0:5000") })//可同时监听两个地址
// .SetDataHandlingAdapter(() => { return new NormalDataHandlingAdapter(); })//配置适配器
.SetMaxCount(10000)
//.SetThreadCount(1000)
//.SetCacheTimeoutEnable(false)
.ConfigureContainer(a =>
{
a.AddConsoleLogger();
})
.ConfigurePlugins(a =>
{
a.UseCheckClear();
}));
service.Start();//启动
service.Logger.Info("扫码服务器成功启动");
messageModel.success = true;
messageModel.msg = "OK";
return messageModel;
}
#endregion
}
}