ningshuxia
2022-10-27 2b6eeda2c91f0e570b58b2799758475b46c1ad61
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
using IStation.DataDockingSocket.MsgHandle;
using IStation.Untity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.DataDockingSocket 
{
    internal class ShunDownMsgHelper
    {
        public static eShutDownInstructionStatus  InstructionStatus = eShutDownInstructionStatus.未发送;
        public static DateTime sendTime  ;
        /// <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)
            {
                byte[] bts = GetControlMsg();
 
                session.Send(bts, 0, bts.Length);
                InstructionStatus = eShutDownInstructionStatus.关机机指令发送;
                sendTime = DateTime.Now;
                NtLogHelper.Debug("关机指令发送:" + session.SessionName + ":" + BitTransfer.ToString(bts) + ", 发送一条请求控制指令");
 
                return true;
            }
            else
            {
                NtLogHelper.Error($"关机时 session:Not connected");
                return false;
            }
 
        }
 
 
        /// <summary>
        /// 是否是控制指令
        /// </summary>
        /// <param name="byteMessage"></param>
        /// <returns></returns>
        public static bool IsControlMsg(byte[] byteMessage)
        {
  
      
            return false;
        }
 
        /// <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("01-06-00-10-00-01-49-CF");
            }
          //  return BitTransfer.FromString(appParas.InstructionStartUp.Content);
        }
 
        /// <summary>
        /// 读取返回消息 
        /// </summary>
        /// <param name="byteMessage"></param>
        public static void HandleReceive(Model.IMonitorDataDockingSession session,byte[] byteMessage)
        {
            if (InstructionStatus == eShutDownInstructionStatus.未发送)
                return;
            if (InstructionStatus == eShutDownInstructionStatus.关机机指令发送)
            {
                if (byteMessage.Length == 8)
                {
                    if (byteMessage[0] == 0x01 && byteMessage[1] == 0x06 && byteMessage[2] == 0x00
        && byteMessage[3] == 0x10 && byteMessage[4] == 0x00 && byteMessage[5] == 0x01
        && byteMessage[6] == 0x49 && byteMessage[7] == 0xCF)
                    {//01-06-00-0F-00-01-78-09
                        InstructionStatus = eShutDownInstructionStatus.未发送;
 
                        NtLogHelper.Debug("关机指令返回:" + BitTransfer.ToString(byteMessage)  );
 
                        return;
                    }
                }
                //返回失败, 再发送一次再确认一下
                if((DateTime.Now- sendTime).TotalSeconds > 3)
                {
                    StartJob(session);
                }
               
            }
    
          
        }
       
    }
}