ningshuxia
2022-10-28 3ccb7c60e1ed8b6748ed7fb8b64b1dbe50d62abf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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;
        }
       
    }
}