mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-18 17:05:55 +08:00
Merge remote-tracking branch 'origin/master' into version1.2
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user