| | |
| | | */
|
| | | public class HDLConnectHelper {
|
| | |
|
| | | private static final String TAG="HDLConnectHelper";
|
| | | private static final String TAG = "HDLConnectHelper";
|
| | | private static final Long DEF_SEND_TIMEOUT = 8000L;
|
| | | private static final int DEF_MAX_RETRY = 1;//最大重发数
|
| | | private static final int DEF_SEND_ONE = 1;
|
| | |
| | |
|
| | | private String replyTopic;
|
| | |
|
| | | private boolean useSubThread=false;
|
| | | private boolean useSubThread = false;
|
| | |
|
| | | public interface HdlSocketListener {
|
| | | void onSucceed(Object msg);
|
| | |
| | |
|
| | | public HDLConnectHelper(Long sendAwaitTime, int maxRetry, String ipAddress, int port,
|
| | | LinkRequest linkRequest, HdlSocketListener listener, boolean isTcp) {
|
| | | this(sendAwaitTime,maxRetry,ipAddress,port,linkRequest,listener,isTcp,false);
|
| | | this(sendAwaitTime, maxRetry, ipAddress, port, linkRequest, listener, isTcp, false);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 发送UDP或者TCP数据
|
| | | *
|
| | |
| | | * @param isTcp 是否TCP
|
| | | */
|
| | | public HDLConnectHelper(Long sendAwaitTime, int maxRetry, String ipAddress, int port,
|
| | | LinkRequest linkRequest, HdlSocketListener listener, boolean isTcp,boolean useSubThread) {
|
| | | LinkRequest linkRequest, HdlSocketListener listener, boolean isTcp, boolean useSubThread) {
|
| | | this.sendAwaitTime = sendAwaitTime;
|
| | | this.maxRetry = maxRetry;
|
| | | this.ipAddress = ipAddress;
|
| | |
| | | this.replyTopic = linkRequest.getReplyTopic();
|
| | | this.listener = listener;
|
| | | this.isTcp = isTcp;
|
| | | this.useSubThread=useSubThread;
|
| | | this.useSubThread = useSubThread;
|
| | |
|
| | |
|
| | | eventListener = new EventListener() {
|
| | |
| | | LinkResponse linkResponse = (LinkResponse) object;
|
| | | JSONObject jsonObject = JSON.parseObject(linkResponse.getData());
|
| | | String id = null;
|
| | | Integer code=null;
|
| | | if(jsonObject!=null) {
|
| | | Integer code = null;
|
| | | if (jsonObject != null) {
|
| | | id = jsonObject.getString("id");
|
| | | code = jsonObject.getInteger("code");
|
| | | }
|
| | |
| | | notifySucceed(object);
|
| | | } else if (object instanceof BusProResponse) {
|
| | | notifySucceed(object);
|
| | | } else if(object instanceof RealLinkResponse){
|
| | | } else if (object instanceof RealLinkResponse) {
|
| | | //真实的link数据,不包含透传的原生数据
|
| | | notifySucceed(object);
|
| | | }
|
| | | else {
|
| | | } else {
|
| | | notifyFailure(new HDLLinkCode(HDLLinkCode.HDL_OBJECT_NOT_SUPPORT.getCode(), "Object Name:" + object));
|
| | | }
|
| | | } catch (Exception e) {
|
| | |
| | | */
|
| | | public HDLConnectHelper(Long sendAwaitTime, int maxRetry, String ipAddress, int port,
|
| | | LinkRequest linkRequest, HdlSocketListener listener, boolean isTcp, String mac) {
|
| | | this(sendAwaitTime, maxRetry, ipAddress, port, linkRequest, listener, isTcp,false);
|
| | | this(sendAwaitTime, maxRetry, ipAddress, port, linkRequest, listener, isTcp, false);
|
| | | this.mac = mac;
|
| | | }
|
| | |
|
| | | public HDLConnectHelper(Long sendAwaitTime, int maxRetry, String ipAddress, int port,
|
| | | LinkRequest linkRequest, HdlSocketListener listener, boolean isTcp, String mac,boolean subThread) {
|
| | | this(sendAwaitTime, maxRetry, ipAddress, port, linkRequest, listener, isTcp,subThread);
|
| | | LinkRequest linkRequest, HdlSocketListener listener, boolean isTcp, String mac, boolean subThread) {
|
| | | this(sendAwaitTime, maxRetry, ipAddress, port, linkRequest, listener, isTcp, subThread);
|
| | | this.mac = mac;
|
| | | }
|
| | |
|
| | |
| | | */
|
| | | private void registerListener() {
|
| | | if (!TextUtils.isEmpty(replyTopic) && null != listener) {
|
| | | if(useSubThread){//使用子线程回调
|
| | | if (useSubThread) {//使用子线程回调
|
| | | EventDispatcher.getInstance().asyncRegister(replyTopic, eventListener);
|
| | | }
|
| | | else {
|
| | | } else {
|
| | | //默认用主线程
|
| | | EventDispatcher.getInstance().register(replyTopic, eventListener);
|
| | | }
|
| | |
| | |
|
| | | public void send() {
|
| | |
|
| | | scheduledFuture= getSendThread().scheduleWithFixedDelay(new Runnable() {
|
| | | scheduledFuture = getSendThread().scheduleWithFixedDelay(new Runnable() {
|
| | | @Override
|
| | | public void run() {
|
| | | try {
|
| | |
| | |
|
| | | //如是tcp或者mqtt
|
| | | if (isTcp) {
|
| | | //BMS设备支持远程发送命令,不支持本地命令,听说后面会调整也是本地发送
|
| | | GatewayBean gateway = HDLLinkLocalGateway.getInstance().getLocalGateway(mac);
|
| | | boolean isSupportLocalSendCommands = gateway != null && gateway.isSupportLocalSendCommands();
|
| | | //mqtt
|
| | | if (TextUtils.isEmpty(ipAddress) || !HDLTcpConnect.getTcpSocketBoot(ipAddress).isConnected()) {
|
| | | if (TextUtils.isEmpty(ipAddress) || !HDLTcpConnect.getTcpSocketBoot(ipAddress).isConnected() || !isSupportLocalSendCommands) {
|
| | | if (!linkRequest.getTopic().endsWith("heartbeat")) {//心跳主题数据过多,过滤下
|
| | | //LogUtils.i("心跳包发送数据:\r\n" + new String(linkRequest.getCloudSendBytes()));
|
| | | } else {
|
| | |
| | | }
|
| | | }
|
| | | }
|
| | | }catch (Exception e){
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | |
| | | private void notifyFailure(HDLLinkCode hdlLinkCode) {
|
| | | //移除监听
|
| | | removeListener();
|
| | | if(scheduledFuture!=null){
|
| | | if (scheduledFuture != null) {
|
| | | scheduledFuture.cancel(false);
|
| | | }
|
| | | // if (sendThread != null) {
|
| | |
| | | }
|
| | |
|
| | |
|
| | |
|
| | | // /**
|
| | | // * 支持毫米类型
|
| | | // *
|
| | |
| | | // */
|
| | | // public static List<String> getGatewayTypeList() {
|
| | | // List<String> typeList = new ArrayList<>();
|
| | | //// typeList.add("sensor.mmv_sleep");//睡眠毫米波spk
|
| | | //// typeList.add("sensor.mmv_pose");//姿态毫米波spk
|
| | |
|
| | | /// / typeList.add("sensor.mmv_sleep");//睡眠毫米波spk
|
| | | /// / typeList.add("sensor.mmv_pose");//姿态毫米波spk
|
| | | // typeList.add("energy.hdl_inverter");//逆变器spk
|
| | | // typeList.add("sensor.mmv_sleep");//睡眠毫米波spk
|
| | | // typeList.add("sensor.mmv_pose");//姿态毫米波spk
|
| | |
| | |
|
| | | /**
|
| | | * 获取除了网关的其它网络设备,上面写的那两个方法不一致getGatewayTypeList,getMillimeterTypeList,统一了下,以免后期出问题
|
| | | *
|
| | | * @return
|
| | | */
|
| | | public static List<String> getNotGatewayTypeList(){
|
| | | public static List<String> getNotGatewayTypeList() {
|
| | | List<String> typeList = new ArrayList<>();
|
| | | typeList.add("energy.hdl_inverter");//逆变器spk
|
| | | typeList.add("sensor.mmv_sleep");//睡眠毫米波spk
|
| | | typeList.add("sensor.mmv_pose");//姿态毫米波spk
|
| | | typeList.add("sensor.hdl_mmw_pose");//Wi-Fi毫米波ZT版本
|
| | | typeList.add("sensor.hdl_mmw_sleep");//Wi-Fi毫米波睡眠版
|
| | | return typeList;
|
| | | return typeList;
|
| | | }
|
| | |
|
| | | public static List<String> getNewMillimeterTypeList() {
|
| | |
| | | private void notifySucceed(Object msg) {
|
| | | //移除监听
|
| | | removeListener();
|
| | | if(scheduledFuture!=null){
|
| | | if (scheduledFuture != null) {
|
| | | scheduledFuture.cancel(false);
|
| | | }
|
| | | // if (sendThread != null) {
|
| | |
| | | listener = null;
|
| | | }
|
| | | }
|
| | |
|
| | |
|
| | |
|
| | | public static boolean isInverterTopic(String topic) {
|