高支模代码-未完成

This commit is contained in:
Administrator 2023-03-08 19:34:48 +08:00
parent d0cb93bf9f
commit c5e500b94e
7 changed files with 541 additions and 1 deletions

View File

@ -0,0 +1,108 @@
package com.zhgd.xmgl.modules.highformwork.netty.tcp.constant;
/**
* 高支模常量
*/
public interface HighFormworkSupport {
public static final String TCP_DATA_PREFIX = "$LRKKJ$ ";
public static final String TCP_DATA_END = "END";
/**
* 设备类型
*/
public static final String DEV_TYPE = "DEV_type";
/**
* 设备编号
*/
public static final String DEV_BH = "DEV_BH";
/**
* 定位信息
*/
public static final String GPS_DEV_J = "GPS_DEV_J";
/**
* 定位信息
*/
public static final String GPS_DEV_W = "GPS_DEV_W";
/**
* 工程编号
*/
public static final String TR_BH = "TR_BH";
/**
* 上传编号
*/
public static final String SC_BH = "SC_BH";
/**
* 测点总数
*/
public static final String CD_AL = "CD_AL";
/**
* 采样频率
*/
public static final String JC_T = "JC_T";
/**
* 上传频率
*/
public static final String SC_T = "SC_T";
/**
* 开始采集时间
*/
public static final String TIME = "TIME";
/**
* 终端编号
*/
public static final String DEV_SN = "DEV_sn";
/**
* 测点编号
*/
public static final String SN_CJ = "SN_CJ";
/**
* 测点初始值
*/
public static final String GZ_CS = "GZ_CS";
/**
* 测点报警值
*/
public static final String GZ_BJ = "GZ_BJ";
/**
* 测点预警值
*/
public static final String GZ_YJ = "GZ_YJ";
/**
* 测点坐标
*/
public static final String GZ_ZB = "GZ_ZB";
/**
* 测点编号
*/
public static final String GZ_CJ = "GZ_CJ";
/**
* 测点数据
*/
public static final String GZ_DS = "GZ_DS";
/**
* 测点状态
*/
public static final String GZ_ZT = "GZ_ZT";
/**
* 测点编号1
*/
public static final String GZ_CJ1 = "GZ_CJ1";
/**
* 测点数据1
*/
public static final String GZ_DS1 = "GZ_DS1";
/**
* 测点状态1
*/
public static final String GZ_ZT1 = "GZ_ZT1";
/**
* 状态
*/
public static final String GZ_GZT = "GZ_GZT";
/**
* 结束时间
*/
public static final String JS_T = "JS_T";
}

View File

@ -0,0 +1,78 @@
package com.zhgd.xmgl.modules.highformwork.netty.tcp.handler;
import com.zhgd.xmgl.modules.highformwork.netty.tcp.constant.HighFormworkSupport;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
/**
* Created by chenws on 2020/3/24.
*/
@ChannelHandler.Sharable
@Slf4j
@Component
public class TcpNettyHandler extends SimpleChannelInboundHandler<String> {
@Autowired
private HighFormworkSupportService highFormworkSupportService;
@Override
public void channelRead0(ChannelHandlerContext ctx, String msg) {
log.info("I get the message >>> \r\n {} ", msg);
//把消息写到缓冲区
ctx.writeAndFlush("ok");
writeToFile(msg);
//接收高支模数据保存到mysql中
msg = msg.trim();
if (StringUtils.startsWith(msg, HighFormworkSupport.TCP_DATA_PREFIX) && StringUtils.endsWith(msg, HighFormworkSupport.TCP_DATA_END)) {
try {
highFormworkSupportService.saveTcpData(msg);
} catch (Exception e) {
log.error("接收高支模数据保存到mysql中exception >>> \r\n ", e);
} finally {
highFormworkSupportResponseOk(ctx);
}
}
}
private void highFormworkSupportResponseOk(ChannelHandlerContext ctx) {
ctx.writeAndFlush("$LRKKJ$ MSG:\"\"; OK;END");
}
private void writeToFile(String msg) {
File file = new File("C:\\Users\\Administrator\\Desktop\\data.txt");
String ENTER = "\n";
try {
FileUtils.writeStringToFile(file, LocalDateTime.now() + ENTER, true);
FileUtils.writeStringToFile(file, msg + ENTER, true);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
//刷新缓冲区把消息发出去
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
//异常处理如果该handler不处理将会传递到下一个handler
cause.printStackTrace();
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush("connect success!");
}
}

View File

@ -0,0 +1,21 @@
package com.zhgd.xmgl.modules.highformwork.netty.tcp.listener;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
/**
* @author chenws
* @date 2020/03/17 15:58:10
*/
public class BindListener implements GenericFutureListener<Future<? super Void>> {
@Override
public void operationComplete(Future future) throws Exception {
if (future.isSuccess()) {
System.out.println("绑定成功");
} else {
//打印错误信息也可自定义处理
future.cause().printStackTrace();
}
}
}

View File

@ -0,0 +1,19 @@
package com.zhgd.xmgl.modules.highformwork.netty.tcp.listener;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
/**
* @author chenws
* @date 2020/03/17 16:01:15
*/
public class CloseListener implements GenericFutureListener<Future<? super Void>> {
@Override
public void operationComplete(Future future) throws Exception {
if (future.isSuccess()) {
System.out.println("关闭成功");
} else {
future.cause().printStackTrace();
}
}
}

View File

@ -0,0 +1,95 @@
package com.zhgd.xmgl.modules.highformwork.netty.tcp.server;
import com.zhgd.xmgl.modules.highformwork.netty.tcp.handler.TcpNettyHandler;
import com.zhgd.xmgl.modules.highformwork.netty.tcp.listener.BindListener;
import com.zhgd.xmgl.modules.highformwork.netty.tcp.listener.CloseListener;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @author chenws
* @date 2020/03/17 15:45:35
*/
@Component
public class TcpNettyServer {
@Autowired
private TcpNettyHandler tcpNettyHandler;
@Autowired
private StringEncoder stringEncoder;
@Autowired
private StringDecoder stringDecoder;
@Autowired
private BindListener bindListener;
@Autowired
private CloseListener closeListener;
@Value("${high_formwork.netty.port}")
int port;
@Bean
public StringEncoder getStringEncoder() {
return new StringEncoder();
}
@Bean
public StringDecoder getStringDecoder() {
return new StringDecoder();
}
@Bean
public BindListener getBindListener() {
return new BindListener();
}
@Bean
public CloseListener getCloseListener() {
return new CloseListener();
}
@PostConstruct
private void startTcpServer() {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(6);
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
//添加自定义处理器
socketChannel.pipeline()
.addLast(stringEncoder)
.addLast(stringDecoder)
.addLast(tcpNettyHandler);
}
});
//监听器当服务绑定成功后执行
ChannelFuture channelFuture = serverBootstrap.bind(port).sync().addListener(bindListener);
//监听器当停止服务后执行
channelFuture.channel().closeFuture().sync().addListener(closeListener);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}

View File

@ -0,0 +1,217 @@
package com.zhgd.xmgl.modules.highformwork.netty.tcp.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhgd.xmgl.modules.highformwork.mapper.HighFormworkMeasureDeviceMapper;
import com.zhgd.xmgl.modules.highformwork.netty.tcp.constant.HighFormworkSupport;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@Component
@Slf4j
public class HighFormworkSupportService {
//@Autowired
//private HighFormworkSupportDeviceDataMapper highFormworkSupportDeviceDataMapper;
//@Autowired
//private HighFormworkSupportDeviceMapper highFormworkSupportDeviceMapper;
//@Autowired
//private HighFormworkSupportMeasurePointMapper highFormworkSupportMeasurePointMapper;
//@Autowired
//private HighFormworkSupportUploadMapper highFormworkSupportUploadMapper;
//@Autowired
//private IHighFormworkSupportDeviceDataService highFormworkSupportDeviceDataService;
@Autowired
private HighFormworkMeasureDeviceMapper highFormworkMeasureDeviceMapper;
public void saveTcpData(String msg) throws ParseException {
/*
设备类型 DEV_type LKR_GZM
设备编号 DEV_BH 数组字母组合
定位信息 GPS_DEV_JGPS_DEV_W 单位°浮点数值
工程编号 TR_BH 字符串
上传编号 SC_BH 字符串
测点总数 CD_AL 数字
采样频率 JC_T 数字单位秒
上传频率 SC_T 数字单位秒
开始采集时间 TIME: Yyyy-mm-dd hhmmss
帧尾 END
工程信息
$LRKKJ$ DEV_type:LRK_GZM;DEV_BH:1;GPS_DEV_J:0;GPS_DEV_W:0;TR_BH:001;CD_AL:6;SC_BH:20200711;JC_T:2;SC_T:10;TIME:2020-07-11 144713;END
1次
设备编号 DEV_BH 字符串
工程编号 TR_BH 字符串
终端编号 DEV_sn 字符串
测点编号 SN_CJ 字符串
测点初始值 GZ_CS
测点报警值 GZ_BJ
测点预警值 GZ_YJ
测点坐标 GZ_ZB x,y x,y坐标原点为工程图左上角位置,坐标为像素
测点信息
$LRKKJ$ DEV_BH:1;TR_BH:001;DEV_sn:0001;SN_CJ:0001RL;GZ_CS:0.000000;GZ_BJ:5.000000;GZ_YJ:1.000000;GZ_ZB:0,0;END
$LRKKJ$ DEV_BH:1;TR_BH:001;DEV_sn:0001;SN_CJ:00011#F;GZ_CS:0.000000;GZ_BJ:5.000000;GZ_YJ:1.000000;GZ_ZB:0,0;END
1次
设备类型 DEV_type LRK_GZM
设备编号 DEV_BH 字符串
终端编号 DEV_sn 字符串
工程编号 TR_BH 字符串
上传编号 SC_BH 字符串
测点编号1 GZ_CJ1 字符串
测点数据1 GZ_DS1
测点状态1 GZ_ZT1
测点编号2 GZ_CJ2
测点数据2 GZ_DS2
测点状态2 GZ_ZT2
测点编号3 GZ_CJ3
测点数据3 GZ_DS3
测点状态3 GZ_ZT3
采集时间 TIME Yyyy-mm-dd hhmmss
测点数据
$LRKKJ$ DEV_type:LRK_GZM;DEV_BH:1;DEV_sn:0001;TR_BH:001;SC_BH:20200711;GZ_CJ1:0001RL;GZ_DS1:0.000000;GZ_ZT1:2;GZ_CJ2:00011#F;GZ_DS2:0.000000;GZ_ZT2:2;GZ_CJ3:00012#V;GZ_DS3:0.000000;GZ_ZT3:2;TIME:2020-07-11 144722;END
$LRKKJ$ DEV_type:LRK_GZM;DEV_BH:;DEV_sn:0002;TR_BH:001;SC_BH:20200711;GZ_CJ1:0002RL;GZ_DS1:0.000000;GZ_ZT1:2;GZ_CJ2:00021#F;GZ_DS2:0.000000;GZ_ZT2:2;GZ_CJ3:00022#V;GZ_DS3:0.000000;GZ_ZT3:2;TIME:2020-07-11 144722;END
多次
设备类型 DEV_type LRK_GZM
设备编号 DEV_BH 字符串
工程编号 TR_BH 字符串
上传编号 SC_BH 字符串
状态 GZ_GZT 123 1.正常完成2.异常结束3.暂停采样
结束时间 JS_T Yyyy-mm-dd hhmmss
结束信息以暂停监测为例
$LRKKJ$ DEV_type:LRK_GZM;DEV_BH:1;TR_BH:001;SC_BH:20200711;GZ_GZT:3;JS_T:2020-07-11144736;END
*/
String data = StringUtils.substringBetween(msg, HighFormworkSupport.TCP_DATA_PREFIX, HighFormworkSupport.TCP_DATA_END);
String[] dataStrMap = data.split(";");
HashMap<String, String> dataMap = new HashMap<>();
for (String onePair : dataStrMap) {
String[] one = onePair.split(":");
dataMap.put(one[0], one[1]);
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmmss");
log.info("saveTcpData() >>> \r\n {}", dataMap);
LambdaQueryWrapper<HighFormworkSupportDevice> deviceLambdaQueryWrapper = new LambdaQueryWrapper<HighFormworkSupportDevice>()
.eq(HighFormworkSupportDevice::getDevBh, dataMap.get(HighFormworkSupport.DEV_BH))
.eq(HighFormworkSupportDevice::getTrBh, dataMap.get(HighFormworkSupport.TR_BH));
if (dataMap.keySet().stream().anyMatch(HighFormworkSupport.GPS_DEV_J::equals)) {
log.info("saveTcpData() >>> \r\n {}", "高支模设备-工程信息");
HighFormworkSupportDevice highFormworkSupportDevice = new HighFormworkSupportDevice();
highFormworkSupportDevice.setDevType(dataMap.get(HighFormworkSupport.DEV_TYPE));
highFormworkSupportDevice.setDevBh(dataMap.get(HighFormworkSupport.DEV_BH));
highFormworkSupportDevice.setGpsDevJ(Double.valueOf(dataMap.get(HighFormworkSupport.GPS_DEV_J)));
highFormworkSupportDevice.setGpsDevW(Double.valueOf(dataMap.get(HighFormworkSupport.GPS_DEV_W)));
highFormworkSupportDevice.setTrBh(dataMap.get(HighFormworkSupport.TR_BH));
HighFormworkSupportDevice device = highFormworkSupportDeviceMapper.selectOne(deviceLambdaQueryWrapper);
//高支模设备-工程信息
if (device == null) {
highFormworkSupportDeviceMapper.insert(highFormworkSupportDevice);
} else {
highFormworkSupportDeviceMapper.update(highFormworkSupportDevice, deviceLambdaQueryWrapper);
highFormworkSupportDevice.setId(device.getId());
}
HighFormworkSupportUpload upload = new HighFormworkSupportUpload();
upload.setHighFormworkSupportDeviceId(highFormworkSupportDevice.getId());
upload.setScBh(dataMap.get(HighFormworkSupport.SC_BH));
upload.setCdAl(Integer.valueOf(dataMap.get(HighFormworkSupport.CD_AL)));
upload.setJcT(Integer.valueOf(dataMap.get(HighFormworkSupport.JC_T)));
upload.setScT(Integer.valueOf(dataMap.get(HighFormworkSupport.SC_T)));
upload.setTime(sdf.parse(dataMap.get(HighFormworkSupport.TIME)));
upload.setDevSn(dataMap.get(HighFormworkSupport.DEV_TYPE));
highFormworkSupportUploadMapper.insert(upload);
} else if (dataMap.keySet().stream().anyMatch(HighFormworkSupport.GZ_CS::equals)) {
log.info("saveTcpData() >>> \r\n {}", "高支模测点信息");
HighFormworkSupportDevice device = highFormworkSupportDeviceMapper.selectOne(deviceLambdaQueryWrapper
);
//高支模测点信息
HighFormworkSupportMeasurePoint point = new HighFormworkSupportMeasurePoint();
point.setHighFormworkSupportDeviceId(device != null ? device.getId() : null);
point.setDevSn(dataMap.get(HighFormworkSupport.DEV_SN));
point.setSnCj(dataMap.get(HighFormworkSupport.SN_CJ));
point.setGzCs(dataMap.get(HighFormworkSupport.GZ_CS));
point.setGzBj(dataMap.get(HighFormworkSupport.GZ_BJ));
point.setGzYj(dataMap.get(HighFormworkSupport.GZ_YJ));
String coord = dataMap.get(HighFormworkSupport.GZ_ZB);
point.setGzZbX(Integer.valueOf(coord.split(",")[0]));
point.setGzZbY(Integer.valueOf(coord.split(",")[1]));
highFormworkSupportMeasurePointMapper.insert(point);
} else if (dataMap.keySet().stream().anyMatch(s -> s.contains(HighFormworkSupport.GZ_CJ))) {
log.info("saveTcpData() >>> \r\n {}", "高支模设备采集数据");
//高支模设备采集数据
HashMap<String, HashMap<String, String>> pointMapByNo = getPointMapByNo(dataMap);
ArrayList<HighFormworkSupportDeviceData> dataArrayList = new ArrayList<>();
for (Map.Entry<String, HashMap<String, String>> entry : pointMapByNo.entrySet()) {
HashMap<String, String> map = entry.getValue();
HighFormworkSupportDeviceData deviceData = new HighFormworkSupportDeviceData();
deviceData.setGzCj(map.get(HighFormworkSupport.GZ_CJ));
deviceData.setGzDs(map.get(HighFormworkSupport.GZ_DS));
deviceData.setGzZt(map.get(HighFormworkSupport.GZ_ZT));
deviceData.setTime(sdf.parse(dataMap.get(HighFormworkSupport.TIME)));
deviceData.setScBh(dataMap.get(HighFormworkSupport.SC_BH));
deviceData.setHighFormworkSupportUploadId(highFormworkSupportDeviceMapper.getUploadIdByInfo(dataMap));
dataArrayList.add(deviceData);
}
highFormworkSupportDeviceDataService.saveBatch(dataArrayList);
} else if (dataMap.keySet().stream().anyMatch(HighFormworkSupport.GZ_GZT::equals)) {
log.info("saveTcpData() >>> \r\n {}", "高支模结束信息");
//高支模结束信息
Integer uploadIdByInfo = highFormworkSupportDeviceMapper.getUploadIdByInfo(dataMap);
HighFormworkSupportUpload upload = new HighFormworkSupportUpload();
upload.setJsT(sdf.parse(dataMap.get(HighFormworkSupport.JS_T)));
upload.setGzGzt(Integer.valueOf(dataMap.get(HighFormworkSupport.GZ_GZT)));
upload.setId(uploadIdByInfo);
highFormworkSupportUploadMapper.updateById(upload);
}
}
private boolean existDevice(HashMap<String, String> dataMap) {
HighFormworkSupportDevice device = highFormworkSupportDeviceMapper.selectOne(new LambdaQueryWrapper<HighFormworkSupportDevice>()
.eq(HighFormworkSupportDevice::getDevBh, dataMap.get(HighFormworkSupport.DEV_BH))
.eq(HighFormworkSupportDevice::getTrBh, dataMap.get(HighFormworkSupport.TR_BH)));
return device != null;
}
private HashMap<String, HashMap<String, String>> getPointMapByNo(HashMap<String, String> dataMap) {
HashMap<String, HashMap<String, String>> pointMap = new HashMap<>();
for (Map.Entry<String, String> entry : dataMap.entrySet()) {
String key = entry.getKey();
if (key.startsWith(HighFormworkSupport.GZ_CJ)) {
String pointNo = StringUtils.substringAfter(entry.getKey(), HighFormworkSupport.GZ_CJ);
HashMap<String, String> map = pointMap.get(pointNo);
if (map == null) {
map = new HashMap<>();
}
map.put(HighFormworkSupport.GZ_CJ, entry.getValue());
pointMap.put(pointNo, map);
} else if (key.startsWith(HighFormworkSupport.GZ_DS)) {
String pointNo = StringUtils.substringAfter(entry.getKey(), HighFormworkSupport.GZ_DS);
HashMap<String, String> map = pointMap.get(pointNo);
if (map == null) {
map = new HashMap<>();
}
map.put(HighFormworkSupport.GZ_DS, entry.getValue());
pointMap.put(pointNo, map);
} else if (key.startsWith(HighFormworkSupport.GZ_ZT)) {
String pointNo = StringUtils.substringAfter(entry.getKey(), HighFormworkSupport.GZ_ZT);
HashMap<String, String> map = pointMap.get(pointNo);
if (map == null) {
map = new HashMap<>();
}
map.put(HighFormworkSupport.GZ_ZT, entry.getValue());
pointMap.put(pointNo, map);
}
}
return pointMap;
}
}

View File

@ -70,4 +70,6 @@ spring.boot.admin.client.instance.name=zjsj
double-carbon.water-data-url=http://test.cesms.net
double-carbon.ammeter-data-url=http://test.cesms.net
license.licensePath=C:/jxj/prod/backEnd/license/license.lic
license.publicKeysStorePath=C:/jxj/prod/backEnd/license/publicCerts.keystore
license.publicKeysStorePath=C:/jxj/prod/backEnd/license/publicCerts.keystore
#\u9AD8\u652F\u6A21\u7684tcp\u670D\u52A1\u7AEF\u7684\u7AEF\u53E3\u53F7
high_formwork.netty.port=15333