JLChen
2020-09-24 e91af284643d5e370b0d18c384fe8de65f59d9b3
Android_HDL_SDK_XW/hdl_core/src/main/java/com/hdl/sdk/hdl_core/HDLDeviceManger/Core/SearchZigbeeGateway.java
@@ -12,7 +12,6 @@
import java.util.TimerTask;
import com.hdl.sdk.hdl_core.HDLDeviceManger.Bean.ZigbeeBean.ZigbeeGatewayInfoBean;
import com.hdl.sdk.hdl_core.HDLDeviceManger.Callback.PushCallback;
import com.hdl.sdk.hdl_core.Util.LogUtil.HDLLog;
@@ -30,13 +29,15 @@
    public static Timer searchTimer = null;
    private static DatagramSocket datagramSocket; // 数据报套接字
    private static boolean isStop = false;
    public interface OnSearchListener{
         void onSuccess( List<ZigbeeGatewayInfoBean> zigbeeGatewayInfoBeanList);
         void onEorror(Exception e);
    public interface OnSearchListener {
        void onSuccess(List<ZigbeeGatewayInfoBean> zigbeeGatewayInfoBeanList);
        void onEorror(Exception e);
    }
    public static void release() {
        if (datagramSocket != null){
        if (datagramSocket != null) {
            isStop = true;
            datagramSocket.disconnect();
            datagramSocket = null;
@@ -58,99 +59,99 @@
            public void run() {
                if (zigbeeGatewayInfoBean.getCount() < 20) {
                    zigbeeGatewayInfoBean.setCount(zigbeeGatewayInfoBean.getCount() + 1);
                            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;
                                    //Log.d(TAG, "sendBytes: "+StringUtil.ByteArrToHex(sendBytes,0,sendBytes.length));
                                    InetAddress address = InetAddress.getByName(NetWorkUtil.getLocalBroadCast());
                                    DatagramPacket packet = new DatagramPacket(
                                            sendBytes,
                                            sendBytes.length,
                                            address, 7624);
                                    datagramSocket= new DatagramSocket();
                                    datagramSocket.send(packet);
                    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;
                        //Log.d(TAG, "sendBytes: "+StringUtil.ByteArrToHex(sendBytes,0,sendBytes.length));
                        InetAddress address = InetAddress.getByName(NetWorkUtil.getLocalBroadCast());
                        DatagramPacket packet = new DatagramPacket(
                                sendBytes,
                                sendBytes.length,
                                address, 7624);
                        datagramSocket = new DatagramSocket();
                        datagramSocket.send(packet);
                                    byte[] receiveBytes = new byte[1024];
                                    DatagramPacket dataPacket = new DatagramPacket(receiveBytes, receiveBytes.length);
                                    datagramSocket.receive(dataPacket);
                                    //是否主网关-01为主网关-00为从网关
                                    String IsDomain = StringUtil.Byte2Hex(dataPacket.getData()[44]);
                                    int headLength = 1 + 2 + 4 + 36 + 1 + 1 + 4 ;
                                    if (receiveBytes.length > headLength) {
                                        //网关名称长度
                                        String str = StringUtil.Byte2Hex(dataPacket.getData()[49]);
                                        int GwNameLength = Integer.parseInt(str,16);
                        byte[] receiveBytes = new byte[1024];
                        DatagramPacket dataPacket = new DatagramPacket(receiveBytes, receiveBytes.length);
                        datagramSocket.receive(dataPacket);
                        //是否主网关-01为主网关-00为从网关
                        String IsDomain = StringUtil.Byte2Hex(dataPacket.getData()[44]);
                        int headLength = 1 + 2 + 4 + 36 + 1 + 1 + 4;
                        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(TAG, "网关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(TAG, "addList:成功添加 ");
                                            }
                                        }
                                    }
                                datagramSocket.close();
                            } catch (Exception e) {
                                Log.d(TAG, "run: "+e.toString());
                                e.printStackTrace();
                                datagramSocket.close();
                                onSearchListener.onEorror(e);
                            //网关名称
                            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(TAG, "网关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(TAG, "addList:成功添加 ");
                                }
                            }
                        }
                        datagramSocket.close();
                    } catch (Exception e) {
                        Log.d(TAG, "run: " + e.toString());
                        e.printStackTrace();
                        datagramSocket.close();
                        onSearchListener.onEorror(e);
                    }
                } else {
                    if (searchTimer != null) {
                        //执行完搜索后到这