@ -120,24 +120,24 @@ import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignReques
import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest ;
import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest ;
/ * *
/ * *
*
*
* @author jwy - style
* @author jwy - style
*
*
* /
* /
@Service
@Service
public class CommonServiceImpl implements CommonService {
public class CommonServiceImpl implements CommonService {
private static Logger logger = LoggerFactory . getLogger ( CommonServiceImpl . class ) ;
private static Logger logger = LoggerFactory . getLogger ( CommonServiceImpl . class ) ;
@Value ( "${amazon.aws.accesskey}" )
@Value ( "${amazon.aws.accesskey}" )
private String awsAccessKeyId ;
private String awsAccessKeyId ;
@Value ( "${amazon.aws.secretkey}" )
@Value ( "${amazon.aws.secretkey}" )
private String awsAccessSecret ;
private String awsAccessSecret ;
@Value ( "${amazon.aws.bucket.asset:tokyobuild-stg-databucket-923770123186}" )
@Value ( "${amazon.aws.bucket.asset:tokyobuild-stg-databucket-923770123186}" )
private String awsBucketAdress ;
private String awsBucketAdress ;
@Value ( "${spring.redis.host}" )
@Value ( "${spring.redis.host}" )
private String redisHost ;
private String redisHost ;
@ -152,41 +152,41 @@ public class CommonServiceImpl implements CommonService {
@Value ( "${third.problem.reports.token}" )
@Value ( "${third.problem.reports.token}" )
private String thirdProblemReportsToken ;
private String thirdProblemReportsToken ;
@Resource
@Resource
public CacheManager cacheManager ;
public CacheManager cacheManager ;
@Autowired
@Autowired
private RedisUtil redisUtil ;
private RedisUtil redisUtil ;
@Autowired
@Autowired
private MsgLanguageChange msgLanguageChange ;
private MsgLanguageChange msgLanguageChange ;
@Autowired
@Autowired
private AthenaQuery athenaQuery ;
private AthenaQuery athenaQuery ;
@Value ( "${spring.datasource.url}" )
@Value ( "${spring.datasource.url}" )
private String dbUrl ;
private String dbUrl ;
@Value ( "${datacenter.v1.query.url}" )
@Value ( "${datacenter.v1.query.url}" )
private String datacenterV1QueryUrl ;
private String datacenterV1QueryUrl ;
@Resource ( name = "trustRestTemplate" )
@Resource ( name = "trustRestTemplate" )
private RestTemplate trustRestTemplate ;
private RestTemplate trustRestTemplate ;
private static final String CONTENT_START_END_REGEX = "\\((.*?)\\)" ;
private static final String CONTENT_START_END_REGEX = "\\((.*?)\\)" ;
private static final String CONTENT_DELIMITER_REGEX = "\\$\\['(.*)'\\]([<>=]{1,2})(-?\\d+(?:\\.\\d+)?) && \\$\\['\\1'\\]([<>=]{1,2})(-?\\d+(?:\\.\\d+)?)" ; ;
private static final String CONTENT_DELIMITER_REGEX = "\\$\\['(.*)'\\]([<>=]{1,2})(-?\\d+(?:\\.\\d+)?) && \\$\\['\\1'\\]([<>=]{1,2})(-?\\d+(?:\\.\\d+)?)" ; ;
@Autowired
@Autowired
private DynamicRouteDataSource dynamicDataSource ;
private DynamicRouteDataSource dynamicDataSource ;
@Autowired
@Autowired
private DataSourceAdminConfig dataSourceAdminConfig ;
private DataSourceAdminConfig dataSourceAdminConfig ;
@Autowired
@Autowired
private CommonOpt commonOpt ;
private CommonOpt commonOpt ;
@Autowired
@Autowired
private CompanyMapperExt companyMapperExt ;
private CompanyMapperExt companyMapperExt ;
@Autowired
@Autowired
@ -217,8 +217,8 @@ public class CommonServiceImpl implements CommonService {
private BasicBuildingMapperExt basicBuildingMapperExt ;
private BasicBuildingMapperExt basicBuildingMapperExt ;
@Autowired
@Autowired
private BasicFloorMapperExt basicFloorMapperExt ;
private BasicFloorMapperExt basicFloorMapperExt ;
@Override
@Override
@ -242,7 +242,7 @@ public class CommonServiceImpl implements CommonService {
paramMap . put ( "apikey" , apikey ) ;
paramMap . put ( "apikey" , apikey ) ;
ApikeyInfo2 result = basicCompanyMapperExt . getAuroraInfoByApikey ( paramMap ) ;
ApikeyInfo2 result = basicCompanyMapperExt . getAuroraInfoByApikey ( paramMap ) ;
resp . setCode ( 200 ) ;
resp . setCode ( 200 ) ;
if ( result ! = null ) {
if ( result ! = null ) {
resp . setData ( true ) ;
resp . setData ( true ) ;
} else {
} else {
@ -269,16 +269,16 @@ public class CommonServiceImpl implements CommonService {
try {
try {
HttpHeaders httpHeaders = new HttpHeaders ( ) ;
HttpHeaders httpHeaders = new HttpHeaders ( ) ;
httpHeaders . add ( "Apikey" , datacenterV1QueryParams . getApikey ( ) ) ;
httpHeaders . add ( "Apikey" , datacenterV1QueryParams . getApikey ( ) ) ;
Map < String , Object > params = new HashMap < > ( ) ;
Map < String , Object > params = new HashMap < > ( ) ;
params . put ( "startTime" , datacenterV1QueryParams . getStartTime ( ) ) ;
params . put ( "startTime" , datacenterV1QueryParams . getStartTime ( ) ) ;
params . put ( "endTime" , datacenterV1QueryParams . getEndTime ( ) ) ;
params . put ( "endTime" , datacenterV1QueryParams . getEndTime ( ) ) ;
params . put ( "deviceId" , datacenterV1QueryParams . getDeviceId ( ) ) ;
params . put ( "deviceId" , datacenterV1QueryParams . getDeviceId ( ) ) ;
params . put ( "hashId" , datacenterV1QueryParams . getHashId ( ) ) ;
params . put ( "hashId" , datacenterV1QueryParams . getHashId ( ) ) ;
params . put ( "date" , datacenterV1QueryParams . getDate ( ) ) ;
params . put ( "date" , datacenterV1QueryParams . getDate ( ) ) ;
ResponseEntity < String > responseEntity = doDatacenterV1QueryRequest ( params , httpHeaders , 3 ) ;
ResponseEntity < String > responseEntity = doDatacenterV1QueryRequest ( params , httpHeaders , 3 ) ;
ObjectMapper objectMapper = new ObjectMapper ( ) ;
ObjectMapper objectMapper = new ObjectMapper ( ) ;
simpleDataResponse = objectMapper . readValue ( responseEntity . getBody ( ) , SimpleDataResponse . class ) ;
simpleDataResponse = objectMapper . readValue ( responseEntity . getBody ( ) , SimpleDataResponse . class ) ;
} catch ( Exception e ) {
} catch ( Exception e ) {
@ -286,7 +286,7 @@ public class CommonServiceImpl implements CommonService {
simpleDataResponse . setCode ( ResponseCode . SERVER_ERROR ) ;
simpleDataResponse . setCode ( ResponseCode . SERVER_ERROR ) ;
simpleDataResponse . setMsg ( ResponseCode . SERVER_ERROR_MSG ) ;
simpleDataResponse . setMsg ( ResponseCode . SERVER_ERROR_MSG ) ;
}
}
return simpleDataResponse ;
return simpleDataResponse ;
}
}
@ -327,7 +327,7 @@ public class CommonServiceImpl implements CommonService {
@Override
@Override
public SimpleDataResponse logEmailSentResult ( JSONObject jsonObj ) {
public SimpleDataResponse logEmailSentResult ( JSONObject jsonObj ) {
try {
try {
//get large company id
//get large company id
Map < String , Object > paramMap = new HashMap < > ( ) ;
Map < String , Object > paramMap = new HashMap < > ( ) ;
paramMap . put ( "companyId" , jsonObj . getString ( "companyId" ) ) ;
paramMap . put ( "companyId" , jsonObj . getString ( "companyId" ) ) ;
@ -336,10 +336,10 @@ public class CommonServiceImpl implements CommonService {
logger . error ( "logEmailSentResult error: company not found" ) ;
logger . error ( "logEmailSentResult error: company not found" ) ;
return SimpleDataResponse . fail ( ResponseCode . MSG_ERROR , "company not found" ) ;
return SimpleDataResponse . fail ( ResponseCode . MSG_ERROR , "company not found" ) ;
}
}
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
MailSentResult mailSentResult = new MailSentResult ( ) ;
MailSentResult mailSentResult = new MailSentResult ( ) ;
mailSentResult . setDeviceId ( jsonObj . getString ( "deviceId" ) ) ;
mailSentResult . setDeviceId ( jsonObj . getString ( "deviceId" ) ) ;
mailSentResult . setErrorMsg ( jsonObj . getString ( "errorMsg" ) ) ;
mailSentResult . setErrorMsg ( jsonObj . getString ( "errorMsg" ) ) ;
@ -352,9 +352,9 @@ public class CommonServiceImpl implements CommonService {
mailSentResult . setDeviceName ( jsonObj . getString ( "deviceName" ) ) ;
mailSentResult . setDeviceName ( jsonObj . getString ( "deviceName" ) ) ;
mailSentResult . setDeviceSn ( jsonObj . getString ( "deviceSn" ) ) ;
mailSentResult . setDeviceSn ( jsonObj . getString ( "deviceSn" ) ) ;
mailSentResult . setAlertType ( jsonObj . getInteger ( "alertType" ) ) ;
mailSentResult . setAlertType ( jsonObj . getInteger ( "alertType" ) ) ;
mailSentResultMapperExt . insertSelective ( mailSentResult ) ;
mailSentResultMapperExt . insertSelective ( mailSentResult ) ;
return SimpleDataResponse . success ( ) ;
return SimpleDataResponse . success ( ) ;
} catch ( Exception e ) {
} catch ( Exception e ) {
SimpleDataResponse resp = new SimpleDataResponse ( ) ;
SimpleDataResponse resp = new SimpleDataResponse ( ) ;
@ -385,17 +385,17 @@ public class CommonServiceImpl implements CommonService {
if ( null = = apikeyInfo ) {
if ( null = = apikeyInfo ) {
return SimpleDataResponse . fail ( ResponseCode . MSG_ERROR , "Apikey is invalid" ) ;
return SimpleDataResponse . fail ( ResponseCode . MSG_ERROR , "Apikey is invalid" ) ;
}
}
if ( StringUtils . isBlank ( pageSearchParam . getSymbol ( ) ) | |
if ( StringUtils . isBlank ( pageSearchParam . getSymbol ( ) ) | |
( StringUtils . isBlank ( pageSearchParam . getBuildingName ( ) ) & & StringUtils . isBlank ( pageSearchParam . getUdfBuildingId ( ) ) ) ) {
( StringUtils . isBlank ( pageSearchParam . getBuildingName ( ) ) & & StringUtils . isBlank ( pageSearchParam . getUdfBuildingId ( ) ) ) ) {
return SimpleDataResponse . fail ( ResponseCode . MSG_ERROR , "symbol or buildingName/udfBuildingId is required" ) ;
return SimpleDataResponse . fail ( ResponseCode . MSG_ERROR , "symbol or buildingName/udfBuildingId is required" ) ;
}
}
ApikeyInfo2 companyInfo = basicCompanyMapperExt . getAuroraInfoByApikey ( paramMap ) ;
ApikeyInfo2 companyInfo = basicCompanyMapperExt . getAuroraInfoByApikey ( paramMap ) ;
long companyId = companyInfo . getId ( ) ;
long companyId = companyInfo . getId ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
if ( StringUtils . isBlank ( pageSearchParam . getCompanyIds ( ) ) ) {
if ( StringUtils . isBlank ( pageSearchParam . getCompanyIds ( ) ) ) {
pageSearchParam . setCompanyIdList ( Arrays . asList ( companyId ) ) ;
pageSearchParam . setCompanyIdList ( Arrays . asList ( companyId ) ) ;
} else {
} else {
@ -421,55 +421,29 @@ public class CommonServiceImpl implements CommonService {
List < ApiDeviceInfoVO > deviceInfos = deviceInfoMapperExt . getInfo4QueryAssetInfo ( DeviceParamMap ) ;
List < ApiDeviceInfoVO > deviceInfos = deviceInfoMapperExt . getInfo4QueryAssetInfo ( DeviceParamMap ) ;
// Query latest data and time if Aurora URL exists and devices are found
// Query latest data and time if Aurora URL exists and devices are found
if ( StringUtils . isNotBlank ( apikeyInfo . getAuroraUrl ( ) ) & & CollectionUtils . isNotEmpty ( deviceInfos ) ) {
for ( ApiDeviceInfoVO apiDeviceInfoVO : deviceInfos ) {
String deviceId = apiDeviceInfoVO . getDeviceId ( ) ;
Class . forName ( "com.mysql.cj.jdbc.Driver" ) ;
// 计算分片
int partitionIndex = Math . abs ( deviceId . hashCode ( ) ) % Constants . REDIS_PARTITION_NUM ;
String regex = "(jdbc:mysql://)([^/]+)(/data_center_aeon_admin.*)" ;
String redisKey = Constants . REDIS_RAW_DATA_REALTIME + partitionIndex ;
Pattern pattern = Pattern . compile ( regex ) ;
Matcher matcher = pattern . matcher ( dbUrl ) ;
// 从 Redis Hash 获取 JSON
String newJdbcUrl = "" ;
Object redisData = redisUtil . HGet ( redisKey , deviceId ) ;
if ( matcher . find ( ) ) {
if ( redisData ! = null ) {
newJdbcUrl = matcher . replaceAll ( "$1" + apikeyInfo . getAuroraUrl ( ) + "$3" ) ;
JSONObject jsonObject = JSON . parseObject ( JSON . toJSONString ( redisData ) , Feature . OrderedField ) ;
}
Long receiveTs = jsonObject . getLong ( "receive_ts" ) ;
String rawData = jsonObject . getString ( "rawData" ) ;
try ( Connection conn = DriverManager . getConnection (
newJdbcUrl . replace ( "data_center_aeon_admin" , "aeon" ) + "&allowPublicKeyRetrieval=true" ,
apiDeviceInfoVO . setLatestRawData ( rawData ) ;
DESUtil . decrypt ( apikeyInfo . getAuroraUsername ( ) , Constants . DES_SALT ) ,
apiDeviceInfoVO . setLatestDataTime ( receiveTs ) ;
DESUtil . decrypt ( apikeyInfo . getAuroraPassword ( ) , Constants . DES_SALT ) ) ) {
// 遍历所有键值对
for ( ApiDeviceInfoVO apiDeviceInfoVO : deviceInfos ) {
JSONObject rawDataObj = JSON . parseObject ( rawData , Feature . OrderedField ) ;
String sql = "select rawData, receive_ts from rawData_realtime where deviceId = ? limit 1" ;
List < Object > valueList = new ArrayList < > ( ) ;
logger . info ( "queryAssetInfo aurora sql:" + sql ) ;
for ( String key : rawDataObj . keySet ( ) ) {
valueList . add ( rawDataObj . get ( key ) ) ;
try ( PreparedStatement preparedStatement = conn . prepareStatement ( sql ) ) {
preparedStatement . setString ( 1 , apiDeviceInfoVO . getDeviceId ( ) ) ;
ResultSet retult = preparedStatement . executeQuery ( ) ;
while ( retult . next ( ) ) {
String rawData = retult . getString ( "rawData" ) ;
Long receiveTs = retult . getLong ( "receive_ts" ) ;
apiDeviceInfoVO . setLatestDataTime ( receiveTs ) ;
apiDeviceInfoVO . setLatestRawData ( rawData ) ;
JSONObject jsonObject = JSON . parseObject ( rawData , Feature . OrderedField ) ;
// 遍历所有键值对
List < Object > valueList = new ArrayList < > ( ) ;
for ( String key : jsonObject . keySet ( ) ) {
valueList . add ( jsonObject . get ( key ) ) ;
}
apiDeviceInfoVO . setDataValue ( StringUtils . join ( valueList , "," ) ) ;
}
preparedStatement . close ( ) ;
}
}
}
apiDeviceInfoVO . setDataValue ( StringUtils . join ( valueList , "," ) ) ;
conn . close ( ) ;
} catch ( Exception e ) {
logger . error ( "queryAssetInfo aurora query error" , e ) ;
return SimpleDataResponse . fail ( ResponseCode . SERVER_ERROR , ResponseCode . SERVER_ERROR_MSG ) ;
}
}
}
}
@ -504,10 +478,10 @@ public class CommonServiceImpl implements CommonService {
ApikeyInfo2 companyInfo = basicCompanyMapperExt . getAuroraInfoByApikey ( paramMap ) ;
ApikeyInfo2 companyInfo = basicCompanyMapperExt . getAuroraInfoByApikey ( paramMap ) ;
long companyId = companyInfo . getId ( ) ;
long companyId = companyInfo . getId ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
if ( StringUtils . isBlank ( pageSearchParam . getCompanyIds ( ) ) ) {
if ( StringUtils . isBlank ( pageSearchParam . getCompanyIds ( ) ) ) {
pageSearchParam . setCompanyIdList ( Arrays . asList ( companyId ) ) ;
pageSearchParam . setCompanyIdList ( Arrays . asList ( companyId ) ) ;
} else {
} else {
@ -584,22 +558,22 @@ public class CommonServiceImpl implements CommonService {
responseSetThresholdV1 . setMessage ( "Invalid apikey" ) ;
responseSetThresholdV1 . setMessage ( "Invalid apikey" ) ;
return responseSetThresholdV1 ;
return responseSetThresholdV1 ;
}
}
ApikeyInfo2 companyInfo = basicCompanyMapperExt . getAuroraInfoByApikey ( companyParamMap ) ;
ApikeyInfo2 companyInfo = basicCompanyMapperExt . getAuroraInfoByApikey ( companyParamMap ) ;
long companyId = companyInfo . getId ( ) ;
long companyId = companyInfo . getId ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
// Convert the set back to a string with commas
// Convert the set back to a string with commas
String finalTargetIds = String . join ( "," , uniqueTargetId ) ;
String finalTargetIds = String . join ( "," , uniqueTargetId ) ;
Map < String , Object > idParamMap = new HashMap < > ( ) ;
Map < String , Object > idParamMap = new HashMap < > ( ) ;
idParamMap . put ( "companyId" , companyId ) ;
idParamMap . put ( "companyId" , companyId ) ;
idParamMap . put ( "targetIds" , finalTargetIds ) ;
idParamMap . put ( "targetIds" , finalTargetIds ) ;
Set < String > dbTargetIdSet = doCheckTargetIds ( idParamMap , uniqueTargetId ) ;
Set < String > dbTargetIdSet = doCheckTargetIds ( idParamMap , uniqueTargetId ) ;
if ( ! dbTargetIdSet . equals ( uniqueTargetId ) ) {
if ( ! dbTargetIdSet . equals ( uniqueTargetId ) ) {
Set < String > difference = new HashSet < > ( uniqueTargetId ) ;
Set < String > difference = new HashSet < > ( uniqueTargetId ) ;
difference . removeAll ( dbTargetIdSet ) ;
difference . removeAll ( dbTargetIdSet ) ;
@ -738,7 +712,7 @@ public class CommonServiceImpl implements CommonService {
if ( null = = apikeyInfo ) {
if ( null = = apikeyInfo ) {
responseQueryStatusV1 . setMessage ( "Invalid apikey" ) ;
responseQueryStatusV1 . setMessage ( "Invalid apikey" ) ;
return responseQueryStatusV1 ;
return responseQueryStatusV1 ;
}
}
// else {
// else {
// companyId = companyInfo.getId();
// companyId = companyInfo.getId();
// }
// }
@ -856,10 +830,10 @@ public class CommonServiceImpl implements CommonService {
ApikeyInfo2 companyInfo = basicCompanyMapperExt . getAuroraInfoByApikey ( paramMap ) ;
ApikeyInfo2 companyInfo = basicCompanyMapperExt . getAuroraInfoByApikey ( paramMap ) ;
long companyId = companyInfo . getId ( ) ;
long companyId = companyInfo . getId ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
if ( StringUtils . isBlank ( apiAssetSearchByClassParams . getCompanyIds ( ) ) ) {
if ( StringUtils . isBlank ( apiAssetSearchByClassParams . getCompanyIds ( ) ) ) {
apiAssetSearchByClassParams . setCompanyIdList ( Arrays . asList ( companyId ) ) ;
apiAssetSearchByClassParams . setCompanyIdList ( Arrays . asList ( companyId ) ) ;
} else {
} else {
@ -948,7 +922,7 @@ public class CommonServiceImpl implements CommonService {
paramMap2 . put ( "assetSymbol" , apiAlarmDeviceSearchParams . getAssetSymbol ( ) ) ;
paramMap2 . put ( "assetSymbol" , apiAlarmDeviceSearchParams . getAssetSymbol ( ) ) ;
paramMap2 . put ( "udfBuildingId" , apiAlarmDeviceSearchParams . getUdfBuildingId ( ) ) ;
paramMap2 . put ( "udfBuildingId" , apiAlarmDeviceSearchParams . getUdfBuildingId ( ) ) ;
paramMap2 . put ( "companyId" , companyId ) ;
paramMap2 . put ( "companyId" , companyId ) ;
deviceInfoVOs = deviceInfoMapperExt . getAlarmDeviceInfo ( paramMap2 ) ;
deviceInfoVOs = deviceInfoMapperExt . getAlarmDeviceInfo ( paramMap2 ) ;
@ -969,7 +943,7 @@ public class CommonServiceImpl implements CommonService {
DESUtil . decrypt ( apikeyInfo . getAuroraUsername ( ) , Constants . DES_SALT ) ,
DESUtil . decrypt ( apikeyInfo . getAuroraUsername ( ) , Constants . DES_SALT ) ,
DESUtil . decrypt ( apikeyInfo . getAuroraPassword ( ) , Constants . DES_SALT ) ) ) {
DESUtil . decrypt ( apikeyInfo . getAuroraPassword ( ) , Constants . DES_SALT ) ) ) {
for ( ApiAlarmDeviceInfoVO apiAlarmDeviceInfoVO : deviceInfoVOs ) {
for ( ApiAlarmDeviceInfoVO apiAlarmDeviceInfoVO : deviceInfoVOs ) {
// String sql = " select rawData, receive_ts, alertTitle, alertLevel,alertLevelName,alertTypeName from "+formatRawDataWithDate()+" where deviceId = '" + apiAlarmDeviceInfoVO.getDeviceId() + "' order by receive_ts desc limit 1" ;
// String sql = " select rawData, receive_ts, alertTitle, alertLevel,alertLevelName,alertTypeName from "+formatRawDataWithDate()+" where deviceId = '" + apiAlarmDeviceInfoVO.getDeviceId() + "' order by receive_ts desc limit 1" ;
// String sql = " select rawData, receive_ts, alertTitle, alertLevel, alertTypeName from "+formatRawDataWithDate()+" where deviceId = '" + apiAlarmDeviceInfoVO.getDeviceId() + "' order by receive_ts desc limit 1" ;
// String sql = " select rawData, receive_ts, alertTitle, alertLevel, alertTypeName from "+formatRawDataWithDate()+" where deviceId = '" + apiAlarmDeviceInfoVO.getDeviceId() + "' order by receive_ts desc limit 1" ;
String sql = "select rawData, receive_ts, alertTitle, alertLevel, alertLevelName, alertTypeName from alertData where deviceId = ? order by receive_ts desc limit 1" ;
String sql = "select rawData, receive_ts, alertTitle, alertLevel, alertLevelName, alertTypeName from alertData where deviceId = ? order by receive_ts desc limit 1" ;
@ -979,7 +953,7 @@ public class CommonServiceImpl implements CommonService {
preparedStatement . setString ( 1 , apiAlarmDeviceInfoVO . getDeviceId ( ) ) ;
preparedStatement . setString ( 1 , apiAlarmDeviceInfoVO . getDeviceId ( ) ) ;
ResultSet retult = preparedStatement . executeQuery ( ) ;
ResultSet retult = preparedStatement . executeQuery ( ) ;
while ( retult . next ( ) ) {
while ( retult . next ( ) ) {
String rawData = retult . getString ( "rawData" ) ;
String rawData = retult . getString ( "rawData" ) ;
Long receiveTs = retult . getLong ( "receive_ts" ) ;
Long receiveTs = retult . getLong ( "receive_ts" ) ;
@ -1002,7 +976,7 @@ public class CommonServiceImpl implements CommonService {
}
}
apiAlarmDeviceInfoVO . setDataValue ( StringUtils . join ( valueList , "," ) ) ;
apiAlarmDeviceInfoVO . setDataValue ( StringUtils . join ( valueList , "," ) ) ;
}
}
preparedStatement . close ( ) ;
preparedStatement . close ( ) ;
}
}
}
}
@ -1047,8 +1021,8 @@ public class CommonServiceImpl implements CommonService {
// return "rawData_" + formattedDate;
// return "rawData_" + formattedDate;
// }
// }
// }
// }
// 定义方法改为接收参数的日期表名
// 定义方法改为接收参数的日期表名
private String formatRawDataWithDate ( LocalDate date , Long deviceCategoryId ) {
private String formatRawDataWithDate ( LocalDate date , Long deviceCategoryId ) {
DateTimeFormatter formatter = DateTimeFormatter . ofPattern ( "yyyy_MM_dd" ) ;
DateTimeFormatter formatter = DateTimeFormatter . ofPattern ( "yyyy_MM_dd" ) ;
@ -1090,23 +1064,23 @@ public class CommonServiceImpl implements CommonService {
Map < String , Object > paramMap = new HashMap < > ( ) ;
Map < String , Object > paramMap = new HashMap < > ( ) ;
paramMap . put ( "apikey" , apikey ) ;
paramMap . put ( "apikey" , apikey ) ;
ApikeyInfo2 apikeyInfo = commonOpt . getAuroraInfoByApikey ( paramMap ) ;
ApikeyInfo2 apikeyInfo = commonOpt . getAuroraInfoByApikey ( paramMap ) ;
if ( null = = apikeyInfo ) {
if ( null = = apikeyInfo ) {
return SimpleDataResponse . fail ( ResponseCode . MSG_ERROR , "Apikey is invalid" ) ;
return SimpleDataResponse . fail ( ResponseCode . MSG_ERROR , "Apikey is invalid" ) ;
}
}
ApikeyInfo2 companyInfo = basicCompanyMapperExt . getAuroraInfoByApikey ( paramMap ) ;
ApikeyInfo2 companyInfo = basicCompanyMapperExt . getAuroraInfoByApikey ( paramMap ) ;
long companyId = companyInfo . getId ( ) ;
long companyId = companyInfo . getId ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
if ( StringUtils . isBlank ( apiAssetClassSearchParams . getCompanyIds ( ) ) ) {
if ( StringUtils . isBlank ( apiAssetClassSearchParams . getCompanyIds ( ) ) ) {
apiAssetClassSearchParams . setCompanyIdList ( Arrays . asList ( companyId ) ) ;
apiAssetClassSearchParams . setCompanyIdList ( Arrays . asList ( companyId ) ) ;
}
}
List < ApiAssetClassInfoVO > resultList = basicMonitoringAssetMapperExt . getClassInfo ( apiAssetClassSearchParams ) ;
List < ApiAssetClassInfoVO > resultList = basicMonitoringAssetMapperExt . getClassInfo ( apiAssetClassSearchParams ) ;
Map < String , List < ApiAssetClassInfoVO > > groupedByBuilding = resultList . stream ( ) . collect ( Collectors . groupingBy ( ApiAssetClassInfoVO : : getBuildingName ) ) ;
Map < String , List < ApiAssetClassInfoVO > > groupedByBuilding = resultList . stream ( ) . collect ( Collectors . groupingBy ( ApiAssetClassInfoVO : : getBuildingName ) ) ;
List < Building > buildings = new ArrayList < > ( ) ;
List < Building > buildings = new ArrayList < > ( ) ;
@ -1143,7 +1117,7 @@ public class CommonServiceImpl implements CommonService {
buildings . add ( new Building ( buildingName , classBigs ) ) ;
buildings . add ( new Building ( buildingName , classBigs ) ) ;
}
}
return SimpleDataResponse . success ( buildings ) ;
return SimpleDataResponse . success ( buildings ) ;
} catch ( Exception e ) {
} catch ( Exception e ) {
@ -1175,10 +1149,10 @@ public class CommonServiceImpl implements CommonService {
if ( StringUtils . isBlank ( params . getAssetSymbol ( ) ) ) {
if ( StringUtils . isBlank ( params . getAssetSymbol ( ) ) ) {
return SimpleDataResponse . fail ( ResponseCode . MSG_ERROR , "assetSymbol is required" ) ;
return SimpleDataResponse . fail ( ResponseCode . MSG_ERROR , "assetSymbol is required" ) ;
}
}
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
//资产关联的设备的targetId获取
//资产关联的设备的targetId获取
List < TargetIdOfAssetVO > targetIdOfAssetList = basicMonitoringAssetMapperExt . getTargetIdListByAssetName ( params ) ;
List < TargetIdOfAssetVO > targetIdOfAssetList = basicMonitoringAssetMapperExt . getTargetIdListByAssetName ( params ) ;
if ( targetIdOfAssetList . size ( ) = = 0 ) {
if ( targetIdOfAssetList . size ( ) = = 0 ) {
@ -1360,7 +1334,7 @@ public class CommonServiceImpl implements CommonService {
if ( null = = companyInfo ) {
if ( null = = companyInfo ) {
responseSetThresholdV1 . setMessage ( "Invalid apikey" ) ;
responseSetThresholdV1 . setMessage ( "Invalid apikey" ) ;
return responseSetThresholdV1 ;
return responseSetThresholdV1 ;
}
}
// else {
// else {
// dbCompanyId = companyInfo.getId()+"";
// dbCompanyId = companyInfo.getId()+"";
// }
// }
@ -1377,12 +1351,12 @@ public class CommonServiceImpl implements CommonService {
//
//
ApikeyInfo2 apikeyInfo2 = basicCompanyMapperExt . getAuroraInfoByApikey ( companyParamMap ) ;
ApikeyInfo2 apikeyInfo2 = basicCompanyMapperExt . getAuroraInfoByApikey ( companyParamMap ) ;
paramMap . put ( "companyId" , apikeyInfo2 . getId ( ) ) ;
paramMap . put ( "companyId" , apikeyInfo2 . getId ( ) ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + companyInfo . getId ( ) ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + companyInfo . getId ( ) ) ;
Set < String > dbTargetIdSet = doCheckTargetIds ( paramMap , uniqueTargetId ) ;
Set < String > dbTargetIdSet = doCheckTargetIds ( paramMap , uniqueTargetId ) ;
if ( dbTargetIdSet . equals ( uniqueTargetId ) ) {
if ( dbTargetIdSet . equals ( uniqueTargetId ) ) {
responseSetThresholdV1 . setCode ( ResponseCode . SUCCESS ) ;
responseSetThresholdV1 . setCode ( ResponseCode . SUCCESS ) ;
responseSetThresholdV1 . setMessage ( "success" ) ;
responseSetThresholdV1 . setMessage ( "success" ) ;
@ -1406,7 +1380,7 @@ public class CommonServiceImpl implements CommonService {
List < String > dbIdList = deviceAlertConfigMapperExt . checkTargetIds ( paramMap ) ;
List < String > dbIdList = deviceAlertConfigMapperExt . checkTargetIds ( paramMap ) ;
Set < String > dbTargetIdSet = new HashSet < > ( ) ;
Set < String > dbTargetIdSet = new HashSet < > ( ) ;
dbTargetIdSet . addAll ( dbIdList ) ;
dbTargetIdSet . addAll ( dbIdList ) ;
return dbTargetIdSet ;
return dbTargetIdSet ;
}
}
@ -1479,7 +1453,7 @@ public class CommonServiceImpl implements CommonService {
paramMap . put ( "deviceIdList" , deviceIdList ) ;
paramMap . put ( "deviceIdList" , deviceIdList ) ;
paramMap . put ( "companyId" , selfCompanyInfo . getId ( ) ) ;
paramMap . put ( "companyId" , selfCompanyInfo . getId ( ) ) ;
dpfDeviceInfoVOs = deviceInfoMapperExt . getRiliDeviceInfo ( paramMap ) ;
dpfDeviceInfoVOs = deviceInfoMapperExt . getRiliDeviceInfo ( paramMap ) ;
// Retrieve the set of DeviceId from DeviceInfoVOs
// Retrieve the set of DeviceId from DeviceInfoVOs
Set < String > deviceIdsInVOs = Optional . ofNullable ( dpfDeviceInfoVOs )
Set < String > deviceIdsInVOs = Optional . ofNullable ( dpfDeviceInfoVOs )
. orElse ( Collections . emptyList ( ) )
. orElse ( Collections . emptyList ( ) )
@ -1495,46 +1469,31 @@ public class CommonServiceImpl implements CommonService {
}
}
//Query latest data from Aurora if configured and devices are found
//Query latest data from Aurora if configured and devices are found
if ( StringUtils . isNotBlank ( apikeyInfo . getAuroraUrl ( ) ) & & CollectionUtils . isNotEmpty ( dpfDeviceInfoVOs ) ) {
if ( CollectionUtils . isNotEmpty ( dpfDeviceInfoVOs ) ) {
Class . forName ( "com.mysql.cj.jdbc.Driver" ) ;
for ( ApiRiliDeviceInfoVO apiRiliDeviceInfoVO : dpfDeviceInfoVOs ) {
String deviceId = apiRiliDeviceInfoVO . getDeviceId ( ) ;
String regex = "(jdbc:mysql://)([^/]+)(/data_center_aeon_admin.*)" ;
// 计算分片
Pattern pattern = Pattern . compile ( regex ) ;
int partitionIndex = Math . abs ( deviceId . hashCode ( ) ) % Constants . REDIS_PARTITION_NUM ;
Matcher matcher = pattern . matcher ( dbUrl ) ;
String redisKey = Constants . REDIS_RAW_DATA_REALTIME + partitionIndex ;
String newJdbcUrl = "" ;
if ( matcher . find ( ) ) {
// 从 Redis Hash 获取 JSON
newJdbcUrl = matcher . replaceAll ( "$1" + apikeyInfo . getAuroraUrl ( ) + "$3" ) ;
Object redisData = redisUtil . HGet ( redisKey , deviceId ) ;
}
if ( redisData ! = null ) {
JSONObject jsonObject = JSON . parseObject ( JSON . toJSONString ( redisData ) , Feature . OrderedField ) ;
try ( Connection conn = DriverManager . getConnection (
Long receiveTs = jsonObject . getLong ( "receive_ts" ) ;
newJdbcUrl . replace ( "data_center_aeon_admin" , "aeon" ) + "&allowPublicKeyRetrieval=true" ,
String rawData = jsonObject . getString ( "rawData" ) ;
DESUtil . decrypt ( apikeyInfo . getAuroraUsername ( ) , Constants . DES_SALT ) ,
DESUtil . decrypt ( apikeyInfo . getAuroraPassword ( ) , Constants . DES_SALT ) ) ) {
apiRiliDeviceInfoVO . setLatestRawData ( rawData ) ;
apiRiliDeviceInfoVO . setLatestDataTime ( receiveTs ) ;
for ( ApiRiliDeviceInfoVO apiRiliDeviceInfoVO : dpfDeviceInfoVOs ) {
// 遍历所有键值对
Map < String , Object > categoryIdSearchMap = new HashMap < > ( ) ;
JSONObject rawDataObj = JSON . parseObject ( rawData , Feature . OrderedField ) ;
categoryIdSearchMap . put ( "deviceId" , apiRiliDeviceInfoVO . getDeviceId ( ) ) ;
List < Object > valueList = new ArrayList < > ( ) ;
Long deviceCategoryId = deviceInfoMapperExt . getDeviceCategoryId ( categoryIdSearchMap ) ;
for ( String key : rawDataObj . keySet ( ) ) {
valueList . add ( rawDataObj . get ( key ) ) ;
String baseSql = "SELECT rawData, receive_ts FROM rawData_realtime WHERE deviceId = ? LIMIT 1" ;
}
// String sql = String.format(baseSql, formatRawDataWithDate(LocalDate.now(), deviceCategoryId));
apiRiliDeviceInfoVO . setDataValue ( StringUtils . join ( valueList , "," ) ) ;
String sql = baseSql ;
}
logger . info ( "queryDeviceInfo aurora sql: {}" , sql ) ;
try ( PreparedStatement preparedStatement = conn . prepareStatement ( sql ) ) {
preparedStatement . setString ( 1 , apiRiliDeviceInfoVO . getDeviceId ( ) ) ;
try ( ResultSet result = preparedStatement . executeQuery ( ) ) {
if ( result . next ( ) ) {
processResult ( result , apiRiliDeviceInfoVO ) ;
}
}
}
}
} catch ( Exception e ) {
logger . error ( "queryDeviceInfo processing aurora error" , e ) ;
return SimpleDataResponse . fail ( ResponseCode . SERVER_ERROR , ResponseCode . SERVER_ERROR_MSG , new ArrayList < > ( ) ) ;
}
}
}
}
}
}
@ -1569,7 +1528,7 @@ public class CommonServiceImpl implements CommonService {
private void processRiliPersona ( ) {
private void processRiliPersona ( ) {
}
}
private void processResult ( ResultSet result , ApiRiliDeviceInfoVO apiRiliDeviceInfoVO ) throws SQLException {
private void processResult ( ResultSet result , ApiRiliDeviceInfoVO apiRiliDeviceInfoVO ) throws SQLException {
String rawData = result . getString ( "rawData" ) ;
String rawData = result . getString ( "rawData" ) ;
Long receiveTs = result . getLong ( "receive_ts" ) ;
Long receiveTs = result . getLong ( "receive_ts" ) ;
@ -1594,9 +1553,9 @@ public class CommonServiceImpl implements CommonService {
if ( null = = jsonObj | | null = = jsonObj . getLong ( "companyId" ) ) {
if ( null = = jsonObj | | null = = jsonObj . getLong ( "companyId" ) ) {
return new SimpleDataResponse ( ResponseCode . MSG_ERROR , "No companyId" ) ;
return new SimpleDataResponse ( ResponseCode . MSG_ERROR , "No companyId" ) ;
}
}
dataSourceAdminConfig . updateTargetDataSources ( dynamicDataSource , jsonObj . getLongValue ( "companyId" ) ) ;
dataSourceAdminConfig . updateTargetDataSources ( dynamicDataSource , jsonObj . getLongValue ( "companyId" ) ) ;
return SimpleDataResponse . success ( ) ;
return SimpleDataResponse . success ( ) ;
} catch ( Exception e ) {
} catch ( Exception e ) {
logger . error ( "updateTargetDataSources error" , e ) ;
logger . error ( "updateTargetDataSources error" , e ) ;
@ -1611,9 +1570,9 @@ public class CommonServiceImpl implements CommonService {
if ( StringUtils . isBlank ( getFileTemporaryParams . getS3FileKey ( ) ) ) {
if ( StringUtils . isBlank ( getFileTemporaryParams . getS3FileKey ( ) ) ) {
return new SimpleDataResponse ( ResponseCode . MSG_ERROR , "s3FileKey required" ) ;
return new SimpleDataResponse ( ResponseCode . MSG_ERROR , "s3FileKey required" ) ;
}
}
String s3FileKey = getFileTemporaryParams . getS3FileKey ( ) ;
String s3FileKey = getFileTemporaryParams . getS3FileKey ( ) ;
String bucketName = awsBucketAdress ;
String bucketName = awsBucketAdress ;
String accessKey = awsAccessKeyId ;
String accessKey = awsAccessKeyId ;
String secretKey = awsAccessSecret ;
String secretKey = awsAccessSecret ;
@ -1628,7 +1587,7 @@ public class CommonServiceImpl implements CommonService {
// default:
// default:
// return new SimpleDataResponse(ResponseCode.MSG_ERROR, "invalid source");
// return new SimpleDataResponse(ResponseCode.MSG_ERROR, "invalid source");
// }
// }
S3FileMappingExample s3FileMappingExample = new S3FileMappingExample ( ) ;
S3FileMappingExample s3FileMappingExample = new S3FileMappingExample ( ) ;
S3FileMappingExample . Criteria criteria = s3FileMappingExample . createCriteria ( ) ;
S3FileMappingExample . Criteria criteria = s3FileMappingExample . createCriteria ( ) ;
criteria . andFileKeyEqualTo ( getFileTemporaryParams . getS3FileKey ( ) ) ;
criteria . andFileKeyEqualTo ( getFileTemporaryParams . getS3FileKey ( ) ) ;
@ -1636,9 +1595,9 @@ public class CommonServiceImpl implements CommonService {
if ( CollectionUtils . isEmpty ( mappings ) ) {
if ( CollectionUtils . isEmpty ( mappings ) ) {
return new SimpleDataResponse ( ResponseCode . MSG_ERROR , "invalid source" ) ;
return new SimpleDataResponse ( ResponseCode . MSG_ERROR , "invalid source" ) ;
}
}
String objectKey = mappings . get ( 0 ) . getS3Path ( ) ;
String objectKey = mappings . get ( 0 ) . getS3Path ( ) ;
//Temporary link validity period, 2 hours for images, 24 hours for PDF
//Temporary link validity period, 2 hours for images, 24 hours for PDF
int expire = 6 ; //hours
int expire = 6 ; //hours
if ( isPdf ( s3FileKey ) ) {
if ( isPdf ( s3FileKey ) ) {
@ -1647,12 +1606,12 @@ public class CommonServiceImpl implements CommonService {
if ( isImage ( s3FileKey ) ) {
if ( isImage ( s3FileKey ) ) {
expire = 2 ;
expire = 2 ;
}
}
URL url = null ;
URL url = null ;
S3Presigner presigner = null ;
S3Presigner presigner = null ;
try {
try {
AwsBasicCredentials awsCredentials = AwsBasicCredentials . create ( accessKey , secretKey ) ;
AwsBasicCredentials awsCredentials = AwsBasicCredentials . create ( accessKey , secretKey ) ;
// Try to get the object metadata
// Try to get the object metadata
HeadObjectRequest headObjectRequest = HeadObjectRequest . builder ( )
HeadObjectRequest headObjectRequest = HeadObjectRequest . builder ( )
. bucket ( bucketName )
. bucket ( bucketName )
@ -1665,33 +1624,33 @@ public class CommonServiceImpl implements CommonService {
. build ( ) ;
. build ( ) ;
HeadObjectResponse headObjectResponse = s3Client . headObject ( headObjectRequest ) ;
HeadObjectResponse headObjectResponse = s3Client . headObject ( headObjectRequest ) ;
if ( headObjectResponse ! = null ) {
if ( headObjectResponse ! = null ) {
// Generate a presigned URL using S3Prestige
// Generate a presigned URL using S3Prestige
presigner = S3Presigner . builder ( )
presigner = S3Presigner . builder ( )
. region ( Region . AP_NORTHEAST_1 )
. region ( Region . AP_NORTHEAST_1 )
. credentialsProvider ( StaticCredentialsProvider . create ( awsCredentials ) )
. credentialsProvider ( StaticCredentialsProvider . create ( awsCredentials ) )
. build ( ) ;
. build ( ) ;
software . amazon . awssdk . services . s3 . model . GetObjectRequest getObjectRequest = software . amazon . awssdk . services . s3 . model . GetObjectRequest . builder ( )
software . amazon . awssdk . services . s3 . model . GetObjectRequest getObjectRequest = software . amazon . awssdk . services . s3 . model . GetObjectRequest . builder ( )
. bucket ( bucketName )
. bucket ( bucketName )
. key ( objectKey )
. key ( objectKey )
. build ( ) ;
. build ( ) ;
GetObjectPresignRequest presignRequest = GetObjectPresignRequest . builder ( )
GetObjectPresignRequest presignRequest = GetObjectPresignRequest . builder ( )
. signatureDuration ( Duration . ofHours ( expire ) ) // expiration time (e.g. 12 hour)
. signatureDuration ( Duration . ofHours ( expire ) ) // expiration time (e.g. 12 hour)
. getObjectRequest ( getObjectRequest )
. getObjectRequest ( getObjectRequest )
. build ( ) ;
. build ( ) ;
// presigned URL
// presigned URL
PresignedGetObjectRequest presignedRequest = presigner . presignGetObject ( presignRequest ) ;
PresignedGetObjectRequest presignedRequest = presigner . presignGetObject ( presignRequest ) ;
url = presignedRequest . url ( ) ;
url = presignedRequest . url ( ) ;
// 输出临时链接
// 输出临时链接
// System.out.println("临时链接为: " + url);
// System.out.println("临时链接为: " + url);
}
}
return SimpleDataResponse . success ( url ) ;
return SimpleDataResponse . success ( url ) ;
} catch ( NoSuchKeyException e ) {
} catch ( NoSuchKeyException e ) {
logger . error ( "getTemporaryLink NoSuchKeyException" , e ) ;
logger . error ( "getTemporaryLink NoSuchKeyException" , e ) ;
return new SimpleDataResponse ( ResponseCode . MSG_ERROR , "File not found" ) ;
return new SimpleDataResponse ( ResponseCode . MSG_ERROR , "File not found" ) ;
@ -1704,7 +1663,7 @@ public class CommonServiceImpl implements CommonService {
}
}
}
}
}
}
public static boolean isPdf ( String fileName ) {
public static boolean isPdf ( String fileName ) {
return fileName ! = null & & fileName . toLowerCase ( ) . endsWith ( ".pdf" ) ;
return fileName ! = null & & fileName . toLowerCase ( ) . endsWith ( ".pdf" ) ;
}
}
@ -1721,8 +1680,8 @@ public class CommonServiceImpl implements CommonService {
lower . endsWith ( ".raw" ) | | lower . endsWith ( ".cr2" ) | |
lower . endsWith ( ".raw" ) | | lower . endsWith ( ".cr2" ) | |
lower . endsWith ( ".nef" ) ;
lower . endsWith ( ".nef" ) ;
}
}
private void parsePdfInfoToList ( String str , List < PdfInfo > targetList ) {
private void parsePdfInfoToList ( String str , List < PdfInfo > targetList ) {
if ( StringUtils . isNotBlank ( str ) ) {
if ( StringUtils . isNotBlank ( str ) ) {
Map < String , S3FileInfoEntity > map = JSON . parseObject ( str , new TypeReference < Map < String , S3FileInfoEntity > > ( ) { } ) ;
Map < String , S3FileInfoEntity > map = JSON . parseObject ( str , new TypeReference < Map < String , S3FileInfoEntity > > ( ) { } ) ;
@ -1734,7 +1693,7 @@ public class CommonServiceImpl implements CommonService {
}
}
}
}
}
}
private void parsePdfInfoToList ( String str , List < PdfInfo > targetList , String attribute ) {
private void parsePdfInfoToList ( String str , List < PdfInfo > targetList , String attribute ) {
if ( StringUtils . isNotBlank ( str ) ) {
if ( StringUtils . isNotBlank ( str ) ) {
Map < String , S3FileInfoEntity > map = JSON . parseObject ( str , new TypeReference < Map < String , S3FileInfoEntity > > ( ) { } ) ;
Map < String , S3FileInfoEntity > map = JSON . parseObject ( str , new TypeReference < Map < String , S3FileInfoEntity > > ( ) { } ) ;
@ -1747,7 +1706,7 @@ public class CommonServiceImpl implements CommonService {
}
}
}
}
}
}
private void parseImageInfoToList ( String str , List < ImageInfo > targetList ) {
private void parseImageInfoToList ( String str , List < ImageInfo > targetList ) {
if ( StringUtils . isNotBlank ( str ) ) {
if ( StringUtils . isNotBlank ( str ) ) {
Map < String , S3FileInfoEntity > imagesIntroduction = JSON . parseObject ( str , new TypeReference < Map < String , S3FileInfoEntity > > ( ) { } ) ;
Map < String , S3FileInfoEntity > imagesIntroduction = JSON . parseObject ( str , new TypeReference < Map < String , S3FileInfoEntity > > ( ) { } ) ;
@ -1759,7 +1718,7 @@ public class CommonServiceImpl implements CommonService {
}
}
}
}
}
}
@Override
@Override
public SimpleDataResponse querySpaceInfo ( String apikey , ApiSpaceSearchParams searchParams ) {
public SimpleDataResponse querySpaceInfo ( String apikey , ApiSpaceSearchParams searchParams ) {
@ -1768,7 +1727,7 @@ public class CommonServiceImpl implements CommonService {
}
}
return getSpaceInfo ( apikey , searchParams ) ;
return getSpaceInfo ( apikey , searchParams ) ;
}
}
private SimpleDataResponse getSpaceInfo ( String apikey , ApiSpaceSearchParams searchParams ) {
private SimpleDataResponse getSpaceInfo ( String apikey , ApiSpaceSearchParams searchParams ) {
try {
try {
Map < String , Object > paramMap = new HashMap < > ( ) ;
Map < String , Object > paramMap = new HashMap < > ( ) ;
@ -1781,10 +1740,10 @@ public class CommonServiceImpl implements CommonService {
ApikeyInfo2 companyInfo = basicCompanyMapperExt . getAuroraInfoByApikey ( paramMap ) ;
ApikeyInfo2 companyInfo = basicCompanyMapperExt . getAuroraInfoByApikey ( paramMap ) ;
long companyId = companyInfo . getId ( ) ;
long companyId = companyInfo . getId ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
searchParams . setCompanyIdList ( Arrays . asList ( companyId ) ) ;
searchParams . setCompanyIdList ( Arrays . asList ( companyId ) ) ;
List < ApiSpaceWithAssetVO > resultList = basicSpaceMapperExt . getApiQuerySpaceInfo ( searchParams ) ;
List < ApiSpaceWithAssetVO > resultList = basicSpaceMapperExt . getApiQuerySpaceInfo ( searchParams ) ;
@ -1835,7 +1794,7 @@ public class CommonServiceImpl implements CommonService {
if ( CollectionUtils . isEmpty ( batchGetFileTemporaryParams . getKeys ( ) ) ) {
if ( CollectionUtils . isEmpty ( batchGetFileTemporaryParams . getKeys ( ) ) ) {
return new SimpleDataResponse ( ResponseCode . MSG_ERROR , "keys required" ) ;
return new SimpleDataResponse ( ResponseCode . MSG_ERROR , "keys required" ) ;
}
}
Map < String , Object > paramMap = new HashMap < > ( ) ;
Map < String , Object > paramMap = new HashMap < > ( ) ;
paramMap . put ( "apikey" , apikey ) ;
paramMap . put ( "apikey" , apikey ) ;
ApikeyInfo2 apikeyInfo = commonOpt . getAuroraInfoByApikey ( paramMap ) ;
ApikeyInfo2 apikeyInfo = commonOpt . getAuroraInfoByApikey ( paramMap ) ;
@ -1843,7 +1802,7 @@ public class CommonServiceImpl implements CommonService {
if ( null = = apikeyInfo ) {
if ( null = = apikeyInfo ) {
return SimpleDataResponse . fail ( ResponseCode . MSG_ERROR , "Apikey is invalid" ) ;
return SimpleDataResponse . fail ( ResponseCode . MSG_ERROR , "Apikey is invalid" ) ;
}
}
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
@ -1853,10 +1812,10 @@ public class CommonServiceImpl implements CommonService {
AwsBasicCredentials awsCredentials = AwsBasicCredentials . create ( accessKey , secretKey ) ;
AwsBasicCredentials awsCredentials = AwsBasicCredentials . create ( accessKey , secretKey ) ;
Region region = Region . AP_NORTHEAST_1 ;
Region region = Region . AP_NORTHEAST_1 ;
S3Presigner presigner = null ;
S3Presigner presigner = null ;
List < TemporaryInfo > result = new ArrayList < > ( ) ;
List < TemporaryInfo > result = new ArrayList < > ( ) ;
try {
try {
presigner = S3Presigner . builder ( )
presigner = S3Presigner . builder ( )
. region ( region )
. region ( region )
@ -1914,11 +1873,11 @@ public class CommonServiceImpl implements CommonService {
PresignedGetObjectRequest presignedRequest = presigner . presignGetObject ( presignRequest ) ;
PresignedGetObjectRequest presignedRequest = presigner . presignGetObject ( presignRequest ) ;
temporaryInfo . setUrl ( presignedRequest . url ( ) . toString ( ) ) ; ;
temporaryInfo . setUrl ( presignedRequest . url ( ) . toString ( ) ) ; ;
} catch ( NoSuchKeyException e ) {
} catch ( NoSuchKeyException e ) {
logger . error ( "Key not found: {}" , s3FileKey ) ;
logger . error ( "Key not found: {}" , s3FileKey ) ;
}
}
result . add ( temporaryInfo ) ;
result . add ( temporaryInfo ) ;
}
}
@ -1944,19 +1903,19 @@ public class CommonServiceImpl implements CommonService {
if ( null = = apikeyInfo ) {
if ( null = = apikeyInfo ) {
return SimpleDataResponse . fail ( ResponseCode . MSG_ERROR , "Apikey is invalid" ) ;
return SimpleDataResponse . fail ( ResponseCode . MSG_ERROR , "Apikey is invalid" ) ;
}
}
ApikeyInfo2 companyInfo = basicCompanyMapperExt . getAuroraInfoByApikey ( paramMap ) ;
ApikeyInfo2 companyInfo = basicCompanyMapperExt . getAuroraInfoByApikey ( paramMap ) ;
long companyId = companyInfo . getId ( ) ;
long companyId = companyInfo . getId ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
OptBuildingParams optBuildingParams = new OptBuildingParams ( ) ;
OptBuildingParams optBuildingParams = new OptBuildingParams ( ) ;
optBuildingParams . setBuildingBucket ( addBuildingParams . getBuildingBucket ( ) ) ;
optBuildingParams . setBuildingBucket ( addBuildingParams . getBuildingBucket ( ) ) ;
optBuildingParams . setBuildingName ( addBuildingParams . getBuildingName ( ) ) ;
optBuildingParams . setBuildingName ( addBuildingParams . getBuildingName ( ) ) ;
optBuildingParams . setUdfBuildingId ( addBuildingParams . getUdfBuildingId ( ) ) ;
optBuildingParams . setUdfBuildingId ( addBuildingParams . getUdfBuildingId ( ) ) ;
// optBuildingParams.setFloorInfoList(addBuildingParams.getFloorInfoList());
// optBuildingParams.setFloorInfoList(addBuildingParams.getFloorInfoList());
return buildingService . add ( optBuildingParams , null , null , companyId , 2 ) ;
return buildingService . add ( optBuildingParams , null , null , companyId , 2 ) ;
}
}
@ -1971,10 +1930,10 @@ public class CommonServiceImpl implements CommonService {
if ( null = = apikeyInfo ) {
if ( null = = apikeyInfo ) {
return SimpleDataResponse . fail ( ResponseCode . MSG_ERROR , "Apikey is invalid" ) ;
return SimpleDataResponse . fail ( ResponseCode . MSG_ERROR , "Apikey is invalid" ) ;
}
}
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
DataSourceContextHolder . setCurrentDataSourceKey ( Constants . DATASOURCE_PREFIX + apikeyInfo . getId ( ) ) ;
BasicBuildingExample example = new BasicBuildingExample ( ) ;
BasicBuildingExample example = new BasicBuildingExample ( ) ;
BasicBuildingExample . Criteria criteria = example . createCriteria ( ) ;
BasicBuildingExample . Criteria criteria = example . createCriteria ( ) ;
criteria . andUdfBuildingIdEqualTo ( queryBuilding . getUdfBuildingId ( ) ) ;
criteria . andUdfBuildingIdEqualTo ( queryBuilding . getUdfBuildingId ( ) ) ;
@ -1993,7 +1952,7 @@ public class CommonServiceImpl implements CommonService {
// buildingInfo.setFloorInfoList(floorInfos);
// buildingInfo.setFloorInfoList(floorInfos);
// }
// }
buildingInfo . setFloorInfoList ( basicFloorMapperExt . getFloorInfo ( buildingList . get ( 0 ) . getBuildingId ( ) ) ) ;
buildingInfo . setFloorInfoList ( basicFloorMapperExt . getFloorInfo ( buildingList . get ( 0 ) . getBuildingId ( ) ) ) ;
return SimpleDataResponse . success ( Collections . singletonList ( buildingInfo ) ) ;
return SimpleDataResponse . success ( Collections . singletonList ( buildingInfo ) ) ;
}
}
}
}
@ -2041,7 +2000,7 @@ public class CommonServiceImpl implements CommonService {
Long removedCount = redisUtil . zRemRangeByScore ( redisKey , 0 , oneWeekAgoTimestamp ) ;
Long removedCount = redisUtil . zRemRangeByScore ( redisKey , 0 , oneWeekAgoTimestamp ) ;
logger . info ( "Deleted " + removedCount + " elements in " + redisKey ) ;
logger . info ( "Deleted " + removedCount + " elements in " + redisKey ) ;
Set < String > cancelAlarmDeviceIdList = redisUtil . zSetOperations . rangeByScore ( redisKey , 0 , Double . POSITIVE_INFINITY ) ;
Set < String > cancelAlarmDeviceIdList = redisUtil . zSetOperations . rangeByScore ( redisKey , 0 , Double . POSITIVE_INFINITY ) ;
if ( CollectionUtils . isNotEmpty ( cancelAlarmDeviceIdList ) ) {
if ( CollectionUtils . isNotEmpty ( cancelAlarmDeviceIdList ) ) {
Map < String , Object > paramMap2 = new HashMap < > ( ) ;
Map < String , Object > paramMap2 = new HashMap < > ( ) ;
paramMap2 . put ( "deviceIdList" , cancelAlarmDeviceIdList ) ;
paramMap2 . put ( "deviceIdList" , cancelAlarmDeviceIdList ) ;
@ -2054,73 +2013,42 @@ public class CommonServiceImpl implements CommonService {
paramMap2 . put ( "assetSymbol" , apiAlarmDeviceSearchParams . getAssetSymbol ( ) ) ;
paramMap2 . put ( "assetSymbol" , apiAlarmDeviceSearchParams . getAssetSymbol ( ) ) ;
paramMap2 . put ( "udfBuildingId" , apiAlarmDeviceSearchParams . getUdfBuildingId ( ) ) ;
paramMap2 . put ( "udfBuildingId" , apiAlarmDeviceSearchParams . getUdfBuildingId ( ) ) ;
paramMap2 . put ( "companyId" , companyId ) ;
paramMap2 . put ( "companyId" , companyId ) ;
deviceInfoVOs = deviceInfoMapperExt . getCancelAlarmDeviceInfo ( paramMap2 ) ;
deviceInfoVOs = deviceInfoMapperExt . getCancelAlarmDeviceInfo ( paramMap2 ) ;
//Query latest data from Aurora if configured and devices are found
//Query latest data from Aurora if configured and devices are found
if ( StringUtils . isNotBlank ( apikeyInfo . getAuroraUrl ( ) ) & & CollectionUtils . isNotEmpty ( deviceInfoVOs ) ) {
if ( CollectionUtils . isNotEmpty ( deviceInfoVOs ) ) {
Class . forName ( "com.mysql.cj.jdbc.Driver" ) ;
for ( ApiCancelAlarmDeviceInfoVO apiCancelAlarmDeviceInfoVO : deviceInfoVOs ) {
String deviceId = apiCancelAlarmDeviceInfoVO . getDeviceId ( ) ;
String regex = "(jdbc:mysql://)([^/]+)(/data_center_aeon_admin.*)" ;
// 计算分片
Pattern pattern = Pattern . compile ( regex ) ;
int partitionIndex = Math . abs ( deviceId . hashCode ( ) ) % Constants . REDIS_PARTITION_NUM ;
Matcher matcher = pattern . matcher ( dbUrl ) ;
String realtimeRedisKey = Constants . REDIS_RAW_DATA_REALTIME + partitionIndex ;
String newJdbcUrl = "" ;
if ( matcher . find ( ) ) {
// 从 Redis Hash 获取 JSON
newJdbcUrl = matcher . replaceAll ( "$1" + apikeyInfo . getAuroraUrl ( ) + "$3" ) ;
Object redisData = redisUtil . HGet ( realtimeRedisKey , deviceId ) ;
}
if ( redisData ! = null ) {
JSONObject jsonObject = JSON . parseObject ( JSON . toJSONString ( redisData ) , Feature . OrderedField ) ;
try ( Connection conn = DriverManager . getConnection (
Long receiveTs = jsonObject . getLong ( "receive_ts" ) ;
newJdbcUrl . replace ( "data_center_aeon_admin" , "aeon" ) + "&allowPublicKeyRetrieval=true" ,
String rawData = jsonObject . getString ( "rawData" ) ;
DESUtil . decrypt ( apikeyInfo . getAuroraUsername ( ) , Constants . DES_SALT ) ,
String alertLevel = jsonObject . getString ( "alertLevel" ) ;
DESUtil . decrypt ( apikeyInfo . getAuroraPassword ( ) , Constants . DES_SALT ) ) ) {
String alertLevelName = jsonObject . getString ( "alertLevelName" ) ;
String alertCancelTitle = jsonObject . getString ( "alertCancelTitle" ) ;
for ( ApiCancelAlarmDeviceInfoVO apiCancelAlarmDeviceInfoVO : deviceInfoVOs ) {
String alertTypeName = jsonObject . getString ( "alertTypeName" ) ;
// String sql = " select rawData, receive_ts, alertTitle, alertLevel,alertLevelName,alertTypeName from "+formatRawDataWithDate()+" where deviceId = '" + apiAlarmDeviceInfoVO.getDeviceId() + "' order by receive_ts desc limit 1" ;
apiCancelAlarmDeviceInfoVO . setLatestDataTime ( receiveTs ) ;
// String sql = " select rawData, receive_ts, alertTitle, alertLevel, alertTypeName from "+formatRawDataWithDate()+" where deviceId = '" + apiAlarmDeviceInfoVO.getDeviceId() + "' order by receive_ts desc limit 1" ;
apiCancelAlarmDeviceInfoVO . setLatestRawData ( rawData ) ;
String sql = "select rawData, receive_ts, alertCancelTitle, alertLevel, alertLevelName, alertTypeName from rawData_realtime where deviceId = ? limit 1" ;
apiCancelAlarmDeviceInfoVO . setAlertLevel ( alertLevel ) ;
logger . info ( "queryAlarmDevice aurora sql: " + sql ) ;
apiCancelAlarmDeviceInfoVO . setAlertCancelTitle ( alertCancelTitle ) ;
apiCancelAlarmDeviceInfoVO . setAlertTypeName ( alertTypeName ) ;
try ( PreparedStatement preparedStatement = conn . prepareStatement ( sql ) ) {
apiCancelAlarmDeviceInfoVO . setAlertLevelName ( alertLevelName ) ;
preparedStatement . setString ( 1 , apiCancelAlarmDeviceInfoVO . getDeviceId ( ) ) ;
// 遍历所有键值对
ResultSet retult = preparedStatement . executeQuery ( ) ;
JSONObject rawDataObj = JSON . parseObject ( rawData , Feature . OrderedField ) ;
List < Object > valueList = new ArrayList < > ( ) ;
while ( retult . next ( ) ) {
for ( String key : rawDataObj . keySet ( ) ) {
String rawData = retult . getString ( "rawData" ) ;
valueList . add ( rawDataObj . get ( key ) ) ;
Long receiveTs = retult . getLong ( "receive_ts" ) ;
}
String alertLevel = retult . getString ( "alertLevel" ) ;
apiCancelAlarmDeviceInfoVO . setDataValue ( StringUtils . join ( valueList , "," ) ) ;
String alertLevelName = retult . getString ( "alertLevelName" ) ;
String alertCancelTitle = retult . getString ( "alertCancelTitle" ) ;
String alertTypeName = retult . getString ( "alertTypeName" ) ;
apiCancelAlarmDeviceInfoVO . setLatestDataTime ( receiveTs ) ;
apiCancelAlarmDeviceInfoVO . setLatestRawData ( rawData ) ;
apiCancelAlarmDeviceInfoVO . setAlertLevel ( alertLevel ) ;
apiCancelAlarmDeviceInfoVO . setAlertCancelTitle ( alertCancelTitle ) ;
apiCancelAlarmDeviceInfoVO . setAlertTypeName ( alertTypeName ) ;
apiCancelAlarmDeviceInfoVO . setAlertLevelName ( alertLevelName ) ;
JSONObject jsonObject = JSON . parseObject ( rawData , Feature . OrderedField ) ;
// 遍历所有键值对
List < Object > valueList = new ArrayList < > ( ) ;
for ( String key : jsonObject . keySet ( ) ) {
valueList . add ( jsonObject . get ( key ) ) ;
}
apiCancelAlarmDeviceInfoVO . setDataValue ( StringUtils . join ( valueList , "," ) ) ;
}
preparedStatement . close ( ) ;
}
}
conn . close ( ) ;
} catch ( Exception e ) {
logger . error ( "queryAlarmDevice processing aurora error" , e ) ;
return SimpleDataResponse . fail ( ResponseCode . SERVER_ERROR , ResponseCode . SERVER_ERROR_MSG ) ;
} finally {
DataSourceContextHolder . clearCurrentDataSourceKey ( ) ;
if ( jedis ! = null ) {
// Close Jedis connection
jedis . close ( ) ;
}
}
}
}
}
}