using IStation.Untity;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace IStation.DataDockingSocket
|
{
|
internal class ShutDownMsgHelper
|
{
|
public static eShutDownInstructionStatus InstructionStatus = eShutDownInstructionStatus.未发送;
|
public static DateTime _lastSendTime ;
|
/// <summary>
|
/// 开始关机
|
/// </summary>
|
/// <param name="session"></param>
|
/// <returns></returns>
|
public static bool StartJob(Model.IMonitorDataDockingSession session)
|
{
|
if (session == null)
|
{
|
NtLogHelper.Error($"关机 session:Close");
|
return false;
|
}
|
|
if (!session.IsConnected)
|
{
|
NtLogHelper.Error($"关机时 session:Not connected");
|
return false;
|
}
|
|
byte[] bts = GetControlMsg();
|
|
session.Send(bts, 0, bts.Length);
|
InstructionStatus = eShutDownInstructionStatus.关机指令发送;
|
_lastSendTime = DateTime.Now;
|
|
|
|
NtLogHelper.Debug("关机指令发送:" + session.SessionName + ":" + BitTransfer.ToString(bts) );
|
|
return true;
|
}
|
|
/// <summary>
|
/// 自动关闭
|
/// </summary>
|
static System.Timers.Timer _timeAutoClose;
|
public static void StartAutoClose(double second)
|
{
|
try
|
{
|
NtLogHelper.Info("开启自动关机模式 时间:" + second + "s");
|
_timeAutoClose = new System.Timers.Timer(1000 * second);//实例化Timer类,设置间隔时间为10000毫秒;
|
_timeAutoClose.Elapsed += new System.Timers.ElapsedEventHandler(ExecuteAutoClose);//到达时间的时候执行事件;
|
_timeAutoClose.AutoReset = false;//设置是执行一次(false)还是一直执行(true);
|
_timeAutoClose.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
|
_timeAutoClose.Start(); //启动定时器
|
NtLogHelper.Info("_timeAutoClose.Start()");
|
}
|
catch (Exception ex)
|
{
|
NtLogHelper.Error("_timeAutoClose:" + ex.Message);
|
}
|
}
|
|
private static void ExecuteAutoClose(object source, System.Timers.ElapsedEventArgs e)
|
{
|
if (_timeAutoClose == null)
|
return;
|
try
|
{
|
NtLogHelper.Info("ExecuteAutoClose");
|
_timeAutoClose.Stop(); //先关闭定时器
|
NtLogHelper.Info(" _timeAutoClose.Stop()");
|
_timeAutoClose = null;
|
//要执行的业务代码
|
NtLogHelper.Info("正在自动关机....");
|
StartJob(HandleHelper.GetLastSession());
|
}
|
catch (Exception ex)
|
{
|
NtLogHelper.Error(ex.Message);
|
}
|
|
// t.Start(); //执行完毕后再开启器
|
}
|
/// <summary>
|
/// 是否是控制指令
|
/// </summary>
|
/// <param name="byteMessage"></param>
|
/// <returns></returns>
|
public static bool IsControlMsg(byte[] byteMessage)
|
{
|
return false;
|
}
|
|
/// <summary>
|
/// 关机指令
|
/// </summary>
|
static string ControlMsg = "01-06-00-10-00-01-49-CF";
|
|
/// <summary>
|
/// 获取控制指令(关机指令)
|
/// </summary>
|
/// <returns></returns>
|
public static byte[] GetControlMsg()
|
{
|
// var appParas = AppParasHelper.GetInstance();
|
// if (appParas == null || appParas.InstructionStartUp == null || appParas.InstructionStartUp.Content == null)
|
{
|
return BitTransfer.FromString(ControlMsg);
|
}
|
// return BitTransfer.FromString(appParas.InstructionStartUp.Content);
|
}
|
|
/// <summary>
|
/// 是否需要当前类进行处理
|
/// </summary>
|
/// <param name="byteMessage"></param>
|
/// <returns></returns>
|
public static bool IsNeedHandle(byte[] byteMessage)
|
{
|
if (InstructionStatus == eShutDownInstructionStatus.关机指令发送)
|
return true ;
|
|
return false;
|
}
|
/// <summary>
|
/// 读取返回消息 处理成功返回成功, 不成功或者不需要处理返回false
|
/// </summary>
|
/// <param name="byteMessage"></param>
|
public static bool HandleReceive(Model.IMonitorDataDockingSession session,byte[] byteMessage)
|
{
|
if (InstructionStatus == eShutDownInstructionStatus.未发送)
|
return false ;
|
if (InstructionStatus == eShutDownInstructionStatus.关机指令发送)
|
{
|
if (byteMessage.Length >= ControlMsg.Length)
|
{
|
string strMessage = BitConverter.ToString(byteMessage, 0, byteMessage.Length);
|
if (strMessage == ControlMsg)
|
{
|
InstructionStatus = eShutDownInstructionStatus.未发送;
|
|
NtLogHelper.Debug("关机指令返回:" + BitTransfer.ToString(byteMessage) );
|
|
if(_timeAutoClose != null)
|
{
|
_timeAutoClose.Stop(); //先关闭定时器
|
_timeAutoClose = null;
|
}
|
|
return true ;
|
}
|
}
|
//返回失败, 再发送一次再确认一下
|
if((DateTime.Now- _lastSendTime).TotalSeconds > 3)
|
{
|
StartJob(session);
|
}
|
}
|
|
return false;
|
}
|
|
}
|
}
|