JLChen
2021-12-10 739d5bad0d24c5f4913f8d8e09292b5b98991fac
HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java
@@ -99,7 +99,9 @@
        pipeLine.add(new LinkMessageDecoder());
        pipeLine.add(new LinkMessageEncoder());
        options.setHandleMessage(pipeLine);
        options.setEnabledHeartbeat(false);
        options.setHeartbeatTimeInterval(10*1000L);
        options.setHeartbeatData("UDP");
        options.setEnabledHeartbeat(true);//是否开启心跳包发送检测
        return options;
    }
@@ -170,20 +172,31 @@
            @Override
            public void onSucceed(Object msg) {
                if (callBack == null) return;
                AuthenticateResponse bean = getAuthenticateResponseBean(msg);
                if (bean != null) {
                    String localSecret = bean.getAuth().getLocalSecret();
                    String gatewayId = bean.getObjects().getGatewayID();
                    String ipAddress = bean.getObjects().getIPAddress();
                    //判断网关是否已经注册到云端
                    if (TextUtils.isEmpty(localSecret) || TextUtils.isEmpty(gatewayId)) {
                        //认证失败,网关未注册到云端
                        callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR_GATEWAY_NOT_REGISTERED));
                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("认证成功");
                        }
                    } else {
                        HDLLinkConfig.getInstance().saveConfig(localSecret, gatewayId, ipAddress);
                        callBack.onSuccess("认证成功");
                        callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_ERROR));
                    }
                } else {
                } catch (Exception e) {
                    callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_ERROR));
                }
            }
@@ -285,8 +298,6 @@
    }
    /**
     * 设备控制
     */
@@ -350,6 +361,21 @@
        );
    }
    /**
     * 通用发送指令 只发一次,不监听回复,不重发
     *
     * @param topic   发送数据
     * @param bodyStr 回复的主题
     */
    public void udpSendMsg(String topic, String bodyStr) {
        if (TextUtils.isEmpty(topic) || TextUtils.isEmpty(bodyStr)) {
            LogUtils.e("udpSendMsg", "参数不能为空");
            return;
        }
        LinkRequest message = new LinkRequest(topic, bodyStr);
        String ip = IpUtils.getBroadcastAddress();
        getUdpBoot(ip).sendMsg(message.getSendBytes());
    }
    private GatewaySearchBean getGatewaySearchBean(Object msg) {
        GatewaySearchBean searchBean = null;
@@ -399,13 +425,15 @@
    /**
     * 网关搜索相关
     */
    private static final int MAX_SEARCH_COUNT  = 10;//总共搜索测试
    private final AtomicInteger searchGatewayCount = new AtomicInteger(0);;
    private static final int MAX_SEARCH_COUNT = 10;//总共搜索测试
    private final AtomicInteger searchGatewayCount = new AtomicInteger(0);
    ;
    private final AtomicBoolean isSearchGatewaySuccess = new AtomicBoolean(true);
    private String searchGatewayId = "";
    private SearchGatewayCallBack mSearchGatewayCallBack;
    private void initSearchGatewayEvent(){
        LogUtils.i("搜索网关--","initSearchGatewayEvent");
    private void initSearchGatewayEvent() {
        LogUtils.i("搜索网关--", "initSearchGatewayEvent");
        searchGatewayEvent = new EventListener() {
            @Override
            public void onMessage(Object msg) {
@@ -423,6 +451,8 @@
                                    removeSearchGatewayEvent();//移除搜索网关监听
                                    isSearchGatewaySuccess.set(true);//搜索成功标记
                                    searchGatewayCount.set(11);//次数标记
                                    HDLLinkConfig.getInstance().setCurrentGateway(searchBean);//设置当前网关
                                    HDLLinkConfig.getInstance().setLocalEncrypt(searchBean.isLocalEncrypt());//设置是否加密
                                    if (mSearchGatewayCallBack != null) {
                                        mSearchGatewayCallBack.onSuccess(searchBean);
                                    }
@@ -461,11 +491,11 @@
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (searchGatewayCount.get() < 10 && (!isSearchGatewaySuccess.get()) ) {
                while (searchGatewayCount.get() < 10 && (!isSearchGatewaySuccess.get())) {
                    try {
                        //搜索网关
                        searchGatewayCount.set(searchGatewayCount.get() + 1);
                        LogUtils.i("搜索网关--","搜索网关第"+searchGatewayCount.get()+"次");
                        LogUtils.i("搜索网关--", "搜索网关第" + searchGatewayCount.get() + "次");
                        getUdpBoot(ip).sendMsg(message.getSendBytes());
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
@@ -473,10 +503,10 @@
                    }
                }
                if(!isSearchGatewaySuccess.get()){
                if (!isSearchGatewaySuccess.get()) {
                    //搜索10次,指定网关都没回复,回调超时
                    callBackSearchGatewayTimeout();
                    LogUtils.e("搜索网关--","搜索10次,指定网关都没回复,回调超时");
                    LogUtils.e("搜索网关--", "搜索10次,指定网关都没回复,回调超时");
                }
            }
        }).start();
@@ -486,27 +516,28 @@
    /**
     * 注册搜索网关监听
     */
    private void registerSearchGatewayEvent(){
        LogUtils.i("搜索网关--","注册搜索网关监听");
    private void registerSearchGatewayEvent() {
        LogUtils.i("搜索网关--", "注册搜索网关监听");
        EventDispatcher.getInstance().registerIo(TopicConstant.GATEWAY_SEARCH_REPLY, searchGatewayEvent);
    }
    /**
     * 移除搜索网关监听
     */
    private void removeSearchGatewayEvent(){
        LogUtils.i("搜索网关--","移除搜索网关监听");
    private void removeSearchGatewayEvent() {
        LogUtils.i("搜索网关--", "移除搜索网关监听");
        EventDispatcher.getInstance().remove(TopicConstant.GATEWAY_SEARCH_REPLY, searchGatewayEvent);
    }
    /**
     * 回调搜索网关超时
     */
    private void callBackSearchGatewayTimeout(){
    private void callBackSearchGatewayTimeout() {
        removeSearchGatewayEvent();
        ThreadToolUtils.getInstance().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(mSearchGatewayCallBack != null){
                if (mSearchGatewayCallBack != null) {
                    mSearchGatewayCallBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEARCH_GATEWAY_TIMEOUT_ERROR));
                }
            }