Merge remote-tracking branch 'origin/master'

# Conflicts:
#	springboot/wumei-framework/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java
#	springboot/wumei-iot/src/main/java/com/ruoyi/iot/mqtt/EmqxService.java
#	springboot/wumei-iot/src/main/java/com/ruoyi/iot/tdengine/config/TDengineConfig.java
#	springboot/wumei-iot/src/main/resources/mapper/tdengine/DatabaseMapper.xml
#	springboot/wumei-iot/src/main/resources/mapper/tdengine/TDDeviceLogMapper.xml
This commit is contained in:
wxy001
2022-05-24 01:18:18 +08:00
32 changed files with 2110 additions and 1251 deletions

View File

@@ -1,21 +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;
@@ -34,8 +47,8 @@ import com.ruoyi.common.utils.StringUtils;
* @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;
@@ -119,10 +132,11 @@ public class MyBatisConfig
@Bean(name = "mysqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception
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);
@@ -146,4 +160,5 @@ public class MyBatisConfig
public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

View File

@@ -138,13 +138,13 @@ public class ToolController extends BaseController {
// 设备端
String[] clientInfo = clientid.split("&");
if (clientInfo.length != 2) {
// 设备未加密认证
// 设备简单认证
String deviceNum = clientInfo[0];
Device device = deviceService.selectShortDeviceBySerialNumber(deviceNum);
if (device !=null && mqttConfig.getusername().equals(username) && mqttConfig.getpassword().equals(password)) {
System.out.println("-----------认证成功,clientId:" + clientid + "---------------");
ProductAuthorize authorize = new ProductAuthorize(null, device.getProductId(), device.getDeviceId(), device.getSerialNumber(), 1L, "admin");
authorizeService.boundProductAuthorize(authorize);
// ProductAuthorize authorize = new ProductAuthorize(null, device.getProductId(), device.getDeviceId(), device.getSerialNumber(), 1L, "admin");
// authorizeService.boundProductAuthorize(authorize);
return ResponseEntity.ok().body("ok");
}
return returnUnauthorized(clientid, username, password, "认证信息有误");
@@ -178,16 +178,16 @@ public class ToolController extends BaseController {
// 设备状态验证 1-未激活2-禁用3-在线4-离线)
if (model.getDeviceId() != null && model.getDeviceId() != 0 && model.getStatus() != 2) {
System.out.println("-----------认证成功,clientId:" + clientid + "---------------");
ProductAuthorize authorize = new ProductAuthorize(null, model.getProductId(), model.getDeviceId(), model.getSerialNumber(), 1L, "admin");
authorizeService.boundProductAuthorize(authorize);
// ProductAuthorize authorize = new ProductAuthorize(null, model.getProductId(), model.getDeviceId(), model.getSerialNumber(), 1L, "admin");
// authorizeService.boundProductAuthorize(authorize);
return ResponseEntity.ok().body("ok");
} else {
// 自动添加设备
int result = deviceService.insertDeviceAuto(deviceNum, userId, productId);
if (result == 1) {
System.out.println("-----------认证成功,clientId:" + clientid + "---------------");
ProductAuthorize authorize = new ProductAuthorize(null, model.getProductId(), model.getDeviceId(), model.getSerialNumber(), 1L, "admin");
authorizeService.boundProductAuthorize(authorize);
// ProductAuthorize authorize = new ProductAuthorize(null, model.getProductId(), model.getDeviceId(), model.getSerialNumber(), 1L, "admin");
// authorizeService.boundProductAuthorize(authorize);
return ResponseEntity.ok().body("ok");
}
}

View File

@@ -0,0 +1,76 @@
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");
}
}
}

View File

@@ -0,0 +1,25 @@
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<DeviceLog> selectDeviceLogList(DeviceLog deviceLog);
List<MonitorModel> selectMonitorList(DeviceLog deviceLog);
}

View File

@@ -0,0 +1,74 @@
package com.ruoyi.iot.log.service.factory;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.framework.config.MyBatisConfig;
import com.ruoyi.iot.log.service.impl.MySqlLogServiceImpl;
import com.ruoyi.iot.log.service.impl.TdengineLogServiceImpl;
import com.ruoyi.iot.model.MonitorModel;
import com.ruoyi.iot.tdengine.config.TDengineConfig;
import com.ruoyi.iot.domain.DeviceLog;
import com.ruoyi.iot.log.service.ILogService;
import com.ruoyi.iot.mapper.DeviceLogMapper;
import com.ruoyi.iot.tdengine.mapper.TDDeviceLogMapper;
import com.ruoyi.iot.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.lang.Nullable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 类名: DeviceLogServiceImpl
* 描述: TODO
* 时间: 2022/5/19,0019 18:09
* 开发人: wxy
*/
@Component
public class LogServiceFactory {
@Autowired
private ApplicationContext applicationContext;
@Bean
public ILogService getLogService() {
//先获取TDengine的配置检测TDengine是否已经配置
if (containBean(TDengineConfig.class)) {
TDengineConfig tDengineConfig = applicationContext.getBean(TDengineConfig.class);
TDDeviceLogMapper tDDeviceLogMapper = applicationContext.getBean(TDDeviceLogMapper.class);
ILogService logService = new TdengineLogServiceImpl(tDengineConfig, tDDeviceLogMapper);
return logService;
} else if (containBean(MyBatisConfig.class)) {
//没有配置TDengine那么使用MySQL的日志配置
DeviceLogMapper deviceLogMapper = applicationContext.getBean( DeviceLogMapper.class);
ILogService logService = new MySqlLogServiceImpl(deviceLogMapper);
return logService;
} else {
return null;
}
}
/**
* @Method containBean
* @Description 根据类判断是否有对应bean
* @Param 类
* @return
* @date 2022/5/22,0022 14:12
* @author wxy
*
*/
private boolean containBean(@Nullable Class<?> T) {
String[] beans = applicationContext.getBeanNamesForType(T);
if (beans == null || beans.length == 0) {
return false;
} else {
return true;
}
}
}

View File

@@ -0,0 +1,38 @@
package com.ruoyi.iot.log.service.impl;
import com.ruoyi.iot.domain.DeviceLog;
import com.ruoyi.iot.log.service.ILogService;
import com.ruoyi.iot.mapper.DeviceLogMapper;
import com.ruoyi.iot.model.MonitorModel;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 类名: MySqlLogServiceImpl
* 描述: MySQL存储日志实现类
* 时间: 2022/5/22,0022 13:37
* 开发人: admin
*/
public class MySqlLogServiceImpl implements ILogService {
private DeviceLogMapper deviceLogMapper;
public MySqlLogServiceImpl(DeviceLogMapper _deviceLogMapper){
this.deviceLogMapper=_deviceLogMapper;
}
@Override
public int saveDeviceLog(DeviceLog deviceLog) {
return deviceLogMapper.insertDeviceLog(deviceLog);
}
@Override
public List<DeviceLog> selectDeviceLogList(DeviceLog deviceLog) {
return deviceLogMapper.selectDeviceLogList(deviceLog);
}
@Override
public List<MonitorModel> selectMonitorList(DeviceLog deviceLog) {
return deviceLogMapper.selectMonitorList(deviceLog);
}
}

View File

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

View File

@@ -79,7 +79,7 @@ public class EmqxClient {
token.waitForCompletion();
}catch (Exception e){
logger.error("=====>>>>>mqtt连接失败 message={}",e.getMessage());
throw new ServiceException("mqtt客户端连接错误"+e.getMessage());
// e.printStackTrace();
}
}
}
@@ -98,7 +98,7 @@ public class EmqxClient {
logger.debug("====>>>mqtt客户端启动成功");
}catch (MqttException e){
logger.error("mqtt客户端连接错误 error={}",e.getMessage());
throw new ServiceException("mqtt客户端连接错误"+e.getMessage());
// e.printStackTrace();
}
}
}

View File

@@ -3,13 +3,16 @@ 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.tdengine.service.ILogService;
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;
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;

View File

@@ -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.dao.TDDeviceLogMapper;
import com.ruoyi.iot.tdengine.mapper.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.dao"}, sqlSessionTemplateRef = "tdengineSqlSessionTemplate")
@MapperScan(basePackages = {"com.ruoyi.iot.tdengine.mapper"}, sqlSessionTemplateRef = "tdengineSqlSessionTemplate")
@ConditionalOnProperty(name = "spring.datasource.druid.tdengine-server.enabled", havingValue = "true")
public class TDengineConfig {
@@ -64,6 +64,16 @@ 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;
}

View File

@@ -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();
}

View File

@@ -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<DeviceLog> selectSTable(String database,DeviceLog deviceLog);
int delete(String dbName, DeviceLog deviceLog);
// List<DeviceLog> selectLogListByPage(String dbName, Integer pageSize, Integer pageNum, String deviceNum, Date beginDate, Date endDate);
List<DeviceLog> selectLogList(String dbName, Long deviceId, String serialNumber, Long isMonitor, Long logType, Date beginDate, Date endDate);
List<MonitorModel> selectMonitorList(@Param("database") String database, @Param("device") DeviceLog deviceLog);
List<DeviceLog> selectDeviceLogList(@Param("database") String database,@Param("device") DeviceLog deviceLog);
}

View File

@@ -2,7 +2,7 @@
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.iot.tdengine.dao.DatabaseMapper">
<mapper namespace="com.ruoyi.iot.tdengine.mapper.DatabaseMapper">
<!-- 创建数据库-->
<update id="createDB" parameterType="java.lang.String">

View File

@@ -2,7 +2,7 @@
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.iot.tdengine.dao.TDDeviceLogMapper">
<mapper namespace="com.ruoyi.iot.tdengine.mapper.TDDeviceLogMapper">
<resultMap id="BaseResultMap" type="com.ruoyi.iot.domain.DeviceLog">
<id column="ts" property="ts"/>