From 1d39ab3c93541080381b91d2096f4deb011090ad Mon Sep 17 00:00:00 2001 From: "wenhua.zhou" Date: Thu, 9 Nov 2023 16:59:32 +0800 Subject: [PATCH] mqtt --- pom.xml | 113 +++++ src/main/java/com/xydl/MqttApplication.java | 18 + .../java/com/xydl/config/DruidConfig.java | 15 + .../java/com/xydl/config/MyBatisConfig.java | 20 + .../com/xydl/controller/JDBCController.java | 44 ++ .../com/xydl/controller/TestController.java | 26 ++ src/main/java/com/xydl/mapper/EaifMapper.java | 21 + src/main/java/com/xydl/mapper/EiaMapper.java | 21 + src/main/java/com/xydl/mapper/EpaMapper.java | 34 ++ src/main/java/com/xydl/mapper/EtpMapper.java | 21 + src/main/java/com/xydl/mapper/MicMapper.java | 24 + src/main/java/com/xydl/mapper/MoaMapper.java | 24 + src/main/java/com/xydl/mapper/PdMapper.java | 25 + .../java/com/xydl/mapper/RptTemperMapper.java | 24 + src/main/java/com/xydl/mapper/ScurMapper.java | 25 + src/main/java/com/xydl/mapper/Sf6Mapper.java | 22 + .../java/com/xydl/mapper/Sf6envMapper.java | 22 + src/main/java/com/xydl/model/Eaif.java | 30 ++ src/main/java/com/xydl/model/Eia.java | 33 ++ src/main/java/com/xydl/model/Epa.java | 65 +++ src/main/java/com/xydl/model/Etp.java | 33 ++ .../java/com/xydl/model/Microclimate.java | 42 ++ src/main/java/com/xydl/model/Moa.java | 36 ++ src/main/java/com/xydl/model/Pd.java | 30 ++ src/main/java/com/xydl/model/RptTemper.java | 42 ++ src/main/java/com/xydl/model/Scur.java | 22 + src/main/java/com/xydl/model/Sf6.java | 35 ++ src/main/java/com/xydl/model/Sf6env.java | 43 ++ src/main/java/com/xydl/model/SuperModel.java | 231 +++++++++ .../java/com/xydl/service/EiaService.java | 33 ++ .../com/xydl/service/impl/EiaServiceImpl.java | 438 ++++++++++++++++++ src/main/java/com/xydl/util/CommonUtils.java | 259 +++++++++++ .../java/com/xydl/util/DataSourceUtils.java | 101 ++++ src/main/java/com/xydl/util/FormatUtil.java | 34 ++ src/main/java/com/xydl/util/MqttUtil.java | 106 +++++ src/main/resources/application.yml | 27 ++ .../resources/com/xydl/mapper/EaifMapper.xml | 62 +++ .../resources/com/xydl/mapper/EiaMapper.xml | 69 +++ .../resources/com/xydl/mapper/EpaMapper.xml | 86 ++++ .../resources/com/xydl/mapper/EtpMapper.xml | 69 +++ .../resources/com/xydl/mapper/MicMapper.xml | 70 +++ .../resources/com/xydl/mapper/MoaMapper.xml | 68 +++ .../resources/com/xydl/mapper/PdMapper.xml | 69 +++ .../com/xydl/mapper/RptTemperMapper.xml | 68 +++ .../resources/com/xydl/mapper/ScurMapper.xml | 68 +++ .../resources/com/xydl/mapper/Sf6Mapper.xml | 67 +++ .../com/xydl/mapper/Sf6envMapper.xml | 68 +++ src/main/resources/templates/a.html | 12 + 48 files changed, 2915 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/com/xydl/MqttApplication.java create mode 100644 src/main/java/com/xydl/config/DruidConfig.java create mode 100644 src/main/java/com/xydl/config/MyBatisConfig.java create mode 100644 src/main/java/com/xydl/controller/JDBCController.java create mode 100644 src/main/java/com/xydl/controller/TestController.java create mode 100644 src/main/java/com/xydl/mapper/EaifMapper.java create mode 100644 src/main/java/com/xydl/mapper/EiaMapper.java create mode 100644 src/main/java/com/xydl/mapper/EpaMapper.java create mode 100644 src/main/java/com/xydl/mapper/EtpMapper.java create mode 100644 src/main/java/com/xydl/mapper/MicMapper.java create mode 100644 src/main/java/com/xydl/mapper/MoaMapper.java create mode 100644 src/main/java/com/xydl/mapper/PdMapper.java create mode 100644 src/main/java/com/xydl/mapper/RptTemperMapper.java create mode 100644 src/main/java/com/xydl/mapper/ScurMapper.java create mode 100644 src/main/java/com/xydl/mapper/Sf6Mapper.java create mode 100644 src/main/java/com/xydl/mapper/Sf6envMapper.java create mode 100644 src/main/java/com/xydl/model/Eaif.java create mode 100644 src/main/java/com/xydl/model/Eia.java create mode 100644 src/main/java/com/xydl/model/Epa.java create mode 100644 src/main/java/com/xydl/model/Etp.java create mode 100644 src/main/java/com/xydl/model/Microclimate.java create mode 100644 src/main/java/com/xydl/model/Moa.java create mode 100644 src/main/java/com/xydl/model/Pd.java create mode 100644 src/main/java/com/xydl/model/RptTemper.java create mode 100644 src/main/java/com/xydl/model/Scur.java create mode 100644 src/main/java/com/xydl/model/Sf6.java create mode 100644 src/main/java/com/xydl/model/Sf6env.java create mode 100644 src/main/java/com/xydl/model/SuperModel.java create mode 100644 src/main/java/com/xydl/service/EiaService.java create mode 100644 src/main/java/com/xydl/service/impl/EiaServiceImpl.java create mode 100644 src/main/java/com/xydl/util/CommonUtils.java create mode 100644 src/main/java/com/xydl/util/DataSourceUtils.java create mode 100644 src/main/java/com/xydl/util/FormatUtil.java create mode 100644 src/main/java/com/xydl/util/MqttUtil.java create mode 100644 src/main/resources/application.yml create mode 100644 src/main/resources/com/xydl/mapper/EaifMapper.xml create mode 100644 src/main/resources/com/xydl/mapper/EiaMapper.xml create mode 100644 src/main/resources/com/xydl/mapper/EpaMapper.xml create mode 100644 src/main/resources/com/xydl/mapper/EtpMapper.xml create mode 100644 src/main/resources/com/xydl/mapper/MicMapper.xml create mode 100644 src/main/resources/com/xydl/mapper/MoaMapper.xml create mode 100644 src/main/resources/com/xydl/mapper/PdMapper.xml create mode 100644 src/main/resources/com/xydl/mapper/RptTemperMapper.xml create mode 100644 src/main/resources/com/xydl/mapper/ScurMapper.xml create mode 100644 src/main/resources/com/xydl/mapper/Sf6Mapper.xml create mode 100644 src/main/resources/com/xydl/mapper/Sf6envMapper.xml create mode 100644 src/main/resources/templates/a.html diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..905408c --- /dev/null +++ b/pom.xml @@ -0,0 +1,113 @@ + + + 4.0.0 + + com.xydl + mqtt + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 2.6.1 + + + + + 1.8 + 8 + 8 + UTF-8 + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + org.eclipse.paho + org.eclipse.paho.client.mqttv3 + 1.2.1 + + + + mysql + mysql-connector-java + 8.0.28 + + + + + com.alibaba + fastjson + 1.2.31_noneautotype + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.0.0 + + + + + com.baomidou + mybatis-plus-boot-starter + 3.4.2 + + + + + com.alibaba + druid + 1.1.10 + + + + + com.alibaba + druid-spring-boot-starter + 1.2.9 + + + org.projectlombok + lombok + + + + + + + + + maven-compiler-plugin + 2.3.2 + + 1.8 + 1.8 + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/src/main/java/com/xydl/MqttApplication.java b/src/main/java/com/xydl/MqttApplication.java new file mode 100644 index 0000000..5831936 --- /dev/null +++ b/src/main/java/com/xydl/MqttApplication.java @@ -0,0 +1,18 @@ +package com.xydl; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; + +@EnableScheduling +@MapperScan(basePackages = "com.xydl.mapper") +@SpringBootApplication//标识该类为主程序启动类 +public class MqttApplication { + //主程序启动方法 + public static void main(String[] args) { + SpringApplication.run(MqttApplication.class,args); + } + + +} diff --git a/src/main/java/com/xydl/config/DruidConfig.java b/src/main/java/com/xydl/config/DruidConfig.java new file mode 100644 index 0000000..f7e14f6 --- /dev/null +++ b/src/main/java/com/xydl/config/DruidConfig.java @@ -0,0 +1,15 @@ +package com.xydl.config; + +import com.alibaba.druid.pool.DruidDataSource; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class DruidConfig { + @ConfigurationProperties(prefix = "spring.datasource") + @Bean + public DruidDataSource getDurid(){ + return new DruidDataSource(); + } +} diff --git a/src/main/java/com/xydl/config/MyBatisConfig.java b/src/main/java/com/xydl/config/MyBatisConfig.java new file mode 100644 index 0000000..09be1fb --- /dev/null +++ b/src/main/java/com/xydl/config/MyBatisConfig.java @@ -0,0 +1,20 @@ +package com.xydl.config; + + +import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer; +import com.baomidou.mybatisplus.core.MybatisConfiguration; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class MyBatisConfig { + + public ConfigurationCustomizer configurationCustomizer(){ + return new ConfigurationCustomizer() { + @Override + public void customize(MybatisConfiguration configuration) { + //下划线与驼峰命名进行自动映射 + configuration.setMapUnderscoreToCamelCase(true); + } + }; + } +} diff --git a/src/main/java/com/xydl/controller/JDBCController.java b/src/main/java/com/xydl/controller/JDBCController.java new file mode 100644 index 0000000..6e4573f --- /dev/null +++ b/src/main/java/com/xydl/controller/JDBCController.java @@ -0,0 +1,44 @@ +package com.xydl.controller; + +import com.xydl.util.DataSourceUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.sql.DataSource; +import java.sql.*; + + +@RestController +public class JDBCController { + private static final Logger logger = LoggerFactory.getLogger(DataSourceUtils.class); + + + @RequestMapping("/data") + public void getData() { + Connection conn = null; + PreparedStatement pstmt = null; + try { + conn = DataSourceUtils.getConnection(); + String sql = "insert into pop_id(node_id,id) values(?,?)"; + pstmt = conn.prepareStatement(sql); + pstmt.setString(1, ""); + pstmt.setInt(2, 100); + pstmt.executeUpdate(); + } catch (Exception e) { + logger.error("execute sql exception:", e); + } finally { + DataSourceUtils.closeResource(pstmt, conn); + } + } + + + + + +} + + + + diff --git a/src/main/java/com/xydl/controller/TestController.java b/src/main/java/com/xydl/controller/TestController.java new file mode 100644 index 0000000..2114835 --- /dev/null +++ b/src/main/java/com/xydl/controller/TestController.java @@ -0,0 +1,26 @@ +package com.xydl.controller; + + +import com.xydl.model.Eia; +import com.xydl.service.EiaService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import java.util.List; + +@Controller +public class TestController { + + @Autowired + private EiaService eiaService; + + @RequestMapping("/eia") + @ResponseBody + public List eia() { + System.out.println("get eia"); + return eiaService.getEia(); + } + +} \ No newline at end of file diff --git a/src/main/java/com/xydl/mapper/EaifMapper.java b/src/main/java/com/xydl/mapper/EaifMapper.java new file mode 100644 index 0000000..ad6b59f --- /dev/null +++ b/src/main/java/com/xydl/mapper/EaifMapper.java @@ -0,0 +1,21 @@ +package com.xydl.mapper; + + +import com.xydl.model.Eaif; +import com.xydl.model.Epa; +import org.apache.ibatis.annotations.Mapper; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Mapper +@Repository +public interface EaifMapper { + + + //红外测温 + List getEaif(); + + + +} diff --git a/src/main/java/com/xydl/mapper/EiaMapper.java b/src/main/java/com/xydl/mapper/EiaMapper.java new file mode 100644 index 0000000..a51bfda --- /dev/null +++ b/src/main/java/com/xydl/mapper/EiaMapper.java @@ -0,0 +1,21 @@ +package com.xydl.mapper; + +import com.xydl.model.Eia; +import org.apache.ibatis.annotations.Mapper; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Mapper +@Repository +public interface EiaMapper { + + + //铁芯、夹件、电容性设备 + List getEia(); + + + + + +} diff --git a/src/main/java/com/xydl/mapper/EpaMapper.java b/src/main/java/com/xydl/mapper/EpaMapper.java new file mode 100644 index 0000000..ad27a16 --- /dev/null +++ b/src/main/java/com/xydl/mapper/EpaMapper.java @@ -0,0 +1,34 @@ +package com.xydl.mapper; + +import com.xydl.model.Epa; +import com.xydl.model.SuperModel; +import org.apache.ibatis.annotations.Mapper; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Mapper +@Repository +public interface EpaMapper { + + + //油色谱 + List getEpa(int devid); + + List getData(String sql); + + List getEqmidsByTableName(String tableName); + + List getTableNamesBySyncTable(String syncTable); + + String getSqlBySyncTable(String syncTable, String tableName); + + + + + + + + + +} diff --git a/src/main/java/com/xydl/mapper/EtpMapper.java b/src/main/java/com/xydl/mapper/EtpMapper.java new file mode 100644 index 0000000..62909c7 --- /dev/null +++ b/src/main/java/com/xydl/mapper/EtpMapper.java @@ -0,0 +1,21 @@ +package com.xydl.mapper; + +import com.xydl.model.Etp; +import org.apache.ibatis.annotations.Mapper; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Mapper +@Repository +public interface EtpMapper { + + + //绕组测温 + List getEtp(); + + + + + +} diff --git a/src/main/java/com/xydl/mapper/MicMapper.java b/src/main/java/com/xydl/mapper/MicMapper.java new file mode 100644 index 0000000..232c068 --- /dev/null +++ b/src/main/java/com/xydl/mapper/MicMapper.java @@ -0,0 +1,24 @@ +package com.xydl.mapper; + + +import com.xydl.model.Epa; +import com.xydl.model.Microclimate; +import org.apache.ibatis.annotations.Mapper; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Mapper +@Repository +public interface MicMapper { + + + + //微气象 + List getMicroclimate(); + + + + + +} diff --git a/src/main/java/com/xydl/mapper/MoaMapper.java b/src/main/java/com/xydl/mapper/MoaMapper.java new file mode 100644 index 0000000..06e3bbe --- /dev/null +++ b/src/main/java/com/xydl/mapper/MoaMapper.java @@ -0,0 +1,24 @@ +package com.xydl.mapper; + + +import com.xydl.model.Epa; +import com.xydl.model.Moa; +import org.apache.ibatis.annotations.Mapper; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Mapper +@Repository +public interface MoaMapper { + + + + //绝缘监测-金属氧化物 + List getMoa(); + + + + + +} diff --git a/src/main/java/com/xydl/mapper/PdMapper.java b/src/main/java/com/xydl/mapper/PdMapper.java new file mode 100644 index 0000000..fccc8f0 --- /dev/null +++ b/src/main/java/com/xydl/mapper/PdMapper.java @@ -0,0 +1,25 @@ +package com.xydl.mapper; + + +import com.xydl.model.Epa; +import com.xydl.model.Pd; +import org.apache.ibatis.annotations.Mapper; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Mapper +@Repository +public interface PdMapper { + + + + //局放监测 + List getPd(); + + + + + + +} diff --git a/src/main/java/com/xydl/mapper/RptTemperMapper.java b/src/main/java/com/xydl/mapper/RptTemperMapper.java new file mode 100644 index 0000000..148ecc3 --- /dev/null +++ b/src/main/java/com/xydl/mapper/RptTemperMapper.java @@ -0,0 +1,24 @@ +package com.xydl.mapper; + +import com.xydl.model.Eia; +import com.xydl.model.Epa; +import com.xydl.model.Etp; +import com.xydl.model.RptTemper; +import org.apache.ibatis.annotations.Mapper; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Mapper +@Repository +public interface RptTemperMapper { + + + + //测温点 + List getRptTemper(); + + + + +} diff --git a/src/main/java/com/xydl/mapper/ScurMapper.java b/src/main/java/com/xydl/mapper/ScurMapper.java new file mode 100644 index 0000000..c437371 --- /dev/null +++ b/src/main/java/com/xydl/mapper/ScurMapper.java @@ -0,0 +1,25 @@ +package com.xydl.mapper; + + +import com.xydl.model.Epa; +import com.xydl.model.Scur; +import org.apache.ibatis.annotations.Mapper; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Mapper +@Repository +public interface ScurMapper { + + + + //电缆环流 + List getScur(); + + + + + + +} diff --git a/src/main/java/com/xydl/mapper/Sf6Mapper.java b/src/main/java/com/xydl/mapper/Sf6Mapper.java new file mode 100644 index 0000000..53c497f --- /dev/null +++ b/src/main/java/com/xydl/mapper/Sf6Mapper.java @@ -0,0 +1,22 @@ +package com.xydl.mapper; + +import com.xydl.model.Epa; +import com.xydl.model.Sf6; +import org.apache.ibatis.annotations.Mapper; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Mapper +@Repository +public interface Sf6Mapper { + + + //SF6监测 + List getSf6(); + + + + + +} diff --git a/src/main/java/com/xydl/mapper/Sf6envMapper.java b/src/main/java/com/xydl/mapper/Sf6envMapper.java new file mode 100644 index 0000000..22fb0f0 --- /dev/null +++ b/src/main/java/com/xydl/mapper/Sf6envMapper.java @@ -0,0 +1,22 @@ +package com.xydl.mapper; + + +import com.xydl.model.Epa; +import com.xydl.model.Sf6env; +import org.apache.ibatis.annotations.Mapper; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Mapper +@Repository +public interface Sf6envMapper { + + + //SF6环境 + List getSf6env(); + + + + +} diff --git a/src/main/java/com/xydl/model/Eaif.java b/src/main/java/com/xydl/model/Eaif.java new file mode 100644 index 0000000..2905b27 --- /dev/null +++ b/src/main/java/com/xydl/model/Eaif.java @@ -0,0 +1,30 @@ +package com.xydl.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +import java.sql.Timestamp; +import java.util.Date; + +@Data +public class Eaif { + + @JsonProperty("SubDeviceID") + private String subDeviceID; + + @JsonProperty("SensorCode") + private String sensorId; + + @JsonProperty("AcquisitionTime") + private String captureTime; + + @JsonProperty("MaxTmp") + private double maxTemp; + + @JsonProperty("MinTmp") + private double minTemp; + + @JsonProperty("AvgTmp") + private double avgTemp; + +} diff --git a/src/main/java/com/xydl/model/Eia.java b/src/main/java/com/xydl/model/Eia.java new file mode 100644 index 0000000..ba7ab65 --- /dev/null +++ b/src/main/java/com/xydl/model/Eia.java @@ -0,0 +1,33 @@ +package com.xydl.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +import java.util.Date; + +@Data +public class Eia { + + @JsonProperty("SubDeviceID") + private String subDeviceID; + + @JsonProperty("SensorCode") + private String sensorId; + + @JsonProperty("AcquisitionTime") + private String captureTime; + + @JsonProperty("MaxTmp") + private String maxTemp; + + @JsonProperty("MinTmp") + private String minTemp; + + @JsonProperty("AvgTmp") + private String avgTemp; + + @JsonProperty("Phase") + private String phase; + + +} diff --git a/src/main/java/com/xydl/model/Epa.java b/src/main/java/com/xydl/model/Epa.java new file mode 100644 index 0000000..d28fb7b --- /dev/null +++ b/src/main/java/com/xydl/model/Epa.java @@ -0,0 +1,65 @@ +package com.xydl.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; +import net.sf.jsqlparser.expression.DateTimeLiteralExpression; + +import java.sql.Timestamp; +import java.text.SimpleDateFormat; +import java.util.Date; + +/* +油色谱 + */ +@Data +public class Epa { + + @JsonProperty("SubDeviceID") + private String subDeviceID; + + @JsonProperty("SensorCode") + private String sensorId; + + @JsonProperty("AcquisitionTime") + private String dTime ; + + @JsonProperty("H2") + private double h2ppm; + + @JsonProperty("CH4") + private double ch4ppm; + + @JsonProperty("C2H6") + private double c2h6ppm; + + @JsonProperty("C2H4") + private double c2h4ppm; + + @JsonProperty("C2H2") + private double c2h2ppm; + + @JsonProperty("CO") + private double coppm; + + @JsonProperty("CO2") + private double co2ppm; + + @JsonProperty("O2") + private double o2ppm; + + @JsonProperty("N2") + private double n2ppm; + + @JsonProperty("TotalHydrocarbon") + private double totalHydroCarbon; + + @JsonProperty("GasPress") + private double gaspress; + + @JsonProperty("H2O") + private double h2oppm; + + @JsonProperty("Phase") + private String phase; + +} diff --git a/src/main/java/com/xydl/model/Etp.java b/src/main/java/com/xydl/model/Etp.java new file mode 100644 index 0000000..bc51eaa --- /dev/null +++ b/src/main/java/com/xydl/model/Etp.java @@ -0,0 +1,33 @@ +package com.xydl.model; + +import com.baomidou.mybatisplus.annotation.TableField; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +import java.sql.Timestamp; +import java.util.Date; + +@Data +public class Etp { + + @JsonProperty("SubDeviceID") + private String subDeviceID; + + @JsonProperty("SensorCode") + private String sensorId; + + @JsonProperty("AcquisitionTime") + private String dTime; + + @JsonProperty("MaxTmp") + private double t1; + + + + + + + + + +} diff --git a/src/main/java/com/xydl/model/Microclimate.java b/src/main/java/com/xydl/model/Microclimate.java new file mode 100644 index 0000000..33762d3 --- /dev/null +++ b/src/main/java/com/xydl/model/Microclimate.java @@ -0,0 +1,42 @@ +package com.xydl.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +import java.sql.Timestamp; +import java.util.Date; + +@Data +public class Microclimate { + @JsonProperty("SubDeviceID") + private String subDeviceID; + + @JsonProperty("SensorCode") + private String sensorId; + + @JsonProperty("AcquisitionTime") + private String dTime; + + @JsonProperty("AirTemperature") + private double envTmp; + + @JsonProperty("AirPressure") + private double envPres; + + @JsonProperty("Humidity") + private double envHum; + + @JsonProperty("Precipitation") + private double rnfll; + + @JsonProperty("PrecipitationIntensity") + private double PreciInten = 0; + + @JsonProperty("RadiationIntensity") + private double radiInten = 0; + + + + + +} diff --git a/src/main/java/com/xydl/model/Moa.java b/src/main/java/com/xydl/model/Moa.java new file mode 100644 index 0000000..65a939d --- /dev/null +++ b/src/main/java/com/xydl/model/Moa.java @@ -0,0 +1,36 @@ +package com.xydl.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +import java.sql.Timestamp; +import java.util.Date; + +@Data +public class Moa { + + @JsonProperty("SubDeviceID") + private String subDeviceID; + + @JsonProperty("SensorCode") + private String sensorId; + + @JsonProperty("AcquisitionTime") + private String captureTime; + + @JsonProperty("SystemVoltage") + private double pt1; + + @JsonProperty("TotalCurrent") + private double lc1; + + @JsonProperty("ResistiveCurrent") + private double rc1; + + @JsonProperty("ActionCount") + private double ligcnt1; + + @JsonProperty("LastActionTime") + private String lastligtm1; + +} diff --git a/src/main/java/com/xydl/model/Pd.java b/src/main/java/com/xydl/model/Pd.java new file mode 100644 index 0000000..f5b9450 --- /dev/null +++ b/src/main/java/com/xydl/model/Pd.java @@ -0,0 +1,30 @@ +package com.xydl.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +import java.sql.Timestamp; +import java.util.Date; + +@Data +public class Pd { + + @JsonProperty("SubDeviceID") + private String subDeviceID; + + @JsonProperty("SensorCode") + private String sensorId; + + @JsonProperty("AcquisitionTime") + private String dTime; + + @JsonProperty("DischargeCapacity") + private double waveForm; + + @JsonProperty("DischargePosition") + private double apppadsch; + + @JsonProperty("PulseCount") + private double plsNum; + +} diff --git a/src/main/java/com/xydl/model/RptTemper.java b/src/main/java/com/xydl/model/RptTemper.java new file mode 100644 index 0000000..aefe16f --- /dev/null +++ b/src/main/java/com/xydl/model/RptTemper.java @@ -0,0 +1,42 @@ +package com.xydl.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; +import java.sql.Timestamp; +import java.util.Date; + +@Data +public class RptTemper { + + @JsonProperty("SubDeviceID") + private String subDeviceID; + + @JsonProperty("SensorCode") + private String sensorId; + + @JsonProperty("AcquisitionTime") + private String createTime; + + @JsonProperty("OlTmpA") + private double aoTemper; + + @JsonProperty("OlTmpB") + private double boTemper; + + @JsonProperty("OlTmpC") + private double coTemper; + + @JsonProperty("IlTmpA") + private double aiTemper; + + @JsonProperty("IlTmpB") + private double biTemper; + + @JsonProperty("IlTmpC") + private double ciTemper; + + @JsonProperty("OntologyTmp") + private double boxTemper; + + +} diff --git a/src/main/java/com/xydl/model/Scur.java b/src/main/java/com/xydl/model/Scur.java new file mode 100644 index 0000000..bf38181 --- /dev/null +++ b/src/main/java/com/xydl/model/Scur.java @@ -0,0 +1,22 @@ +package com.xydl.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; +import java.sql.Timestamp; +import java.util.Date; + +@Data +public class Scur { + + @JsonProperty("SubDeviceID") + private String subDeviceID; + + @JsonProperty("SensorCode") + private String sensorId; + + @JsonProperty("AcquisitionTime") + private String dTime; + + @JsonProperty("CoreCurrent") + private double currentVal; +} diff --git a/src/main/java/com/xydl/model/Sf6.java b/src/main/java/com/xydl/model/Sf6.java new file mode 100644 index 0000000..ffda062 --- /dev/null +++ b/src/main/java/com/xydl/model/Sf6.java @@ -0,0 +1,35 @@ +package com.xydl.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; +import java.sql.Timestamp; +import java.util.Date; + +@Data +public class Sf6 { + + @JsonProperty("SubDeviceID") + private String subDeviceID; + + @JsonProperty("SensorCode") + private String sensorId; + + @JsonProperty("AcquisitionTime") + private String dTime; + + @JsonProperty("Temperature") + private double temp1; + + @JsonProperty("Pressure20C") + private double pressure1; + + @JsonProperty("AbsolutePressure") + private double AbsolutePressure = pressure1 + 900; + + @JsonProperty("Density") + private double md1; + + @JsonProperty("Moisture") + private double pm1; + +} diff --git a/src/main/java/com/xydl/model/Sf6env.java b/src/main/java/com/xydl/model/Sf6env.java new file mode 100644 index 0000000..d6ff49d --- /dev/null +++ b/src/main/java/com/xydl/model/Sf6env.java @@ -0,0 +1,43 @@ +package com.xydl.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; +import java.sql.Timestamp; +import java.util.Date; + +@Data +public class Sf6env { + + @JsonProperty("SubDeviceID") + private String subDeviceID; + + @JsonProperty("SensorCode") + private String sensorId; + + @JsonProperty("AcquisitionTime") + private String dTime; + + @JsonProperty("gas1") + private double gas1; + + @JsonProperty("yq1") + private double yq1; + + @JsonProperty("md1") + private double md1; + + @JsonProperty("pm1") + private double pm1; + + @JsonProperty("gascnt1") + private double gascnt1; + + @JsonProperty("hmcnt1") + private double hmcnt1; + + @JsonProperty("sf6warn1") + private double sf6warn1; + + @JsonProperty("o2warn1") + private double o2warn1; +} diff --git a/src/main/java/com/xydl/model/SuperModel.java b/src/main/java/com/xydl/model/SuperModel.java new file mode 100644 index 0000000..5eac7c3 --- /dev/null +++ b/src/main/java/com/xydl/model/SuperModel.java @@ -0,0 +1,231 @@ +package com.xydl.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class SuperModel { + + /** + * EPA + */ + @JsonProperty("SubDeviceID") + private String subDeviceID; + + @JsonProperty("SensorCode") + private String sensorId; + + @JsonProperty("AcquisitionTime") + private String dTime ; + + @JsonProperty("H2") + private double h2ppm; + + @JsonProperty("CH4") + private double ch4ppm; + + @JsonProperty("C2H6") + private double c2h6ppm; + + @JsonProperty("C2H4") + private double c2h4ppm; + + @JsonProperty("C2H2") + private double c2h2ppm; + + @JsonProperty("CO") + private double coppm; + + @JsonProperty("CO2") + private double co2ppm; + + @JsonProperty("O2") + private double o2ppm; + + @JsonProperty("N2") + private double n2ppm; + + @JsonProperty("TotalHydrocarbon") + private double totalHydroCarbon; + + @JsonProperty("GasPress") + private double gaspress; + + @JsonProperty("H2O") + private double h2oppm; + + @JsonProperty("Phase") + private String phase; + + /** + * Eaif + */ + +// @JsonProperty("AcquisitionTime") +// private String captureTime; + + @JsonProperty("MaxTmp") + private double maxTemp; + + @JsonProperty("MinTmp") + private double minTemp; + + @JsonProperty("AvgTmp") + private double avgTemp; + + /** + * Eia + */ + + /** + * Etp + */ + + +// @JsonProperty("MaxTmp") +// private double t1; + + + /** + * Microclimate + */ + + + @JsonProperty("AirTemperature") + private double envTmp; + + @JsonProperty("AirPressure") + private double envPres; + + @JsonProperty("Humidity") + private double envHum; + + @JsonProperty("Precipitation") + private double rnfll; + + @JsonProperty("PrecipitationIntensity") + private double PreciInten = 0; + + @JsonProperty("RadiationIntensity") + private double radiInten = 0; + + /** + * Moa + */ + + + @JsonProperty("SystemVoltage") + private double pt1; + + @JsonProperty("TotalCurrent") + private double lc1; + + @JsonProperty("ResistiveCurrent") + private double rc1; + + @JsonProperty("ActionCount") + private double ligcnt1; + + @JsonProperty("LastActionTime") + private String lastligtm1; + + /** + * Pd + */ + + + @JsonProperty("DischargeCapacity") + private double waveForm; + + @JsonProperty("DischargePosition") + private double apppadsch; + + @JsonProperty("PulseCount") + private double plsNum; + + /** + * RptTemper + */ + + +// @JsonProperty("AcquisitionTime") +// private String createTime; + + @JsonProperty("OlTmpA") + private double aoTemper; + + @JsonProperty("OlTmpB") + private double boTemper; + + @JsonProperty("OlTmpC") + private double coTemper; + + @JsonProperty("IlTmpA") + private double aiTemper; + + @JsonProperty("IlTmpB") + private double biTemper; + + @JsonProperty("IlTmpC") + private double ciTemper; + + @JsonProperty("OntologyTmp") + private double boxTemper; + + /** + * Scur + */ + + + @JsonProperty("CoreCurrent") + private double currentVal; + + /** + * Sf6 + */ + + + @JsonProperty("Temperature") + private double temp1; + + @JsonProperty("Pressure20C") + private double pressure1; + + @JsonProperty("AbsolutePressure") + private double AbsolutePressure = pressure1 + 900; + + @JsonProperty("Density") + private double md1Sf6; + + @JsonProperty("Moisture") + private double pm1Sf6; + + + /** + * Sf6env + */ + + @JsonProperty("gas1") + private double gas1; + + @JsonProperty("yq1") + private double yq1; + + @JsonProperty("md1") + private double md1; + + @JsonProperty("pm1") + private double pm1; + + @JsonProperty("gascnt1") + private double gascnt1; + + @JsonProperty("hmcnt1") + private double hmcnt1; + + @JsonProperty("sf6warn1") + private double sf6warn1; + + @JsonProperty("o2warn1") + private double o2warn1; + + +} diff --git a/src/main/java/com/xydl/service/EiaService.java b/src/main/java/com/xydl/service/EiaService.java new file mode 100644 index 0000000..efb8f7b --- /dev/null +++ b/src/main/java/com/xydl/service/EiaService.java @@ -0,0 +1,33 @@ +package com.xydl.service; + +import com.xydl.model.*; + +import java.util.List; +import java.util.Map; + +public interface EiaService { + + List getEaif(); + + List getEia(); + + void getEpa(); + + List getEtp(); + + List getMic(); + + List getMoa(); + + List getPd(); + + List getRptTemper(); + + List getScur(); + + List getSf6(); + + List getSf6env(); + + +} diff --git a/src/main/java/com/xydl/service/impl/EiaServiceImpl.java b/src/main/java/com/xydl/service/impl/EiaServiceImpl.java new file mode 100644 index 0000000..4acda12 --- /dev/null +++ b/src/main/java/com/xydl/service/impl/EiaServiceImpl.java @@ -0,0 +1,438 @@ +package com.xydl.service.impl; + + +import com.xydl.mapper.*; +import com.xydl.model.*; +import com.xydl.service.EiaService; +import com.xydl.util.CommonUtils; +import com.xydl.util.DataSourceUtils; +import com.xydl.util.FormatUtil; +import com.xydl.util.MqttUtil; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +import java.sql.*; +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.Date; + + +@Service +public class EiaServiceImpl implements EiaService { + private static final Logger logger = LoggerFactory.getLogger(EiaServiceImpl.class); + + private static final String SYNC_TABLE = "sync_tables_info"; + + @Autowired + private EaifMapper eaifMapper; + + @Autowired + private EpaMapper epaMapper; + + @Autowired + private EiaMapper eiaMapper; + + @Autowired + private EtpMapper etpMapper; + + @Autowired + private MicMapper micMapper; + + @Autowired + private MoaMapper moaMapper; + + @Autowired + private PdMapper pdMapper; + + @Autowired + private RptTemperMapper rptTemperMapper; + + @Autowired + private ScurMapper scurMapper; + + @Autowired + private Sf6Mapper sf6Mapper; + + @Autowired + private Sf6envMapper sf6envMapper; + + + @Override + public List getEaif() { + return eaifMapper.getEaif(); +// + } + + @Override + public List getEia() { + return eiaMapper.getEia(); + } + + @Override + public void getEpa() { + List tableNames = epaMapper.getTableNamesBySyncTable(SYNC_TABLE); + +// for(String tableName : tableNames){ +// System.out.println(tableName); +// List eqmids = epaMapper.getEqmidsByTableName(tableName); +// for(int eqmid : eqmids){ +// List epas = epaMapper.getEpa(eqmid); +// String jsonStringEpa = FormatUtil.list2Json(epas); +// MqttUtil.publish2MQTT(jsonStringEpa); +// } +// } + for(String tableName : tableNames){ + String sql = epaMapper.getSqlBySyncTable(SYNC_TABLE,tableName); + List eqmids = epaMapper.getEqmidsByTableName(tableName); + if(sql.contains("?")){ + for(int eqmid : eqmids){ + String newSql = sql.replaceAll("\\?",eqmid+""); + List epas = epaMapper.getData(newSql); + String jsonStringEpa = FormatUtil.list2Json(epas); + MqttUtil.publish2MQTT(jsonStringEpa); + } + } + } + } + + @Override + public List getEtp() { + return etpMapper.getEtp(); + } + + @Override + public List getMic() { + return micMapper.getMicroclimate(); + } + + @Override + public List getMoa() { + return moaMapper.getMoa(); + } + + @Override + public List getPd() { + return pdMapper.getPd(); + } + + @Override + public List getRptTemper() { + return rptTemperMapper.getRptTemper(); + } + + @Override + public List getScur() { + return scurMapper.getScur(); + } + + @Override + public List getSf6() { + return sf6Mapper.getSf6(); + } + + @Override + public List getSf6env() { + return sf6envMapper.getSf6env(); + } + + public List getAllTableName() { + Connection conn = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + List tableNames = new ArrayList<>(); + try { + conn = DataSourceUtils.getConnection(); + String sql = "select DISTINCT table_name from sync_tables_info"; + pstmt = conn.prepareStatement(sql); + rs = pstmt.executeQuery(); + + while(rs.next()){ + tableNames.add(rs.getString("table_name")); + } + } catch (SQLException e) { + logger.error("execute sql exception:", e); + } finally { + DataSourceUtils.closeResource(rs, pstmt, conn); + } + + return tableNames; + } + + public Map getFieldMap(String tableName) { + Connection conn = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + Map fieldsMap = new HashMap<>(); + try { + conn = DataSourceUtils.getConnection(); + String sql = "select sync_fields_info.field_name, sync_fields_info.dest_field_name " + + "from sync_fields_info,sync_tables_info " + + "where sync_fields_info.client_id = 10 and sync_fields_info.table_name = sync_tables_info.table_name and sync_tables_info.table_name=?"; + pstmt = conn.prepareStatement(sql); + pstmt.setString(1, tableName); + rs = pstmt.executeQuery(); + ResultSetMetaData metaData = rs.getMetaData(); + + while(rs.next()){ + fieldsMap.put(rs.getString("field_name"),rs.getString("dest_field_name")); + } + } catch (SQLException e) { + logger.error("execute sql exception:", e); + } finally { + DataSourceUtils.closeResource(rs, pstmt, conn); + } + + return fieldsMap; + } + + public String getLastRecordTimeSended(String tableName,String deviceId) { + Connection conn = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + Timestamp timeStamp = null; + try { + conn = DataSourceUtils.getConnection(); + String sql = "select field_val2 from sync_records where table_name =? and devid_val=?"; + pstmt = conn.prepareStatement(sql); + pstmt.setString(1, tableName); + pstmt.setString(2, deviceId); + rs = pstmt.executeQuery(); + if(rs.next()){ + timeStamp = rs.getTimestamp("field_val2"); + + } + } catch (SQLException e) { + logger.error("execute sql exception:", e); + } finally { + DataSourceUtils.closeResource(rs, pstmt, conn); + } + + return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timeStamp); + } + + public List getDeviceID(String tableName) { + Connection conn = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + String sqlExecuting = null ; + List deviceIDs = new ArrayList<>(); + try { + conn = DataSourceUtils.getConnection(); + String sql = "select distinct devid_val from sync_records where table_name =?"; + pstmt = conn.prepareStatement(sql); + pstmt.setString(1, tableName); + rs = pstmt.executeQuery(); + while(rs.next()){ + deviceIDs.add(rs.getString("devid_val")); + } + } catch (SQLException e) { + logger.error("execute sql exception:", e); + } finally { + DataSourceUtils.closeResource(rs, pstmt, conn); + } + + return deviceIDs; + } + + public String getSQL(String tableName) { + Connection conn = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + String sqlExecuting = null ; + try { + conn = DataSourceUtils.getConnection(); + String sql = "select * from sync_tables_info where table_name =?"; + pstmt = conn.prepareStatement(sql); + pstmt.setString(1, tableName); + rs = pstmt.executeQuery(); + if(rs.next()){ + sqlExecuting = rs.getString("sql"); + } + } catch (SQLException e) { + logger.error("execute sql exception:", e); + } finally { + DataSourceUtils.closeResource(rs, pstmt, conn); + } + + return sqlExecuting; + } + + public List> getData(String sqlExecuting, String deviceId, String timeStamp) { + Connection conn = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + List> records = new ArrayList<>(); + try { + conn = DataSourceUtils.getConnection(); + String sql = sqlExecuting; + pstmt = conn.prepareStatement(sql); + pstmt.setString(1, deviceId); + pstmt.setString(2,timeStamp); + rs = pstmt.executeQuery(); + int columnCount = rs.getMetaData().getColumnCount(); //获取列的数量 + while(rs.next()){ + Map record = new HashMap<>(); + for (int col = 0; col < columnCount; col++) { + String columnName = rs.getMetaData().getColumnName(col + 1); + String columnValue = rs.getString(columnName); + record.put(columnName,columnValue); + } + records.add(record); + } + } catch (SQLException e) { + logger.error("execute sql exception:", e); + } finally { + DataSourceUtils.closeResource(rs, pstmt, conn); + } + + return records; + } + + + @Scheduled(initialDelay=1000, fixedRate = 1000 * 3600) //通过@Scheduled声明该方法是计划任务,使用fixedRate属性每隔固定时间执行 + public void reportRecord(){ + List allTableNames = getAllTableName(); + Map> tableFieldMap = new HashMap<>(); + Map>>>> allTableData = new HashMap<>(); + for(String tablenName : allTableNames){ + //if用来测试 + if(!"data_eaif_h".equals(tablenName)){ + Map fieldMap = getFieldMap(tablenName); + tableFieldMap.put(tablenName,fieldMap); + + String sqlExecuting = getSQL(tablenName); + List deviceIDs = getDeviceID(tablenName); + List>>> dataOfoneTable = new ArrayList<>(); + for(String deviceID : deviceIDs){ + Map>> deviceIDMap = new HashMap<>(); + String timeStamp = getLastRecordTimeSended(tablenName,deviceID); + List> dataOfoneDeviceID = getData(sqlExecuting,deviceID,timeStamp); + deviceIDMap.put(deviceID,dataOfoneDeviceID); + dataOfoneTable.add(deviceIDMap); + } + allTableData.put(tablenName,dataOfoneTable); + } + + } + System.out.println("旧数据: "+allTableData); + System.out.println("==============================="); + + + Map>>>> newAllData = new HashMap<>(); + for(Map.Entry>>>> dataEntry : allTableData.entrySet()){ + List>>> newRecords = transformFields(dataEntry.getKey(),tableFieldMap,dataEntry.getValue()); + newAllData.put(dataEntry.getKey(),newRecords); + } + + for(String tableName : newAllData.keySet()){ + List>>> records = newAllData.get(tableName); + String jsonStringData = FormatUtil.list2Json(records); + if(MqttUtil.publish2MQTT(jsonStringData)){ + updateLastRecordTimeSended(tableName,records); + }else{ + System.out.println("消息推送失败"); + } + } + + + } + + //返回替换字段名的records + private List>>> transformFields(String recordTableName, Map> tableFieldMap, List>>> records) { + List>>> newRecords = new ArrayList<>(); + if(tableFieldMap.containsKey(recordTableName)){ + for(Map>> record : records ){ + newRecords.add(transformMore(tableFieldMap.get(recordTableName),record)); + } + } + return newRecords; + } + + private Map>> transformMore(Map fieldMap, Map>> deviceIDDataMap) { + Map>> newDeviceIDData = new HashMap<>(); + for(Map.Entry>> entry : deviceIDDataMap.entrySet()){ + newDeviceIDData.put(entry.getKey(),transformMoreAgain(fieldMap, entry.getValue())); + } + return newDeviceIDData; + } + + private List> transformMoreAgain(Map fieldMap, List> deviceIDData) { + List> newDeviceIDData = new ArrayList<>(); + for(Map fieldValueMap : deviceIDData){ + newDeviceIDData.add(transformMoreAgain2(fieldMap,fieldValueMap)); + } + return newDeviceIDData; + } + + private Map transformMoreAgain2(Map fieldMap, Map fieldValueMap) { + Map newFieldValueMap = new HashMap<>(); + for(String field : fieldMap.keySet()){ + for(String columnName : fieldValueMap.keySet() ){ + if(Objects.equals(field,columnName)){ + newFieldValueMap.put(fieldMap.get(field),fieldValueMap.get(columnName) ); + } + } + } + return newFieldValueMap; + } + + + public void updateLastRecordTimeSended(String tableName, List>>> records) { + List deviceIDs = getDeviceID(tableName); + for(String deviceID : deviceIDs){ + String lastRecordTimesJustSended = null; + for(Map>> recordMap : records){ + if(recordMap.get(deviceID) != null){ + List> deviceIDData = recordMap.get(deviceID); + lastRecordTimesJustSended = (String) deviceIDData.get(deviceIDData.size()-1).get("AcquisitionTime"); + System.out.println(tableName+"表"+deviceID+"最后一条记录时间: "+lastRecordTimesJustSended); + } + + } +// updateSyncRecordsTable(tableName, deviceID, lastRecordTimesJustSended); + + } + + } + + + public boolean updateSyncRecordsTable(String tableName, String deviceID, String time) { + Connection conn = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + conn = DataSourceUtils.getConnection(); + String sql = "update sync_records set field_val2 = ? where table_name = ? and devid_val = ?"; + pstmt = conn.prepareStatement(sql); + pstmt.setString(1, time); + pstmt.setString(2, tableName); + pstmt.setString(3, deviceID); + pstmt.executeUpdate(); + + } catch (SQLException e) { + logger.error("execute sql exception:", e); + return false; + } finally { + DataSourceUtils.closeResource(rs, pstmt, conn); + } + return true; + } + + + @Scheduled(fixedDelay = Long.MAX_VALUE) // 用一个非常大的延迟值,确保只执行一次 + public void subScribeSamle() { + System.out.println("subScribe执行一次 "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); + MqttUtil.subScribeMQTT(); + } + + + + + + +} + diff --git a/src/main/java/com/xydl/util/CommonUtils.java b/src/main/java/com/xydl/util/CommonUtils.java new file mode 100644 index 0000000..5c3a872 --- /dev/null +++ b/src/main/java/com/xydl/util/CommonUtils.java @@ -0,0 +1,259 @@ +package com.xydl.util; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.sql.Array; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.text.SimpleDateFormat; +import java.util.*; + +public class CommonUtils { + private static final Logger logger = LoggerFactory.getLogger(CommonUtils.class); + + private static final boolean languageZh = getPropertyValue("system.language", "zh").equals("zh"); + private static final ObjectMapper objectMapper = new ObjectMapper(); + + + + private static final long startupTime = System.currentTimeMillis(); + private static final int startingTime = Integer.parseInt(getPropertyValue("system.startup.time", "60000")); + + + + public static long getStartupTime() { + return startupTime; + } + + public static boolean started() { + return System.currentTimeMillis() - startupTime > startingTime; + } + + public static boolean isLanguageZh() { + return languageZh; + } + + + private static boolean updateConfig(String name, Map value) { + Connection conn = null; + PreparedStatement pstmt = null; + try { + conn = DataSourceUtils.getConnection(); + String sql = "update global_configuration set value=?::json where name=?"; + pstmt = conn.prepareStatement(sql); + pstmt.setString(1, CommonUtils.getJsonFromObject(value)); + pstmt.setString(2, name); + pstmt.executeUpdate(); + } catch (Exception e) { + logger.error("execute sql exception:", e); + return false; + } finally { + DataSourceUtils.closeResource(pstmt, conn); + } + return true; + } + + private static boolean addConfig(String name, Map value) { + Connection conn = null; + PreparedStatement pstmt = null; + try { + conn = DataSourceUtils.getConnection(); + String sql = "INSERT INTO global_configuration(name, value) VALUES (?, ?::json)"; + pstmt = conn.prepareStatement(sql); + pstmt.setString(1, name); + pstmt.setString(2, CommonUtils.getJsonFromObject(value)); + pstmt.executeUpdate(); + } catch (Exception e) { + logger.error("execute sql exception:", e); + return false; + } finally { + DataSourceUtils.closeResource(pstmt, conn); + } + return true; + } + + public static ObjectMapper getObjectMapper() { + return objectMapper; + } + + public static T convertJsonToObject(String content, Class valueType) { + if (content == null) { + return null; + } + try { + return objectMapper.readValue(content.getBytes(StandardCharsets.UTF_8), valueType); + } catch (IOException e) { + logger.error("convert json to Object exception:", e); + } + return null; + } + + public static List convertJsonToList(String content, Class valueType) { + if (content == null || content.length() == 0) { + return new ArrayList<>(); + } + try { + List list = objectMapper.readValue(content.getBytes(StandardCharsets.UTF_8), new TypeReference>() { + }); + List ret = new ArrayList<>(); + for (T t : list) { + ret.add(objectMapper.convertValue(t, valueType)); + } + return ret; + } catch (IOException e) { + logger.error("convert json to Object exception:", e); + } + return null; + } + + public static String getJsonFromObject(Object object) { + String json = null; + try { + json = CommonUtils.getObjectMapper().writeValueAsString(object); + } catch (JsonProcessingException ignored) { + } + return json; + } + + public static String getJsonValue(String jsonString, String key) { + Map jsonMap; + try { + jsonMap = objectMapper.readValue(jsonString, new TypeReference>() { + }); + } catch (IOException ignored) { + return null; + } + + return jsonMap.get(key); + } + + public static String getJsonString(Map map) { + String result = ""; + try { + result = objectMapper.writeValueAsString(map); + } catch (JsonProcessingException ignored) { + } + return result; + } + + public static Map getJsonMap(String jsonString) { + Map jsonMap; + try { + jsonMap = objectMapper.readValue(jsonString, new TypeReference>() { + }); + } catch (IOException ignored) { + jsonMap = new HashMap<>(); + } + return jsonMap; + } + + public static Map getMapFromJson(String jsonString) { + try { + return objectMapper.readValue(jsonString, new TypeReference>() { + }); + } catch (IOException ignored) { + } + return new HashMap<>(); + } + + public static String getPropertyValue(String propertyName) { + Properties properties = loadPropertyFile("sdwan.common.cfg"); + if (properties != null) { + return properties.getProperty(propertyName); + } + return null; + } + + public static String getPropertyValue(String propertyName, String defaultValue) { + Properties properties = loadPropertyFile("sdwan.common.cfg"); + if (properties != null) { + String value = properties.getProperty(propertyName); + return value == null ? defaultValue : value; + } + return defaultValue; + } + + public static Properties loadPropertyFile(String fileName) { + String filePath = System.getProperty("user.dir").concat("/etc/").concat(fileName); + File file = new File(filePath); + if (!file.exists()) { + return null; + } + + try (InputStream is = new FileInputStream(file)) { + Properties properties = new Properties(); + properties.load(is); + return properties; + } catch (IOException e) { + logger.error("load property file exception:", e); + } + + return null; + } + + public static boolean setProperty(String fileName, String key, String value) { + Properties properties = loadPropertyFile(fileName); + if (properties == null) { + properties = new Properties(); + } + properties.setProperty(key, value); + return savePropertyFile(fileName, properties); + } + + public static boolean savePropertyFile(String fileName, Properties properties) { + String filePath = System.getProperty("user.dir").concat("/etc/").concat(fileName); + File file = new File(filePath); + if (!file.exists()) { + return false; + } + + try (OutputStream os = new FileOutputStream(file)) { + properties.store(os, null); + } catch (IOException e) { + logger.error("write file exception:", e); + return false; + } + + return true; + } + + + + + + @SuppressWarnings("unchecked") + public static List getListFromArray(Array array) { + if (array == null) { + return null; + } + try { + return Arrays.asList((T[]) array.getArray()); + } catch (SQLException ignored) { + } + + return null; + } + + + + + + + + public static String formatDate(Date date) { + return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); + } + + + +// public static void main(String args[]) { +// System.out.println(CommonUtils.getLanguage()); +// } +} diff --git a/src/main/java/com/xydl/util/DataSourceUtils.java b/src/main/java/com/xydl/util/DataSourceUtils.java new file mode 100644 index 0000000..0649ec1 --- /dev/null +++ b/src/main/java/com/xydl/util/DataSourceUtils.java @@ -0,0 +1,101 @@ +package com.xydl.util; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.pool.DruidDataSourceFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.sql.DataSource; +import java.sql.*; +import java.util.Properties; + +public class DataSourceUtils { + private static final Logger logger = LoggerFactory.getLogger(DataSourceUtils.class); + private static DataSource dataSource; + private static String url; + + static { + initDataSource(); + } + + private static void initDataSource() { + Properties properties = CommonUtils.loadPropertyFile("sdwan.datasource.druid.cfg"); + if (properties == null) { + logger.error("init dataSource failed, no config found."); + properties = new Properties(); + properties.setProperty("driverClassName", "com.mysql.cj.jdbc.Driver"); + properties.setProperty("url", "jdbc:mysql://localhost:3306/cac"); + properties.setProperty("username", "root"); + properties.setProperty("password", "root"); + } + try { + dataSource = DruidDataSourceFactory.createDataSource(properties); + url = ((DruidDataSource) dataSource).getUrl(); + } catch (Exception e) { + logger.error("init dataSource exception:", e); + } + logger.info("Data source has been initialized successfully!"); + } + + // 提供获取连接的方法 + public static Connection getConnection() throws SQLException { + return dataSource.getConnection(); + } + + public static String getDatabaseIp() { + try { + return url.substring(url.indexOf("//") + 2, url.lastIndexOf(":")); + } catch (IndexOutOfBoundsException ignored) { + } + return ""; + } + + // 提供关闭资源的方法【connection是归还到连接池】 + // 提供关闭资源的方法 【方法重载】3 dql + public static void closeResource(ResultSet resultSet, Statement statement, Connection connection) { + // 关闭结果集 + closeResultSet(resultSet); + // 关闭语句执行者 + closeStatement(statement); + // 关闭连接 + closeConnection(connection); + } + + // 提供关闭资源的方法 【方法重载】 2 dml + public static void closeResource(Statement statement, Connection connection) { + // 关闭语句执行者 + closeStatement(statement); + // 关闭连接 + closeConnection(connection); + } + + private static void closeConnection(Connection connection) { + if (connection != null) { + try { + connection.close(); + } catch (SQLException e) { + logger.error("closeConnection exception", e); + } + } + } + + private static void closeStatement(Statement statement) { + if (statement != null) { + try { + statement.close(); + } catch (SQLException e) { + logger.error("closeStatement exception", e); + } + } + } + + private static void closeResultSet(ResultSet resultSet) { + if (resultSet != null) { + try { + resultSet.close(); + } catch (SQLException e) { + logger.error("closeResultSet exception", e); + } + } + } +} diff --git a/src/main/java/com/xydl/util/FormatUtil.java b/src/main/java/com/xydl/util/FormatUtil.java new file mode 100644 index 0000000..bc0fdad --- /dev/null +++ b/src/main/java/com/xydl/util/FormatUtil.java @@ -0,0 +1,34 @@ +package com.xydl.util; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class FormatUtil { + + public static String list2Json(List list){ + ObjectMapper objectMapper = new ObjectMapper(); + List> assetList = new ArrayList<>(); + assetList.add(new HashMap(){{ + put("AssetCode","ironCore"); + put("AttributeList",list); + put("Timestamp", 1606800979591089792L); + }}); + Map resultMap = new HashMap(){{ + put("AssetList",assetList); + }}; + String jsonString = null; + try { + jsonString = objectMapper.writeValueAsString(resultMap); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + return jsonString; + } + + +} diff --git a/src/main/java/com/xydl/util/MqttUtil.java b/src/main/java/com/xydl/util/MqttUtil.java new file mode 100644 index 0000000..fc93a9c --- /dev/null +++ b/src/main/java/com/xydl/util/MqttUtil.java @@ -0,0 +1,106 @@ +package com.xydl.util; + +import org.eclipse.paho.client.mqttv3.*; +import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class MqttUtil { + + public static boolean publish2MQTT(String content){ + String broker = "tcp://139.196.211.222:10883"; + String topic = "mqtt/test"; +// String content = "Hello MQTT"; + int qos = 0; + String username = "test"; + String password = "AliOS%1688"; + String clientid = "publish_client"; + MqttClient client; + MqttDeliveryToken token; + try { + client = new MqttClient(broker, clientid, new MemoryPersistence()); + } catch (MqttException e) { + throw new RuntimeException(e); + } + MqttConnectOptions options = new MqttConnectOptions(); + options.setUserName(username); + options.setPassword(password.toCharArray()); + options.setConnectionTimeout(60); + options.setKeepAliveInterval(30); + // connect + try { + client.connect(options); + } catch (MqttException e) { + throw new RuntimeException(e); + } + // create message and setup QoS + MqttMessage message = new MqttMessage(content.getBytes()); + message.setQos(qos); + // publish message + try { + token = client.getTopic(topic).publish(message); + token.waitForCompletion(); + //打印发送状态 + System.out.println("messagei is published completely!" + token.isComplete()); +// client.publish(topic, message); + } catch (MqttException e) { + throw new RuntimeException(e); + } + System.out.println("Message published"); + System.out.println("topic: " + topic); + System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+ " message content推送: " + content); + // disconnect + try { + client.disconnect(); + } catch (MqttException e) { + throw new RuntimeException(e); + } + // close client + try { + client.close(); + } catch (MqttException e) { + throw new RuntimeException(e); + } + + return token.isComplete(); + } + + public static void subScribeMQTT(){ + String broker = "tcp://139.196.211.222:10883"; + String topic = "mqtt/test"; + String username = "test"; + String password = "AliOS%1688"; + String clientid = "subscribe_client"; + int qos = 0; + try { + MqttClient client = new MqttClient(broker, clientid, new MemoryPersistence()); + // connect options + MqttConnectOptions options = new MqttConnectOptions(); + options.setUserName(username); + options.setPassword(password.toCharArray()); + options.setConnectionTimeout(60); + options.setKeepAliveInterval(30); + // setup callback + client.setCallback(new MqttCallback() { + public void connectionLost(Throwable cause) { + System.out.println("connectionLost: " + cause.getMessage()); + } + public void messageArrived(String topic, MqttMessage message) { + System.out.println("topic: " + topic); + System.out.println("Qos: " + message.getQos()); + System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+" message content接收: " + new String(message.getPayload())); + + } + public void deliveryComplete(IMqttDeliveryToken token) { + System.out.println("deliveryComplete---------" + token.isComplete()); + } + }); + client.connect(options); + client.subscribe(topic, qos); + } catch (Exception e) { + e.printStackTrace(); } + } + + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..f57d40d --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,27 @@ +#端口号 +server: + port: 8088 + + #数据源配置 +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/cac?characterEncoding=utf-8&serverTimezone=UTC + username: root + password: root + #sql: + #init: + #指定脚本文件位置 + #schema-locations: classpath:user.sql + #初始化方式 + #mode: always + #设置数据源类型C + type: com.alibaba.druid.pool.DruidDataSource +mybatis: + configuration: + map-underscore-to-camel-case: true + #mybatis: + #指定mybatis配置文件的位置 + #config-location: classpath:mybatis/mybatis-config.xml + #指定映射文件的位置 + mapper-locations: classpath:mybatis/mapper/*.xml \ No newline at end of file diff --git a/src/main/resources/com/xydl/mapper/EaifMapper.xml b/src/main/resources/com/xydl/mapper/EaifMapper.xml new file mode 100644 index 0000000..6d2fe78 --- /dev/null +++ b/src/main/resources/com/xydl/mapper/EaifMapper.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + insert into employee values (#{id},#{lastName},#{email},#{gender},#{department.id}) + + + + update employee + + + last_name=#{employee.lastName}, + + + email=#{employee.email}, + + + gender=#{employee.gender}, + + + d_id=#{employee.department.id} + + + where id = #{id} + + + + delete from employee where id=#{id} + + \ No newline at end of file diff --git a/src/main/resources/com/xydl/mapper/EiaMapper.xml b/src/main/resources/com/xydl/mapper/EiaMapper.xml new file mode 100644 index 0000000..5372a41 --- /dev/null +++ b/src/main/resources/com/xydl/mapper/EiaMapper.xml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + insert into employee values (#{id},#{lastName},#{email},#{gender},#{department.id}) + + + + update employee + + + last_name=#{employee.lastName}, + + + email=#{employee.email}, + + + gender=#{employee.gender}, + + + d_id=#{employee.department.id} + + + where id = #{id} + + + + delete from employee where id=#{id} + + \ No newline at end of file diff --git a/src/main/resources/com/xydl/mapper/EpaMapper.xml b/src/main/resources/com/xydl/mapper/EpaMapper.xml new file mode 100644 index 0000000..c5d0e4f --- /dev/null +++ b/src/main/resources/com/xydl/mapper/EpaMapper.xml @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + insert into employee values (#{id},#{lastName},#{email},#{gender},#{department.id}) + + + + update employee + + + last_name=#{employee.lastName}, + + + email=#{employee.email}, + + + gender=#{employee.gender}, + + + d_id=#{employee.department.id} + + + where id = #{id} + + + + delete from employee where id=#{id} + + \ No newline at end of file diff --git a/src/main/resources/com/xydl/mapper/EtpMapper.xml b/src/main/resources/com/xydl/mapper/EtpMapper.xml new file mode 100644 index 0000000..c80f768 --- /dev/null +++ b/src/main/resources/com/xydl/mapper/EtpMapper.xml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + insert into employee values (#{id},#{lastName},#{email},#{gender},#{department.id}) + + + + update employee + + + last_name=#{employee.lastName}, + + + email=#{employee.email}, + + + gender=#{employee.gender}, + + + d_id=#{employee.department.id} + + + where id = #{id} + + + + delete from employee where id=#{id} + + \ No newline at end of file diff --git a/src/main/resources/com/xydl/mapper/MicMapper.xml b/src/main/resources/com/xydl/mapper/MicMapper.xml new file mode 100644 index 0000000..6ad6a16 --- /dev/null +++ b/src/main/resources/com/xydl/mapper/MicMapper.xml @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + insert into employee values (#{id},#{lastName},#{email},#{gender},#{department.id}) + + + + update employee + + + last_name=#{employee.lastName}, + + + email=#{employee.email}, + + + gender=#{employee.gender}, + + + d_id=#{employee.department.id} + + + where id = #{id} + + + + delete from employee where id=#{id} + + \ No newline at end of file diff --git a/src/main/resources/com/xydl/mapper/MoaMapper.xml b/src/main/resources/com/xydl/mapper/MoaMapper.xml new file mode 100644 index 0000000..e7d9436 --- /dev/null +++ b/src/main/resources/com/xydl/mapper/MoaMapper.xml @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + insert into employee values (#{id},#{lastName},#{email},#{gender},#{department.id}) + + + + update employee + + + last_name=#{employee.lastName}, + + + email=#{employee.email}, + + + gender=#{employee.gender}, + + + d_id=#{employee.department.id} + + + where id = #{id} + + + + delete from employee where id=#{id} + + \ No newline at end of file diff --git a/src/main/resources/com/xydl/mapper/PdMapper.xml b/src/main/resources/com/xydl/mapper/PdMapper.xml new file mode 100644 index 0000000..e20de19 --- /dev/null +++ b/src/main/resources/com/xydl/mapper/PdMapper.xml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + insert into employee values (#{id},#{lastName},#{email},#{gender},#{department.id}) + + + + update employee + + + last_name=#{employee.lastName}, + + + email=#{employee.email}, + + + gender=#{employee.gender}, + + + d_id=#{employee.department.id} + + + where id = #{id} + + + + delete from employee where id=#{id} + + \ No newline at end of file diff --git a/src/main/resources/com/xydl/mapper/RptTemperMapper.xml b/src/main/resources/com/xydl/mapper/RptTemperMapper.xml new file mode 100644 index 0000000..35b33cb --- /dev/null +++ b/src/main/resources/com/xydl/mapper/RptTemperMapper.xml @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + insert into employee values (#{id},#{lastName},#{email},#{gender},#{department.id}) + + + + update employee + + + last_name=#{employee.lastName}, + + + email=#{employee.email}, + + + gender=#{employee.gender}, + + + d_id=#{employee.department.id} + + + where id = #{id} + + + + delete from employee where id=#{id} + + \ No newline at end of file diff --git a/src/main/resources/com/xydl/mapper/ScurMapper.xml b/src/main/resources/com/xydl/mapper/ScurMapper.xml new file mode 100644 index 0000000..4ec60a1 --- /dev/null +++ b/src/main/resources/com/xydl/mapper/ScurMapper.xml @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + insert into employee values (#{id},#{lastName},#{email},#{gender},#{department.id}) + + + + update employee + + + last_name=#{employee.lastName}, + + + email=#{employee.email}, + + + gender=#{employee.gender}, + + + d_id=#{employee.department.id} + + + where id = #{id} + + + + delete from employee where id=#{id} + + \ No newline at end of file diff --git a/src/main/resources/com/xydl/mapper/Sf6Mapper.xml b/src/main/resources/com/xydl/mapper/Sf6Mapper.xml new file mode 100644 index 0000000..c334cd5 --- /dev/null +++ b/src/main/resources/com/xydl/mapper/Sf6Mapper.xml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + insert into employee values (#{id},#{lastName},#{email},#{gender},#{department.id}) + + + + update employee + + + last_name=#{employee.lastName}, + + + email=#{employee.email}, + + + gender=#{employee.gender}, + + + d_id=#{employee.department.id} + + + where id = #{id} + + + + delete from employee where id=#{id} + + \ No newline at end of file diff --git a/src/main/resources/com/xydl/mapper/Sf6envMapper.xml b/src/main/resources/com/xydl/mapper/Sf6envMapper.xml new file mode 100644 index 0000000..35d414f --- /dev/null +++ b/src/main/resources/com/xydl/mapper/Sf6envMapper.xml @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + insert into employee values (#{id},#{lastName},#{email},#{gender},#{department.id}) + + + + update employee + + + last_name=#{employee.lastName}, + + + email=#{employee.email}, + + + gender=#{employee.gender}, + + + d_id=#{employee.department.id} + + + where id = #{id} + + + + delete from employee where id=#{id} + + \ No newline at end of file diff --git a/src/main/resources/templates/a.html b/src/main/resources/templates/a.html new file mode 100644 index 0000000..ebd090b --- /dev/null +++ b/src/main/resources/templates/a.html @@ -0,0 +1,12 @@ + + + + + Title + + +

${list}

+

${name}

+ + + \ No newline at end of file