1、增加转发到测试服务器的功能
This commit is contained in:
parent
a3b16829a2
commit
3fd43578e8
@ -18,10 +18,8 @@ import java.util.concurrent.TimeUnit;
|
||||
public class TCPClient {
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
//@Value("${xfz.server.host}")
|
||||
private String host;
|
||||
|
||||
//@Value("${xfz.server.port}")
|
||||
private int port;
|
||||
|
||||
private Bootstrap bootstrap;
|
||||
@ -30,7 +28,7 @@ public class TCPClient {
|
||||
LocalDateTime connectTime = LocalDateTime.now();
|
||||
|
||||
public void start() {
|
||||
new Thread(this::connect, "xfz-tcp-client").start();
|
||||
new Thread(this::connect, "forwarder tcp-client").start();
|
||||
}
|
||||
|
||||
public void init(String dest_addr, int dest_port) {
|
||||
@ -64,7 +62,7 @@ public class TCPClient {
|
||||
if (!future.isSuccess()) {
|
||||
//重连交给后端线程执行
|
||||
future.channel().eventLoop().schedule(() -> {
|
||||
logger.info("xfz tcp client reconnect");
|
||||
logger.info("tcp client reconnect");
|
||||
try {
|
||||
connect();
|
||||
} catch (Exception e) {
|
||||
@ -72,7 +70,7 @@ public class TCPClient {
|
||||
}
|
||||
}, 5000, TimeUnit.MILLISECONDS);
|
||||
} else {
|
||||
logger.info("xfz tcp client start success!");
|
||||
logger.info("tcp client start success!");
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -80,7 +78,7 @@ public class TCPClient {
|
||||
this.channel = cf.channel();
|
||||
this.channel.closeFuture().sync();
|
||||
} catch (Exception e) {
|
||||
logger.error("xfz netty client error:", e);
|
||||
logger.error("netty client error:", e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -89,9 +87,9 @@ public class TCPClient {
|
||||
sendBuffer.writeBytes(json.getBytes(StandardCharsets.UTF_8));
|
||||
channel.writeAndFlush(sendBuffer).addListener(future -> {
|
||||
if (future.isSuccess()) {
|
||||
logger.info("send to xfz server succeed.");
|
||||
logger.info("send to tcp:"+host+" succeed.");
|
||||
} else {
|
||||
logger.info("send to xfz server failed.");
|
||||
logger.info("send to tcp:"+host+" failed.");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -0,0 +1,92 @@
|
||||
package com.imdroid.beidou_fwd.task;
|
||||
|
||||
import com.imdroid.beidou_fwd.entity.XFZData;
|
||||
import com.imdroid.beidou_fwd.service.TCPClient;
|
||||
import com.imdroid.common.util.GsonUtil;
|
||||
import com.imdroid.common.util.NumberUtils;
|
||||
import com.imdroid.secapi.dto.GnssCalcData;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
public class SaasForwarder extends Forwarder{
|
||||
private final Logger logger = LoggerFactory.getLogger(SaasForwarder.class);
|
||||
|
||||
static final String FORWARDER_NAME = "测试";
|
||||
@Value("${sass.server.host}")
|
||||
private String host;
|
||||
|
||||
@Value("${sass.server.host}")
|
||||
private int port;
|
||||
|
||||
private TCPClient tcpClient;
|
||||
|
||||
final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
@PostConstruct
|
||||
void registerMe(){
|
||||
init(FORWARDER_NAME, "TCP "+host+":"+port,1,false,30);
|
||||
tcpClient = new TCPClient();
|
||||
tcpClient.init(host, port);
|
||||
tcpClient.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* 每半小时转发GNSS解算结果
|
||||
*/
|
||||
@Scheduled(cron = "0 20,50 * * * ?") // 每30分钟执行一次
|
||||
private void forwardGnss() {
|
||||
logger.info("saas forwardGnss");
|
||||
forwardCurrentGnss();
|
||||
}
|
||||
|
||||
@Override
|
||||
int send(String projectId, List<GnssCalcData> records, LocalDateTime sentTime){
|
||||
int sendNum = 0;
|
||||
if(records.size() == 0) return 0;
|
||||
|
||||
XFZData tcpMessage = new XFZData();
|
||||
tcpMessage.setProjectID(projectId);
|
||||
tcpMessage.setWorkPointID(projectId);
|
||||
|
||||
List<XFZData.Data> dataList = new ArrayList<>(records.size());
|
||||
tcpMessage.setData(dataList);
|
||||
|
||||
for(GnssCalcData locationRecord: records) {
|
||||
XFZData.Data data = new XFZData.Data();
|
||||
dataList.add(data);
|
||||
data.setDataTime(locationRecord.getCreatetime().format(dateFormatter));
|
||||
data.setDevNum(locationRecord.getDeviceid());
|
||||
data.setDevtype("GNSS");
|
||||
// 单位由mm转化为m
|
||||
data.setX(NumberUtils.scale(locationRecord.getRpose() * 0.001, 5));
|
||||
data.setY(NumberUtils.scale(locationRecord.getRposn() * 0.001, 5));
|
||||
data.setZ(NumberUtils.scale(locationRecord.getRposd() * 0.001, 5));
|
||||
sendNum++;
|
||||
}
|
||||
String json = "#" + GsonUtil.toJson(tcpMessage) + "!";
|
||||
logger.info("project " + projectId + ": push calculation result to SAAS");
|
||||
logger.info(json);
|
||||
try {
|
||||
tcpClient.writeAndFlush(json);
|
||||
Thread.sleep(1000);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
return sendNum;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user