JLChen
2020-06-04 6d55af8792cf8fbef0055e677b900fc352dba9a2
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
using System;
using System.Text;
using System.Collections.Generic;
using Shared;
//1440
namespace SuperGateWay
{
    public class SendAndReceiveDatas
    {
        public static PacketInfo CurrentPacketInfo;
      
 
        /// <summary>
        /// 接收数据的缓冲区
        /// </summary>
        static List<PacketInfo> receiveDataList = new List<PacketInfo> ();//接收分段,接收完毕之后要拼起来
 
        public static void Recevice (byte subnetId, byte deviceId, byte [] bytes)
        {
            var packetInfo = CurrentPacketInfo;
            if (packetInfo == null ) {
                return;
            }
            if (packetInfo.Command != 0) {
                if (packetInfo.Command != bytes [0]) {
                    return;
                } 
            }
 
            if (subnetId != packetInfo.SubnetID || deviceId != packetInfo.DeviceID) return;
            System.Console.WriteLine ("接收到新数据");
            switch (bytes [0]) {
            case 0:
                packetInfo.ResponeBytes = bytes;
                break;
            case 1:
                if (packetInfo.Command != 0 || packetInfo.FileBytes == null) {
                    return;
                }
                uint index = bytes [4] + (uint)bytes [3] * 256 + (uint)bytes [2] * 256 * 256 + (uint)bytes [1] * 256 * 256 * 256;
                uint lenght = bytes [6] + (uint)bytes [5] * 256;
                //如果请求的文件过大,当成无效数据
                if (1024 * 1.5 < lenght) {
                    return;
                }
                //如果请求的数据大于当前文件的长度,就以当前文件的最大长度为请求长度
                if (packetInfo.FileBytes.Length <= index + lenght) {
                    lenght = (uint)packetInfo.FileBytes.Length - index;
                    System.Console.WriteLine ("发送数据完成");
                    packetInfo.IsUploadFinish = true;
                }
                var buffer = new byte [1 + 4 + 2 + lenght];
                buffer [0] = bytes [0];
                buffer [1] = bytes [1];
                buffer [2] = bytes [2];
                buffer [3] = bytes [3];
                buffer [4] = bytes [4];
                buffer [5] = (byte)((lenght >> 8) & 0xFF);
                buffer [6] = (byte)((lenght >> 0) & 0xFF);
                System.Array.Copy (packetInfo.FileBytes, index, buffer, 7, lenght);
                System.Console.WriteLine ("发送数据长度"+lenght);
                Shared.Control.ControlBytesSend (Command.SuperGatewayAgent, subnetId, deviceId, buffer, Shared.SendCount.Zero);
                break;
            case 2: break;
            case 3:
                packetInfo.ResponeBytes = bytes;
                break;
            case 4:
                for (int i = 1; i < 5; i++) {
                    if (bytes [i] != packetInfo.Buffer [i]) {
                        //如果不是当前发送的命令,就反馈回去
                        return;
                    }
                }
                packetInfo.ResponeBytes = bytes;
                break;
            case 5: 
                packetInfo.ResponeBytes = bytes;
              break;
            case 6:
                for (int i = 5; i < packetInfo.Buffer.Length; i++) {
                    if (bytes [i] != packetInfo.Buffer [i]) {
                        //如果不是当前发送的命令,就反馈回去
                        return;
                    }
                }
                packetInfo.ResponeBytes = bytes;
                break;
            case 7: break;
            case 8: break;
            }
        }
 
       
    }
 
    public class PacketInfo
    {
        //  /scene   /device   /logic
 
        /// <summary>
        /// 上传是App传数据到高级网关
        /// 下载是高级网关从App下载文件
        /// 0 通知上传
        /// 1 请求上传
        /// 2 请求上传完毕
        /// 3 通知下载
        /// 4 请求下载
        /// 5 请求下载完毕
        /// 6 浏览文件
        /// 7 获取文件大小
        /// 8 删除文件
        /// </summary>
        public int Command;
 
        public byte [] Buffer = { };//json数据集合,发送的时候要分包发送
 
        [System.NonSerialized]
        //发送数据了之后当前线程等待或者运行的信号
        public System.Threading.ManualResetEvent ManualResetEvent = new System.Threading.ManualResetEvent (false);
        /// <summary>
        /// 回复的数据
        /// </summary>
        public byte [] ResponeBytes;
        public bool IsFinish {
            get {
                return ResponeBytes != null;
            }
        }
        /// <summary>
        /// 重发并次数
        /// </summary>
        public int ReSendCount = 3;
        public byte SubnetID =ScenePage.SubnetID;
        public byte DeviceID= ScenePage.DeviceID;
        public byte [] FileBytes;
        public bool IsUploadFinish;
 
 
 
      
    }
}