From 06cf00b0833747429062890e247908a8e29d53c4 Mon Sep 17 00:00:00 2001 From: 张磊磊 <201175954@qq.com> Date: 星期三, 18 十月 2023 17:07:44 +0800 Subject: [PATCH] 烟尘程序/TCP --- hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/service/impl/HelmetServiceImpl.java | 6 hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/service/HelmetService.java | 3 hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/utils/ApplicationContextUtil.java | 61 ++++++ hd/pipe/materialsManage/src/main/java/com/thhy/MaterialsApplication.java | 15 + hd/pipe/materialsManage/src/main/resources/mapping/THelmetDeviceMapper.xml | 131 ++++++++++++++ hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/entity/TDataSmock.java | 122 +++++++++++++ hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/smoke/SmokeMessage.java | 91 ++++++++++ hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/mapper/HelmetMapper.java | 4 hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/smoke/SmokeServer.java | 75 ++++++++ 9 files changed, 506 insertions(+), 2 deletions(-) diff --git a/hd/pipe/materialsManage/src/main/java/com/thhy/MaterialsApplication.java b/hd/pipe/materialsManage/src/main/java/com/thhy/MaterialsApplication.java index 9fb0f51..fe1fda3 100644 --- a/hd/pipe/materialsManage/src/main/java/com/thhy/MaterialsApplication.java +++ b/hd/pipe/materialsManage/src/main/java/com/thhy/MaterialsApplication.java @@ -1,18 +1,31 @@ package com.thhy; +import com.thhy.materials.modules.biz.helmet.smoke.SmokeServer; import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.scheduling.annotation.EnableScheduling; +import javax.annotation.Resource; + @SpringBootApplication @MapperScan(basePackages = {"com.thhy.materials.modules.*.*.mapper"}) @EnableFeignClients @EnableScheduling -public class MaterialsApplication { +public class MaterialsApplication implements CommandLineRunner { + + @Resource + private SmokeServer smokeServer; public static void main(String[] args) { SpringApplication.run(MaterialsApplication.class,args); } + + @Override + public void run(String... args) throws Exception { + smokeServer.run(8973); + } + } diff --git a/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/entity/TDataSmock.java b/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/entity/TDataSmock.java new file mode 100644 index 0000000..7cdc954 --- /dev/null +++ b/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/entity/TDataSmock.java @@ -0,0 +1,122 @@ +package com.thhy.materials.modules.biz.helmet.entity; + +import lombok.Data; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +import java.io.Serializable; + +@Data +@Getter +@Setter +@ToString +public class TDataSmock implements Serializable { + /** + * + */ + private Integer smokeId; + + /** + * + */ + private String gatewayCyc; + + /** + * + */ + private String fans; + + /** + * + */ + private String v1run; + + /** + * + */ + private String uv1run; + + /** + * + */ + private String fanrun; + + /** + * + */ + private String uv2run; + + /** + * + */ + private String ssvrun; + + /** + * + */ + private String dp; + + /** + * + */ + private String t; + + /** + * + */ + private String oc; + + /** + * + */ + private String gatew; + + /** + * + */ + private String nxrun; + + /** + * + */ + private String alarm; + + /** + * + */ + private String times; + + /** + * + */ + private String rtcVolt; + + /** + * + */ + private String v2run; + + /** + * + */ + private String dxqvrun; + + /** + * + */ + private String press; + + /** + * + */ + private String dc; + + /** + * This field was generated by MyBatis Generator. + * This field corresponds to the database table t_data_smock + * + * @mbg.generated Wed Oct 18 15:12:02 CST 2023 + */ + private static final long serialVersionUID = 1L; +} \ No newline at end of file diff --git a/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/mapper/HelmetMapper.java b/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/mapper/HelmetMapper.java index 0fcdf46..5303370 100644 --- a/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/mapper/HelmetMapper.java +++ b/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/mapper/HelmetMapper.java @@ -5,6 +5,7 @@ import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -46,4 +47,7 @@ List<ReportNumDto> helmetReportTotal(Map<String, Object> values); List<THelmetReport> helmetReportUser(Map<String, Object> values); + + void smockInsert(TDataSmock tDataSmock); + } diff --git a/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/service/HelmetService.java b/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/service/HelmetService.java index 30a5717..9c5497b 100644 --- a/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/service/HelmetService.java +++ b/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/service/HelmetService.java @@ -3,6 +3,7 @@ import com.thhy.general.common.BasicResult; import com.thhy.materials.modules.biz.helmet.entity.*; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -40,4 +41,6 @@ BasicResult helmetReportTotal(Map<String, Object> values); BasicResult helmetReportUser(Map<String, Object> values); + + void smockInsert(TDataSmock tDataSmock); } diff --git a/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/service/impl/HelmetServiceImpl.java b/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/service/impl/HelmetServiceImpl.java index 4309279..eb38b37 100644 --- a/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/service/impl/HelmetServiceImpl.java +++ b/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/service/impl/HelmetServiceImpl.java @@ -132,4 +132,10 @@ List<THelmetReport> helmetReports = helmetMapper.helmetReportUser(values); return BasicResult.success(helmetReports); } + + @Override + public void smockInsert(TDataSmock tDataSmock) { + System.out.println("参数为---"+tDataSmock.toString()); + helmetMapper.smockInsert(tDataSmock); + } } diff --git a/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/smoke/SmokeMessage.java b/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/smoke/SmokeMessage.java new file mode 100644 index 0000000..45069df --- /dev/null +++ b/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/smoke/SmokeMessage.java @@ -0,0 +1,91 @@ +package com.thhy.materials.modules.biz.helmet.smoke; +import com.alibaba.fastjson.JSONObject; +import com.thhy.materials.modules.biz.helmet.entity.TDataSmock; +import com.thhy.materials.modules.biz.helmet.mapper.HelmetMapper; +import com.thhy.materials.modules.biz.helmet.service.HelmetService; +import com.thhy.materials.modules.biz.utils.ApplicationContextUtil; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.util.CharsetUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.HashMap; +import java.util.Map; + +@Component +public class SmokeMessage extends SimpleChannelInboundHandler<ByteBuf>{ + + @Autowired + public HelmetService helmetService; + + private static String[] alphabets = {"A", "B", "C", "D", "E", "F", "G", "H", "I", + "J", "K", "L", "M", "N", "O", "P"}; + + @Override + public void channelActive(ChannelHandlerContext ctx) { + for(int i=0; i<10; i++) { + StringBuilder builder = new StringBuilder(); + builder.append("这是第"); + builder.append(i); + builder.append("条消息, 内容是:"); + for(int j=0; j<100; j++) { + builder.append(alphabets[i]); + } + builder.append("......"); + builder.append("#"); + + + System.out.println(builder.toString().getBytes().length); + + ctx.writeAndFlush(Unpooled.copiedBuffer(builder.toString(), + CharsetUtil.UTF_8)); + } + } + + @Override + public void channelRead0(ChannelHandlerContext ctx, ByteBuf in) { + System.out.println("客户端接收到消息: " + in.toString(CharsetUtil.UTF_8)); + JSONObject jsonObject11 = JSONObject.parseObject(in.toString(CharsetUtil.UTF_8)); + System.out.println("客户端接收到消息: " + jsonObject11); + TDataSmock tDataSmock = new TDataSmock(); + tDataSmock.setGatewayCyc(jsonObject11.get("gateway_cyc").toString()); + tDataSmock.setFans(jsonObject11.get("FanS").toString()); + tDataSmock.setV1run(jsonObject11.get("V1Run").toString()); + tDataSmock.setUv1run(jsonObject11.get("UV1Run").toString()); + tDataSmock.setFanrun(jsonObject11.get("FanRun").toString()); + tDataSmock.setUv2run(jsonObject11.get("UV2Run").toString()); + tDataSmock.setSsvrun(jsonObject11.get("SSVRun").toString()); + tDataSmock.setDp(jsonObject11.get("DP").toString()); + tDataSmock.setT(jsonObject11.get("T").toString()); + tDataSmock.setOc(jsonObject11.get("OC").toString()); + tDataSmock.setGatew(jsonObject11.get("gatew").toString()); + tDataSmock.setNxrun(jsonObject11.get("NXRun").toString()); + tDataSmock.setTimes(jsonObject11.get("time").toString()); + tDataSmock.setRtcVolt(jsonObject11.get("RTC_VOLT").toString()); + tDataSmock.setV2run(jsonObject11.get("V2Run").toString()); + tDataSmock.setDxqvrun(jsonObject11.get("DXQVRun").toString()); + tDataSmock.setPress(jsonObject11.get("Press").toString()); + tDataSmock.setDc(jsonObject11.get("Dc").toString()); + System.out.println("-------"+tDataSmock); + getUserService().smockInsert(tDataSmock); + } + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + cause.printStackTrace(); + ctx.close(); + } + + + public static HelmetService getUserService(){ + ApplicationContext ac = ApplicationContextUtil.getApplicationContext(); + HelmetService userService = (HelmetService) ac.getBean("helmetServiceImpl"); + return userService; + } + +} diff --git a/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/smoke/SmokeServer.java b/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/smoke/SmokeServer.java new file mode 100644 index 0000000..264ba19 --- /dev/null +++ b/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/helmet/smoke/SmokeServer.java @@ -0,0 +1,75 @@ +package com.thhy.materials.modules.biz.helmet.smoke; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +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 org.springframework.stereotype.Component; + +@Component +public class SmokeServer { + +// public static void main(String[] args) throws Exception { +// int port = 8084; +// if (args != null && args.length > 0) { +// try { +// port = Integer.valueOf(args[0]); +// } catch (NumberFormatException e) { +// // 采用默认值 +// } +// } +// new Smoke().bind(port); +// } + + + public void run(int port) throws Exception { + new SmokeServer().bind(port); + System.out.println("服务器启动成功!"); + } + + + public void bind(int port) throws Exception { + // 第一步: + // 配置服务端的NIO线程组 + // 主线程组, 用于接受客户端的连接,但是不做任何具体业务处理,像老板一样,负责接待客户,不具体服务客户 + EventLoopGroup bossGroup = new NioEventLoopGroup(1); + // 工作线程组, 老板线程组会把任务丢给他,让手下线程组去做任务,服务客户 + EventLoopGroup workerGroup = new NioEventLoopGroup(); + try { + // 类ServerBootstrap用于配置Server相关参数,并启动Server + ServerBootstrap b = new ServerBootstrap(); + + // 链式调用 + // 配置parentGroup和childGroup + b.group(bossGroup, workerGroup) + // 配置Server通道 + .channel(NioServerSocketChannel.class) + // 配置通道的ChannelPipeline + .childHandler(new ChildChannelHandler()); + + // 绑定端口,并启动server,同时设置启动方式为同步 + ChannelFuture f = b.bind(port).sync(); + + System.out.println( + SmokeServer.class.getName() + " 启动成功,在地址[" + f.channel().localAddress() + "]上等待客户请求......"); + + // 等待服务端监听端口关闭 + f.channel().closeFuture().sync(); + } finally { + // 优雅退出,释放线程池资源 + bossGroup.shutdownGracefully(); + workerGroup.shutdownGracefully(); + } + } + + private class ChildChannelHandler extends ChannelInitializer<SocketChannel> { + @Override + protected void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new SmokeMessage()); + } + } + +} diff --git a/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/utils/ApplicationContextUtil.java b/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/utils/ApplicationContextUtil.java new file mode 100644 index 0000000..a135e43 --- /dev/null +++ b/hd/pipe/materialsManage/src/main/java/com/thhy/materials/modules/biz/utils/ApplicationContextUtil.java @@ -0,0 +1,61 @@ +package com.thhy.materials.modules.biz.utils; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +@Component +public class ApplicationContextUtil implements ApplicationContextAware { + /** + * 上下文对象实例 + */ + private static ApplicationContext applicationContext; + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + ApplicationContextUtil.applicationContext = applicationContext; + } + + /** + * 获取applicationContext + * + * @return + */ + public static ApplicationContext getApplicationContext() { + return applicationContext; + } + + /** + * 通过name获取 Bean. + * + * @param name + * @return + */ + public static Object getBean(String name) { + return getApplicationContext().getBean(name); + } + + /** + * 通过class获取Bean. + * + * @param clazz + * @param <T> + * @return + */ + public static <T> T getBean(Class<T> clazz) { + return getApplicationContext().getBean(clazz); + } + + /** + * 通过name,以及Clazz返回指定的Bean + * + * @param name + * @param clazz + * @param <T> + * @return + */ + public static <T> T getBean(String name, Class<T> clazz) { + return getApplicationContext().getBean(name, clazz); + } +} diff --git a/hd/pipe/materialsManage/src/main/resources/mapping/THelmetDeviceMapper.xml b/hd/pipe/materialsManage/src/main/resources/mapping/THelmetDeviceMapper.xml index c8897d0..9690b58 100644 --- a/hd/pipe/materialsManage/src/main/resources/mapping/THelmetDeviceMapper.xml +++ b/hd/pipe/materialsManage/src/main/resources/mapping/THelmetDeviceMapper.xml @@ -532,7 +532,7 @@ WHERE user_id = #{userId} <if test="strTime!=null and strTime!='' and endTime!=null and endTime!='' "> - and sm_time between #{strTime} and #{endTime} + and DATE(sm_time) between #{strTime} and #{endTime} </if> GROUP BY DATE( sm_time ) @@ -549,4 +549,133 @@ user_id=#{userId} order by sm_time desc </select> + + <insert id="smockInsert" > + insert into t_data_smock + <trim prefix="(" suffix=")" suffixOverrides=","> + <if test="smokeId != null"> + smoke_id, + </if> + <if test="gatewayCyc != null"> + gateway_cyc, + </if> + <if test="fans != null"> + FanS, + </if> + <if test="v1run != null"> + V1Run, + </if> + <if test="uv1run != null"> + UV1Run, + </if> + <if test="fanrun != null"> + FanRun, + </if> + <if test="uv2run != null"> + UV2Run, + </if> + <if test="ssvrun != null"> + SSVRun, + </if> + <if test="dp != null"> + DP, + </if> + <if test="t != null"> + T, + </if> + <if test="oc != null"> + OC, + </if> + <if test="gatew != null"> + gatew, + </if> + <if test="nxrun != null"> + NXRun, + </if> + <if test="alarm != null"> + Alarm, + </if> + <if test="times != null"> + times, + </if> + <if test="rtcVolt != null"> + RTC_VOLT, + </if> + <if test="v2run != null"> + V2Run, + </if> + <if test="dxqvrun != null"> + DXQVRun, + </if> + <if test="press != null"> + Press, + </if> + <if test="dc != null"> + Dc, + </if> + </trim> + <trim prefix="values (" suffix=")" suffixOverrides=","> + <if test="smokeId != null"> + #{smokeId,jdbcType=INTEGER}, + </if> + <if test="gatewayCyc != null"> + #{gatewayCyc,jdbcType=VARCHAR}, + </if> + <if test="fans != null"> + #{fans,jdbcType=VARCHAR}, + </if> + <if test="v1run != null"> + #{v1run,jdbcType=VARCHAR}, + </if> + <if test="uv1run != null"> + #{uv1run,jdbcType=VARCHAR}, + </if> + <if test="fanrun != null"> + #{fanrun,jdbcType=VARCHAR}, + </if> + <if test="uv2run != null"> + #{uv2run,jdbcType=VARCHAR}, + </if> + <if test="ssvrun != null"> + #{ssvrun,jdbcType=VARCHAR}, + </if> + <if test="dp != null"> + #{dp,jdbcType=VARCHAR}, + </if> + <if test="t != null"> + #{t,jdbcType=VARCHAR}, + </if> + <if test="oc != null"> + #{oc,jdbcType=VARCHAR}, + </if> + <if test="gatew != null"> + #{gatew,jdbcType=VARCHAR}, + </if> + <if test="nxrun != null"> + #{nxrun,jdbcType=VARCHAR}, + </if> + <if test="alarm != null"> + #{alarm,jdbcType=VARCHAR}, + </if> + <if test="times != null"> + #{times,jdbcType=VARCHAR}, + </if> + <if test="rtcVolt != null"> + #{rtcVolt,jdbcType=VARCHAR}, + </if> + <if test="v2run != null"> + #{v2run,jdbcType=VARCHAR}, + </if> + <if test="dxqvrun != null"> + #{dxqvrun,jdbcType=VARCHAR}, + </if> + <if test="press != null"> + #{press,jdbcType=VARCHAR}, + </if> + <if test="dc != null"> + #{dc,jdbcType=VARCHAR}, + </if> + </trim> + </insert> + </mapper> \ No newline at end of file -- Gitblit v1.9.3