wjc
2025-01-07 90d5f028ccdaaaf64286f9d632cb335a4d0544b9
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
package com.hdl.sdk.link.core.connect;
 
 
import android.text.TextUtils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hdl.link.error.ErrorUtils;
import com.hdl.link.error.HDLLinkCode;
import com.hdl.sdk.link.common.event.EventDispatcher;
import com.hdl.sdk.link.common.event.EventListener;
import com.hdl.sdk.link.common.exception.HDLLinkException;
import com.hdl.sdk.link.common.utils.IdUtils;
import com.hdl.sdk.link.common.utils.LogUtils;
import com.hdl.sdk.link.common.utils.gson.GsonConvert;
import com.hdl.sdk.link.core.bean.LinkRequest;
import com.hdl.sdk.link.core.bean.LinkResponse;
import com.hdl.sdk.link.core.bean.RealLinkResponse;
import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
import com.hdl.sdk.link.core.bean.response.BaseLocalResponse;
import com.hdl.sdk.link.core.callback.RealLinkCallBack;
import com.hdl.sdk.link.core.config.HDLLinkConfig;
import com.hdl.sdk.link.gateway.HDLLinkLocalGateway;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * Created by hxb on 2021/12/8.
 * 原生通讯相关接口
 */
public class HDLRealLinkConnect {
 
    private static final String TAG = "HDLRealLinkConnect";
    private static HDLRealLinkConnect instance;
 
    /**
     * 返回当前实例,不存在就创建并同时注册监听事件
     *
     * @return
     */
    public static HDLRealLinkConnect getInstance() {
        if (null == instance) {
            instance = new HDLRealLinkConnect();
            instance.initEventListener();
        }
        return instance;
    }
 
    /**
     * 初始化监听事件
     */
    private void initEventListener() {
        final EventListener eventListener = new EventListener() {
            @Override
            public void onMessage(Object msg) {
                try {
                    if (msg instanceof LinkResponse) {
                        LinkResponse linkResponse = (LinkResponse) msg;
                        //透传数据不处理
                        if (linkResponse.getTopic() == null || linkResponse.getTopic().contains("/custom/native/")) {
                            return;
                        }
 
                        JSONObject jsonObject = JSON.parseObject(linkResponse.getData());
                        String id = jsonObject.getString("id");
                        Integer code = jsonObject.getInteger("code");
                        RealLinkResponse realLinkResponse = new RealLinkResponse();
                        String topic = String.format(linkResponse.getTopic() + "/s%", id);//用topic+信息id表示唯一
                        realLinkResponse.setTopic(topic);
                        realLinkResponse.setData(linkResponse.getData());
                        realLinkResponse.setByteData(linkResponse.getByteData());
                        realLinkResponse.setHdlLinkCode(ErrorUtils.getByCode(code));
                        String oid = linkResponse.getTopic().split("/")[2];
                        //是否是通过主网关透传主题
                        if (linkResponse.getTopic().contains("/slaveoid/")) {
                            oid = linkResponse.getTopic().split("/")[8];
                        }
                        realLinkResponse.setOid(oid);
                        for (GatewayBean gatewayBean : HDLLinkLocalGateway.getInstance().getGatewayList()) {
                            if (oid.equals(gatewayBean.getGatewayId()) || oid.equals(gatewayBean.getDevice_mac()) || oid.equals(gatewayBean.getOid())) {
                                //上面的oid可能是网关id或者mac或者是oid,不管是哪个统一使用oid表示方式
                                realLinkResponse.setOid(gatewayBean.getOid());
                                break;
                            }
                        }
                        EventDispatcher.getInstance().post(topic, realLinkResponse);
                    }
                } catch (Exception e) {
                    LogUtils.e(TAG, "LinkResponse转RealLinkResponse异常:" + e.getMessage());
                }
            }
        };
 
        EventDispatcher.getInstance().registerAllTopicsListener(eventListener);
    }
 
    /**
     * 发送原生数据
     *
     * @param gatewayOidOrGatewayId 目标网关的oid或者网关Id
     * @param payload               发送数据
     * @param realLinkCallBack      结果回调
     */
    public void Send(String gatewayOidOrGatewayId, String topic, String payload, final RealLinkCallBack realLinkCallBack) {
        Send(gatewayOidOrGatewayId, topic, payload, 4, realLinkCallBack);//默认4秒超时
    }
 
    /**
     * 发送原生数据
     *
     * @param gatewayOidOrGatewayId 目标网关的oid或者网关Id
     * @param payload               发送数据
     * @param timeout               超时时间(s)
     * @param realLinkCallBack      结果回调
     */
    public void Send(String gatewayOidOrGatewayId, String topic, String payload, int timeout, final RealLinkCallBack realLinkCallBack) {
 
        if(TextUtils.isEmpty(gatewayOidOrGatewayId)||TextUtils.isEmpty(payload)||TextUtils.isEmpty(payload)) {
            LogUtils.i(TAG, String.format("部分数据为空,gatewayOidOrGatewayId:%s,topic:%s,payload:%s", gatewayOidOrGatewayId, topic, payload));
            realLinkCallBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEND_ERROR));
            return;
        }
 
        String time = String.valueOf(System.currentTimeMillis());
        final BaseLocalResponse<List<JSONObject>> data = new BaseLocalResponse<>();
        data.setId(IdUtils.getUUId());
        data.setTime_stamp(time);
        List<JSONObject> objectList = new ArrayList<>();
        objectList.add(JSONObject.parseObject(payload));
        data.setObjects(objectList);
 
        LinkRequest request = new LinkRequest(topic, GsonConvert.getGson().toJson(data), HDLLinkConfig.getInstance().isLocalEncrypt());
        request.setReplyTopic(String.format(request.getReplyTopic() + "/s%", data.getId()));//用topic+消息id表示唯一
        long awaitTime = timeout * 1000L;
 
        new HDLConnectHelper(awaitTime, 1, HDLLinkConfig.getInstance().getIpAddress(), 8586, request, new HDLConnectHelper.HdlSocketListener() {
            @Override
            public void onSucceed(Object object) {
                if (object instanceof RealLinkResponse) {
                    if (realLinkCallBack != null) {
                        HDLLinkCode hdlLinkCode = ((RealLinkResponse) object).getHdlLinkCode();
                        Integer code = hdlLinkCode.getCode();
                        //有的话只有200才会成功
                        if (code.intValue() == 200 || code.intValue() == 0) {
                            realLinkCallBack.onSuccess((RealLinkResponse) object);
                        } else {
                            realLinkCallBack.onError(HDLLinkException.getErrorWithCode(hdlLinkCode));
                        }
                    }
                } else {
                    LogUtils.e(TAG, "发送Link回调对象类型非数组类型,类型是" + object.getClass());
                }
            }
            @Override
            public void onFailure(HDLLinkCode hdlLinkCode) {
                if (realLinkCallBack != null) {
                    realLinkCallBack.onError(HDLLinkException.getErrorWithCode(hdlLinkCode));
                }
            }
        }, true, gatewayOidOrGatewayId).send();
    }
}