Browse Source

20210511热成像图存入数据库

logistics
zy_Java 4 years ago
parent
commit
3bf8f759d7
  1. 9
      logistics/src/main/java/com/ccsens/logistics/LogisticsApplication.java
  2. 14
      logistics/src/main/java/com/ccsens/logistics/Netty/NettyClient.java
  3. 12
      logistics/src/main/java/com/ccsens/logistics/Netty/SimpleClientHandler.java
  4. 3
      logistics/src/main/java/com/ccsens/logistics/service/IThermalImageryService.java
  5. 16
      logistics/src/main/java/com/ccsens/logistics/service/ThermalImageryService.java
  6. 4
      logistics/src/main/resources/application.yml

9
logistics/src/main/java/com/ccsens/logistics/LogisticsApplication.java

@ -1,6 +1,7 @@
package com.ccsens.logistics;
import com.ccsens.logistics.Netty.NettyClient;
import com.ccsens.logistics.service.IThermalImageryService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
@ -8,9 +9,13 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import javax.annotation.Resource;
@MapperScan(basePackages = {"com.ccsens.logistics.persist.*"})
@ComponentScan(basePackages = {"com.ccsens.*"})
@ServletComponentScan
@EnableAsync
//开启断路器功能
@ -19,6 +24,9 @@ import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication(scanBasePackages = "com.ccsens")
public class LogisticsApplication implements CommandLineRunner {
@Resource
private IThermalImageryService thermalImageryService;
public static void main(String[] args) {
SpringApplication.run(LogisticsApplication.class, args);
}
@ -26,6 +34,7 @@ public class LogisticsApplication implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(thermalImageryService);
NettyClient.start();
}
}

14
logistics/src/main/java/com/ccsens/logistics/Netty/NettyClient.java

@ -4,6 +4,7 @@ import com.ccsens.logistics.service.IThermalImageryService;
import com.ccsens.logistics.service.ThermalImageryService;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
@ -11,15 +12,24 @@ import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringEncoder;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Component
public class NettyClient {
@Resource
private IThermalImageryService thermalImageryService;
private ChannelHandler simpleClientHandler;
private static NettyClient nettyClient;
@PostConstruct
public void init(){
nettyClient = this;
nettyClient.simpleClientHandler = this.simpleClientHandler;
}
public static void start() throws Exception {
// 首先,netty通过ServerBootstrap启动服务端
@ -42,7 +52,7 @@ public class NettyClient {
ch.pipeline().addLast(new DelimiterBasedFrameDecoder(
Integer.MAX_VALUE, Delimiters.lineDelimiter()[0]));
//找到他的管道 增加他的handler
ch.pipeline().addLast(new SimpleClientHandler());
ch.pipeline().addLast(nettyClient.simpleClientHandler);
}
});

12
logistics/src/main/java/com/ccsens/logistics/Netty/SimpleClientHandler.java

@ -1,15 +1,21 @@
package com.ccsens.logistics.Netty;
import cn.hutool.core.util.StrUtil;
import com.ccsens.logistics.service.IThermalImageryService;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.AttributeKey;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.nio.charset.Charset;
/**
* @author
*/
@ChannelHandler.Sharable
@Component
public class SimpleClientHandler extends ChannelInboundHandlerAdapter{
@Resource
private IThermalImageryService thermalImageryService;
@ -19,8 +25,10 @@ public class SimpleClientHandler extends ChannelInboundHandlerAdapter{
if (msg instanceof ByteBuf) {
String value = ((ByteBuf) msg).toString(Charset.defaultCharset());
System.out.println("服务器端返回的数据:" + value);
if(StrUtil.isNotEmpty(value)) {
thermalImageryService.disposeMessage(value);
}
}
// AttributeKey<String> key = AttributeKey.valueOf("ServerData");
// ctx.channel().attr(key).set("客户端处理完毕");

3
logistics/src/main/java/com/ccsens/logistics/service/IThermalImageryService.java

@ -2,6 +2,9 @@ package com.ccsens.logistics.service;
import java.util.List;
/**
* @author
*/
public interface IThermalImageryService {
List<String> getImagery();

16
logistics/src/main/java/com/ccsens/logistics/service/ThermalImageryService.java

@ -2,7 +2,6 @@ package com.ccsens.logistics.service;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ccsens.logistics.Util.Constant;
@ -12,7 +11,6 @@ import com.ccsens.logistics.bean.po.LogisticsEquipmentExample;
import com.ccsens.logistics.bean.po.LogisticsHeatImagingRecord;
import com.ccsens.logistics.persist.mapper.LogisticsEquipmentMapper;
import com.ccsens.logistics.persist.mapper.LogisticsHeatImagingRecordMapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
@ -23,6 +21,9 @@ import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
* @author
*/
@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@ -66,6 +67,11 @@ public class ThermalImageryService implements IThermalImageryService{
//TODO 心跳不处理
break;
case Constant.THERMAL_IMAGERY:
LogisticsEquipmentExample equipmentExample = new LogisticsEquipmentExample();
equipmentExample.createCriteria().andEquipmentNumberEqualTo(thermalImagery.getThing_id());
List<LogisticsEquipment> logisticsEquipments = logisticsEquipmentMapper.selectByExample(equipmentExample);
if (CollectionUtil.isNotEmpty(logisticsEquipments)) {
LogisticsEquipment equipment = logisticsEquipments.get(0);
if (StrUtil.isNotEmpty(thermalImagery.getData())) {
ThermalImageryDto.GetThermalImageryDate date = JSONObject.parseObject(thermalImagery.getData(), ThermalImageryDto.GetThermalImageryDate.class);
LogisticsHeatImagingRecord heatImagingRecord = new LogisticsHeatImagingRecord();
@ -73,14 +79,16 @@ public class ThermalImageryService implements IThermalImageryService{
heatImagingRecord.setMaxT(new BigDecimal(date.getMaxT()));
heatImagingRecord.setMaxTx(date.getMaxTx());
heatImagingRecord.setMaxTy(date.getMaxTy());
heatImagingRecord.setImageData(date.getImageData());
heatImagingRecord.setImageData("data:image/png;base64," + date.getImageData());
heatImagingRecord.setRecordTime(System.currentTimeMillis());
heatImagingRecord.setEquipmentId(equipment.getId());
heatImagingRecordMapper.insertSelective(heatImagingRecord);
}
}
break;
default:
break;
}
thermalImagery.getType();
} catch (Exception e) {
log.info("解析热成像返回数据异常 {}", e);
}

4
logistics/src/main/resources/application.yml

@ -1,6 +1,6 @@
spring:
profiles:
active: test
include: common,util-test
active: dev
include: common,util-dev

Loading…
Cancel
Save