mirror of
https://github.com/kerwincui/FastBee.git
synced 2025-10-11 11:00:30 +08:00
Merge remote-tracking branch 'origin/master' into version1.2
This commit is contained in:
@@ -16,6 +16,17 @@ spring:
|
||||
url:
|
||||
username:
|
||||
password:
|
||||
|
||||
# TDengine数据库
|
||||
tdengine-server:
|
||||
# 默认不启用TDengine,true=启用,false=不启用
|
||||
enabled: false
|
||||
driverClassName: com.taosdata.jdbc.TSDBDriver
|
||||
url: jdbc:TAOS://127.0.0.1:6030/wumei_smart_log?timezone=Asia/Beijing&charset=utf-8
|
||||
username: root
|
||||
password: taosdata
|
||||
dbName: wumei_smart_log
|
||||
|
||||
# 初始连接数
|
||||
initialSize: 5
|
||||
# 最小连接池数量
|
||||
|
@@ -8,7 +8,7 @@ ruoyi:
|
||||
copyrightYear: 2021
|
||||
# 实例演示开关
|
||||
demoEnabled: true
|
||||
# 文件路径 示例( Windows配置D:/wumei-smart/uploadPath,Linux配置 /var/wumei-smart/java/uploadPath)
|
||||
# 文件路径,以uploadPath结尾 示例( Windows配置D:/wumei-smart/uploadPath,Linux配置 /var/wumei-smart/java/uploadPath)
|
||||
profile: /var/wumei-smart/java/uploadPath
|
||||
# 获取ip地址开关
|
||||
addressEnabled: true
|
||||
|
@@ -9,10 +9,13 @@ import javax.sql.DataSource;
|
||||
import org.apache.ibatis.io.VFS;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
import org.mybatis.spring.SqlSessionTemplate;
|
||||
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -21,12 +24,13 @@ import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
|
||||
import org.springframework.core.type.classreading.MetadataReader;
|
||||
import org.springframework.core.type.classreading.MetadataReaderFactory;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
|
||||
/**
|
||||
* Mybatis支持*匹配扫描包
|
||||
*
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Configuration
|
||||
@@ -113,7 +117,8 @@ public class MyBatisConfig
|
||||
return resources.toArray(new Resource[resources.size()]);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Bean(name = "mysqlSessionFactory")
|
||||
@Primary
|
||||
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception
|
||||
{
|
||||
String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
|
||||
@@ -129,4 +134,16 @@ public class MyBatisConfig
|
||||
sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
|
||||
return sessionFactory.getObject();
|
||||
}
|
||||
|
||||
@Bean(name = "mysqlTransactionManager")
|
||||
@Primary
|
||||
public DataSourceTransactionManager mysqlTransactionManager(@Qualifier("dynamicDataSource") DataSource dataSource) {
|
||||
return new DataSourceTransactionManager(dataSource);
|
||||
}
|
||||
|
||||
@Bean(name = "mysqlSqlSessionTemplate")
|
||||
@Primary
|
||||
public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
|
||||
return new SqlSessionTemplate(sqlSessionFactory);
|
||||
}
|
||||
}
|
@@ -89,6 +89,15 @@
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- TDengine连接 START-->
|
||||
<dependency>
|
||||
<groupId>com.taosdata.jdbc</groupId>
|
||||
<artifactId>taos-jdbcdriver</artifactId>
|
||||
<version>2.0.38</version>
|
||||
</dependency>
|
||||
<!-- TDengine连接 END-->
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@@ -5,9 +5,11 @@ import org.apache.commons.lang3.builder.ToStringStyle;
|
||||
import com.ruoyi.common.annotation.Excel;
|
||||
import com.ruoyi.common.core.domain.BaseEntity;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 设备日志对象 iot_device_log
|
||||
*
|
||||
*
|
||||
* @author kerwincui
|
||||
* @date 2022-01-13
|
||||
*/
|
||||
@@ -15,6 +17,8 @@ public class DeviceLog extends BaseEntity
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Excel(name = "时间戳")
|
||||
private Date ts;
|
||||
/** 设备日志ID */
|
||||
private Long logId;
|
||||
|
||||
@@ -46,6 +50,15 @@ public class DeviceLog extends BaseEntity
|
||||
@Excel(name = "是否监测数据", readConverterExp = "1==是,0=否")
|
||||
private Integer isMonitor;
|
||||
|
||||
|
||||
public Date getTs() {
|
||||
return ts;
|
||||
}
|
||||
|
||||
public void setTs(Date ts) {
|
||||
this.ts = ts;
|
||||
}
|
||||
|
||||
public String getSerialNumber() {
|
||||
return serialNumber;
|
||||
}
|
||||
|
@@ -4,11 +4,12 @@ import com.ruoyi.iot.domain.DeviceLog;
|
||||
import com.ruoyi.iot.model.MonitorModel;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 设备日志Mapper接口
|
||||
*
|
||||
*
|
||||
* @author kerwincui
|
||||
* @date 2022-01-13
|
||||
*/
|
||||
@@ -17,7 +18,7 @@ public interface DeviceLogMapper
|
||||
{
|
||||
/**
|
||||
* 查询设备日志
|
||||
*
|
||||
*
|
||||
* @param logId 设备日志主键
|
||||
* @return 设备日志
|
||||
*/
|
||||
@@ -25,7 +26,7 @@ public interface DeviceLogMapper
|
||||
|
||||
/**
|
||||
* 查询设备日志列表
|
||||
*
|
||||
*
|
||||
* @param deviceLog 设备日志
|
||||
* @return 设备日志集合
|
||||
*/
|
||||
@@ -41,7 +42,7 @@ public interface DeviceLogMapper
|
||||
|
||||
/**
|
||||
* 新增设备日志
|
||||
*
|
||||
*
|
||||
* @param deviceLog 设备日志
|
||||
* @return 结果
|
||||
*/
|
||||
@@ -49,7 +50,7 @@ public interface DeviceLogMapper
|
||||
|
||||
/**
|
||||
* 修改设备日志
|
||||
*
|
||||
*
|
||||
* @param deviceLog 设备日志
|
||||
* @return 结果
|
||||
*/
|
||||
@@ -57,7 +58,7 @@ public interface DeviceLogMapper
|
||||
|
||||
/**
|
||||
* 删除设备日志
|
||||
*
|
||||
*
|
||||
* @param logId 设备日志主键
|
||||
* @return 结果
|
||||
*/
|
||||
@@ -65,7 +66,7 @@ public interface DeviceLogMapper
|
||||
|
||||
/**
|
||||
* 批量删除设备日志
|
||||
*
|
||||
*
|
||||
* @param logIds 需要删除的数据主键集合
|
||||
* @return 结果
|
||||
*/
|
||||
@@ -78,4 +79,7 @@ public interface DeviceLogMapper
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteDeviceLogByDeviceIds(Long[] deviceIds);
|
||||
|
||||
|
||||
// List<DeviceLog> selectLogList(Long deviceId,String serialNumber,Long isMonitor,Long logType, Date beginDate, Date endDate);
|
||||
}
|
||||
|
@@ -10,6 +10,7 @@ import com.ruoyi.iot.model.ThingsModels.ThingsModelValueRemarkItem;
|
||||
import com.ruoyi.iot.model.ThingsModels.ThingsModelValuesInput;
|
||||
import com.ruoyi.iot.service.IDeviceLogService;
|
||||
import com.ruoyi.iot.service.IDeviceService;
|
||||
import com.ruoyi.iot.tdengine.service.ILogService;
|
||||
import org.eclipse.paho.client.mqttv3.MqttAsyncClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
@@ -32,8 +33,11 @@ public class EmqxService {
|
||||
@Autowired
|
||||
private IDeviceService deviceService;
|
||||
|
||||
// @Autowired
|
||||
// private IDeviceLogService deviceLogService;
|
||||
|
||||
@Autowired
|
||||
private IDeviceLogService deviceLogService;
|
||||
private ILogService logService;
|
||||
|
||||
/**
|
||||
* 订阅的主题
|
||||
@@ -190,7 +194,8 @@ public class EmqxService {
|
||||
deviceLog.setIdentity(thingsModelValueRemarkItems.get(i).getId());
|
||||
deviceLog.setLogType(3);
|
||||
deviceLog.setIsMonitor(0);
|
||||
deviceLogService.insertDeviceLog(deviceLog);
|
||||
// deviceLogService.insertDeviceLog(deviceLog);
|
||||
logService.saveDeviceLog(deviceLog);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("接收事件,解析数据时异常 message={}", e.getMessage());
|
||||
|
@@ -2,6 +2,7 @@ package com.ruoyi.iot.service.impl;
|
||||
|
||||
import com.ruoyi.common.utils.DateUtils;
|
||||
import com.ruoyi.iot.domain.DeviceLog;
|
||||
import com.ruoyi.iot.tdengine.service.ILogService;
|
||||
import com.ruoyi.iot.mapper.DeviceLogMapper;
|
||||
import com.ruoyi.iot.model.MonitorModel;
|
||||
import com.ruoyi.iot.service.IDeviceLogService;
|
||||
@@ -22,6 +23,9 @@ public class DeviceLogServiceImpl implements IDeviceLogService
|
||||
@Autowired
|
||||
private DeviceLogMapper deviceLogMapper;
|
||||
|
||||
@Autowired
|
||||
private ILogService logService;
|
||||
|
||||
/**
|
||||
* 查询设备日志
|
||||
*
|
||||
@@ -43,7 +47,8 @@ public class DeviceLogServiceImpl implements IDeviceLogService
|
||||
@Override
|
||||
public List<DeviceLog> selectDeviceLogList(DeviceLog deviceLog)
|
||||
{
|
||||
return deviceLogMapper.selectDeviceLogList(deviceLog);
|
||||
// deviceLogMapper.selectDeviceLogList(deviceLog);
|
||||
return logService.selectDeviceLogList(deviceLog);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,7 +60,8 @@ public class DeviceLogServiceImpl implements IDeviceLogService
|
||||
@Override
|
||||
public List<MonitorModel> selectMonitorList(DeviceLog deviceLog)
|
||||
{
|
||||
return deviceLogMapper.selectMonitorList(deviceLog);
|
||||
// return deviceLogMapper.selectMonitorList(deviceLog);
|
||||
return logService.selectMonitorList(deviceLog);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,7 +74,8 @@ public class DeviceLogServiceImpl implements IDeviceLogService
|
||||
public int insertDeviceLog(DeviceLog deviceLog)
|
||||
{
|
||||
deviceLog.setCreateTime(DateUtils.getNowDate());
|
||||
return deviceLogMapper.insertDeviceLog(deviceLog);
|
||||
// return deviceLogMapper.insertDeviceLog(deviceLog);
|
||||
return logService.saveDeviceLog(deviceLog);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -11,13 +11,13 @@ import com.ruoyi.common.utils.ip.IpUtils;
|
||||
import com.ruoyi.iot.domain.Device;
|
||||
import com.ruoyi.iot.domain.DeviceLog;
|
||||
import com.ruoyi.iot.domain.Product;
|
||||
import com.ruoyi.iot.tdengine.service.ILogService;
|
||||
import com.ruoyi.iot.mapper.DeviceLogMapper;
|
||||
import com.ruoyi.iot.mapper.DeviceMapper;
|
||||
import com.ruoyi.iot.mapper.DeviceUserMapper;
|
||||
import com.ruoyi.iot.model.*;
|
||||
import com.ruoyi.iot.model.ThingsModelItem.*;
|
||||
import com.ruoyi.iot.model.ThingsModels.*;
|
||||
import com.ruoyi.iot.service.IDeviceLogService;
|
||||
import com.ruoyi.iot.service.IDeviceService;
|
||||
import com.ruoyi.iot.service.IProductService;
|
||||
import com.ruoyi.iot.service.IToolService;
|
||||
@@ -73,8 +73,11 @@ public class DeviceServiceImpl implements IDeviceService {
|
||||
@Autowired
|
||||
private ISysUserService userService;
|
||||
|
||||
// @Autowired
|
||||
// private IDeviceLogService deviceLogService;
|
||||
|
||||
@Autowired
|
||||
private IDeviceLogService deviceLogService;
|
||||
private ILogService logService;
|
||||
|
||||
/**
|
||||
* 查询设备
|
||||
@@ -196,7 +199,8 @@ public class DeviceServiceImpl implements IDeviceService {
|
||||
deviceLog.setCreateTime(DateUtils.getNowDate());
|
||||
deviceLog.setIsMonitor(valueList.get(k).getIsMonitor());
|
||||
deviceLog.setLogType(type);
|
||||
deviceLogMapper.insertDeviceLog(deviceLog);
|
||||
logService.saveDeviceLog(deviceLog);
|
||||
// deviceLogMapper.insertDeviceLog(deviceLog);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -591,7 +595,8 @@ public class DeviceServiceImpl implements IDeviceService {
|
||||
deviceLog.setIdentity("offline");
|
||||
deviceLog.setLogType(6);
|
||||
}
|
||||
deviceLogService.insertDeviceLog(deviceLog);
|
||||
logService.saveDeviceLog(deviceLog);
|
||||
// deviceLogService.insertDeviceLog(deviceLog);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,93 @@
|
||||
package com.ruoyi.iot.tdengine.config;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
import org.mybatis.spring.SqlSessionTemplate;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 类名: TDengineConfig
|
||||
* 描述: TDengine配置类
|
||||
* 时间: 2022/5/13,0016 1:14
|
||||
* 开发人: wxy
|
||||
*/
|
||||
@Configuration
|
||||
@MapperScan(basePackages = {"com.ruoyi.iot.tdengine.dao"}, sqlSessionTemplateRef = "tdengineSqlSessionTemplate")
|
||||
@ConditionalOnProperty(name = "spring.datasource.druid.tdengine-server.enabled", havingValue = "true")
|
||||
public class TDengineConfig {
|
||||
|
||||
@Value("${spring.datasource.druid.tdengine-server.dbName}")
|
||||
private String dbName;
|
||||
|
||||
|
||||
@Bean(name = "tDengineDataSource")
|
||||
@ConfigurationProperties(prefix = "spring.datasource.druid.tdengine-server")
|
||||
public DataSource tdengineDataSource() {
|
||||
return new DruidDataSource();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Bean(name = "tDengineSqlSessionFactory")
|
||||
public SqlSessionFactory tDengineSqlSessionFactory(@Qualifier("tDengineDataSource") DataSource dataSource) throws Exception {
|
||||
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
|
||||
sqlSessionFactoryBean.setMapperLocations(resolveMapperLocations(StringUtils.split("classpath:mapper/tdengine/*Mapper.xml", ",")));
|
||||
sqlSessionFactoryBean.setDataSource(dataSource);
|
||||
return sqlSessionFactoryBean.getObject();
|
||||
}
|
||||
|
||||
|
||||
@Bean(name = "tdengineSqlSessionTemplate")
|
||||
public SqlSessionTemplate tdengineSqlSessionTemplate(@Qualifier("tDengineSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
|
||||
return new SqlSessionTemplate(sqlSessionFactory);
|
||||
}
|
||||
|
||||
public String getDbName() {
|
||||
return dbName;
|
||||
}
|
||||
|
||||
public void setDbName(String dbName) {
|
||||
this.dbName = dbName;
|
||||
}
|
||||
public Resource[] resolveMapperLocations(String[] mapperLocations)
|
||||
{
|
||||
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
|
||||
List<Resource> resources = new ArrayList<Resource>();
|
||||
if (mapperLocations != null)
|
||||
{
|
||||
for (String mapperLocation : mapperLocations)
|
||||
{
|
||||
try
|
||||
{
|
||||
Resource[] mappers = resourceResolver.getResources(mapperLocation);
|
||||
resources.addAll(Arrays.asList(mappers));
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
return resources.toArray(new Resource[resources.size()]);
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
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();
|
||||
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
package com.ruoyi.iot.tdengine.dao;
|
||||
|
||||
import com.ruoyi.iot.domain.DeviceLog;
|
||||
import com.ruoyi.iot.model.MonitorModel;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @package com.ruoyi.mysql.mysql.tdengine
|
||||
* 类名: DatabaseMapper
|
||||
* 描述: TODO
|
||||
* 时间: 2022/5/16,0016 1:27
|
||||
* 开发人: wxy
|
||||
*/
|
||||
@Repository
|
||||
public interface TDDeviceLogDAO {
|
||||
|
||||
|
||||
int createDB( String database);
|
||||
|
||||
int createSTable(String database);
|
||||
|
||||
int createTable(String database,String deviceId);
|
||||
|
||||
int save(@Param("database") String database,@Param("device") DeviceLog deviceLog);
|
||||
|
||||
List<DeviceLog> selectSTable(String database,DeviceLog deviceLog);
|
||||
|
||||
int delete(String dbName, DeviceLog deviceLog);
|
||||
|
||||
// List<DeviceLog> selectLogListByPage(String dbName, Integer pageSize, Integer pageNum, String deviceNum, Date beginDate, Date endDate);
|
||||
|
||||
List<DeviceLog> selectLogList(String dbName, Long deviceId, String serialNumber, Long isMonitor, Long logType, Date beginDate, Date endDate);
|
||||
|
||||
List<MonitorModel> selectMonitorList(@Param("database") String database, @Param("device") DeviceLog deviceLog);
|
||||
|
||||
List<DeviceLog> selectDeviceLogList(@Param("database") String database,@Param("device") DeviceLog deviceLog);
|
||||
}
|
@@ -0,0 +1,79 @@
|
||||
package com.ruoyi.iot.tdengine.init;
|
||||
|
||||
|
||||
import com.ruoyi.iot.tdengine.config.TDengineConfig;
|
||||
import com.ruoyi.iot.tdengine.dao.TDDeviceLogDAO;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 类名: ApplicationStarted
|
||||
* 描述: TODO
|
||||
* 时间: 2022/5/18,0018 1:41
|
||||
* 开发人: wxy
|
||||
*/
|
||||
@Component
|
||||
public class ApplicationStarted implements ApplicationRunner {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationStarted.class);
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@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);
|
||||
LOGGER.info("使用TDengine存储设备数据,初始化成功");
|
||||
}else{
|
||||
LOGGER.info("使用MySql存储设备数据,初始化成功");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @Method
|
||||
* @Description 开始初始化加载系统参数, 创建数据库和超级表
|
||||
* @Param null
|
||||
* @date 2022/5/22,0022 14:27
|
||||
* @author wxy
|
||||
*/
|
||||
public void initTDengine(TDengineConfig dengineConfig, TDDeviceLogDAO deviceLogMapper) {
|
||||
try {
|
||||
String dbName = dengineConfig.getDbName();
|
||||
// TODO 目前还不支持自动创建数据库
|
||||
int db = deviceLogMapper.createDB(dbName);
|
||||
deviceLogMapper.createSTable(dbName);
|
||||
} catch (Exception e) {
|
||||
LOGGER.info("错误",e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @Method containBean
|
||||
* @Description 根据类判断是否有对应bean
|
||||
* @Param 类
|
||||
* @date 2022/5/22,0022 14:12
|
||||
* @author wxy
|
||||
*/
|
||||
private boolean containBean(@Nullable Class<?> T) {
|
||||
String[] beans = applicationContext.getBeanNamesForType(T);
|
||||
if (beans == null || beans.length == 0) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
package com.ruoyi.iot.tdengine.service;
|
||||
|
||||
import com.ruoyi.iot.domain.DeviceLog;
|
||||
|
||||
import com.ruoyi.iot.model.MonitorModel;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @package iot.iot.log
|
||||
* 类名: LogService
|
||||
* 描述: 设备日志记录接口
|
||||
* 时间: 2022/5/19,0019 18:04
|
||||
* 开发人: admin
|
||||
*/
|
||||
public interface ILogService {
|
||||
|
||||
int saveDeviceLog(DeviceLog deviceLog);
|
||||
|
||||
List<DeviceLog> selectDeviceLogList(DeviceLog deviceLog);
|
||||
|
||||
List<MonitorModel> selectMonitorList(DeviceLog deviceLog);
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
package com.ruoyi.iot.tdengine.service.factory;
|
||||
|
||||
import com.ruoyi.framework.config.MyBatisConfig;
|
||||
import com.ruoyi.iot.tdengine.service.impl.MySqlLogServiceImpl;
|
||||
import com.ruoyi.iot.tdengine.service.impl.TdengineLogServiceImpl;
|
||||
import com.ruoyi.iot.tdengine.config.TDengineConfig;
|
||||
import com.ruoyi.iot.tdengine.service.ILogService;
|
||||
import com.ruoyi.iot.mapper.DeviceLogMapper;
|
||||
import com.ruoyi.iot.tdengine.dao.TDDeviceLogDAO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 类名: DeviceLogServiceImpl
|
||||
* 描述: TODO
|
||||
* 时间: 2022/5/19,0019 18:09
|
||||
* 开发人: wxy
|
||||
*/
|
||||
@Component
|
||||
public class LogServiceFactory {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Bean
|
||||
public ILogService getLogService() {
|
||||
//先获取TDengine的配置,检测TDengine是否已经配置
|
||||
if (containBean(TDengineConfig.class)) {
|
||||
TDengineConfig tDengineConfig = applicationContext.getBean(TDengineConfig.class);
|
||||
TDDeviceLogDAO tDDeviceLogDAO = applicationContext.getBean(TDDeviceLogDAO.class);
|
||||
ILogService logService = new TdengineLogServiceImpl(tDengineConfig, tDDeviceLogDAO);
|
||||
return logService;
|
||||
} else if (containBean(MyBatisConfig.class)) {
|
||||
//没有配置TDengine,那么使用MySQL的日志配置
|
||||
DeviceLogMapper deviceLogMapper = applicationContext.getBean( DeviceLogMapper.class);
|
||||
ILogService logService = new MySqlLogServiceImpl(deviceLogMapper);
|
||||
return logService;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @Method containBean
|
||||
* @Description 根据类判断是否有对应bean
|
||||
* @Param 类
|
||||
* @return
|
||||
* @date 2022/5/22,0022 14:12
|
||||
* @author wxy
|
||||
*
|
||||
*/
|
||||
private boolean containBean(@Nullable Class<?> T) {
|
||||
String[] beans = applicationContext.getBeanNamesForType(T);
|
||||
if (beans == null || beans.length == 0) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
package com.ruoyi.iot.tdengine.service.impl;
|
||||
|
||||
import com.ruoyi.iot.domain.DeviceLog;
|
||||
import com.ruoyi.iot.tdengine.service.ILogService;
|
||||
import com.ruoyi.iot.mapper.DeviceLogMapper;
|
||||
import com.ruoyi.iot.model.MonitorModel;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 类名: MySqlLogServiceImpl
|
||||
* 描述: MySQL存储日志实现类
|
||||
* 时间: 2022/5/22,0022 13:37
|
||||
* 开发人: admin
|
||||
*/
|
||||
public class MySqlLogServiceImpl implements ILogService {
|
||||
|
||||
private DeviceLogMapper deviceLogMapper;
|
||||
|
||||
public MySqlLogServiceImpl(DeviceLogMapper _deviceLogMapper){
|
||||
this.deviceLogMapper=_deviceLogMapper;
|
||||
}
|
||||
@Override
|
||||
public int saveDeviceLog(DeviceLog deviceLog) {
|
||||
return deviceLogMapper.insertDeviceLog(deviceLog);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DeviceLog> selectDeviceLogList(DeviceLog deviceLog) {
|
||||
return deviceLogMapper.selectDeviceLogList(deviceLog);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MonitorModel> selectMonitorList(DeviceLog deviceLog) {
|
||||
return deviceLogMapper.selectMonitorList(deviceLog);
|
||||
}
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
package com.ruoyi.iot.tdengine.service.impl;
|
||||
|
||||
import com.ruoyi.iot.domain.DeviceLog;
|
||||
import com.ruoyi.iot.tdengine.service.ILogService;
|
||||
import com.ruoyi.iot.model.MonitorModel;
|
||||
import com.ruoyi.iot.tdengine.config.TDengineConfig;
|
||||
import com.ruoyi.iot.tdengine.dao.TDDeviceLogDAO;
|
||||
import com.ruoyi.iot.util.SnowflakeIdWorker;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 类名: TdengineLogServiceImpl
|
||||
* 描述: TDengine存储日志数据实现类
|
||||
* 时间: 2022/5/22,0022 13:38
|
||||
* 开发人: admin
|
||||
*/
|
||||
public class TdengineLogServiceImpl implements ILogService {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private TDDeviceLogDAO tdDeviceLogDAO;
|
||||
|
||||
private TDengineConfig tDengineConfig;
|
||||
|
||||
private SnowflakeIdWorker snowflakeIdWorker;
|
||||
|
||||
private String dbName;
|
||||
|
||||
public TdengineLogServiceImpl(TDengineConfig _tDengineConfig, TDDeviceLogDAO _tdDeviceLogDAO) {
|
||||
this.tdDeviceLogDAO = _tdDeviceLogDAO;
|
||||
this.tDengineConfig = _tDengineConfig;
|
||||
snowflakeIdWorker=new SnowflakeIdWorker(1);
|
||||
this.dbName=_tDengineConfig.getDbName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int saveDeviceLog(DeviceLog deviceLog) {
|
||||
long logId = snowflakeIdWorker.nextId();
|
||||
deviceLog.setLogId(logId);
|
||||
return tdDeviceLogDAO.save(dbName,deviceLog);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DeviceLog> selectDeviceLogList(DeviceLog deviceLog) {
|
||||
return tdDeviceLogDAO.selectDeviceLogList(dbName,deviceLog);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MonitorModel> selectMonitorList(DeviceLog deviceLog) {
|
||||
return tdDeviceLogDAO.selectMonitorList(dbName,deviceLog);
|
||||
}
|
||||
}
|
@@ -0,0 +1,104 @@
|
||||
package com.ruoyi.iot.util;
|
||||
|
||||
public class SnowflakeIdWorker {
|
||||
/**
|
||||
* 开始时间:2020-01-01 00:00:00
|
||||
*/
|
||||
private final long beginTs = 1577808000000L;
|
||||
|
||||
private final long workerIdBits = 10;
|
||||
|
||||
/**
|
||||
* 2^10 - 1 = 1023
|
||||
*/
|
||||
private final long maxWorkerId = -1L ^ (-1L << workerIdBits);
|
||||
|
||||
private final long sequenceBits = 12;
|
||||
|
||||
/**
|
||||
* 2^12 - 1 = 4095
|
||||
*/
|
||||
private final long maxSequence = -1L ^ (-1L << sequenceBits);
|
||||
|
||||
/**
|
||||
* 时间戳左移22位
|
||||
*/
|
||||
private final long timestampLeftOffset = workerIdBits + sequenceBits;
|
||||
|
||||
/**
|
||||
* 业务ID左移12位
|
||||
*/
|
||||
private final long workerIdLeftOffset = sequenceBits;
|
||||
|
||||
/**
|
||||
* 合并了机器ID和数据标示ID,统称业务ID,10位
|
||||
*/
|
||||
private long workerId;
|
||||
|
||||
/**
|
||||
* 毫秒内序列,12位,2^12 = 4096个数字
|
||||
*/
|
||||
private long sequence = 0L;
|
||||
|
||||
/**
|
||||
* 上一次生成的ID的时间戳,同一个worker中
|
||||
*/
|
||||
private long lastTimestamp = -1L;
|
||||
|
||||
public SnowflakeIdWorker(long workerId) {
|
||||
if (workerId > maxWorkerId || workerId < 0) {
|
||||
throw new IllegalArgumentException(String.format("WorkerId必须大于或等于0且小于或等于%d", maxWorkerId));
|
||||
}
|
||||
|
||||
this.workerId = workerId;
|
||||
}
|
||||
|
||||
public synchronized long nextId() {
|
||||
long ts = System.currentTimeMillis();
|
||||
if (ts < lastTimestamp) {
|
||||
throw new RuntimeException(String.format("系统时钟回退了%d毫秒", (lastTimestamp - ts)));
|
||||
}
|
||||
|
||||
// 同一时间内,则计算序列号
|
||||
if (ts == lastTimestamp) {
|
||||
// 序列号溢出
|
||||
if (++sequence > maxSequence) {
|
||||
ts = tilNextMillis(lastTimestamp);
|
||||
sequence = 0L;
|
||||
}
|
||||
} else {
|
||||
// 时间戳改变,重置序列号
|
||||
sequence = 0L;
|
||||
}
|
||||
|
||||
lastTimestamp = ts;
|
||||
|
||||
// 0 - 00000000 00000000 00000000 00000000 00000000 0 - 00000000 00 - 00000000 0000
|
||||
// 左移后,低位补0,进行按位或运算相当于二进制拼接
|
||||
// 本来高位还有个0<<63,0与任何数字按位或都是本身,所以写不写效果一样
|
||||
return (ts - beginTs) << timestampLeftOffset | workerId << workerIdLeftOffset | sequence;
|
||||
}
|
||||
|
||||
/**
|
||||
* 阻塞到下一个毫秒
|
||||
*
|
||||
* @param lastTimestamp
|
||||
* @return
|
||||
*/
|
||||
private long tilNextMillis(long lastTimestamp) {
|
||||
long ts = System.currentTimeMillis();
|
||||
while (ts <= lastTimestamp) {
|
||||
ts = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
return ts;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(7);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
long id = snowflakeIdWorker.nextId();
|
||||
System.out.println(id);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,40 @@
|
||||
<?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>
|
@@ -0,0 +1,139 @@
|
||||
<?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.TDDeviceLogDAO">
|
||||
|
||||
<resultMap id="BaseResultMap" type="com.ruoyi.iot.domain.DeviceLog">
|
||||
<id column="ts" property="ts"/>
|
||||
<result column="log_id" property="logId"/>
|
||||
<result column="device_name" property="deviceName"/>
|
||||
<result column="serial_number" property="serialNumber"/>
|
||||
<result column="log_type" property="logType"/>
|
||||
<result column="log_value" property="logValue"/>
|
||||
<result column="identity" property="identity"/>
|
||||
<result column="is_monitor" property="isMonitor"/>
|
||||
<result column="create_by" property="createBy"/>
|
||||
<result column="create_time" property="createTime"/>
|
||||
<result column="remark" property="remark"/>
|
||||
</resultMap>
|
||||
|
||||
<resultMap type="com.ruoyi.iot.model.MonitorModel" id="MonitorResult">
|
||||
<result property="value" column="log_value" />
|
||||
<result property="time" column="create_time" />
|
||||
</resultMap>
|
||||
|
||||
<resultMap type="com.ruoyi.iot.domain.DeviceLog" id="DeviceLogResult">
|
||||
<result property="logId" column="log_id" />
|
||||
<result property="logType" column="log_type" />
|
||||
<result property="logValue" column="log_value" />
|
||||
<result property="deviceId" column="device_id" />
|
||||
<result property="deviceName" column="device_name" />
|
||||
<result property="serialNumber" column="serial_number" />
|
||||
<result property="identity" column="identity" />
|
||||
<result property="createBy" column="create_by" />
|
||||
<result property="isMonitor" column="is_monitor" />
|
||||
<result property="createTime" column="create_time" />
|
||||
<result property="remark" column="remark" />
|
||||
</resultMap>
|
||||
|
||||
<update id="createDB">
|
||||
create database if not exists ${database};
|
||||
</update>
|
||||
|
||||
<update id="createSTable">
|
||||
|
||||
create STABLE if not exists ${database}.device_log
|
||||
(
|
||||
ts timestamp,
|
||||
log_id BIGINT,
|
||||
log_type TINYINT,
|
||||
`identity` BINARY(100),
|
||||
`log_value` BINARY(100),
|
||||
is_monitor TINYINT,
|
||||
create_by BINARY(100),
|
||||
create_time timestamp,
|
||||
remark BINARY(500)
|
||||
|
||||
)
|
||||
TAGS(
|
||||
device_id BIGINT,
|
||||
device_name BINARY(100),
|
||||
serial_number BINARY(50));
|
||||
</update>
|
||||
|
||||
<update id="createTable">
|
||||
create TABLE if not exists ${database}.${tableName}
|
||||
USING ${database}.device_${deviceId} TAGS(log_type,is_monitor,create_by,create_time,remark)
|
||||
(ts2 timestamp,
|
||||
log_type BINARY(100)
|
||||
);
|
||||
</update>
|
||||
|
||||
<select id="selectSTable" resultMap="BaseResultMap">
|
||||
select
|
||||
*
|
||||
from ${database}.device_log
|
||||
order by ts desc
|
||||
</select>
|
||||
|
||||
<insert id="save" parameterType="com.ruoyi.iot.domain.DeviceLog">
|
||||
INSERT INTO ${database}.device_${device.serialNumber} USING device_log
|
||||
TAGS (#{device.deviceId},#{device.deviceName},#{device.serialNumber})
|
||||
VALUES (now, #{device.logId}, #{device.logType},#{device.identity},#{device.logValue},#{device.isMonitor},#{device.createBy},now,#{device.remark} );
|
||||
</insert>
|
||||
|
||||
<delete id="delete" parameterType="com.ruoyi.iot.domain.DeviceLog">
|
||||
delete from ${database}.device_${device.serialNumber}
|
||||
<where>
|
||||
<if test="logId !=null">
|
||||
log_id = #{logId}
|
||||
</if>
|
||||
<if test="deviceId !=null and deviceId!=''">
|
||||
device_id = #{deviceId}
|
||||
</if>
|
||||
<if test="serialNumber !=null and serialNumber!=''">
|
||||
serial_number = #{serialNumber}
|
||||
</if>
|
||||
</where>
|
||||
</delete>
|
||||
|
||||
<select id="selectLogList" resultMap="BaseResultMap">
|
||||
select * from ${database}.device_log
|
||||
<where>
|
||||
<if test="deviceId != null "> and device_id = #{deviceId}</if>
|
||||
<if test="serialNumber != null and serialNumber!=''"> and serial_number = #{serialNumber}</if>
|
||||
<if test="isMonitor != null "> and is_monitor = #{isMonitor}</if>
|
||||
<if test="logType != null "> and log_type = #{logType}</if>
|
||||
|
||||
<if test="beginDate != null "> and create_time <= #{beginDate}</if>
|
||||
<if test="endDate != null "> and create_time >= #{endDate}</if>
|
||||
|
||||
</where>
|
||||
order by ts desc
|
||||
</select>
|
||||
|
||||
<select id="selectMonitorList" parameterType="com.ruoyi.iot.domain.DeviceLog" resultMap="MonitorResult">
|
||||
select log_value, create_time from ${database}.device_log
|
||||
<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>
|
||||
|
||||
order by ts desc
|
||||
limit 2000
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<select id="selectDeviceLogList" parameterType="com.ruoyi.iot.domain.DeviceLog" resultMap="DeviceLogResult">
|
||||
select * from ${database}.device_log
|
||||
<where>
|
||||
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>
|
||||
</where>
|
||||
order by ts desc
|
||||
</select>
|
||||
|
||||
</mapper>
|
Reference in New Issue
Block a user