hxb
2022-03-27 cc6023291bc94a229b53d7c2d83bf2b39bbfa9a0
HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java
@@ -68,7 +68,7 @@
    /**
     * instance
     */
    private volatile static HDLAuthSocket instance;
    private  static final HDLAuthSocket instance=new HDLAuthSocket();
    private HDLAuthSocket() {
        initListenerGatewayEvent();
@@ -84,14 +84,14 @@
     *
     * @return HDLAuthSocket
     */
    public static synchronized HDLAuthSocket getInstance() {
        if (instance == null) {
            synchronized (HDLLinkConfig.class) {
                if (instance == null) {
                    instance = new HDLAuthSocket();
                }
            }
        }
    public  static HDLAuthSocket getInstance() {
//        if (instance == null) {
//            synchronized (HDLAuthSocket.class) {
//        if (instance == null) {
//            instance = new HDLAuthSocket();
//        }
//            }
//        }
        return instance;
    }
@@ -110,14 +110,14 @@
     *
     * @return 返回当前对象
     */
    private UdpSocketBoot getUdpBoot() {
    private synchronized UdpSocketBoot getUdpBoot() {
        try {
            if (udpSocketBoot == null) {
                udpSocketBoot = UdpClient.init(UDP_PORT, getUdpOptions());
                udpSocketBoot.bind();
            }
        } catch (Exception e) {
            return null;
//            return null;
        }
        return udpSocketBoot;
@@ -141,15 +141,15 @@
            public void onMessage(Object msg) {
                NetworkAccessBroadcastResponse bean = getNetworkAccessBroadcastResponse(msg);
                if (bean != null) {
                    LogUtils.i(TAG, "网关入网广播IP: " + bean.getIPAddress());
                    //移除监听
                    EventDispatcher.getInstance().remove(TopicConstant.GATEWAY_AUTH_BROADCAST);
                    LogUtils.i("网关入网广播IP: " + bean.getIPAddress());
                    String ipStr = bean.getIPAddress();
                    if (!TextUtils.isEmpty(ipStr)) {
                        sendAuthenticateRequest(ipStr, request, callBack);
                    }
                }
                //移除监听
                EventDispatcher.getInstance().remove(TopicConstant.GATEWAY_AUTH_BROADCAST);
                LogUtils.i(TAG, "移除监听 authEvent");
            }
        });
    }
@@ -178,30 +178,39 @@
        String requestStr = gs.toJson(request);
        LinkRequest message = new LinkRequest(topic, requestStr);
        HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message,new HdlSocketHelper.HdlSocketListener() {
        HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() {
            @Override
            public void onSucceed(Object msg) {
                if (callBack == null) return;
                try {
                    AuthenticateResponse bean = getAuthenticateResponseBean(msg);
                    if (bean != null) {
                        String localSecret = "";
                        String gatewayId = "";
                        String ipAddress = "";
                        if (bean.getAuth() != null) {
                            localSecret = bean.getAuth().getLocalSecret();
                        }
                        if (bean.getObjects() != null) {
                            gatewayId = bean.getObjects().getGatewayID();
                            ipAddress = bean.getObjects().getIPAddress();
                        }
                        //判断网关是否已经注册到云端
                        if (TextUtils.isEmpty(localSecret) || TextUtils.isEmpty(gatewayId)) {
                            //认证失败,网关未注册到云端
                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR_GATEWAY_NOT_REGISTERED));
                        } else {
                            HDLLinkConfig.getInstance().saveConfig(localSecret, gatewayId, ipAddress);
                            callBack.onSuccess("认证成功");
                        if (bean.getCode().equals("200")) {
                            String localSecret = "";
                            String gatewayId = "";
                            String ipAddress = "";
                            if (bean.getAuth() != null) {
                                localSecret = bean.getAuth().getLocalSecret();
                            }
                            if (bean.getObjects() != null) {
                                gatewayId = bean.getObjects().getGatewayID();
                                ipAddress = bean.getObjects().getIPAddress();
                            }
                            //判断网关是否已经注册到云端
                            if (TextUtils.isEmpty(localSecret) || TextUtils.isEmpty(gatewayId)) {
                                //认证失败,网关未注册到云端
                                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR_GATEWAY_NOT_REGISTERED));
                            } else {
                                HDLLinkConfig.getInstance().saveConfig(localSecret, gatewayId, ipAddress);
                                callBack.onSuccess("认证成功");
                            }
                        } else if (bean.getCode().equals("14013")){
                            //认证失败,该MAC对应的设备密钥不存在
                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_MAC_KEY_ERROR_));
                        } else{
                            //认证失败,错误码:
                            LogUtils.e("认证失败,错误码:" + bean.getCode());
                            callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR));
                        }
                    } else {
                        callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_ERROR));
@@ -219,6 +228,7 @@
            }
        });
    }
//    /**
//     * 发送入网及认证请求
@@ -350,9 +360,10 @@
     *
     * @param topic    发送数据
     * @param bodyStr  回复的主题
     * @param broadcast 是否要广播
     * @param callBack 回调
     */
    public void udpSendMsg(String topic, String bodyStr, HDLLinkResponseCallBack callBack) {
    public void udpSendMsg(String topic, String bodyStr,boolean broadcast,  HDLLinkResponseCallBack callBack) {
        if (TextUtils.isEmpty(topic) || TextUtils.isEmpty(bodyStr)) {
            if (callBack != null) {
                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_NULL_ERROR));
@@ -369,6 +380,9 @@
        LinkRequest message = new LinkRequest(topic, bodyStr);
        String ip = HDLLinkConfig.getInstance().getIpAddress();
        if(broadcast) {
            ip = IpUtils.getBroadcastAddress();
        }
        HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() {
                    @Override
                    public void onSucceed(Object msg) {
@@ -390,18 +404,22 @@
     *
     * @param topic   发送数据
     * @param bodyStr 回复的主题
     * @param broadcast 是否广播
     */
    public void udpSendMsg(String topic, String bodyStr) {
    public void udpSendMsg(String topic, String bodyStr,boolean broadcast) {
        if (TextUtils.isEmpty(topic) || TextUtils.isEmpty(bodyStr)) {
            LogUtils.e("udpSendMsg", "参数不能为空");
            LogUtils.e( "参数不能为空,不能发送UDP数据");
            return;
        }
        if (!HDLLinkConfig.getInstance().checkIfCertified()) {
            LogUtils.e("udpSendMsg", "未认证,请先认证");
            LogUtils.e("未认证,请先认证再调用UDP发送方法");
            return;
        }
        LinkRequest message = new LinkRequest(topic, bodyStr);
        String ip = HDLLinkConfig.getInstance().getIpAddress();// IpUtils.getBroadcastAddress();
        String ip = HDLLinkConfig.getInstance().getIpAddress();
        if(broadcast) {
            ip = IpUtils.getBroadcastAddress();
        }
        HdlSocketHelper.sendUdpOne(getUdpBoot(), ip, UDP_PORT, message);
    }
@@ -474,12 +492,12 @@
                            }.getType());
                            GatewaySearchBean searchBean = response.getObjects();
                            if (searchBean != null && !TextUtils.isEmpty(searchBean.getGatewayId())) {
                                if (searchBean.getGatewayId().contains(searchGatewayId)) {
                                searchBean.setIp_address(linkResponse.getSource_ipAddress());
                                if (searchBean.getGatewayId().contains(searchGatewayId)&&!TextUtils.isEmpty(searchGatewayId)) {
                                    removeSearchGatewayEvent();//移除搜索网关监听
                                    isSearchGatewaySuccess.set(true);//搜索成功标记
                                    searchGatewayCount.set(11);//次数标记
                                    HDLLinkConfig.getInstance().setCurrentGateway(searchBean);//设置当前网关
                                    HDLLinkConfig.getInstance().setLocalEncrypt(searchBean.isLocalEncrypt());//设置是否加密
                                    if (mSearchGatewayCallBack != null) {
                                        mSearchGatewayCallBack.onSuccess(searchBean);
                                    }
@@ -509,11 +527,14 @@
                            }.getType());
                            GatewaySearchBean gateway = response.getObjects();
                            if (gateway != null && !TextUtils.isEmpty(gateway.getGatewayId())) {
                                //可能网关带过来的ip不对
                                gateway.setIp_address(linkResponse.getSource_ipAddress());
                                //主网关并且是当前绑定的网关
                                if ("true".equals(gateway.getMaster().toLowerCase()) && gateway.getGatewayId().equals(HDLLinkConfig.getInstance().getGatewayId())) {
                                    HDLLinkConfig.getInstance().setCurrentGateway(gateway);//设置当前网关
                                    HDLLinkConfig.getInstance().setLocalEncrypt(gateway.isLocalEncrypt());//设置是否加密
                                    HDLLinkConfig.getInstance().setIpAddress(gateway.getIp_address());
                                    if(!TextUtils.isEmpty( gateway.getIp_address())) {
                                        HDLLinkConfig.getInstance().setIpAddress(gateway.getIp_address());
                                    }
                                    //更新当前网关的信息
                                    HDLLinkConfig.getInstance().reSaveConfig();
                                }
@@ -566,9 +587,11 @@
                }
                if (!isSearchGatewaySuccess.get()) {
                    //搜索10次,指定网关都没回复,回调超时
                    callBackSearchGatewayTimeout();
                    LogUtils.e("搜索网关", "搜索10次,指定网关都没回复,回调超时");
                    try {
                        LogUtils.e("搜索网关", "搜索10次,指定网关都没回复,回调超时");
                        //搜索10次,指定网关都没回复,回调超时
                        callBackSearchGatewayTimeout();
                    }catch (Exception e){}
                }
            }
        }).start();