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); + } +}