2022年5月22日23:48:54 增加TDengine存储日志功能,

提供基础的新增和查询功能
This commit is contained in:
wxy001
2022-05-22 23:50:01 +08:00
parent 44f682c3ba
commit 81232fbb04
7 changed files with 193 additions and 236 deletions

View File

@@ -6,9 +6,9 @@ spring:
druid: druid:
# 主库数据源 # 主库数据源
master: master:
url: jdbc:mysql://localhost/wumei-smart?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 url: jdbc:mysql://localhost:3316/wumei_smart?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root username: wumei
password: admin password: 123456
# 从库数据源 # 从库数据源
slave: slave:
# 从数据源开关/默认关闭 # 从数据源开关/默认关闭
@@ -16,6 +16,16 @@ spring:
url: url:
username: username:
password: password:
#TDengine数据库
tdengine-server:
enabled: true
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 initialSize: 5
# 最小连接池数量 # 最小连接池数量

View File

@@ -4,6 +4,7 @@ package com.ruoyi.iot.init;
//import com.ruoyi.mysql.config.TDengineConfig; //import com.ruoyi.mysql.config.TDengineConfig;
//import com.ruoyi.iot.tdengine.config.TDengineConfig; //import com.ruoyi.iot.tdengine.config.TDengineConfig;
import com.alibaba.druid.pool.DruidDataSource;
import com.ruoyi.iot.tdengine.config.TDengineConfig; import com.ruoyi.iot.tdengine.config.TDengineConfig;
import com.ruoyi.iot.domain.DeviceLog; import com.ruoyi.iot.domain.DeviceLog;
import com.ruoyi.iot.mapper.DeviceMapper; import com.ruoyi.iot.mapper.DeviceMapper;
@@ -18,6 +19,7 @@ import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.util.Date; import java.util.Date;
/** /**
@@ -37,23 +39,11 @@ public class ApplicationStarted implements ApplicationRunner {
@Autowired @Autowired
private TDDeviceLogMapper deviceLogMapper; private TDDeviceLogMapper deviceLogMapper;
@Autowired
private ApplicationContext applicationContext;
@Autowired @Autowired
private DeviceMapper deviceMapper; private DeviceMapper deviceMapper;
@Autowired
@Qualifier("mysqlSqlSessionTemplate")
private SqlSessionTemplate mysqlSqlSessionTemplate;
@Autowired
@Qualifier("tdengineSqlSessionTemplate")
private SqlSessionTemplate tdengineSqlSessionTemplate;
@Override @Override
public void run(ApplicationArguments args) { public void run(ApplicationArguments args) {
printBean();
deviceMapper.selectDeviceByDeviceId(0L); deviceMapper.selectDeviceByDeviceId(0L);
System.out.println("初始化MySql链接成功"); System.out.println("初始化MySql链接成功");
initTDengine(); initTDengine();
@@ -61,8 +51,14 @@ public class ApplicationStarted implements ApplicationRunner {
} }
/** /**
* 开始初始化加载系统参数 * @Method
*/ * @Description 开始初始化加载系统参数,创建数据库和超级表
* @Param null
* @return
* @date 2022/5/22,0022 14:27
* @author wxy
*
*/
public void initTDengine() { public void initTDengine() {
try { try {
String dbName = dengineConfig.getDbName(); String dbName = dengineConfig.getDbName();
@@ -77,11 +73,4 @@ public class ApplicationStarted implements ApplicationRunner {
} }
private void printBean(){
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
for (int i = 0; i < beanDefinitionNames.length; i++) {
System.out.println(beanDefinitionNames[i]);
}
}
} }

View File

@@ -11,7 +11,7 @@ import java.util.List;
/** /**
* @package iot.iot.log * @package iot.iot.log
* 类名: LogService * 类名: LogService
* 描述: TODO * 描述: 设备日志记录接口
* 时间: 2022/5/19,0019 18:04 * 时间: 2022/5/19,0019 18:04
* 开发人: admin * 开发人: admin
*/ */
@@ -19,14 +19,6 @@ public interface ILogService {
int saveDeviceLog(DeviceLog deviceLog); int saveDeviceLog(DeviceLog deviceLog);
int deleteDeviceLogById(Long logId);
int deleteDeviceLogByDeviceId(Long deviceId);
List<DeviceLog> selectLogList(Long deviceId, String serialNumber, Long isMonitor, Long logType, Date beginDate, Date endDate);
void save(String topic, String message);
List<DeviceLog> selectDeviceLogList(DeviceLog deviceLog); List<DeviceLog> selectDeviceLogList(DeviceLog deviceLog);
List<MonitorModel> selectMonitorList(DeviceLog deviceLog); List<MonitorModel> selectMonitorList(DeviceLog deviceLog);

View File

@@ -0,0 +1,74 @@
package com.ruoyi.iot.log.service.factory;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.framework.config.MyBatisConfig;
import com.ruoyi.iot.log.service.impl.MySqlLogServiceImpl;
import com.ruoyi.iot.log.service.impl.TdengineLogServiceImpl;
import com.ruoyi.iot.model.MonitorModel;
import com.ruoyi.iot.tdengine.config.TDengineConfig;
import com.ruoyi.iot.domain.DeviceLog;
import com.ruoyi.iot.log.service.ILogService;
import com.ruoyi.iot.mapper.DeviceLogMapper;
import com.ruoyi.iot.tdengine.mapper.TDDeviceLogMapper;
import com.ruoyi.iot.util.SnowflakeIdWorker;
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.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 类名: 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);
TDDeviceLogMapper tDDeviceLogMapper = applicationContext.getBean(TDDeviceLogMapper.class);
ILogService logService = new TdengineLogServiceImpl(tDengineConfig, tDDeviceLogMapper);
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;
}
}
}

View File

@@ -1,203 +0,0 @@
package com.ruoyi.iot.log.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.iot.model.MonitorModel;
import com.ruoyi.iot.tdengine.config.TDengineConfig;
import com.ruoyi.iot.domain.DeviceLog;
import com.ruoyi.iot.log.service.ILogService;
import com.ruoyi.iot.mapper.DeviceLogMapper;
import com.ruoyi.iot.tdengine.mapper.TDDeviceLogMapper;
import com.ruoyi.iot.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 类名: DeviceLogServiceImpl
* 描述: TODO
* 时间: 2022/5/19,0019 18:09
* 开发人: admin
*/
@Service
public class LogServiceImpl implements ILogService {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private DeviceLogMapper deviceLogMapper;
private TDDeviceLogMapper tdDeviceLogMapper;
@Autowired
private TDengineConfig dengineConfig;
private SnowflakeIdWorker snowflakeIdWorker;
private String dbName;
@PostConstruct
private void initDeviceLogMapper() {
if (dengineConfig != null) {
dbName = dengineConfig.getDbName();
snowflakeIdWorker = new SnowflakeIdWorker(1);
this.tdDeviceLogMapper = dengineConfig.getTDengineLogMapper();
}
}
@Override
public int saveDeviceLog(DeviceLog deviceLog) {
if (tdDeviceLogMapper != null) {
{
String dbName = dengineConfig.getDbName();
long logId = snowflakeIdWorker.nextId();
deviceLog.setLogId(logId);
return tdDeviceLogMapper.save(dbName, deviceLog);
}
} else if (deviceLogMapper != null) {
deviceLog.setCreateTime(new Date());
return deviceLogMapper.insertDeviceLog(deviceLog);
} else {
return -1;
}
}
@Override
public int deleteDeviceLogById(Long logId) {
if (logId == null) return 0;
if (tdDeviceLogMapper != null) {
{
String dbName = dengineConfig.getDbName();
DeviceLog deviceLog = new DeviceLog();
deviceLog.setLogId(logId);
return tdDeviceLogMapper.delete(dbName, deviceLog);
}
} else if (deviceLogMapper != null) {
return deviceLogMapper.deleteDeviceLogByLogId(logId);
} else {
return -1;
}
}
@Override
public int deleteDeviceLogByDeviceId(Long deviceId) {
if (deviceId == null) return 0;
if (tdDeviceLogMapper != null) {
{
String dbName = dengineConfig.getDbName();
DeviceLog deviceLog = new DeviceLog();
deviceLog.setDeviceId(deviceId);
return tdDeviceLogMapper.delete(dbName, deviceLog);
}
} else if (deviceLogMapper != null) {
Long[] ids = new Long[1];
ids[0] = deviceId;
return deviceLogMapper.deleteDeviceLogByDeviceIds(ids);
} else {
return -1;
}
}
@Override
public List<DeviceLog> selectLogList(Long deviceId, String serialNumber, Long isMonitor, Long logType, Date beginDate, Date endDate) {
return null;
}
// @Override
// public List<DeviceLog> selectLogList(Long deviceId, String serialNumber, Long isMonitor, Long logType, Date beginDate, Date endDate) {
// List<DeviceLog> list = new ArrayList<>();
// if (tdDeviceLogMapper != null) {
// {
// String dbName = dengineConfig.getDbName();
// list = tdDeviceLogMapper.selectLogList(dbName, deviceId, serialNumber, isMonitor, logType, beginDate, endDate);
//
// return list;
// }
// } else if (deviceLogMapper != null) {
// list = deviceLogMapper.selectLogList(deviceId, serialNumber, isMonitor, logType, beginDate, endDate);
// }
// return list;
// }
@Override
@Async
public void save(String topic, String message) {
System.out.println("topic:" + topic);
System.out.println("message:" + message);
String[] topicItem = topic.split("/");
String subTopic = topicItem[0];
String deviceNum = topicItem[1];
JSONObject jsonObject = JSONObject.parseObject(message);
for (String key : jsonObject.keySet()) {
DeviceLog deviceLog = new DeviceLog();
deviceLog.setLogId(new Date().getTime());
deviceLog.setSerialNumber(deviceNum);
deviceLog.setDeviceName(subTopic);
deviceLog.setDeviceId(Long.parseLong(deviceNum));
deviceLog.setDeviceName("灌溉机:" + deviceNum);
deviceLog.setTs(new Date());
deviceLog.setIsMonitor(1);
deviceLog.setLogType(1);
deviceLog.setIdentity(key);
deviceLog.setLogValue(jsonObject.getString(key));
deviceLog.setRemark(message);
tdDeviceLogMapper.save(dbName, deviceLog);
}
}
@Override
public List<DeviceLog> selectDeviceLogList(DeviceLog deviceLog) {
List<DeviceLog> list = new ArrayList<>();
if (tdDeviceLogMapper != null) {
{
String dbName = dengineConfig.getDbName();
list = tdDeviceLogMapper.selectDeviceLogList(dbName, deviceLog);
return list;
}
} else if (deviceLogMapper != null) {
list = deviceLogMapper.selectDeviceLogList(deviceLog);
}
return list;
}
@Override
public List<MonitorModel> selectMonitorList(DeviceLog deviceLog) {
List<MonitorModel> list = new ArrayList<>();
if (tdDeviceLogMapper != null) {
{
String dbName = dengineConfig.getDbName();
list = tdDeviceLogMapper.selectMonitorList(dbName, deviceLog);
return list;
}
} else if (deviceLogMapper != null) {
list = deviceLogMapper.selectMonitorList(deviceLog);
}
return list;
}
// @Override
// public List<DeviceLog> selectLogListPage(Integer pageSize, Integer pageNum,Long deviceId,String serialNumber,Long isMonitor,Long logType, Date beginDate, Date endDate) {
// if (tdDeviceLogMapper != null) {
// {
// String dbName = dengineConfig.getDbName();
// List<DeviceLog> list= tdDeviceLogMapper.selectLogListByPage(dbName,pageSize,pageNum,deviceNum,beginDate,endDate);
//
// return
// }
// } else if (deviceLogMapper != null) {
// return deviceLogMapper.deleteDeviceLogByLogId(logId);
// } else {
// return -1;
// }
// }
}

View File

@@ -0,0 +1,38 @@
package com.ruoyi.iot.log.service.impl;
import com.ruoyi.iot.domain.DeviceLog;
import com.ruoyi.iot.log.service.ILogService;
import com.ruoyi.iot.mapper.DeviceLogMapper;
import com.ruoyi.iot.model.MonitorModel;
import org.springframework.stereotype.Service;
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);
}
}

View File

@@ -0,0 +1,57 @@
package com.ruoyi.iot.log.service.impl;
import com.ruoyi.iot.domain.DeviceLog;
import com.ruoyi.iot.log.service.ILogService;
import com.ruoyi.iot.model.MonitorModel;
import com.ruoyi.iot.tdengine.config.TDengineConfig;
import com.ruoyi.iot.tdengine.mapper.TDDeviceLogMapper;
import com.ruoyi.iot.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 类名: TdengineLogServiceImpl
* 描述: TDengine存储日志数据实现类
* 时间: 2022/5/22,0022 13:38
* 开发人: admin
*/
public class TdengineLogServiceImpl implements ILogService {
private ApplicationContext applicationContext;
private TDDeviceLogMapper tdDeviceLogMapper;
private TDengineConfig tDengineConfig;
private SnowflakeIdWorker snowflakeIdWorker;
private String dbName;
public TdengineLogServiceImpl(TDengineConfig _tDengineConfig,TDDeviceLogMapper _tdDeviceLogMapper) {
this.tdDeviceLogMapper = _tdDeviceLogMapper;
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 tdDeviceLogMapper.save(dbName,deviceLog);
}
@Override
public List<DeviceLog> selectDeviceLogList(DeviceLog deviceLog) {
return tdDeviceLogMapper.selectDeviceLogList(dbName,deviceLog);
}
@Override
public List<MonitorModel> selectMonitorList(DeviceLog deviceLog) {
return tdDeviceLogMapper.selectMonitorList(dbName,deviceLog);
}
}