| | |
| | | import com.hdl.sdk.connect.protocol.LinkMessageEncoder;
|
| | | import com.hdl.sdk.socket.SocketBoot;
|
| | | import com.hdl.sdk.socket.SocketOptions;
|
| | | import com.hdl.sdk.socket.client.IHeartbeat;
|
| | | import com.hdl.sdk.socket.client.TcpClient;
|
| | | import com.hdl.sdk.socket.codec.MessagePipeLine;
|
| | | import com.hdl.sdk.socket.listener.ConnectStatusListener;
|
| | |
| | | //如果没有初始化,或者网关IP更改了,就重新初始化
|
| | | if (tcpBoot == null||!getTcpOptions().getIp().equals(getTcpIp())) {
|
| | | tcpBoot = TcpClient.init(getTcpIp(), getTcpPort(), getTcpOptions());
|
| | | tcpBoot.SetHeartbeat(new IHeartbeat() {
|
| | | @Override
|
| | | public void heartbeat() {
|
| | |
|
| | | if(HDLLinkConfig.getInstance().getDeviceInfoBean()==null||HDLLinkConfig.getInstance().getDeviceInfoBean().getOID()==null) {
|
| | | LogUtils.i("DeviceInfoBean为空,需要设置才能正常心跳");
|
| | | return;
|
| | | }
|
| | |
|
| | | String time = String.valueOf(System.currentTimeMillis());
|
| | | JsonObject jsonObject = new JsonObject();
|
| | | jsonObject.addProperty("id", IdUtils.getUUId());
|
| | | jsonObject.addProperty("time_stamp", time);
|
| | |
|
| | | String topic = String.format(TopicConstant.HEARTBEAT, HDLLinkConfig.getInstance().getDeviceInfoBean().getOID());
|
| | |
|
| | | LinkRequest message = new LinkRequest(topic,
|
| | | jsonObject.toString());
|
| | | sendMsg(message.getSendBytes(), null, null, null);
|
| | | }
|
| | | });
|
| | | }
|
| | |
|
| | | return tcpBoot;
|
| | |
| | | public void sendMsg(byte[] data, String eventTag, HDLLinkCallBack callBack, SendListener sendListener) {
|
| | |
|
| | | try {
|
| | | if(eventTag!=null) {
|
| | | final AtomicInteger sendCount = new AtomicInteger(0);
|
| | |
|
| | | final ScheduledExecutorService threadPool = ThreadToolUtils.getInstance().newScheduledThreadPool(1);
|
| | |
| | | }
|
| | | }, 1000, 500, TimeUnit.MILLISECONDS);
|
| | | EventDispatcher.getInstance().register(eventTag, eventListener);
|
| | | }
|
| | | //先发送一次
|
| | | getTcp().sendMsg(data, new SendListener() {
|
| | | @Override
|