From 3bf8f759d7afd128702c3449418ccfadf46feae5 Mon Sep 17 00:00:00 2001 From: zy_Java <654600784@qq.com> Date: Tue, 11 May 2021 13:04:20 +0800 Subject: [PATCH] =?UTF-8?q?20210511=E7=83=AD=E6=88=90=E5=83=8F=E5=9B=BE?= =?UTF-8?q?=E5=AD=98=E5=85=A5=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../logistics/LogisticsApplication.java | 9 ++++ .../ccsens/logistics/Netty/NettyClient.java | 14 +++++- .../logistics/Netty/SimpleClientHandler.java | 14 ++++-- .../service/IThermalImageryService.java | 3 ++ .../service/ThermalImageryService.java | 50 +++++++++++-------- logistics/src/main/resources/application.yml | 4 +- 6 files changed, 66 insertions(+), 28 deletions(-) diff --git a/logistics/src/main/java/com/ccsens/logistics/LogisticsApplication.java b/logistics/src/main/java/com/ccsens/logistics/LogisticsApplication.java index b4fd7974..e263ed75 100644 --- a/logistics/src/main/java/com/ccsens/logistics/LogisticsApplication.java +++ b/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(); } } diff --git a/logistics/src/main/java/com/ccsens/logistics/Netty/NettyClient.java b/logistics/src/main/java/com/ccsens/logistics/Netty/NettyClient.java index 302d6e70..0d3db04f 100644 --- a/logistics/src/main/java/com/ccsens/logistics/Netty/NettyClient.java +++ b/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); } }); diff --git a/logistics/src/main/java/com/ccsens/logistics/Netty/SimpleClientHandler.java b/logistics/src/main/java/com/ccsens/logistics/Netty/SimpleClientHandler.java index e18f05cc..24f11e93 100644 --- a/logistics/src/main/java/com/ccsens/logistics/Netty/SimpleClientHandler.java +++ b/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,7 +25,9 @@ public class SimpleClientHandler extends ChannelInboundHandlerAdapter{ if (msg instanceof ByteBuf) { String value = ((ByteBuf) msg).toString(Charset.defaultCharset()); System.out.println("服务器端返回的数据:" + value); - thermalImageryService.disposeMessage(value); + if(StrUtil.isNotEmpty(value)) { + thermalImageryService.disposeMessage(value); + } } // AttributeKey key = AttributeKey.valueOf("ServerData"); diff --git a/logistics/src/main/java/com/ccsens/logistics/service/IThermalImageryService.java b/logistics/src/main/java/com/ccsens/logistics/service/IThermalImageryService.java index 50a8ae81..a8bc9d16 100644 --- a/logistics/src/main/java/com/ccsens/logistics/service/IThermalImageryService.java +++ b/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 getImagery(); diff --git a/logistics/src/main/java/com/ccsens/logistics/service/ThermalImageryService.java b/logistics/src/main/java/com/ccsens/logistics/service/ThermalImageryService.java index 2b41a86f..86b5117b 100644 --- a/logistics/src/main/java/com/ccsens/logistics/service/ThermalImageryService.java +++ b/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,10 +21,13 @@ import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; +/** + * @author 逗 + */ @Slf4j @Service -@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) -public class ThermalImageryService implements IThermalImageryService{ +@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) +public class ThermalImageryService implements IThermalImageryService { @Resource private LogisticsEquipmentMapper logisticsEquipmentMapper; @@ -36,7 +37,7 @@ public class ThermalImageryService implements IThermalImageryService{ private Snowflake snowflake; @Override - public List getImagery(){ + public List getImagery() { List str = new ArrayList<>(); //查询redis内的热成像摄像头的编号 @@ -44,8 +45,8 @@ public class ThermalImageryService implements IThermalImageryService{ LogisticsEquipmentExample equipmentExample = new LogisticsEquipmentExample(); equipmentExample.createCriteria().andEquipmentTypeEqualTo((byte) 4); List logisticsEquipments = logisticsEquipmentMapper.selectByExample(equipmentExample); - if(CollectionUtil.isNotEmpty(logisticsEquipments)){ - for (LogisticsEquipment equipment:logisticsEquipments){ + if (CollectionUtil.isNotEmpty(logisticsEquipments)) { + for (LogisticsEquipment equipment : logisticsEquipments) { ThermalImageryDto.RequestThermalImagery thermalImagery = new ThermalImageryDto.RequestThermalImagery(); thermalImagery.setThing_id(equipment.getEquipmentNumber()); String s = JSONObject.toJSONString(thermalImagery); @@ -60,29 +61,36 @@ public class ThermalImageryService implements IThermalImageryService{ public void disposeMessage(String value) { //解析字符串 try { - ThermalImageryDto.RequestThermalImagery thermalImagery = JSONObject.parseObject(value,ThermalImageryDto.RequestThermalImagery.class); - switch (thermalImagery.getType()){ + ThermalImageryDto.RequestThermalImagery thermalImagery = JSONObject.parseObject(value, ThermalImageryDto.RequestThermalImagery.class); + switch (thermalImagery.getType()) { case Constant.HEARTBEAT: //TODO 心跳不处理 break; case Constant.THERMAL_IMAGERY: - if(StrUtil.isNotEmpty(thermalImagery.getData())) { - ThermalImageryDto.GetThermalImageryDate date = JSONObject.parseObject(thermalImagery.getData(), ThermalImageryDto.GetThermalImageryDate.class); - LogisticsHeatImagingRecord heatImagingRecord = new LogisticsHeatImagingRecord(); - heatImagingRecord.setId(snowflake.nextId()); - heatImagingRecord.setMaxT(new BigDecimal(date.getMaxT())); - heatImagingRecord.setMaxTx(date.getMaxTx()); - heatImagingRecord.setMaxTy(date.getMaxTy()); - heatImagingRecord.setImageData(date.getImageData()); - heatImagingRecordMapper.insertSelective(heatImagingRecord); + LogisticsEquipmentExample equipmentExample = new LogisticsEquipmentExample(); + equipmentExample.createCriteria().andEquipmentNumberEqualTo(thermalImagery.getThing_id()); + List 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(); + heatImagingRecord.setId(snowflake.nextId()); + heatImagingRecord.setMaxT(new BigDecimal(date.getMaxT())); + heatImagingRecord.setMaxTx(date.getMaxTx()); + heatImagingRecord.setMaxTy(date.getMaxTy()); + 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); + } catch (Exception e) { + log.info("解析热成像返回数据异常 {}", e); } } diff --git a/logistics/src/main/resources/application.yml b/logistics/src/main/resources/application.yml index 0e768cd1..7e037ef8 100644 --- a/logistics/src/main/resources/application.yml +++ b/logistics/src/main/resources/application.yml @@ -1,6 +1,6 @@ spring: profiles: - active: test - include: common,util-test + active: dev + include: common,util-dev