package com.hdl.sdk.link.core.connect; import android.text.TextUtils; import com.hdl.link.error.HDLLinkCode; import com.hdl.sdk.link.common.config.TopicConstant; 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.LogUtils; import com.hdl.sdk.link.core.bean.LinkRequest; import com.hdl.sdk.link.core.bean.LinkResponse; import com.hdl.sdk.link.core.bean.ModbusResponse; import com.hdl.sdk.link.core.bean.gateway.GatewayBean; import com.hdl.sdk.link.core.callback.ModbusCallBack; import com.hdl.sdk.link.core.config.HDLLinkConfig; import com.hdl.sdk.link.core.utils.ByteUtils; import com.hdl.sdk.link.gateway.HDLLinkLocalGateway; /** * Created by hxb on 2021/12/8. * 原生通讯相关接口 */ public class HDLModBusConnect { private static final String TAG = "HDLModbusConnect"; private static HDLModBusConnect instance; /** * 内部用,主要是处理处理掉透传主题及link主题后,还原modbus原生数据及主题用 */ private final String allTopic = "/Modbus"; /** * 返回当前实例,不存在就创建并同时注册监听事件 * * @return */ public static HDLModBusConnect getInstance() { if (null == instance) { instance = new HDLModBusConnect(); instance.initEventListener(); } return instance; } /** * 注册监听Zigbee所有原生主题及数据 * * @param eventListener */ public void registerListener(EventListener eventListener) { if (null == eventListener) { return; } EventDispatcher.getInstance().register(allTopic, eventListener); } /** * 移除监听Zigbee原生主题及数据 * * @param eventListener */ public void removeListener(EventListener eventListener) { if (null == eventListener) { return; } EventDispatcher.getInstance().remove(allTopic, eventListener); } /** * 初始化监听事件 */ 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/inverter/up")) { return; } byte[] payload = linkResponse.getByteData(); ModbusResponse modbusResponse = new ModbusResponse(); String topic; if(10"); } return; } //如果本地有链接这个网关,则用本地连接 GatewayBean gatewayBean = HDLLinkLocalGateway.getInstance().getGatewayByOidOrGatewayId(gatewayOidOrGatewayId); if (null == gatewayBean) { LogUtils.i("找不到网关,Oid是" + gatewayOidOrGatewayId); if (null != baseCallBack) { baseCallBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GATEWAY_NOT_EXIST)); } return; } String tempTopic = String.format(TopicConstant.NATIVE_MODBUS_DOWN, gatewayOidOrGatewayId); LinkRequest request = new LinkRequest(tempTopic, payload, HDLLinkConfig.getInstance().isLocalEncrypt()); request.setCloudTopic(String.format(TopicConstant.NATIVE_MODBUS_DOWN,gatewayBean.getGatewayId())); if(10