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();
|
}
|
}
|