using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
using IStation.Model;
|
using IStation.Untity;
|
|
namespace IStation.DataDockingSocket
|
{
|
public class HandleHelper : IDataDockingSocket.IHandleHelper
|
{
|
static IMonitorDataDockingSession _session;
|
public static IMonitorDataDockingSession GetLastSession()
|
{//关机用的是定时器, 定时器里面需要获取最近的一次链接
|
return _session;
|
}
|
|
|
/// <summary>
|
/// 处理注册码
|
/// </summary>
|
public void HandleRegisterCode(IMonitorDataDockingSession session)
|
{
|
if (session == null)
|
return;
|
if (!session.IsConnected)
|
return;
|
|
if (SessionHelper.Cache != null)
|
{
|
if (SessionHelper.Cache != session)
|
{
|
if (SessionHelper.Cache.IsConnected)
|
{
|
SessionHelper.Cache.Close($"{session.SessionName} 中,产生新的会话!");
|
}
|
}
|
}
|
|
SessionHelper.Cache = session;
|
|
|
foreach (var item in MonitorMsgHelper.GetInstructionQuerys())
|
{
|
SendInstructionJobHelper.StartJob(session, item);
|
if (item.SpaceTime >= 1)
|
Thread.Sleep(item.SpaceTime * 1000);
|
}
|
|
SendControlJobHelper.StartJob(session);
|
}
|
|
/// <summary>
|
/// 处理心跳包
|
/// </summary>
|
public void HandleHeartbeat(IMonitorDataDockingSession session)
|
{
|
_session = session;
|
}
|
|
/// <summary>
|
/// 处理数据
|
/// </summary>
|
public void HandleData
|
(
|
IMonitorDataDockingSession session,
|
byte[] byteMessage,
|
List<DataDockingConfigure.Mapper> mappers,
|
Action<List<MonitorDataDockingReceiveRecord>> onReceive
|
)
|
{
|
if (session == null)
|
return;
|
if (!session.IsConnected)
|
return;
|
if (byteMessage == null || byteMessage.Length < 4)
|
{
|
return;
|
}
|
NtLogHelper.Info("开始处理数据...");
|
_session = session;
|
if (MonitorMsgHelper.IsNeedHandle(byteMessage))
|
{
|
NtLogHelper.Info("MonitorMsgHelper...");
|
//监控指令
|
var receList = MonitorMsgHelper.HandleReceive(byteMessage);
|
if (receList == null || receList.Count == 0)
|
return;
|
|
NtLogHelper.Info("MonitorMsgHelper Over");
|
NtLogHelper.Src(byteMessage);
|
|
onReceive(receList);
|
|
return;//成功就返回掉
|
}
|
//不要加else
|
if (ShutDownMsgHelper.IsNeedHandle(byteMessage))
|
{
|
NtLogHelper.Info("ShutDownMsgHelper...");
|
if (ShutDownMsgHelper.HandleReceive(session, byteMessage))
|
{
|
NtLogHelper.Info("ShutDownMsgHelper Over");
|
return;
|
}
|
}
|
//不要加else
|
if (StartUpMsgHelper.IsNeedHandle(byteMessage))
|
{
|
NtLogHelper.Info("StartUpMsgHelper... ");
|
if (StartUpMsgHelper.HandleReceive(session, byteMessage))
|
{
|
NtLogHelper.Info("StartUpMsgHelper Over");
|
return;
|
}
|
}
|
NtLogHelper.Info("数据处理结束...");
|
|
}
|
|
|
}
|
}
|