hxb
2021-12-21 8b66be08179b026cb0e601733dacd43de97e5b01
HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java
@@ -71,8 +71,8 @@
    private volatile static HDLAuthSocket instance;
    private HDLAuthSocket() {
        initSearchGatewayEvent();
        initListenerGatewayEvent();
        initSearchGatewayEvent();
    }
//    public interface CallBack extends BaseCallBack {
@@ -89,8 +89,6 @@
            synchronized (HDLLinkConfig.class) {
                if (instance == null) {
                    instance = new HDLAuthSocket();
                    //1.启动Socket 开启监听
                    instance.getUdpBoot();
                }
            }
        }
@@ -109,6 +107,7 @@
    /**
     * 获取当前udp对象,如果不存在就创建
     *
     * @return 返回当前对象
     */
    private UdpSocketBoot getUdpBoot() {
@@ -132,6 +131,8 @@
     */
    public void startAuthenticateRequest(AuthenticateRequest request, HDLLinkCallBack callBack) {
        HDLLinkConfig.getInstance().clearConfig();
        //1.启动Socket 开启监听
        getUdpBoot();
        //2.构建监听Listener
//        authEvent =
        //3.监听网关广播的入网指令
@@ -177,30 +178,39 @@
        String requestStr = gs.toJson(request);
        LinkRequest message = new LinkRequest(topic, requestStr);
        HdlSocketHelper.send(getUdpBoot(), InetSocketAddress.createUnresolved(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));
@@ -293,7 +303,7 @@
     * @param callBack 回调
     */
    public void searchGatewayMulticast(SearchGatewayCallBack callBack) {
        searchGateway(HDLLinkConfig.getInstance().getGatewayId(),  InetSocketAddress.createUnresolved(UDP_GROUP_IP,UDP_PORT), callBack);
        searchGateway(HDLLinkConfig.getInstance().getGatewayId(), UDP_GROUP_IP, UDP_PORT, callBack);
    }
    /**
@@ -302,8 +312,14 @@
     * @param callBack 回调
     */
    public void searchGatewayBroadcast(SearchGatewayCallBack callBack) {
        if (!HDLLinkConfig.getInstance().checkIfCertified()) {
            if (callBack != null) {
                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_UNAUTHORIZED_ERROR));
            }
            return;
        }
        String ip = IpUtils.getBroadcastAddress();
        searchGateway(HDLLinkConfig.getInstance().getGatewayId(),  InetSocketAddress.createUnresolved(ip,UDP_PORT), callBack);
        searchGateway(HDLLinkConfig.getInstance().getGatewayId(), ip, UDP_PORT, callBack);
    }
@@ -321,7 +337,7 @@
                GsonConvert.getGson().toJson(data));
        String ip = IpUtils.getBroadcastAddress();
        HdlSocketHelper.send(getUdpBoot(), InetSocketAddress.createUnresolved(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;
@@ -343,18 +359,30 @@
     *
     * @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));
            }
            return;
        }
        if (!HDLLinkConfig.getInstance().checkIfCertified()) {
            if (callBack != null) {
                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_UNAUTHORIZED_ERROR));
            }
            return;
        }
        LinkRequest message = new LinkRequest(topic, bodyStr);
        String ip = HDLLinkConfig.getInstance().getIpAddress(); IpUtils.getBroadcastAddress();
        HdlSocketHelper.send(getUdpBoot(), InetSocketAddress.createUnresolved(ip,UDP_PORT), message, new HdlSocketHelper.HdlSocketListener() {
        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) {
                        if (callBack == null) return;
@@ -375,15 +403,23 @@
     *
     * @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", "参数不能为空");
            return;
        }
        if (!HDLLinkConfig.getInstance().checkIfCertified()) {
            LogUtils.e("udpSendMsg", "未认证,请先认证");
            return;
        }
        LinkRequest message = new LinkRequest(topic, bodyStr);
        String ip = HDLLinkConfig.getInstance().getIpAddress();// IpUtils.getBroadcastAddress();
        getUdpBoot().sendMsg( InetSocketAddress.createUnresolved(ip,UDP_PORT),message.getSendBytes());
        String ip = HDLLinkConfig.getInstance().getIpAddress();
        if(broadcast) {
            ip = IpUtils.getBroadcastAddress();
        }
        HdlSocketHelper.sendUdpOne(getUdpBoot(), ip, UDP_PORT, message);
    }
    private GatewaySearchBean getGatewaySearchBean(Object msg) {
@@ -476,8 +512,8 @@
    }
    private void initListenerGatewayEvent() {
        LogUtils.i( "初始化永久监听网关广播事件,不用移除此事件");
        EventListener  gatewayEvent = new EventListener() {
        LogUtils.i("初始化永久监听网关广播事件,不用移除此事件");
        EventListener gatewayEvent = new EventListener() {
            @Override
            public void onMessage(Object msg) {
                try {
@@ -513,10 +549,11 @@
     * 搜索指定网关是否在线,搜索到则返回指定的网关对象
     *
     * @param gatewayId 网关id
     * @param desInetSocketAddress        目标的地址
     * @param ipAddress 目标的IP地址
     * @param port      目标的端口
     * @param callBack  回调
     */
    public void searchGateway(String gatewayId, InetSocketAddress desInetSocketAddress, SearchGatewayCallBack callBack) {
    public void searchGateway(String gatewayId, String ipAddress, int port, SearchGatewayCallBack callBack) {
        this.searchGatewayId = gatewayId;
        this.mSearchGatewayCallBack = callBack;
        //重置参数
@@ -538,7 +575,7 @@
                        //搜索网关
                        searchGatewayCount.set(searchGatewayCount.get() + 1);
                        LogUtils.i("搜索网关", "搜索网关第" + searchGatewayCount.get() + "次");
                        getUdpBoot().sendMsg(desInetSocketAddress,message.getSendBytes());
                        getUdpBoot().sendMsg(ipAddress, port, message.getSendBytes());
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
@@ -548,12 +585,12 @@
                if (!isSearchGatewaySuccess.get()) {
                    //搜索10次,指定网关都没回复,回调超时
                    callBackSearchGatewayTimeout();
                    LogUtils.e("搜索网关--", "搜索10次,指定网关都没回复,回调超时");
                    LogUtils.e("搜索网关", "搜索10次,指定网关都没回复,回调超时");
                }
            }
        }).start();
    }
    /**
     * 注册搜索网关监听
@@ -586,4 +623,49 @@
        });
    }
    /**
     * 搜索在线所有的网关,包括当前住宅的及没有绑定过其它住宅的网关
     *
     * @param ipAddress 目标的IP地址
     * @param port      目标的端口
     * @param callBack  回调
     */
    public void searchGateway(String ipAddress, int port, SearchGatewayCallBack callBack) {
//        this.searchGatewayId = gatewayId;
//        this.mSearchGatewayCallBack = callBack;
//        //重置参数
//        searchGatewayCount.set(0);
//        isSearchGatewaySuccess.set(false);
        String time = String.valueOf(System.currentTimeMillis());
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("id", IdUtils.getUUId());
        jsonObject.addProperty("time_stamp", time);
        LinkRequest message = new LinkRequest(TopicConstant.GATEWAY_SEARCH,
                jsonObject.toString());
        //注册搜索网关监听
        registerSearchGatewayEvent();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (searchGatewayCount.get() < 10 && (!isSearchGatewaySuccess.get())) {
                    try {
                        //搜索网关
                        searchGatewayCount.set(searchGatewayCount.get() + 1);
                        LogUtils.i("搜索网关", "搜索网关第" + searchGatewayCount.get() + "次");
                        getUdpBoot().sendMsg(ipAddress, port, message.getSendBytes());
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if (!isSearchGatewaySuccess.get()) {
                    //搜索10次,指定网关都没回复,回调超时
                    callBackSearchGatewayTimeout();
                    LogUtils.e("搜索网关", "搜索10次,指定网关都没回复,回调超时");
                }
            }
        }).start();
    }
}