commit c6de0847fd9aa7be30a27e333ff64f66a556649e
Author: wany <13995595726@qq.com>
Date: Mon Nov 27 10:41:34 2023 +0800
天台山水库获取水质信息
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..89893da
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,212 @@
+
+
+ 4.0.0
+
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.7.1
+
+
+
+ com.whdc
+ ttssk-service
+ 1.0
+ 天台山水库
+
+
+ 8
+ 8
+ 1.8
+ target/release
+
+
+
+
+
+ ali-maven
+ https://maven.aliyun.com/nexus/content/groups/public
+
+ true
+
+
+ true
+ always
+ fail
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+ org.springframework.boot
+ spring-boot-starter-aop
+
+
+ com.baomidou
+ mybatis-plus-boot-starter
+ 3.5.2
+
+
+ org.springframework.boot
+ spring-boot-starter-validation
+
+
+ org.springframework.boot
+ spring-boot-starter-data-redis
+
+
+
+ mysql
+ mysql-connector-java
+ 8.0.16
+
+
+
+
+
+ cn.dev33
+ sa-token-spring-boot-starter
+ 1.30.0
+
+
+ cn.dev33
+ sa-token-dao-redis-jackson
+ 1.30.0
+
+
+
+ org.apache.commons
+ commons-pool2
+
+
+
+
+
+
+ com.github.xiaoymin
+ knife4j-spring-boot-starter
+ 3.0.3
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-devtools
+ runtime
+ true
+
+
+
+
+
+ org.projectlombok
+ lombok
+
+
+
+
+
+ com.alibaba
+ fastjson
+ 1.2.80
+
+
+
+
+ org.eclipse.paho
+ org.eclipse.paho.client.mqttv3
+ 1.2.0
+
+
+
+ com.alibaba
+ druid
+ 1.2.8
+
+
+
+
+
+
+ com.github.jeffreyning
+ mybatisplus-plus
+ 1.7.0-RELEASE
+
+
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-jar-plugin
+
+
+ false
+
+
+ true
+
+ lib/
+
+ com.whdc.SpringApplication
+
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-dependency-plugin
+
+
+ copy-lib
+ package
+
+ copy-dependencies
+
+
+ ${copy.jar.directory}/lib
+ false
+ false
+ runtime
+
+
+
+
+
+
+
+ maven-antrun-plugin
+
+
+ copy
+ package
+
+
+
+
+
+
+
+
+
+
+ run
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/com/whdc/TtsskApplication.java b/src/main/java/com/whdc/TtsskApplication.java
new file mode 100644
index 0000000..4bb1050
--- /dev/null
+++ b/src/main/java/com/whdc/TtsskApplication.java
@@ -0,0 +1,36 @@
+package com.whdc;
+
+import com.github.jeffreyning.mybatisplus.conf.EnableMPP;
+import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
+import lombok.extern.slf4j.Slf4j;
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.beans.BeansException;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+
+/**
+ * @author 李赛
+ * @date 2022-06-26 0:17
+ */
+@Slf4j
+@EnableMPP
+@EnableWebMvc
+@EnableKnife4j
+@EnableScheduling
+@SpringBootApplication
+@MapperScan("com.whdc.mapper")
+public class TtsskApplication {
+ public static void main(String[] args) {
+ try {
+ System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>> 启动程序 <<<<<<<<<<<<<<<<<<<<<<<<<<<");
+
+ SpringApplication.run(TtsskApplication.class, args);
+ } catch (BeansException e) {
+ e.printStackTrace();
+ log.error(e.getMessage(), e);
+ }
+ }
+
+}
diff --git a/src/main/java/com/whdc/entity/WqR.java b/src/main/java/com/whdc/entity/WqR.java
new file mode 100644
index 0000000..9657184
--- /dev/null
+++ b/src/main/java/com/whdc/entity/WqR.java
@@ -0,0 +1,52 @@
+package com.whdc.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.Data;
+import lombok.Getter;
+import lombok.Setter;
+
+import java.math.BigDecimal;
+import java.util.Date;
+
+/**
+ *
+ * 水质信息表
+ *
+ *
+ * @author wanyan
+ * @since 2023-11-01
+ */
+@Getter
+@Setter
+@TableName("wq_r")
+@Data
+public class WqR {
+
+ @TableId(value = "stcd")
+ private String stcd;
+
+ @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+ @TableField("tm")
+ private Date tm;
+
+ @TableField("chlorophyll")
+ private BigDecimal chlorophyll;
+
+ @TableField("blueAlgae")
+ private BigDecimal blueAlgae;
+
+ @TableField("greenAlgae")
+ private BigDecimal greenAlgae;
+
+ @TableField("diatom")
+ private BigDecimal diatom;
+
+ @TableField("cryptomonas")
+ private BigDecimal cryptomonas;
+
+
+}
diff --git a/src/main/java/com/whdc/entity/WqReal.java b/src/main/java/com/whdc/entity/WqReal.java
new file mode 100644
index 0000000..b72d45a
--- /dev/null
+++ b/src/main/java/com/whdc/entity/WqReal.java
@@ -0,0 +1,51 @@
+package com.whdc.entity;
+
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.Data;
+import lombok.Getter;
+import lombok.Setter;
+
+import java.math.BigDecimal;
+import java.util.Date;
+
+/**
+ *
+ * 水质实时信息表
+ *
+ *
+ * @author wanyan
+ * @since 2023-11-01
+ */
+@Getter
+@Setter
+@TableName("wq_real")
+@Data
+public class WqReal {
+
+ @TableId(value = "stcd")
+ private String stcd;
+
+ @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+ @TableField("tm")
+ private Date tm;
+
+ @TableField("chlorophyll")
+ private BigDecimal chlorophyll;
+
+ @TableField("blueAlgae")
+ private BigDecimal blueAlgae;
+
+ @TableField("greenAlgae")
+ private BigDecimal greenAlgae;
+
+ @TableField("diatom")
+ private BigDecimal diatom;
+
+ @TableField("cryptomonas")
+ private BigDecimal cryptomonas;
+
+
+}
diff --git a/src/main/java/com/whdc/mapper/WqRMapper.java b/src/main/java/com/whdc/mapper/WqRMapper.java
new file mode 100644
index 0000000..52916a8
--- /dev/null
+++ b/src/main/java/com/whdc/mapper/WqRMapper.java
@@ -0,0 +1,17 @@
+package com.whdc.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.whdc.entity.WqR;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ *
+ * 水质信息表 Mapper 接口
+ *
+ *
+ * @author wanyan
+ * @since 2023-11-01
+ */
+@Mapper
+public interface WqRMapper extends BaseMapper {
+}
diff --git a/src/main/java/com/whdc/mapper/WqRealMapper.java b/src/main/java/com/whdc/mapper/WqRealMapper.java
new file mode 100644
index 0000000..e49243a
--- /dev/null
+++ b/src/main/java/com/whdc/mapper/WqRealMapper.java
@@ -0,0 +1,17 @@
+package com.whdc.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.whdc.entity.WqReal;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ *
+ * 水质实时信息表 Mapper 接口
+ *
+ *
+ * @author wanyan
+ * @since 2023-11-01
+ */
+@Mapper
+public interface WqRealMapper extends BaseMapper {
+}
diff --git a/src/main/java/com/whdc/mqtt/Config.java b/src/main/java/com/whdc/mqtt/Config.java
new file mode 100644
index 0000000..0635776
--- /dev/null
+++ b/src/main/java/com/whdc/mqtt/Config.java
@@ -0,0 +1,9 @@
+package com.whdc.mqtt;
+
+public class Config {
+ public static final String USERNAME = "wy";
+ public static final char[] PASSWORD = "Gsiot_890".toCharArray();
+
+ public static final String TOPIC_WQ_R = "/Algae/#";
+
+}
diff --git a/src/main/java/com/whdc/mqtt/MQTTConfig.java b/src/main/java/com/whdc/mqtt/MQTTConfig.java
new file mode 100644
index 0000000..5667de2
--- /dev/null
+++ b/src/main/java/com/whdc/mqtt/MQTTConfig.java
@@ -0,0 +1,92 @@
+package com.whdc.mqtt;
+
+import com.alibaba.fastjson.JSON;
+import com.whdc.entity.WqR;
+import com.whdc.entity.WqReal;
+import com.whdc.mapper.WqRMapper;
+import com.whdc.mapper.WqRealMapper;
+import lombok.extern.slf4j.Slf4j;
+import org.eclipse.paho.client.mqttv3.MqttException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Configuration;
+
+import javax.annotation.PostConstruct;
+import java.util.Date;
+
+@Configuration
+@Slf4j
+public class MQTTConfig {
+
+ @Autowired
+ private WqRMapper wqRMapper;
+
+ @Autowired
+ private WqRealMapper wqRealMapper;
+
+ @Value("${mqtt.broker}")
+ private String broker;
+ @Value("${mqtt.clientId}")
+ private String clientId;
+
+ @Value("${enableMqttListening}")
+ private Boolean enable;
+
+ private static Sub sub;
+
+ @PostConstruct
+ public void initMQTT() {
+ if (!enable) {
+ log.debug("开发环境,不开启mqtt");
+ return;
+ }
+ log.debug("开启mqtt broker: %s, clientId: %s", broker, clientId);
+
+ Pub.BROKER = broker;
+ sub = new Sub(broker, clientId, Config.TOPIC_WQ_R);
+ sub.setOnMessageListener(new Sub.OnMessageListener() {
+ @Override
+ public void onMessage(String message) {
+ log.debug("mqtt: " + message);
+ System.out.println(message);
+ try {
+ WqR wqr = JSON.parseObject(message, WqR.class);
+ Date now = new Date();
+ wqr.setStcd("WQ");
+ wqr.setTm(now);
+ wqRMapper.insert(wqr);
+ WqReal wqReal = wqRealMapper.selectById("WQ");
+ if(wqReal == null){
+ wqReal = new WqReal();
+ }
+ wqReal.setChlorophyll(wqr.getChlorophyll());
+ wqReal.setBlueAlgae(wqr.getBlueAlgae());
+ wqReal.setGreenAlgae(wqr.getGreenAlgae());
+ wqReal.setDiatom(wqr.getDiatom());
+ wqReal.setCryptomonas(wqr.getCryptomonas());
+ wqReal.setTm(now);
+ if (wqReal.getStcd() == null) {
+ wqReal.setStcd("WQ");
+ wqRealMapper.insert(wqReal);
+ } else {
+ wqRealMapper.updateById(wqReal);
+ }
+
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
+ }
+ });
+ new Thread(() -> {
+ try {
+ Thread.sleep(10 * 1000);
+ sub.connect(true);
+ } catch (MqttException e) {
+ e.printStackTrace();
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }).start();
+ }
+
+}
diff --git a/src/main/java/com/whdc/mqtt/Pub.java b/src/main/java/com/whdc/mqtt/Pub.java
new file mode 100644
index 0000000..8a68b5b
--- /dev/null
+++ b/src/main/java/com/whdc/mqtt/Pub.java
@@ -0,0 +1,54 @@
+package com.whdc.mqtt;
+
+import org.eclipse.paho.client.mqttv3.MqttClient;
+import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
+import org.eclipse.paho.client.mqttv3.MqttException;
+import org.eclipse.paho.client.mqttv3.MqttMessage;
+import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
+
+public class Pub {
+ private final String clientId;
+ private final String topic;
+ public static String BROKER;
+ private final int qos;
+ private MqttClient client;
+
+ public Pub(String clientId, String topic) {
+ this.clientId = clientId;
+ this.topic = topic;
+ this.qos = 0;
+ }
+
+ public Pub(String clientId, String topic, int qos) {
+ this.clientId = clientId;
+ this.topic = topic;
+ this.qos = qos;
+ }
+
+ public void connect() throws MqttException {
+ this.client = new MqttClient("tcp://120.24.5.249:3189", "mqttx_2b072a3c", new MemoryPersistence());
+ MqttConnectOptions connOpts = new MqttConnectOptions();
+ connOpts.setUserName(Config.USERNAME);
+ connOpts.setPassword(Config.PASSWORD);
+ connOpts.setCleanSession(true);
+ this.client.connect(connOpts);
+ }
+
+ public void close() {
+ try {
+ this.client.disconnectForcibly(200);
+ this.client.close(true);
+ } catch (MqttException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void pub(String msg) throws MqttException {
+ MqttMessage message = new MqttMessage(msg.getBytes());
+ message.setQos(qos);
+ this.client.publish(topic, message);
+ }
+
+
+
+}
diff --git a/src/main/java/com/whdc/mqtt/Sub.java b/src/main/java/com/whdc/mqtt/Sub.java
new file mode 100644
index 0000000..9bca41e
--- /dev/null
+++ b/src/main/java/com/whdc/mqtt/Sub.java
@@ -0,0 +1,84 @@
+package com.whdc.mqtt;
+
+import org.eclipse.paho.client.mqttv3.*;
+import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
+
+
+public class Sub {
+ private final String clientId;
+ private final String topic;
+ private final String broker;
+ private OnMessageListener onMessageListener;
+ private MqttClient client;
+
+ public Sub(String broker, String clientId, String topic) {
+ this.clientId = clientId;
+ this.topic = topic;
+ this.broker = broker;
+ }
+
+ public void setOnMessageListener(OnMessageListener listener) {
+ this.onMessageListener = listener;
+ }
+
+ public void connect() throws MqttException {
+ connect(true);
+ }
+
+ public void connect(boolean autoReconnect) throws MqttException {
+ this.client = new MqttClient(broker, this.clientId, new MemoryPersistence());
+ MqttConnectOptions connOpts = new MqttConnectOptions();
+ connOpts.setUserName(Config.USERNAME);
+ connOpts.setPassword(Config.PASSWORD);
+ connOpts.setCleanSession(true);
+ connOpts.setAutomaticReconnect(autoReconnect);
+
+ this.client.setCallback(new MqttCallback() {
+ @Override
+ public void connectionLost(Throwable throwable) {
+ throwable.printStackTrace();
+ }
+
+ @Override
+ public void messageArrived(String topic, MqttMessage mqttMessage) {
+ byte[] payload = mqttMessage.getPayload();
+ String msg = new String(payload);
+ try {
+ Sub.this.onMessageListener.onMessage(msg);
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
+ }
+ });
+
+ this.client.connect(connOpts);
+ this.client.subscribe(this.topic, 0);
+ }
+
+ public void close() {
+ try {
+ this.client.disconnectForcibly(200);
+ this.client.close(true);
+ } catch (MqttException e) {
+ }
+ }
+
+ public interface OnMessageListener {
+ void onMessage(String message);
+ }
+
+ public static void main(String[] args) throws MqttException {
+ Sub sub = new Sub("tcp://120.24.5.249:3189","mqttx_2b072a3c", Config.TOPIC_WQ_R);
+ sub.setOnMessageListener(new OnMessageListener() {
+ @Override
+ public void onMessage(String message) {
+ System.out.println("received msg: " + message);
+ }
+ });
+ sub.connect(true);
+ }
+}
diff --git a/src/main/java/com/whdc/service/WqRService.java b/src/main/java/com/whdc/service/WqRService.java
new file mode 100644
index 0000000..9064945
--- /dev/null
+++ b/src/main/java/com/whdc/service/WqRService.java
@@ -0,0 +1,16 @@
+package com.whdc.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.whdc.entity.WqR;
+
+/**
+ *
+ * 水质信息表 服务类
+ *
+ *
+ * @author wanyan
+ * @since 2023-11-01
+ */
+public interface WqRService extends IService {
+
+}
diff --git a/src/main/java/com/whdc/service/impl/WqRServiceImp.java b/src/main/java/com/whdc/service/impl/WqRServiceImp.java
new file mode 100644
index 0000000..94c95ff
--- /dev/null
+++ b/src/main/java/com/whdc/service/impl/WqRServiceImp.java
@@ -0,0 +1,20 @@
+package com.whdc.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.whdc.entity.WqR;
+import com.whdc.mapper.WqRMapper;
+import com.whdc.service.WqRService;
+import org.springframework.stereotype.Service;
+
+/**
+ *
+ * 水质信息表 服务实现类
+ *
+ *
+ * @author wanyan
+ * @since 2023-11-01
+ */
+@Service
+public class WqRServiceImp extends ServiceImpl implements WqRService {
+
+}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
new file mode 100644
index 0000000..f1ea230
--- /dev/null
+++ b/src/main/resources/application.yml
@@ -0,0 +1,67 @@
+server:
+ port: 12303
+
+
+spring:
+ #数据库配置
+ datasource:
+ driver-class-name: com.mysql.jdbc.Driver
+ url: jdbc:mysql://rm-wz9n28sq10rz5b0u2o.mysql.rds.aliyuncs.com:3306/ttssk?characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull&useInformationSchema=true&serverTimezone=GMT%2B8&autoReconnect=true
+ username: fxkh
+ password: Fxkh_789_@
+ servlet:
+ multipart:
+ max-file-size: 100MB
+ max-request-size: 100MB
+
+ #jpa配置
+ jpa:
+ properties:
+ hibernate:
+ dialect: org.hibernate.dialect.DmDialect
+
+knife4j:
+ enable: true
+ setting:
+ enableFooter: false
+ enableFooterCustom: true
+ footerCustomContent: Copyright 2018-[湖北纬皓端成科技有限公司](http://www.wavehorizon.cn)
+
+ documents: # 文档配置,可配置多个分组
+ - group: doc-knife4j-1.0.0
+ name: MarkDown文档
+ locations: classpath:markdown/*
+ - group: v1.0
+ name: v1.0
+
+
+logging:
+# level:
+# org.springframework.boot.autoconfigure: error #spring的自动装配日志只打error,否则debug输出的会打印很多自动装配的log信息到控制台
+# com.whdc.zhzmkzapi.mapper: error
+ config: classpath:logback-spring.xml
+mybatis:
+ mapper-locations: classpath:mapper/*.xml
+# Sa-Token配置
+sa-token:
+ # token 名称 (同时也是cookie名称)
+ token-name: token
+ # token 有效期, 24小时,单位s, -1代表永不过期
+ timeout: 86400
+ # token 临时有效期 (指定时间内无操作就视为token过期) 单位: 秒
+ activity-timeout: -1
+ # 是否允许同一账号并发登录 (为true时允许一起登录, 为false时新登录挤掉旧登录)
+ is-concurrent: true
+ # 在多人登录同一账号时,是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
+ is-share: true
+ # token风格
+ token-style: simple-uuid
+ # 是否输出操作日志
+ is-log: false
+
+
+mqtt:
+ broker: tcp://120.24.5.249:3189
+ clientId: mqttx_2b172a3c
+
+enableMqttListening: true
\ No newline at end of file
diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml
new file mode 100644
index 0000000..e14da74
--- /dev/null
+++ b/src/main/resources/logback-spring.xml
@@ -0,0 +1,206 @@
+
+
+
+
+
+
+
+ logback
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ debug
+
+
+ ${CONSOLE_LOG_PATTERN}
+
+ UTF-8
+
+
+
+
+
+
+
+ ${log.path}/web_debug.log
+
+
+ %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
+ UTF-8
+
+
+
+
+ ${log.path}/web-debug-%d{yyyy-MM-dd}.%i.log
+
+ ${maxFileSize}
+
+
+ ${maxHistory}
+
+
+
+ debug
+ ACCEPT
+ DENY
+
+
+
+
+
+
+ ${log.path}/web_info.log
+
+
+ %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
+ UTF-8
+
+
+
+
+ ${log.path}/web-info-%d{yyyy-MM-dd}.%i.log
+
+ ${maxFileSize}
+
+
+ ${maxHistory}
+
+
+
+ info
+ ACCEPT
+ DENY
+
+
+
+
+
+
+ ${log.path}/web_warn.log
+
+
+ %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
+ UTF-8
+
+
+
+ ${log.path}/web-warn-%d{yyyy-MM-dd}.%i.log
+
+ ${maxFileSize}
+
+
+ ${maxHistory}
+
+
+
+ warn
+ ACCEPT
+ DENY
+
+
+
+
+
+
+ ${log.path}/web_error.log
+
+
+ %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
+ UTF-8
+
+
+
+ ${log.path}/web-error-%d{yyyy-MM-dd}.%i.log
+
+ ${maxFileSize}
+
+
+ ${maxHistory}
+
+
+
+ ERROR
+ ACCEPT
+ DENY
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/resources/tpl.xlsx b/src/main/resources/tpl.xlsx
new file mode 100644
index 0000000..454f790
Binary files /dev/null and b/src/main/resources/tpl.xlsx differ