Merge remote-tracking branch 'origin/master' into version1.2

This commit is contained in:
kerwincui
2022-05-25 16:22:39 +08:00
7 changed files with 74 additions and 76 deletions

View File

@@ -36,6 +36,8 @@ public class TDengineConfig {
@Value("${spring.datasource.druid.tdengine-server.dbName}")
private String dbName;
@Value("${spring.datasource.druid.tdengine-server.url}")
private String jdbc;
@Bean(name = "tDengineDataSource")

View File

@@ -1,25 +0,0 @@
package com.ruoyi.iot.tdengine.dao;
import org.springframework.stereotype.Repository;
/**
* @package com.ruoyi.mysql.mysql.tdengine
* 类名: DatabaseMapper
* 描述: TODO
* 时间: 2022/5/16,0016 1:27
* 开发人: wxy
*/
@Repository
public interface DatabaseDAO {
int createDB();
int dropDatabase();
int useDatabase();
int createTable();
}

View File

@@ -1,8 +1,11 @@
package com.ruoyi.iot.tdengine.init;
import com.alibaba.druid.pool.DruidDataSource;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.iot.tdengine.config.TDengineConfig;
import com.ruoyi.iot.tdengine.dao.TDDeviceLogDAO;
import com.taosdata.jdbc.TSDBDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -12,6 +15,13 @@ import org.springframework.context.ApplicationContext;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* 类名: ApplicationStarted
* 描述: TODO
@@ -26,13 +36,22 @@ public class ApplicationStarted implements ApplicationRunner {
@Autowired
private ApplicationContext applicationContext;
private DruidDataSource dataSource;
private TDengineConfig dengineConfig;
private TDDeviceLogDAO deviceLogMapper;
@Override
public void run(ApplicationArguments args) {
//先获取TDengine的配置检测TDengine是否已经配置
if (containBean(TDengineConfig.class)) {
TDengineConfig tDengineConfig = applicationContext.getBean(TDengineConfig.class);
TDDeviceLogDAO tDDeviceLogDAO = applicationContext.getBean(TDDeviceLogDAO.class);
initTDengine(tDengineConfig, tDDeviceLogDAO);
this.dengineConfig = applicationContext.getBean(TDengineConfig.class);
this.dataSource = applicationContext.getBean("tDengineDataSource", DruidDataSource.class);
this.deviceLogMapper= applicationContext.getBean(TDDeviceLogDAO.class);
initTDengine(this.dengineConfig.getDbName());
LOGGER.info("使用TDengine存储设备数据初始化成功");
}else{
LOGGER.info("使用MySql存储设备数据初始化成功");
@@ -47,12 +66,13 @@ public class ApplicationStarted implements ApplicationRunner {
* @date 2022/5/22,0022 14:27
* @author wxy
*/
public void initTDengine(TDengineConfig dengineConfig, TDDeviceLogDAO deviceLogMapper) {
public void initTDengine(String dbName) {
try {
String dbName = dengineConfig.getDbName();
// TODO 目前还不支持自动创建数据库
int db = deviceLogMapper.createDB(dbName);
createDatabase();
//创建数据库
deviceLogMapper.createSTable(dbName);
System.out.println("完成超级表的创建");
LOGGER.info("完成超级表的创建");
} catch (Exception e) {
LOGGER.info("错误",e.getMessage());
e.printStackTrace();
@@ -60,6 +80,43 @@ public class ApplicationStarted implements ApplicationRunner {
}
/**
* @Method
* @Description 根据数据库连接自动创建数据库
* @Param null
* @return
* @date 2022/5/24,0024 14:32
* @author wxy
*
*/
private void createDatabase(){
try {
String dbName = dengineConfig.getDbName();
String jdbcUrl = dataSource.getRawJdbcUrl();
String username = dataSource.getUsername();
String password = dataSource.getPassword();
jdbcUrl += ("&user=" + username);
jdbcUrl += ("&password=" + password);
int startIndex = jdbcUrl.indexOf('/',12);
int endIndex = jdbcUrl.indexOf('?');
String newJdbcUrl = jdbcUrl.substring(0,startIndex);
newJdbcUrl= newJdbcUrl+jdbcUrl.substring(endIndex);
System.out.println(newJdbcUrl);
Properties connProps = new Properties();
connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
Connection conn = DriverManager.getConnection(newJdbcUrl, connProps);
conn.createStatement().execute(String.format("create database if not exists %s;",dbName));
conn.close();
LOGGER.info("完成数据库创建");
} catch (Exception e) {
LOGGER.info("错误",e.getMessage());
e.printStackTrace();
}
}
/**
* @return
* @Method containBean
@@ -76,4 +133,6 @@ public class ApplicationStarted implements ApplicationRunner {
return true;
}
}
}

View File

@@ -49,6 +49,9 @@ public class TdengineLogServiceImpl implements ILogService {
@Override
public List<MonitorModel> selectMonitorList(DeviceLog deviceLog) {
if(deviceLog.getIdentity()!=null){
deviceLog.setIdentity("%"+deviceLog.getIdentity()+"%");
}
return tdDeviceLogDAO.selectMonitorList(dbName,deviceLog);
}
}

View File

@@ -1,40 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.iot.tdengine.dao.DatabaseDAO">
<!-- 创建数据库-->
<update id="createDB" parameterType="java.lang.String">
create database if not exists ${dbName};
</update>
<!-- 删除数据库-->
<update id="dropDatabase" parameterType="java.lang.String">
DROP database if exists ${dbName};
</update>
<!-- 使用数据库-->
<update id="useDatabase" parameterType="java.lang.String">
use ${dbName};
</update>
<update id="createTable">
create stable if not exists ${tableName}
(ts timestamp,
log_id BIGINT,
identity NCHAR(100),
log_type NCHAR(20),
log_value NCHAR(100),
device_id BIGINT,
device_name NCHAR(100),
serial_number NCHAR(100),
is_monitor int,
create_by NCHAR(100),
create_time timestamp,
remark NCHAR(1000),
);
</update>
</mapper>

View File

@@ -42,7 +42,6 @@
</update>
<update id="createSTable">
create STABLE if not exists ${database}.device_log
(
ts timestamp,
@@ -118,7 +117,7 @@
<where>
is_monitor=1
<if test="device.deviceId!=null"> and device_id = #{device.deviceId} </if>
<if test="device.identity != null and device.identity != ''"> and identity like concat('%', #{device.identity}, '%')</if>
<if test="device.identity != null and device.identity != ''"> and identity like #{device.identity}</if>
order by ts desc
limit 2000
@@ -131,7 +130,7 @@
is_monitor !=1
<if test="device.deviceId!=null"> and device_id = #{device.deviceId} </if>
<if test="device.logType != null "> and log_type = #{device.logType}</if>
<if test="device.identity != null and device.identity != ''"> and identity like concat('%', #{device.identity}, '%')</if>
<if test="device.identity != null and device.identity != ''"> and identity like #{device.identity}</if>
</where>
order by ts desc
</select>