JLChen
2020-06-04 9c2506577fc035855f8e23ac8b3f8fcab8c09eb5
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.hdl.sdk.hdl_sdk.activity;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
 
import com.hdl.sdk.hdl_core.Config.Configuration;
import com.hdl.sdk.hdl_core.HDLDeviceManger.Bean.ZigbeeBean.ZigbeeGatewayInfoBean;
import com.hdl.sdk.hdl_core.Util.LogUtil.HDLLog;
import com.hdl.sdk.hdl_core.Util.NetUtil.NetWorkUtil;
import com.hdl.sdk.hdl_core.Util.TransformUtil.StringUtil;
import com.hdl.sdk.hdl_sdk.R;
 
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
 
public class TextUdpActivity extends AppCompatActivity {
    private static DatagramSocket datagramSocket7624 = null;
    public static List<ZigbeeGatewayInfoBean> zigbeeGatewayInfoBeanList = new ArrayList<>();
    public static Timer searchTimer = null;
    private static boolean isStop = false;
    Button button;
    public static void release() {
        isStop = true;
        datagramSocket7624.disconnect();
        datagramSocket7624 = null;
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_udp);
        button = findViewById(R.id.button1);
        init7624();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SearchGateWay();
            }
        });
    }
 
    private static void SearchGateWay() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    byte[] sendBytes = new byte[44];
                    sendBytes[0] = (byte) 0xFE;
                    sendBytes[1] = 0x29;
                    sendBytes[2] = 0;
                    sendBytes[3] = 0;
                    sendBytes[4] = 0;
                    sendBytes[5] = 0;
                    sendBytes[6] = 0;
                    //获取所有网关(无需houseID模式),后面要改
                    sendBytes[7] = 0x67;
                    sendBytes[8] = 0x65;
                    sendBytes[9] = 0x74;
                    sendBytes[10] = 0x61;
                    sendBytes[11] = 0x6c;
                    sendBytes[12] = 0x6c;
                    sendBytes[13] = 0x67;
                    sendBytes[14] = 0x77;
                    for (int i = 15; i<sendBytes.length-1;i++){
                        sendBytes[i] = 0;
                    }
                    sendBytes[sendBytes.length - 1] = 0x02;
                    DatagramSocket socket = new DatagramSocket();
                    InetAddress address = InetAddress.getByName(NetWorkUtil.getLocalBroadCast());
                    DatagramPacket packet = new DatagramPacket(
                            sendBytes,
                            sendBytes.length,
                            address, Configuration.ZIGBEE_PORT);
                    socket.send(packet);
                    socket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
 
    }
 
    /**
     * 初始化7624端口
     */
    public static void init7624(){
        if (datagramSocket7624 != null){
            HDLLog.info( "HDL SDK 7624 已启动");
        }else {
            HDLLog.info( "HDLSocket 7624 init");
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        datagramSocket7624 = new DatagramSocket(Configuration.ZIGBEE_PORT);
                        while (!isStop && datagramSocket7624 != null) {
                            datagramSocket7624.setReceiveBufferSize(1024 * 20);
                            datagramSocket7624.setReuseAddress(true);
                            while (datagramSocket7624 != null){
                            byte[] receiveBytes = new byte[1024];
                            DatagramPacket dataPacket = new DatagramPacket(receiveBytes, receiveBytes.length);
                            if (datagramSocket7624 != null || (!datagramSocket7624.isClosed())) {
                                 datagramSocket7624.receive(dataPacket);
                                }
                            Log.d("11114", "初始化端口接收的数据为receiveBytes: "+StringUtil.ByteArrToHex(receiveBytes,0,receiveBytes.length));
                            handleData(receiveBytes,dataPacket);
                            }
                        }
                    } catch (Exception e) {
                        datagramSocket7624 = null;
                        return;
                    }
                }
            }).start();
        }
    }
 
    private static void handleData(byte[] receiveBytes,DatagramPacket dataPacket) {
        if (receiveBytes[45] ==0){
            return;
        }
        Log.d("11114", "处理的数据为receiveBytes: "+StringUtil.ByteArrToHex(receiveBytes,0,receiveBytes.length));
        //是否主网关-01为主网关-00为从网关
        String IsDomain = StringUtil.Byte2Hex(dataPacket.getData()[44]);
        int headLength = 49;
        if (receiveBytes.length > headLength) {
            //网关名称长度
            String str = StringUtil.Byte2Hex(dataPacket.getData()[49]);
            int GwNameLength = Integer.parseInt(str, 16);
 
            //网关名称
            byte[] gwnamebyte = new byte[GwNameLength];
            for (int i = 0; i < GwNameLength; i++) {
                gwnamebyte[i] = dataPacket.getData()[i + 50];
            }
            String GwName = new String(gwnamebyte, 0, gwnamebyte.length);
 
            //网关id长度
            String str1 = StringUtil.Byte2Hex(dataPacket.getData()[50 + GwNameLength]);
            int GwIdLength = Integer.parseInt(str1, 16);
 
            //获取网关id
            byte[] gatewayId = new byte[GwIdLength];
            for (int i = 0; i < GwIdLength; i++) {
                gatewayId[i] = dataPacket.getData()[i + 51 + GwNameLength];
            }
 
            if (gatewayId.length > 1) {
                String stringGateWayId = new String(gatewayId, 0, gatewayId.length);
                Log.d("11114", "网关id: " + stringGateWayId);
 
                ZigbeeGatewayInfoBean zigbeeGatewayInfoBean = new ZigbeeGatewayInfoBean();
                zigbeeGatewayInfoBean.setGatewayId(stringGateWayId);
                zigbeeGatewayInfoBean.setIpAddress(dataPacket.getAddress().getHostAddress());
                if (IsDomain.equals("01")){
                    zigbeeGatewayInfoBean.setDomain(true);
                }else {
                    zigbeeGatewayInfoBean.setDomain(false);
                }
                zigbeeGatewayInfoBean.setGatewayName(GwName);
                boolean isExit = false;
                for (int i = 0; i < zigbeeGatewayInfoBeanList.size(); i++) {
                    if (zigbeeGatewayInfoBeanList.get(i).getGatewayId()
                            .equals(zigbeeGatewayInfoBean.getGatewayId())) {
                        isExit = true;
                        break;
                    }
                }
                if (!isExit) {
                    zigbeeGatewayInfoBeanList.add(zigbeeGatewayInfoBean);
                    Log.d("11114", "addList:成功添加 ");
                }
            }
        }
    }
}