From 44f682c3bafe7805357c4ccdf8e784fe8a6b53b4 Mon Sep 17 00:00:00 2001 From: wxy001 Date: Fri, 20 May 2022 19:32:14 +0800 Subject: [PATCH 1/4] =?UTF-8?q?2022=E5=B9=B45=E6=9C=8820=E6=97=A519:30:44?= =?UTF-8?q?=201.=E9=9B=86=E6=88=90TDengine=E4=BD=9C=E4=B8=BA=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E5=92=8C=E7=9B=91=E6=B5=8B=E6=95=B0=E6=8D=AE=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E5=BA=93=E4=BD=BF=E7=94=A8=202.=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E7=AE=80=E5=8D=95=E7=9A=84=E6=96=B0=E5=A2=9E=E5=92=8C=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=8A=9F=E8=83=BD=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/config/ApplicationConfig.java | 10 +- .../ruoyi/framework/config/MyBatisConfig.java | 54 ++++- springboot/wumei-iot/pom.xml | 9 + .../java/com/ruoyi/iot/domain/DeviceLog.java | 15 +- .../ruoyi/iot/init/ApplicationStarted.java | 87 ++++++++ .../ruoyi/iot/log/service/ILogService.java | 33 +++ .../iot/log/service/impl/LogServiceImpl.java | 203 ++++++++++++++++++ .../com/ruoyi/iot/mapper/DeviceLogMapper.java | 18 +- .../java/com/ruoyi/iot/mqtt/EmqxService.java | 9 +- .../service/impl/DeviceLogServiceImpl.java | 13 +- .../iot/service/impl/DeviceServiceImpl.java | 13 +- .../iot/tdengine/config/TDengineConfig.java | 113 ++++++++++ .../iot/tdengine/mapper/DatabaseMapper.java | 25 +++ .../tdengine/mapper/TDDeviceLogMapper.java | 42 ++++ .../com/ruoyi/iot/util/SnowflakeIdWorker.java | 104 +++++++++ .../mapper/tdengine/DatabaseMapper.xml | 58 +++++ .../mapper/tdengine/TDDeviceLogMapper.xml | 139 ++++++++++++ 17 files changed, 913 insertions(+), 32 deletions(-) create mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/init/ApplicationStarted.java create mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/ILogService.java create mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/LogServiceImpl.java create mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/config/TDengineConfig.java create mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/DatabaseMapper.java create mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/TDDeviceLogMapper.java create mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/util/SnowflakeIdWorker.java create mode 100644 springboot/wumei-iot/src/main/resources/mapper/tdengine/DatabaseMapper.xml create mode 100644 springboot/wumei-iot/src/main/resources/mapper/tdengine/TDDeviceLogMapper.xml diff --git a/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/ApplicationConfig.java b/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/ApplicationConfig.java index 1d4dc1f7..d4e1a102 100644 --- a/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/ApplicationConfig.java +++ b/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/ApplicationConfig.java @@ -12,11 +12,11 @@ import org.springframework.context.annotation.EnableAspectJAutoProxy; * * @author ruoyi */ -@Configuration -// 表示通过aop框架暴露该代理对象,AopContext能够访问 -@EnableAspectJAutoProxy(exposeProxy = true) -// 指定要扫描的Mapper类的包的路径 -@MapperScan("com.ruoyi.**.mapper") +//@Configuration +//// 表示通过aop框架暴露该代理对象,AopContext能够访问 +//@EnableAspectJAutoProxy(exposeProxy = true) +//// 指定要扫描的Mapper类的包的路径 +//@MapperScan("com.ruoyi.**.mapper") public class ApplicationConfig { /** diff --git a/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java b/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java index 057c9419..d1503d6d 100644 --- a/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java +++ b/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java @@ -1,18 +1,34 @@ package com.ruoyi.framework.config; import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; +import java.util.*; +import javax.servlet.*; import javax.sql.DataSource; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; +import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties; +import com.alibaba.druid.util.Utils; +import com.ruoyi.common.enums.DataSourceType; +import com.ruoyi.common.utils.spring.SpringUtils; +import com.ruoyi.framework.config.properties.DruidProperties; +import com.ruoyi.framework.datasource.DynamicDataSource; 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.annotation.MapperScan; import org.mybatis.spring.boot.autoconfigure.SpringBootVFS; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.jdbc.DataSourceBuilder; +import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.core.annotation.Order; import org.springframework.core.env.Environment; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; @@ -21,17 +37,18 @@ 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 -public class MyBatisConfig -{ +@MapperScan(basePackages = {"com.ruoyi.iot.mapper", "com.ruoyi.system.mapper", "com.ruoyi.quartz.mapper", "com.ruoyi.generator.mapper"}, sqlSessionTemplateRef = "mysqlSqlSessionTemplate") +public class MyBatisConfig { @Autowired private Environment env; @@ -113,11 +130,13 @@ public class MyBatisConfig return resources.toArray(new Resource[resources.size()]); } - @Bean - public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception + @Bean(name = "mysqlSessionFactory") + @Primary + public SqlSessionFactory sqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dataSource) throws Exception { - String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage"); - String mapperLocations = env.getProperty("mybatis.mapperLocations"); + String typeAliasesPackage = "com.ruoyi.**.domain";//env.getProperty("mybatis.typeAliasesPackage"); + String mapperLocations = "classpath:mapper/iot/*Mapper.xml,classpath:mapper/system/*Mapper.xml,classpath:mapper/quartz/*Mapper.xml"; +// String typeAliasesPackage = "com.ruoyi.**.domain"; String configLocation = env.getProperty("mybatis.configLocation"); typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage); VFS.addImplClass(SpringBootVFS.class); @@ -129,4 +148,17 @@ 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) throws Exception { + return new SqlSessionTemplate(sqlSessionFactory); + } + } \ No newline at end of file diff --git a/springboot/wumei-iot/pom.xml b/springboot/wumei-iot/pom.xml index f0d12628..c716b72a 100644 --- a/springboot/wumei-iot/pom.xml +++ b/springboot/wumei-iot/pom.xml @@ -89,6 +89,15 @@ + + + com.taosdata.jdbc + taos-jdbcdriver + 2.0.38 + + + + \ No newline at end of file diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/domain/DeviceLog.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/domain/DeviceLog.java index b2909ef8..83f9c7c8 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/domain/DeviceLog.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/domain/DeviceLog.java @@ -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; } diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/init/ApplicationStarted.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/init/ApplicationStarted.java new file mode 100644 index 00000000..6248a92a --- /dev/null +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/init/ApplicationStarted.java @@ -0,0 +1,87 @@ +package com.ruoyi.iot.init; + + +//import com.ruoyi.mysql.config.TDengineConfig; + +//import com.ruoyi.iot.tdengine.config.TDengineConfig; +import com.ruoyi.iot.tdengine.config.TDengineConfig; + import com.ruoyi.iot.domain.DeviceLog; +import com.ruoyi.iot.mapper.DeviceMapper; +import com.ruoyi.iot.tdengine.mapper.TDDeviceLogMapper; + import org.mybatis.spring.SqlSessionTemplate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; + +import java.util.Date; + +/** + * 类名: 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 TDengineConfig dengineConfig; + + @Autowired + private TDDeviceLogMapper deviceLogMapper; + + @Autowired + private ApplicationContext applicationContext; + + @Autowired + private DeviceMapper deviceMapper; + + @Autowired + @Qualifier("mysqlSqlSessionTemplate") + private SqlSessionTemplate mysqlSqlSessionTemplate; + + @Autowired + @Qualifier("tdengineSqlSessionTemplate") + private SqlSessionTemplate tdengineSqlSessionTemplate; + + @Override + public void run(ApplicationArguments args) { + printBean(); + deviceMapper.selectDeviceByDeviceId(0L); + System.out.println("初始化MySql链接成功"); + initTDengine(); + System.out.println("初始化TDengine链接成功"); + } + + /** + * 开始初始化加载系统参数 + */ + public void initTDengine() { + try { + String dbName = dengineConfig.getDbName(); + int db = deviceLogMapper.createDB(dbName); + deviceLogMapper.createSTable(dbName); + + System.out.println(db); + } catch (Exception e) { + e.printStackTrace(); + System.out.println("ERROR"); + } + + } + + private void printBean(){ + String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames(); + for (int i = 0; i < beanDefinitionNames.length; i++) { + System.out.println(beanDefinitionNames[i]); + + } + } +} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/ILogService.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/ILogService.java new file mode 100644 index 00000000..f1a20036 --- /dev/null +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/ILogService.java @@ -0,0 +1,33 @@ +package com.ruoyi.iot.log.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 + * 描述: TODO + * 时间: 2022/5/19,0019 18:04 + * 开发人: admin + */ +public interface ILogService { + + int saveDeviceLog(DeviceLog deviceLog); + + int deleteDeviceLogById(Long logId); + + int deleteDeviceLogByDeviceId(Long deviceId); + + List selectLogList(Long deviceId, String serialNumber, Long isMonitor, Long logType, Date beginDate, Date endDate); + + void save(String topic, String message); + + List selectDeviceLogList(DeviceLog deviceLog); + + List selectMonitorList(DeviceLog deviceLog); +} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/LogServiceImpl.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/LogServiceImpl.java new file mode 100644 index 00000000..da330a78 --- /dev/null +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/LogServiceImpl.java @@ -0,0 +1,203 @@ +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 selectLogList(Long deviceId, String serialNumber, Long isMonitor, Long logType, Date beginDate, Date endDate) { + return null; + } + +// @Override +// public List selectLogList(Long deviceId, String serialNumber, Long isMonitor, Long logType, Date beginDate, Date endDate) { +// List 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 selectDeviceLogList(DeviceLog deviceLog) { + List 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 selectMonitorList(DeviceLog deviceLog) { + List 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 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 list= tdDeviceLogMapper.selectLogListByPage(dbName,pageSize,pageNum,deviceNum,beginDate,endDate); +// +// return +// } +// } else if (deviceLogMapper != null) { +// return deviceLogMapper.deleteDeviceLogByLogId(logId); +// } else { +// return -1; +// } +// } +} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mapper/DeviceLogMapper.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mapper/DeviceLogMapper.java index 619d0d57..73b66fc9 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mapper/DeviceLogMapper.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mapper/DeviceLogMapper.java @@ -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 selectLogList(Long deviceId,String serialNumber,Long isMonitor,Long logType, Date beginDate, Date endDate); } diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mqtt/EmqxService.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mqtt/EmqxService.java index 98de2000..bd875259 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mqtt/EmqxService.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mqtt/EmqxService.java @@ -3,6 +3,7 @@ package com.ruoyi.iot.mqtt; import com.alibaba.fastjson.JSON; import com.ruoyi.iot.domain.Device; import com.ruoyi.iot.domain.DeviceLog; +import com.ruoyi.iot.log.service.ILogService; import com.ruoyi.iot.model.NtpModel; import com.ruoyi.iot.model.ThingsModels.IdentityAndName; import com.ruoyi.iot.model.ThingsModels.ThingsModelValueItem; @@ -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()); diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceLogServiceImpl.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceLogServiceImpl.java index c0fbd020..da92a13d 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceLogServiceImpl.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceLogServiceImpl.java @@ -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.log.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 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 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); } /** diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceServiceImpl.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceServiceImpl.java index c32a0408..b40e0112 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceServiceImpl.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceServiceImpl.java @@ -11,6 +11,7 @@ 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.log.service.ILogService; import com.ruoyi.iot.mapper.DeviceLogMapper; import com.ruoyi.iot.mapper.DeviceMapper; import com.ruoyi.iot.mapper.DeviceUserMapper; @@ -22,6 +23,7 @@ import com.ruoyi.iot.service.IDeviceService; import com.ruoyi.iot.service.IProductService; import com.ruoyi.iot.service.IToolService; import com.ruoyi.system.service.ISysUserService; +import org.checkerframework.checker.units.qual.A; import org.quartz.SchedulerException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -73,8 +75,11 @@ public class DeviceServiceImpl implements IDeviceService { @Autowired private ISysUserService userService; +// @Autowired +// private IDeviceLogService deviceLogService; + @Autowired - private IDeviceLogService deviceLogService; + private ILogService logService; /** * 查询设备 @@ -196,7 +201,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; } } @@ -588,7 +594,8 @@ public class DeviceServiceImpl implements IDeviceService { deviceLog.setIdentity("offline"); deviceLog.setLogType(6); } - deviceLogService.insertDeviceLog(deviceLog); + logService.saveDeviceLog(deviceLog); +// deviceLogService.insertDeviceLog(deviceLog); return result; } diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/config/TDengineConfig.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/config/TDengineConfig.java new file mode 100644 index 00000000..9343ce19 --- /dev/null +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/config/TDengineConfig.java @@ -0,0 +1,113 @@ +package com.ruoyi.iot.tdengine.config; + +import com.alibaba.druid.pool.DruidDataSource; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.iot.tdengine.mapper.TDDeviceLogMapper; +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.mapper"}, sqlSessionTemplateRef = "tdengineSqlSessionTemplate") +@ConditionalOnProperty(name = "spring.datasource.druid.tdengine-server.enabled", havingValue = "true") +public class TDengineConfig { + + @Autowired + private TDDeviceLogMapper deviceLogMapper; + + @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); + } + + +// @Bean +// @ConditionalOnProperty(name = "spring.datasource.druid.tdengine-server.enabled", havingValue = "true") +// public TDDeviceLogMapper genTdengineLogMapper(@Qualifier("tDengineDataSource") TDDeviceLogMapper mapper) { +// if(this.deviceLogMapper==null){ +// this.deviceLogMapper=mapper; +// } +// return this.deviceLogMapper; +// } + + public TDDeviceLogMapper getTDengineLogMapper() { + return deviceLogMapper; + } + + + + public String getDbName() { + return dbName; + } + + public void setDbName(String dbName) { + this.dbName = dbName; + } + public Resource[] resolveMapperLocations(String[] mapperLocations) + { + ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(); + List resources = new ArrayList(); + 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()]); + } + + +} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/DatabaseMapper.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/DatabaseMapper.java new file mode 100644 index 00000000..076e66ac --- /dev/null +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/DatabaseMapper.java @@ -0,0 +1,25 @@ +package com.ruoyi.iot.tdengine.mapper; + +import org.springframework.stereotype.Repository; + +/** + * @package com.ruoyi.mysql.mysql.tdengine + * 类名: DatabaseMapper + * 描述: TODO + * 时间: 2022/5/16,0016 1:27 + * 开发人: wxy + */ +@Repository +public interface DatabaseMapper { + + int createDB(); + + int dropDatabase(); + + int useDatabase(); + + int createTable(); + + + +} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/TDDeviceLogMapper.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/TDDeviceLogMapper.java new file mode 100644 index 00000000..99aa0f6a --- /dev/null +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/TDDeviceLogMapper.java @@ -0,0 +1,42 @@ +package com.ruoyi.iot.tdengine.mapper; + +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.lang.management.MonitorInfo; +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 TDDeviceLogMapper { + + + 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 selectSTable(String database,DeviceLog deviceLog); + + int delete(String dbName, DeviceLog deviceLog); + +// List selectLogListByPage(String dbName, Integer pageSize, Integer pageNum, String deviceNum, Date beginDate, Date endDate); + + List selectLogList(String dbName, Long deviceId, String serialNumber, Long isMonitor, Long logType, Date beginDate, Date endDate); + + List selectMonitorList(@Param("database") String database, @Param("device") DeviceLog deviceLog); + + List selectDeviceLogList(@Param("database") String database,@Param("device") DeviceLog deviceLog); +} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/util/SnowflakeIdWorker.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/util/SnowflakeIdWorker.java new file mode 100644 index 00000000..ff5a11d3 --- /dev/null +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/util/SnowflakeIdWorker.java @@ -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); + } + } +} \ No newline at end of file diff --git a/springboot/wumei-iot/src/main/resources/mapper/tdengine/DatabaseMapper.xml b/springboot/wumei-iot/src/main/resources/mapper/tdengine/DatabaseMapper.xml new file mode 100644 index 00000000..a2925db9 --- /dev/null +++ b/springboot/wumei-iot/src/main/resources/mapper/tdengine/DatabaseMapper.xml @@ -0,0 +1,58 @@ + + + + + + + create database if not exists ${dbName}; + + + + + DROP database if exists ${dbName}; + + + + + use ${dbName}; + + + + + 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), + ); + + + + 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), + ); + + + + \ No newline at end of file diff --git a/springboot/wumei-iot/src/main/resources/mapper/tdengine/TDDeviceLogMapper.xml b/springboot/wumei-iot/src/main/resources/mapper/tdengine/TDDeviceLogMapper.xml new file mode 100644 index 00000000..a17ef4d2 --- /dev/null +++ b/springboot/wumei-iot/src/main/resources/mapper/tdengine/TDDeviceLogMapper.xml @@ -0,0 +1,139 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + create database if not exists ${database}; + + + + + 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)); + + + + 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) + ); + + + + + + 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} ); + + + + delete from ${database}.device_${device.serialNumber} + + + log_id = #{logId} + + + device_id = #{deviceId} + + + serial_number = #{serialNumber} + + + + + + + + + + + \ No newline at end of file -- Gitee From 81232fbb04b7684f564c63b809f73680f7ae320a Mon Sep 17 00:00:00 2001 From: wxy001 Date: Sun, 22 May 2022 23:50:01 +0800 Subject: [PATCH 2/4] =?UTF-8?q?2022=E5=B9=B45=E6=9C=8822=E6=97=A523:48:54?= =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0TDengine=E5=AD=98=E5=82=A8=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E5=8A=9F=E8=83=BD=EF=BC=8C=20=E6=8F=90=E4=BE=9B?= =?UTF-8?q?=E5=9F=BA=E7=A1=80=E7=9A=84=E6=96=B0=E5=A2=9E=E5=92=8C=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application-druid.yml | 16 +- .../ruoyi/iot/init/ApplicationStarted.java | 31 +-- .../ruoyi/iot/log/service/ILogService.java | 10 +- .../service/factory/LogServiceFactory.java | 74 +++++++ .../iot/log/service/impl/LogServiceImpl.java | 203 ------------------ .../log/service/impl/MySqlLogServiceImpl.java | 38 ++++ .../service/impl/TdengineLogServiceImpl.java | 57 +++++ 7 files changed, 193 insertions(+), 236 deletions(-) create mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/factory/LogServiceFactory.java delete mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/LogServiceImpl.java create mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/MySqlLogServiceImpl.java create mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/TdengineLogServiceImpl.java diff --git a/springboot/wumei-admin/src/main/resources/application-druid.yml b/springboot/wumei-admin/src/main/resources/application-druid.yml index 9578e066..844bd4b9 100644 --- a/springboot/wumei-admin/src/main/resources/application-druid.yml +++ b/springboot/wumei-admin/src/main/resources/application-druid.yml @@ -6,9 +6,9 @@ spring: druid: # 主库数据源 master: - url: jdbc:mysql://localhost/wumei-smart?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 - username: root - password: admin + url: jdbc:mysql://localhost:3316/wumei_smart?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 + username: wumei + password: 123456 # 从库数据源 slave: # 从数据源开关/默认关闭 @@ -16,6 +16,16 @@ spring: url: username: 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 # 最小连接池数量 diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/init/ApplicationStarted.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/init/ApplicationStarted.java index 6248a92a..e809d54e 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/init/ApplicationStarted.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/init/ApplicationStarted.java @@ -4,6 +4,7 @@ package com.ruoyi.iot.init; //import com.ruoyi.mysql.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.domain.DeviceLog; import com.ruoyi.iot.mapper.DeviceMapper; @@ -18,6 +19,7 @@ import org.springframework.boot.ApplicationRunner; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; +import javax.sql.DataSource; import java.util.Date; /** @@ -37,23 +39,11 @@ public class ApplicationStarted implements ApplicationRunner { @Autowired private TDDeviceLogMapper deviceLogMapper; - @Autowired - private ApplicationContext applicationContext; - @Autowired private DeviceMapper deviceMapper; - @Autowired - @Qualifier("mysqlSqlSessionTemplate") - private SqlSessionTemplate mysqlSqlSessionTemplate; - - @Autowired - @Qualifier("tdengineSqlSessionTemplate") - private SqlSessionTemplate tdengineSqlSessionTemplate; - @Override public void run(ApplicationArguments args) { - printBean(); deviceMapper.selectDeviceByDeviceId(0L); System.out.println("初始化MySql链接成功"); 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() { try { 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]); - - } - } } diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/ILogService.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/ILogService.java index f1a20036..829c962a 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/ILogService.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/ILogService.java @@ -11,7 +11,7 @@ import java.util.List; /** * @package iot.iot.log * 类名: LogService - * 描述: TODO + * 描述: 设备日志记录接口 * 时间: 2022/5/19,0019 18:04 * 开发人: admin */ @@ -19,14 +19,6 @@ public interface ILogService { int saveDeviceLog(DeviceLog deviceLog); - int deleteDeviceLogById(Long logId); - - int deleteDeviceLogByDeviceId(Long deviceId); - - List selectLogList(Long deviceId, String serialNumber, Long isMonitor, Long logType, Date beginDate, Date endDate); - - void save(String topic, String message); - List selectDeviceLogList(DeviceLog deviceLog); List selectMonitorList(DeviceLog deviceLog); diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/factory/LogServiceFactory.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/factory/LogServiceFactory.java new file mode 100644 index 00000000..acd7be7f --- /dev/null +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/factory/LogServiceFactory.java @@ -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; + } + } +} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/LogServiceImpl.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/LogServiceImpl.java deleted file mode 100644 index da330a78..00000000 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/LogServiceImpl.java +++ /dev/null @@ -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 selectLogList(Long deviceId, String serialNumber, Long isMonitor, Long logType, Date beginDate, Date endDate) { - return null; - } - -// @Override -// public List selectLogList(Long deviceId, String serialNumber, Long isMonitor, Long logType, Date beginDate, Date endDate) { -// List 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 selectDeviceLogList(DeviceLog deviceLog) { - List 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 selectMonitorList(DeviceLog deviceLog) { - List 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 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 list= tdDeviceLogMapper.selectLogListByPage(dbName,pageSize,pageNum,deviceNum,beginDate,endDate); -// -// return -// } -// } else if (deviceLogMapper != null) { -// return deviceLogMapper.deleteDeviceLogByLogId(logId); -// } else { -// return -1; -// } -// } -} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/MySqlLogServiceImpl.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/MySqlLogServiceImpl.java new file mode 100644 index 00000000..1ba1f610 --- /dev/null +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/MySqlLogServiceImpl.java @@ -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 selectDeviceLogList(DeviceLog deviceLog) { + return deviceLogMapper.selectDeviceLogList(deviceLog); + } + + @Override + public List selectMonitorList(DeviceLog deviceLog) { + return deviceLogMapper.selectMonitorList(deviceLog); + } +} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/TdengineLogServiceImpl.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/TdengineLogServiceImpl.java new file mode 100644 index 00000000..58d381a5 --- /dev/null +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/TdengineLogServiceImpl.java @@ -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 selectDeviceLogList(DeviceLog deviceLog) { + return tdDeviceLogMapper.selectDeviceLogList(dbName,deviceLog); + } + + @Override + public List selectMonitorList(DeviceLog deviceLog) { + return tdDeviceLogMapper.selectMonitorList(dbName,deviceLog); + } +} -- Gitee From d032413a5644f586d40712301e351d2d676a1edf Mon Sep 17 00:00:00 2001 From: wxy001 Date: Tue, 24 May 2022 01:06:40 +0800 Subject: [PATCH 3/4] =?UTF-8?q?2022=E5=B9=B45=E6=9C=8824=E6=97=A501:05:38?= =?UTF-8?q?=20=E8=BF=98=E5=8E=9FApplicationStart,=E8=AE=B2TDengine?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E7=B1=BB=E5=9E=8B=E5=92=8C=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=BD=92=E4=BA=8E=E5=90=8C=E4=B8=80=E4=B8=AA=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application-druid.yml | 6 +- .../src/main/resources/application.yml | 17 ++-- .../framework/config/ApplicationConfig.java | 10 +-- .../ruoyi/framework/config/MyBatisConfig.java | 33 +++----- .../ruoyi/iot/init/ApplicationStarted.java | 76 ------------------ .../java/com/ruoyi/iot/mqtt/EmqxService.java | 5 +- .../service/impl/DeviceLogServiceImpl.java | 2 +- .../iot/service/impl/DeviceServiceImpl.java | 4 +- .../iot/tdengine/config/TDengineConfig.java | 14 +--- .../{mapper => dao}/DatabaseMapper.java | 2 +- .../{mapper => dao}/TDDeviceLogMapper.java | 3 +- .../iot/tdengine/init/ApplicationStarted.java | 79 +++++++++++++++++++ .../service/ILogService.java | 2 +- .../service/factory/LogServiceFactory.java | 21 ++--- .../service/impl/MySqlLogServiceImpl.java | 5 +- .../service/impl/TdengineLogServiceImpl.java | 9 +-- .../mapper/tdengine/DatabaseMapper.xml | 2 +- .../mapper/tdengine/TDDeviceLogMapper.xml | 2 +- 18 files changed, 125 insertions(+), 167 deletions(-) delete mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/init/ApplicationStarted.java rename springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/{mapper => dao}/DatabaseMapper.java (89%) rename springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/{mapper => dao}/TDDeviceLogMapper.java (94%) create mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/init/ApplicationStarted.java rename springboot/wumei-iot/src/main/java/com/ruoyi/iot/{log => tdengine}/service/ILogService.java (92%) rename springboot/wumei-iot/src/main/java/com/ruoyi/iot/{log => tdengine}/service/factory/LogServiceFactory.java (75%) rename springboot/wumei-iot/src/main/java/com/ruoyi/iot/{log => tdengine}/service/impl/MySqlLogServiceImpl.java (87%) rename springboot/wumei-iot/src/main/java/com/ruoyi/iot/{log => tdengine}/service/impl/TdengineLogServiceImpl.java (83%) diff --git a/springboot/wumei-admin/src/main/resources/application-druid.yml b/springboot/wumei-admin/src/main/resources/application-druid.yml index 844bd4b9..ad6dbaf8 100644 --- a/springboot/wumei-admin/src/main/resources/application-druid.yml +++ b/springboot/wumei-admin/src/main/resources/application-druid.yml @@ -6,9 +6,9 @@ spring: druid: # 主库数据源 master: - url: jdbc:mysql://localhost:3316/wumei_smart?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 - username: wumei - password: 123456 + url: jdbc:mysql://localhost/wumei-smart?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 + username: root + password: admin # 从库数据源 slave: # 从数据源开关/默认关闭 diff --git a/springboot/wumei-admin/src/main/resources/application.yml b/springboot/wumei-admin/src/main/resources/application.yml index a99c9bb5..6637f1ac 100644 --- a/springboot/wumei-admin/src/main/resources/application.yml +++ b/springboot/wumei-admin/src/main/resources/application.yml @@ -43,7 +43,7 @@ spring: # 国际化资源文件路径 basename: i18n/messages profiles: - active: druid + active: dev # 文件上传 servlet: multipart: @@ -65,7 +65,7 @@ spring: # 数据库索引 database: 0 # 密码 - password: wumei-smart + password: # 连接超时时间 timeout: 10s lettuce: @@ -80,12 +80,13 @@ spring: max-wait: -1ms # mqtt 配置 mqtt: - username: wumei-smart # 账号 - password: wumei-smart # 密码 - host-url: tcp://localhost:1883 # mqtt连接tcp地址 - client-id: ${random.int} # 客户端Id,不能相同,采用随机数 ${random.value} - default-topic: test # 默认主题 - timeout: 30000 # 超时时间 + username: wumei-smart # 账号 + password: wumei-smart # 密码 + host-url: tcp://wumei.live:1883 # mqtt连接tcp地址 + # host-url: tcp://127.0.0.1:1883 + client-id: ${random.value} # 客户端Id,每个启动的id要不同 随机:${random.value} + default-topic: test # 默认主题 + timeout: 100 # 超时时间 keepalive: 30 # 保持连接 clearSession: true # 清除会话(设置为false,断开连接,重连后使用原来的会话 保留订阅的主题,能接收离线期间的消息) task: diff --git a/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/ApplicationConfig.java b/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/ApplicationConfig.java index d4e1a102..1d4dc1f7 100644 --- a/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/ApplicationConfig.java +++ b/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/ApplicationConfig.java @@ -12,11 +12,11 @@ import org.springframework.context.annotation.EnableAspectJAutoProxy; * * @author ruoyi */ -//@Configuration -//// 表示通过aop框架暴露该代理对象,AopContext能够访问 -//@EnableAspectJAutoProxy(exposeProxy = true) -//// 指定要扫描的Mapper类的包的路径 -//@MapperScan("com.ruoyi.**.mapper") +@Configuration +// 表示通过aop框架暴露该代理对象,AopContext能够访问 +@EnableAspectJAutoProxy(exposeProxy = true) +// 指定要扫描的Mapper类的包的路径 +@MapperScan("com.ruoyi.**.mapper") public class ApplicationConfig { /** diff --git a/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java b/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java index d1503d6d..60155a3b 100644 --- a/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java +++ b/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java @@ -1,34 +1,21 @@ package com.ruoyi.framework.config; import java.io.IOException; -import java.util.*; -import javax.servlet.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; import javax.sql.DataSource; - -import com.alibaba.druid.pool.DruidDataSource; -import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; -import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties; -import com.alibaba.druid.util.Utils; -import com.ruoyi.common.enums.DataSourceType; -import com.ruoyi.common.utils.spring.SpringUtils; -import com.ruoyi.framework.config.properties.DruidProperties; -import com.ruoyi.framework.datasource.DynamicDataSource; 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.annotation.MapperScan; import org.mybatis.spring.boot.autoconfigure.SpringBootVFS; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.boot.jdbc.DataSourceBuilder; -import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; -import org.springframework.core.annotation.Order; import org.springframework.core.env.Environment; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; @@ -47,8 +34,8 @@ import com.ruoyi.common.utils.StringUtils; * @author ruoyi */ @Configuration -@MapperScan(basePackages = {"com.ruoyi.iot.mapper", "com.ruoyi.system.mapper", "com.ruoyi.quartz.mapper", "com.ruoyi.generator.mapper"}, sqlSessionTemplateRef = "mysqlSqlSessionTemplate") -public class MyBatisConfig { +public class MyBatisConfig +{ @Autowired private Environment env; @@ -132,11 +119,10 @@ public class MyBatisConfig { @Bean(name = "mysqlSessionFactory") @Primary - public SqlSessionFactory sqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dataSource) throws Exception + public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { - String typeAliasesPackage = "com.ruoyi.**.domain";//env.getProperty("mybatis.typeAliasesPackage"); - String mapperLocations = "classpath:mapper/iot/*Mapper.xml,classpath:mapper/system/*Mapper.xml,classpath:mapper/quartz/*Mapper.xml"; -// String typeAliasesPackage = "com.ruoyi.**.domain"; + String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage"); + String mapperLocations = env.getProperty("mybatis.mapperLocations"); String configLocation = env.getProperty("mybatis.configLocation"); typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage); VFS.addImplClass(SpringBootVFS.class); @@ -160,5 +146,4 @@ public class MyBatisConfig { public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } - } \ No newline at end of file diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/init/ApplicationStarted.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/init/ApplicationStarted.java deleted file mode 100644 index e809d54e..00000000 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/init/ApplicationStarted.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.ruoyi.iot.init; - - -//import com.ruoyi.mysql.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.domain.DeviceLog; -import com.ruoyi.iot.mapper.DeviceMapper; -import com.ruoyi.iot.tdengine.mapper.TDDeviceLogMapper; - import org.mybatis.spring.SqlSessionTemplate; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; -import org.springframework.context.ApplicationContext; -import org.springframework.stereotype.Component; - -import javax.sql.DataSource; -import java.util.Date; - -/** - * 类名: 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 TDengineConfig dengineConfig; - - @Autowired - private TDDeviceLogMapper deviceLogMapper; - - @Autowired - private DeviceMapper deviceMapper; - - @Override - public void run(ApplicationArguments args) { - deviceMapper.selectDeviceByDeviceId(0L); - System.out.println("初始化MySql链接成功"); - initTDengine(); - System.out.println("初始化TDengine链接成功"); - } - - /** - * @Method - * @Description 开始初始化加载系统参数,创建数据库和超级表 - * @Param null - * @return - * @date 2022/5/22,0022 14:27 - * @author wxy - * - */ - public void initTDengine() { - try { - String dbName = dengineConfig.getDbName(); - int db = deviceLogMapper.createDB(dbName); - deviceLogMapper.createSTable(dbName); - - System.out.println(db); - } catch (Exception e) { - e.printStackTrace(); - System.out.println("ERROR"); - } - - } - -} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mqtt/EmqxService.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mqtt/EmqxService.java index bd875259..0d2d1710 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mqtt/EmqxService.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mqtt/EmqxService.java @@ -3,16 +3,13 @@ package com.ruoyi.iot.mqtt; import com.alibaba.fastjson.JSON; import com.ruoyi.iot.domain.Device; import com.ruoyi.iot.domain.DeviceLog; -import com.ruoyi.iot.log.service.ILogService; +import com.ruoyi.iot.tdengine.service.ILogService; import com.ruoyi.iot.model.NtpModel; import com.ruoyi.iot.model.ThingsModels.IdentityAndName; -import com.ruoyi.iot.model.ThingsModels.ThingsModelValueItem; 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 org.eclipse.paho.client.mqttv3.MqttAsyncClient; -import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttMessage; import org.slf4j.Logger; diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceLogServiceImpl.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceLogServiceImpl.java index da92a13d..54a9ef64 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceLogServiceImpl.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceLogServiceImpl.java @@ -2,7 +2,7 @@ package com.ruoyi.iot.service.impl; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.iot.domain.DeviceLog; -import com.ruoyi.iot.log.service.ILogService; +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; diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceServiceImpl.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceServiceImpl.java index b40e0112..a3eaadc5 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceServiceImpl.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/service/impl/DeviceServiceImpl.java @@ -11,19 +11,17 @@ 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.log.service.ILogService; +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; import com.ruoyi.system.service.ISysUserService; -import org.checkerframework.checker.units.qual.A; import org.quartz.SchedulerException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/config/TDengineConfig.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/config/TDengineConfig.java index 9343ce19..316d034b 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/config/TDengineConfig.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/config/TDengineConfig.java @@ -2,7 +2,7 @@ package com.ruoyi.iot.tdengine.config; import com.alibaba.druid.pool.DruidDataSource; import com.ruoyi.common.utils.StringUtils; -import com.ruoyi.iot.tdengine.mapper.TDDeviceLogMapper; +import com.ruoyi.iot.tdengine.dao.TDDeviceLogMapper; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; @@ -31,7 +31,7 @@ import java.util.List; * 开发人: wxy */ @Configuration -@MapperScan(basePackages = {"com.ruoyi.iot.tdengine.mapper"}, sqlSessionTemplateRef = "tdengineSqlSessionTemplate") +@MapperScan(basePackages = {"com.ruoyi.iot.tdengine.dao"}, sqlSessionTemplateRef = "tdengineSqlSessionTemplate") @ConditionalOnProperty(name = "spring.datasource.druid.tdengine-server.enabled", havingValue = "true") public class TDengineConfig { @@ -64,16 +64,6 @@ public class TDengineConfig { return new SqlSessionTemplate(sqlSessionFactory); } - -// @Bean -// @ConditionalOnProperty(name = "spring.datasource.druid.tdengine-server.enabled", havingValue = "true") -// public TDDeviceLogMapper genTdengineLogMapper(@Qualifier("tDengineDataSource") TDDeviceLogMapper mapper) { -// if(this.deviceLogMapper==null){ -// this.deviceLogMapper=mapper; -// } -// return this.deviceLogMapper; -// } - public TDDeviceLogMapper getTDengineLogMapper() { return deviceLogMapper; } diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/DatabaseMapper.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/DatabaseMapper.java similarity index 89% rename from springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/DatabaseMapper.java rename to springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/DatabaseMapper.java index 076e66ac..5eb3b677 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/DatabaseMapper.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/DatabaseMapper.java @@ -1,4 +1,4 @@ -package com.ruoyi.iot.tdengine.mapper; +package com.ruoyi.iot.tdengine.dao; import org.springframework.stereotype.Repository; diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/TDDeviceLogMapper.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/TDDeviceLogMapper.java similarity index 94% rename from springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/TDDeviceLogMapper.java rename to springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/TDDeviceLogMapper.java index 99aa0f6a..d338823d 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/TDDeviceLogMapper.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/TDDeviceLogMapper.java @@ -1,11 +1,10 @@ -package com.ruoyi.iot.tdengine.mapper; +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.lang.management.MonitorInfo; import java.util.Date; import java.util.List; diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/init/ApplicationStarted.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/init/ApplicationStarted.java new file mode 100644 index 00000000..5a2da7f4 --- /dev/null +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/init/ApplicationStarted.java @@ -0,0 +1,79 @@ +package com.ruoyi.iot.tdengine.init; + + +import com.ruoyi.iot.tdengine.config.TDengineConfig; +import com.ruoyi.iot.tdengine.dao.TDDeviceLogMapper; +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); + TDDeviceLogMapper tDDeviceLogMapper = applicationContext.getBean(TDDeviceLogMapper.class); + initTDengine(tDengineConfig, tDDeviceLogMapper); + System.out.println("初始化TDengine成功"); + }else{ + System.out.println("MySQL初始化成功"); + } + } + + /** + * @return + * @Method + * @Description 开始初始化加载系统参数, 创建数据库和超级表 + * @Param null + * @date 2022/5/22,0022 14:27 + * @author wxy + */ + public void initTDengine(TDengineConfig dengineConfig, TDDeviceLogMapper deviceLogMapper) { + try { + String dbName = dengineConfig.getDbName(); + int db = deviceLogMapper.createDB(dbName); + deviceLogMapper.createSTable(dbName); + System.out.println(db); + } catch (Exception e) { + e.printStackTrace(); + System.out.println("ERROR"); + } + + } + + /** + * @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; + } + } +} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/ILogService.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/ILogService.java similarity index 92% rename from springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/ILogService.java rename to springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/ILogService.java index 829c962a..f775ade5 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/ILogService.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/ILogService.java @@ -1,4 +1,4 @@ -package com.ruoyi.iot.log.service; +package com.ruoyi.iot.tdengine.service; import com.ruoyi.iot.domain.DeviceLog; diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/factory/LogServiceFactory.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/factory/LogServiceFactory.java similarity index 75% rename from springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/factory/LogServiceFactory.java rename to springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/factory/LogServiceFactory.java index acd7be7f..342e7445 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/factory/LogServiceFactory.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/factory/LogServiceFactory.java @@ -1,28 +1,17 @@ -package com.ruoyi.iot.log.service.factory; +package com.ruoyi.iot.tdengine.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.service.impl.MySqlLogServiceImpl; +import com.ruoyi.iot.tdengine.service.impl.TdengineLogServiceImpl; import com.ruoyi.iot.tdengine.config.TDengineConfig; -import com.ruoyi.iot.domain.DeviceLog; -import com.ruoyi.iot.log.service.ILogService; +import com.ruoyi.iot.tdengine.service.ILogService; import com.ruoyi.iot.mapper.DeviceLogMapper; -import com.ruoyi.iot.tdengine.mapper.TDDeviceLogMapper; -import com.ruoyi.iot.util.SnowflakeIdWorker; +import com.ruoyi.iot.tdengine.dao.TDDeviceLogMapper; 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 diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/MySqlLogServiceImpl.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/impl/MySqlLogServiceImpl.java similarity index 87% rename from springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/MySqlLogServiceImpl.java rename to springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/impl/MySqlLogServiceImpl.java index 1ba1f610..ecd4719b 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/MySqlLogServiceImpl.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/impl/MySqlLogServiceImpl.java @@ -1,10 +1,9 @@ -package com.ruoyi.iot.log.service.impl; +package com.ruoyi.iot.tdengine.service.impl; import com.ruoyi.iot.domain.DeviceLog; -import com.ruoyi.iot.log.service.ILogService; +import com.ruoyi.iot.tdengine.service.ILogService; import com.ruoyi.iot.mapper.DeviceLogMapper; import com.ruoyi.iot.model.MonitorModel; -import org.springframework.stereotype.Service; import java.util.List; diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/TdengineLogServiceImpl.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/impl/TdengineLogServiceImpl.java similarity index 83% rename from springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/TdengineLogServiceImpl.java rename to springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/impl/TdengineLogServiceImpl.java index 58d381a5..2b49b460 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/TdengineLogServiceImpl.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/impl/TdengineLogServiceImpl.java @@ -1,15 +1,12 @@ -package com.ruoyi.iot.log.service.impl; +package com.ruoyi.iot.tdengine.service.impl; import com.ruoyi.iot.domain.DeviceLog; -import com.ruoyi.iot.log.service.ILogService; +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.mapper.TDDeviceLogMapper; +import com.ruoyi.iot.tdengine.dao.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; diff --git a/springboot/wumei-iot/src/main/resources/mapper/tdengine/DatabaseMapper.xml b/springboot/wumei-iot/src/main/resources/mapper/tdengine/DatabaseMapper.xml index a2925db9..4cc118cf 100644 --- a/springboot/wumei-iot/src/main/resources/mapper/tdengine/DatabaseMapper.xml +++ b/springboot/wumei-iot/src/main/resources/mapper/tdengine/DatabaseMapper.xml @@ -2,7 +2,7 @@ - + diff --git a/springboot/wumei-iot/src/main/resources/mapper/tdengine/TDDeviceLogMapper.xml b/springboot/wumei-iot/src/main/resources/mapper/tdengine/TDDeviceLogMapper.xml index a17ef4d2..461625c4 100644 --- a/springboot/wumei-iot/src/main/resources/mapper/tdengine/TDDeviceLogMapper.xml +++ b/springboot/wumei-iot/src/main/resources/mapper/tdengine/TDDeviceLogMapper.xml @@ -2,7 +2,7 @@ - + -- Gitee From f783503b5179755711b78cc70cb0a74f561bfa69 Mon Sep 17 00:00:00 2001 From: wxy001 Date: Tue, 24 May 2022 01:53:27 +0800 Subject: [PATCH 4/4] =?UTF-8?q?2022=E5=B9=B45=E6=9C=8824=E6=97=A501:05:38?= =?UTF-8?q?=20=E8=BF=98=E5=8E=9FApplicationStart,=E8=AE=B2TDengine?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E7=B1=BB=E5=9E=8B=E5=92=8C=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=BD=92=E4=BA=8E=E5=90=8C=E4=B8=80=E4=B8=AA=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/framework/config/MyBatisConfig.java | 35 +++------ .../ruoyi/iot/init/ApplicationStarted.java | 76 ------------------- .../ruoyi/iot/log/service/ILogService.java | 25 ------ .../service/factory/LogServiceFactory.java | 74 ------------------ .../log/service/impl/MySqlLogServiceImpl.java | 38 ---------- .../service/impl/TdengineLogServiceImpl.java | 57 -------------- .../java/com/ruoyi/iot/mqtt/EmqxService.java | 2 +- .../iot/tdengine/config/TDengineConfig.java | 22 +----- .../{DatabaseMapper.java => DatabaseDAO.java} | 2 +- ...viceLogMapper.java => TDDeviceLogDAO.java} | 2 +- .../iot/tdengine/init/ApplicationStarted.java | 8 +- .../iot/tdengine/mapper/DatabaseMapper.java | 25 ------ .../tdengine/mapper/TDDeviceLogMapper.java | 42 ---------- .../service/factory/LogServiceFactory.java | 6 +- .../service/impl/TdengineLogServiceImpl.java | 14 ++-- .../mapper/tdengine/DatabaseMapper.xml | 20 +---- .../mapper/tdengine/TDDeviceLogMapper.xml | 2 +- 17 files changed, 30 insertions(+), 420 deletions(-) delete mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/init/ApplicationStarted.java delete mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/ILogService.java delete mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/factory/LogServiceFactory.java delete mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/MySqlLogServiceImpl.java delete mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/TdengineLogServiceImpl.java rename springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/{DatabaseMapper.java => DatabaseDAO.java} (91%) rename springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/{TDDeviceLogMapper.java => TDDeviceLogDAO.java} (97%) delete mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/DatabaseMapper.java delete mode 100644 springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/TDDeviceLogMapper.java diff --git a/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java b/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java index d1503d6d..56463e7a 100644 --- a/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java +++ b/springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java @@ -1,34 +1,21 @@ package com.ruoyi.framework.config; import java.io.IOException; -import java.util.*; -import javax.servlet.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; import javax.sql.DataSource; - -import com.alibaba.druid.pool.DruidDataSource; -import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; -import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties; -import com.alibaba.druid.util.Utils; -import com.ruoyi.common.enums.DataSourceType; -import com.ruoyi.common.utils.spring.SpringUtils; -import com.ruoyi.framework.config.properties.DruidProperties; -import com.ruoyi.framework.datasource.DynamicDataSource; 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.annotation.MapperScan; import org.mybatis.spring.boot.autoconfigure.SpringBootVFS; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.boot.jdbc.DataSourceBuilder; -import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; -import org.springframework.core.annotation.Order; import org.springframework.core.env.Environment; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; @@ -47,8 +34,8 @@ import com.ruoyi.common.utils.StringUtils; * @author ruoyi */ @Configuration -@MapperScan(basePackages = {"com.ruoyi.iot.mapper", "com.ruoyi.system.mapper", "com.ruoyi.quartz.mapper", "com.ruoyi.generator.mapper"}, sqlSessionTemplateRef = "mysqlSqlSessionTemplate") -public class MyBatisConfig { +public class MyBatisConfig +{ @Autowired private Environment env; @@ -132,11 +119,10 @@ public class MyBatisConfig { @Bean(name = "mysqlSessionFactory") @Primary - public SqlSessionFactory sqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dataSource) throws Exception + public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { - String typeAliasesPackage = "com.ruoyi.**.domain";//env.getProperty("mybatis.typeAliasesPackage"); - String mapperLocations = "classpath:mapper/iot/*Mapper.xml,classpath:mapper/system/*Mapper.xml,classpath:mapper/quartz/*Mapper.xml"; -// String typeAliasesPackage = "com.ruoyi.**.domain"; + String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage"); + String mapperLocations = env.getProperty("mybatis.mapperLocations"); String configLocation = env.getProperty("mybatis.configLocation"); typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage); VFS.addImplClass(SpringBootVFS.class); @@ -157,8 +143,7 @@ public class MyBatisConfig { @Bean(name = "mysqlSqlSessionTemplate") @Primary - public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { + public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } - } \ No newline at end of file diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/init/ApplicationStarted.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/init/ApplicationStarted.java deleted file mode 100644 index e809d54e..00000000 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/init/ApplicationStarted.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.ruoyi.iot.init; - - -//import com.ruoyi.mysql.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.domain.DeviceLog; -import com.ruoyi.iot.mapper.DeviceMapper; -import com.ruoyi.iot.tdengine.mapper.TDDeviceLogMapper; - import org.mybatis.spring.SqlSessionTemplate; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; -import org.springframework.context.ApplicationContext; -import org.springframework.stereotype.Component; - -import javax.sql.DataSource; -import java.util.Date; - -/** - * 类名: 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 TDengineConfig dengineConfig; - - @Autowired - private TDDeviceLogMapper deviceLogMapper; - - @Autowired - private DeviceMapper deviceMapper; - - @Override - public void run(ApplicationArguments args) { - deviceMapper.selectDeviceByDeviceId(0L); - System.out.println("初始化MySql链接成功"); - initTDengine(); - System.out.println("初始化TDengine链接成功"); - } - - /** - * @Method - * @Description 开始初始化加载系统参数,创建数据库和超级表 - * @Param null - * @return - * @date 2022/5/22,0022 14:27 - * @author wxy - * - */ - public void initTDengine() { - try { - String dbName = dengineConfig.getDbName(); - int db = deviceLogMapper.createDB(dbName); - deviceLogMapper.createSTable(dbName); - - System.out.println(db); - } catch (Exception e) { - e.printStackTrace(); - System.out.println("ERROR"); - } - - } - -} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/ILogService.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/ILogService.java deleted file mode 100644 index 829c962a..00000000 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/ILogService.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.ruoyi.iot.log.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 selectDeviceLogList(DeviceLog deviceLog); - - List selectMonitorList(DeviceLog deviceLog); -} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/factory/LogServiceFactory.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/factory/LogServiceFactory.java deleted file mode 100644 index acd7be7f..00000000 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/factory/LogServiceFactory.java +++ /dev/null @@ -1,74 +0,0 @@ -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; - } - } -} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/MySqlLogServiceImpl.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/MySqlLogServiceImpl.java deleted file mode 100644 index 1ba1f610..00000000 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/MySqlLogServiceImpl.java +++ /dev/null @@ -1,38 +0,0 @@ -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 selectDeviceLogList(DeviceLog deviceLog) { - return deviceLogMapper.selectDeviceLogList(deviceLog); - } - - @Override - public List selectMonitorList(DeviceLog deviceLog) { - return deviceLogMapper.selectMonitorList(deviceLog); - } -} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/TdengineLogServiceImpl.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/TdengineLogServiceImpl.java deleted file mode 100644 index 58d381a5..00000000 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/log/service/impl/TdengineLogServiceImpl.java +++ /dev/null @@ -1,57 +0,0 @@ -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 selectDeviceLogList(DeviceLog deviceLog) { - return tdDeviceLogMapper.selectDeviceLogList(dbName,deviceLog); - } - - @Override - public List selectMonitorList(DeviceLog deviceLog) { - return tdDeviceLogMapper.selectMonitorList(dbName,deviceLog); - } -} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mqtt/EmqxService.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mqtt/EmqxService.java index bd875259..bca6e263 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mqtt/EmqxService.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/mqtt/EmqxService.java @@ -3,7 +3,6 @@ package com.ruoyi.iot.mqtt; import com.alibaba.fastjson.JSON; import com.ruoyi.iot.domain.Device; import com.ruoyi.iot.domain.DeviceLog; -import com.ruoyi.iot.log.service.ILogService; import com.ruoyi.iot.model.NtpModel; import com.ruoyi.iot.model.ThingsModels.IdentityAndName; import com.ruoyi.iot.model.ThingsModels.ThingsModelValueItem; @@ -11,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; diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/config/TDengineConfig.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/config/TDengineConfig.java index 9343ce19..c71a0812 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/config/TDengineConfig.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/config/TDengineConfig.java @@ -2,7 +2,6 @@ package com.ruoyi.iot.tdengine.config; import com.alibaba.druid.pool.DruidDataSource; import com.ruoyi.common.utils.StringUtils; -import com.ruoyi.iot.tdengine.mapper.TDDeviceLogMapper; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; @@ -31,13 +30,10 @@ import java.util.List; * 开发人: wxy */ @Configuration -@MapperScan(basePackages = {"com.ruoyi.iot.tdengine.mapper"}, sqlSessionTemplateRef = "tdengineSqlSessionTemplate") +@MapperScan(basePackages = {"com.ruoyi.iot.tdengine.dao"}, sqlSessionTemplateRef = "tdengineSqlSessionTemplate") @ConditionalOnProperty(name = "spring.datasource.druid.tdengine-server.enabled", havingValue = "true") public class TDengineConfig { - @Autowired - private TDDeviceLogMapper deviceLogMapper; - @Value("${spring.datasource.druid.tdengine-server.dbName}") private String dbName; @@ -64,22 +60,6 @@ public class TDengineConfig { return new SqlSessionTemplate(sqlSessionFactory); } - -// @Bean -// @ConditionalOnProperty(name = "spring.datasource.druid.tdengine-server.enabled", havingValue = "true") -// public TDDeviceLogMapper genTdengineLogMapper(@Qualifier("tDengineDataSource") TDDeviceLogMapper mapper) { -// if(this.deviceLogMapper==null){ -// this.deviceLogMapper=mapper; -// } -// return this.deviceLogMapper; -// } - - public TDDeviceLogMapper getTDengineLogMapper() { - return deviceLogMapper; - } - - - public String getDbName() { return dbName; } diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/DatabaseMapper.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/DatabaseDAO.java similarity index 91% rename from springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/DatabaseMapper.java rename to springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/DatabaseDAO.java index 5eb3b677..3044d692 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/DatabaseMapper.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/DatabaseDAO.java @@ -10,7 +10,7 @@ import org.springframework.stereotype.Repository; * 开发人: wxy */ @Repository -public interface DatabaseMapper { +public interface DatabaseDAO { int createDB(); diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/TDDeviceLogMapper.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/TDDeviceLogDAO.java similarity index 97% rename from springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/TDDeviceLogMapper.java rename to springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/TDDeviceLogDAO.java index d338823d..b4907756 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/TDDeviceLogMapper.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/dao/TDDeviceLogDAO.java @@ -16,7 +16,7 @@ import java.util.List; * 开发人: wxy */ @Repository -public interface TDDeviceLogMapper { +public interface TDDeviceLogDAO { int createDB( String database); diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/init/ApplicationStarted.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/init/ApplicationStarted.java index 5a2da7f4..44bc356f 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/init/ApplicationStarted.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/init/ApplicationStarted.java @@ -2,7 +2,7 @@ package com.ruoyi.iot.tdengine.init; import com.ruoyi.iot.tdengine.config.TDengineConfig; -import com.ruoyi.iot.tdengine.dao.TDDeviceLogMapper; +import com.ruoyi.iot.tdengine.dao.TDDeviceLogDAO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -31,8 +31,8 @@ public class ApplicationStarted implements ApplicationRunner { //先获取TDengine的配置,检测TDengine是否已经配置 if (containBean(TDengineConfig.class)) { TDengineConfig tDengineConfig = applicationContext.getBean(TDengineConfig.class); - TDDeviceLogMapper tDDeviceLogMapper = applicationContext.getBean(TDDeviceLogMapper.class); - initTDengine(tDengineConfig, tDDeviceLogMapper); + TDDeviceLogDAO tDDeviceLogDAO = applicationContext.getBean(TDDeviceLogDAO.class); + initTDengine(tDengineConfig, tDDeviceLogDAO); System.out.println("初始化TDengine成功"); }else{ System.out.println("MySQL初始化成功"); @@ -47,7 +47,7 @@ public class ApplicationStarted implements ApplicationRunner { * @date 2022/5/22,0022 14:27 * @author wxy */ - public void initTDengine(TDengineConfig dengineConfig, TDDeviceLogMapper deviceLogMapper) { + public void initTDengine(TDengineConfig dengineConfig, TDDeviceLogDAO deviceLogMapper) { try { String dbName = dengineConfig.getDbName(); int db = deviceLogMapper.createDB(dbName); diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/DatabaseMapper.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/DatabaseMapper.java deleted file mode 100644 index 076e66ac..00000000 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/DatabaseMapper.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.ruoyi.iot.tdengine.mapper; - -import org.springframework.stereotype.Repository; - -/** - * @package com.ruoyi.mysql.mysql.tdengine - * 类名: DatabaseMapper - * 描述: TODO - * 时间: 2022/5/16,0016 1:27 - * 开发人: wxy - */ -@Repository -public interface DatabaseMapper { - - int createDB(); - - int dropDatabase(); - - int useDatabase(); - - int createTable(); - - - -} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/TDDeviceLogMapper.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/TDDeviceLogMapper.java deleted file mode 100644 index 99aa0f6a..00000000 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/mapper/TDDeviceLogMapper.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.ruoyi.iot.tdengine.mapper; - -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.lang.management.MonitorInfo; -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 TDDeviceLogMapper { - - - 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 selectSTable(String database,DeviceLog deviceLog); - - int delete(String dbName, DeviceLog deviceLog); - -// List selectLogListByPage(String dbName, Integer pageSize, Integer pageNum, String deviceNum, Date beginDate, Date endDate); - - List selectLogList(String dbName, Long deviceId, String serialNumber, Long isMonitor, Long logType, Date beginDate, Date endDate); - - List selectMonitorList(@Param("database") String database, @Param("device") DeviceLog deviceLog); - - List selectDeviceLogList(@Param("database") String database,@Param("device") DeviceLog deviceLog); -} diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/factory/LogServiceFactory.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/factory/LogServiceFactory.java index 342e7445..49500315 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/factory/LogServiceFactory.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/factory/LogServiceFactory.java @@ -6,7 +6,7 @@ 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.TDDeviceLogMapper; +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; @@ -30,8 +30,8 @@ public class LogServiceFactory { //先获取TDengine的配置,检测TDengine是否已经配置 if (containBean(TDengineConfig.class)) { TDengineConfig tDengineConfig = applicationContext.getBean(TDengineConfig.class); - TDDeviceLogMapper tDDeviceLogMapper = applicationContext.getBean(TDDeviceLogMapper.class); - ILogService logService = new TdengineLogServiceImpl(tDengineConfig, tDDeviceLogMapper); + TDDeviceLogDAO tDDeviceLogDAO = applicationContext.getBean(TDDeviceLogDAO.class); + ILogService logService = new TdengineLogServiceImpl(tDengineConfig, tDDeviceLogDAO); return logService; } else if (containBean(MyBatisConfig.class)) { //没有配置TDengine,那么使用MySQL的日志配置 diff --git a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/impl/TdengineLogServiceImpl.java b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/impl/TdengineLogServiceImpl.java index 2b49b460..01322450 100644 --- a/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/impl/TdengineLogServiceImpl.java +++ b/springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/service/impl/TdengineLogServiceImpl.java @@ -4,7 +4,7 @@ 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.TDDeviceLogMapper; +import com.ruoyi.iot.tdengine.dao.TDDeviceLogDAO; import com.ruoyi.iot.util.SnowflakeIdWorker; import org.springframework.context.ApplicationContext; @@ -20,7 +20,7 @@ public class TdengineLogServiceImpl implements ILogService { private ApplicationContext applicationContext; - private TDDeviceLogMapper tdDeviceLogMapper; + private TDDeviceLogDAO tdDeviceLogDAO; private TDengineConfig tDengineConfig; @@ -28,8 +28,8 @@ public class TdengineLogServiceImpl implements ILogService { private String dbName; - public TdengineLogServiceImpl(TDengineConfig _tDengineConfig,TDDeviceLogMapper _tdDeviceLogMapper) { - this.tdDeviceLogMapper = _tdDeviceLogMapper; + public TdengineLogServiceImpl(TDengineConfig _tDengineConfig, TDDeviceLogDAO _tdDeviceLogDAO) { + this.tdDeviceLogDAO = _tdDeviceLogDAO; this.tDengineConfig = _tDengineConfig; snowflakeIdWorker=new SnowflakeIdWorker(1); this.dbName=_tDengineConfig.getDbName(); @@ -39,16 +39,16 @@ public class TdengineLogServiceImpl implements ILogService { public int saveDeviceLog(DeviceLog deviceLog) { long logId = snowflakeIdWorker.nextId(); deviceLog.setLogId(logId); - return tdDeviceLogMapper.save(dbName,deviceLog); + return tdDeviceLogDAO.save(dbName,deviceLog); } @Override public List selectDeviceLogList(DeviceLog deviceLog) { - return tdDeviceLogMapper.selectDeviceLogList(dbName,deviceLog); + return tdDeviceLogDAO.selectDeviceLogList(dbName,deviceLog); } @Override public List selectMonitorList(DeviceLog deviceLog) { - return tdDeviceLogMapper.selectMonitorList(dbName,deviceLog); + return tdDeviceLogDAO.selectMonitorList(dbName,deviceLog); } } diff --git a/springboot/wumei-iot/src/main/resources/mapper/tdengine/DatabaseMapper.xml b/springboot/wumei-iot/src/main/resources/mapper/tdengine/DatabaseMapper.xml index a2925db9..f5a6853a 100644 --- a/springboot/wumei-iot/src/main/resources/mapper/tdengine/DatabaseMapper.xml +++ b/springboot/wumei-iot/src/main/resources/mapper/tdengine/DatabaseMapper.xml @@ -2,7 +2,7 @@ - + @@ -19,24 +19,6 @@ use ${dbName}; - - - 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), - ); - - create stable if not exists ${tableName} (ts timestamp, diff --git a/springboot/wumei-iot/src/main/resources/mapper/tdengine/TDDeviceLogMapper.xml b/springboot/wumei-iot/src/main/resources/mapper/tdengine/TDDeviceLogMapper.xml index a17ef4d2..1bf0071b 100644 --- a/springboot/wumei-iot/src/main/resources/mapper/tdengine/TDDeviceLogMapper.xml +++ b/springboot/wumei-iot/src/main/resources/mapper/tdengine/TDDeviceLogMapper.xml @@ -2,7 +2,7 @@ - + -- Gitee