@ -27,10 +27,12 @@ import com.ruoyi.dataprocess.service.IDataProcessService;
import com.ruoyi.tdengine.api.RemoteTdEngineService ;
import com.ruoyi.tdengine.api.RemoteTdEngineService ;
import com.ruoyi.tdengine.api.domain.TdField ;
import com.ruoyi.tdengine.api.domain.TdField ;
import com.ruoyi.tdengine.api.domain.TdHistorySelectDto ;
import com.ruoyi.tdengine.api.domain.TdHistorySelectDto ;
import com.ruoyi.tdengine.api.domain.TdReturnDataVo ;
import com.ruoyi.tdengine.api.domain.TdTableVo ;
import com.ruoyi.tdengine.api.domain.TdTableVo ;
import com.sun.org.apache.xpath.internal.operations.Bool ;
import com.sun.org.apache.xpath.internal.operations.Bool ;
import darabonba.core.client.ClientOverrideConfiguration ;
import darabonba.core.client.ClientOverrideConfiguration ;
import org.apache.commons.codec.binary.Base64 ;
import org.apache.commons.codec.binary.Base64 ;
import org.apache.poi.openxml4j.exceptions.InvalidOperationException ;
import org.slf4j.Logger ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Autowired ;
@ -44,13 +46,11 @@ import javax.script.ScriptException;
import java.io.IOException ;
import java.io.IOException ;
import java.math.BigDecimal ;
import java.math.BigDecimal ;
import java.text.SimpleDateFormat ;
import java.text.SimpleDateFormat ;
import java.util.ArrayList ;
import java.util.* ;
import java.util.Date ;
import java.util.List ;
import java.util.Map ;
import java.util.concurrent.CompletableFuture ;
import java.util.concurrent.CompletableFuture ;
import java.util.concurrent.ExecutionException ;
import java.util.concurrent.ExecutionException ;
import java.util.concurrent.atomic.AtomicBoolean ;
import java.util.concurrent.atomic.AtomicBoolean ;
import java.util.stream.Collectors ;
/ * *
/ * *
* @Description : 数 据 处 理 业 务 类
* @Description : 数 据 处 理 业 务 类
@ -220,7 +220,7 @@ public class DataProcessServiceImpl extends CommanHandleService implements IData
final R < ? > insertResult = this . remoteTdEngineService . insertTable ( tdTableVo , SecurityConstants . INNER ) ;
final R < ? > insertResult = this . remoteTdEngineService . insertTable ( tdTableVo , SecurityConstants . INNER ) ;
if ( insertResult . getCode ( ) = = ResultEnums . SUCCESS . getCode ( ) ) {
if ( insertResult . getCode ( ) = = ResultEnums . SUCCESS . getCode ( ) ) {
logger . info ( "Insert data result: {}" , insertResult . get Msg ( ) ) ;
logger . info ( "Insert data result: {}" , insertResult . get Code ( ) ) ;
} else {
} else {
logger . error ( "Insert data Exception: {},data:{}" , insertResult . getMsg ( ) , jsonData ) ;
logger . error ( "Insert data Exception: {},data:{}" , insertResult . getMsg ( ) , jsonData ) ;
}
}
@ -242,6 +242,7 @@ public class DataProcessServiceImpl extends CommanHandleService implements IData
return paramArr . size ( ) ;
return paramArr . size ( ) ;
}
}
private static String getImageFileName ( String imagePath , String imageDomain , String imagePrefix , String valueStr , Long deviceId , String extension ) {
private static String getImageFileName ( String imagePath , String imageDomain , String imagePrefix , String valueStr , Long deviceId , String extension ) {
try {
try {
String imageFileName = ImageUtils . convertBase64ToImage ( imagePath ,
String imageFileName = ImageUtils . convertBase64ToImage ( imagePath ,
@ -363,87 +364,147 @@ public class DataProcessServiceImpl extends CommanHandleService implements IData
* /
* /
private void checkAlarm ( Long deviceId , List < TdField > alarmFields ,
private void checkAlarm ( Long deviceId , List < TdField > alarmFields ,
Long tenantId , Long sceneId , Long monitorUnitId , String topic , String subDeviceCode ) {
Long tenantId , Long sceneId , Long monitorUnitId , String topic , String subDeviceCode ) {
HwAlarmRule queryAlarmRule = new HwAlarmRule ( ) ;
queryAlarmRule . setRuleDeviceId ( deviceId ) ;
queryAlarmRule . setRuleType ( HwDictConstants . ALARM_RULE_RULE_TYPE_DEVICE ) ;
queryAlarmRule . setAlarmRuleStatus ( HwDictConstants . ALARM_RULE_STATUS_ENABLE ) ;
List < HwAlarmRule > alarmRules = hwAlarmRuleMapper . selectHwAlarmRulesWithLink ( queryAlarmRule ) ;
ScriptEngineManager manager = new ScriptEngineManager ( ) ;
ScriptEngine engine = manager . getEngineByName ( "js" ) ;
if ( alarmRules ! = null ) {
Date currentDate = new Date ( ) ;
for ( HwAlarmRule alarmRule : alarmRules ) {
String triggerExpression = alarmRule . getTriggerExpression ( )
. replaceAll ( "and" , "&&" )
. replaceAll ( "or" , "||" ) ;
AtomicBoolean isAlarmed = new AtomicBoolean ( false ) ;
List < HwAlarmDetail > alarmDetails = new ArrayList < HwAlarmDetail > ( ) ;
for ( TdField alarmField : alarmFields ) {
String fieldName = alarmField . getFieldName ( ) ;
Object filedValue = alarmField . getFieldValue ( ) ;
if ( triggerExpression . contains ( "{" + fieldName + "}" ) ) {
isAlarmed . set ( true ) ;
triggerExpression = triggerExpression . replaceAll ( "\\{" + fieldName + "\\}" , String . valueOf ( filedValue ) ) ;
HwAlarmDetail alarmDetail = new HwAlarmDetail ( ) ;
alarmDetail . setDeviceId ( deviceId ) ;
alarmDetail . setFunctionIdentifier ( fieldName ) ;
alarmDetail . setFunctionValue ( String . valueOf ( filedValue ) ) ;
alarmDetail . setMonitorTime ( currentDate ) ;
alarmDetails . add ( alarmDetail ) ;
}
}
if ( isAlarmed . get ( ) & & ! triggerExpression . contains ( "{" )
try {
& & RegexUtils . findSymbolInText ( triggerExpression ) . size ( ) > 0
HwAlarmRule queryAlarmRule = new HwAlarmRule ( ) ;
& & RegexUtils . findNumberInText ( triggerExpression ) . size ( ) > 0 ) {
queryAlarmRule . setRuleDeviceId ( deviceId ) ;
queryAlarmRule . setRuleType ( HwDictConstants . ALARM_RULE_RULE_TYPE_DEVICE ) ;
queryAlarmRule . setAlarmRuleStatus ( HwDictConstants . ALARM_RULE_STATUS_ENABLE ) ;
List < HwAlarmRule > alarmRules = hwAlarmRuleMapper . selectHwAlarmRulesWithLink ( queryAlarmRule ) ;
ScriptEngineManager manager = new ScriptEngineManager ( ) ;
ScriptEngine engine = manager . getEngineByName ( "js" ) ;
if ( alarmRules ! = null ) {
Date currentDate = new Date ( ) ;
for ( HwAlarmRule alarmRule : alarmRules ) {
String triggerExpression = alarmRule . getTriggerExpression ( )
. replaceAll ( "and" , "&&" )
. replaceAll ( "or" , "||" ) ;
AtomicBoolean isAlarmed = new AtomicBoolean ( false ) ;
List < HwAlarmDetail > alarmDetails = new ArrayList < HwAlarmDetail > ( ) ;
for ( TdField alarmField : alarmFields ) {
String fieldName = alarmField . getFieldName ( ) ;
Object filedValue = alarmField . getFieldValue ( ) ;
if ( triggerExpression . contains ( "{" + fieldName + "}" ) ) {
isAlarmed . set ( true ) ;
Boolean triggerExpressionBool = null ;
/ * *
try {
* Add By 获 取 五 分 钟 内 的 数 据 WenJY 2024 - 07 - 03 14 : 13 : 21
triggerExpressionBool = ( Boolean ) engine . eval ( triggerExpression ) ;
* /
} catch ( ScriptException e ) {
if ( alarmRule . getTriggerTimeFrame ( ) ! = null & & alarmRule . getTriggerTimeFrame ( ) > 0 ) {
throw new RuntimeException ( e ) ;
double difValue = FilterDeviceValue ( deviceId , fieldName , alarmRule . getTriggerTimeFrame ( ) ) ;
}
if ( difValue > 0 ) {
if ( triggerExpressionBool ) {
filedValue = difValue ;
HwAlarmInfo alarmInfo = new HwAlarmInfo ( ) ;
}
alarmInfo . setAlarmInfoType ( HwDictConstants . ALARM_INFO_TYPE_DEVICE ) ;
alarmInfo . setAlarmReleatedId ( alarmRule . getAlarmRuleId ( ) ) ;
alarmInfo . setDeviceId ( deviceId ) ;
alarmInfo . setMonitorUnitId ( monitorUnitId ) ;
alarmInfo . setTenantId ( tenantId ) ;
alarmInfo . setSceneId ( sceneId ) ;
alarmInfo . setAlarmLevelId ( alarmRule . getAlarmLevelId ( ) ) ;
alarmInfo . setAlarmTypeId ( alarmRule . getAlarmTypeId ( ) ) ;
alarmInfo . setHwAlarmDetailList ( alarmDetails ) ;
alarmInfo . setHandleStatus ( HwDictConstants . ALARM_HANDLE_STATUS_NO ) ;
alarmInfo . setCreateTime ( currentDate ) ;
alarmInfo . setAlarmTime ( currentDate ) ;
hwAlarmInfoMapper . insertHwAlarmInfo ( alarmInfo ) ;
this . insertHwAlarmDetail ( alarmInfo ) ;
this . handleAlarmLink ( alarmRule , topic , subDeviceCode ) ;
try {
HwDevice hwDevice = hwDeviceMapper . GetDeviceById ( deviceId ) ;
if ( hwDevice ! = null ) {
AlarmMsgTemplateParam alarmMsgTemplateParam = new AlarmMsgTemplateParam ( ) ;
alarmMsgTemplateParam . setWarning ( alarmRule . getAlarmRuleName ( ) ) ;
alarmMsgTemplateParam . setParentname ( hwDevice . getMonitorUnitName ( ) ) ;
alarmMsgTemplateParam . setSensorID ( deviceId . toString ( ) ) ;
SendAlarmInfoToSms ( alarmRule . getPhoneNumbers ( ) , alarmMsgTemplateParam ) ;
}
}
} catch ( ExecutionException | InterruptedException e ) {
/** End **/
throw new RuntimeException ( e . getMessage ( ) ) ;
triggerExpression = triggerExpression . replaceAll ( "\\{" + fieldName + "\\}" , String . valueOf ( filedValue ) ) ;
HwAlarmDetail alarmDetail = new HwAlarmDetail ( ) ;
alarmDetail . setDeviceId ( deviceId ) ;
alarmDetail . setFunctionIdentifier ( fieldName ) ;
alarmDetail . setFunctionValue ( String . valueOf ( filedValue ) ) ;
alarmDetail . setMonitorTime ( currentDate ) ;
alarmDetails . add ( alarmDetail ) ;
}
}
}
break ;
if ( isAlarmed . get ( ) & & ! triggerExpression . contains ( "{" )
& & RegexUtils . findSymbolInText ( triggerExpression ) . size ( ) > 0
& & RegexUtils . findNumberInText ( triggerExpression ) . size ( ) > 0 ) {
Boolean triggerExpressionBool = ( Boolean ) engine . eval ( triggerExpression ) ;
if ( triggerExpressionBool ) {
HwAlarmInfo alarmInfo = new HwAlarmInfo ( ) ;
alarmInfo . setAlarmInfoType ( HwDictConstants . ALARM_INFO_TYPE_DEVICE ) ;
alarmInfo . setAlarmReleatedId ( alarmRule . getAlarmRuleId ( ) ) ;
alarmInfo . setDeviceId ( deviceId ) ;
alarmInfo . setMonitorUnitId ( monitorUnitId ) ;
alarmInfo . setTenantId ( tenantId ) ;
alarmInfo . setSceneId ( sceneId ) ;
alarmInfo . setAlarmLevelId ( alarmRule . getAlarmLevelId ( ) ) ;
alarmInfo . setAlarmTypeId ( alarmRule . getAlarmTypeId ( ) ) ;
alarmInfo . setHwAlarmDetailList ( alarmDetails ) ;
alarmInfo . setHandleStatus ( HwDictConstants . ALARM_HANDLE_STATUS_NO ) ;
alarmInfo . setCreateTime ( currentDate ) ;
alarmInfo . setAlarmTime ( currentDate ) ;
hwAlarmInfoMapper . insertHwAlarmInfo ( alarmInfo ) ;
this . insertHwAlarmDetail ( alarmInfo ) ;
this . handleAlarmLink ( alarmRule , topic , subDeviceCode ) ;
/ * *
* Add By WenJY 下 发 报 警 信 息
* /
if ( StringUtils . isNotEmpty ( alarmRule . getPhoneNumbers ( ) ) ) {
SendAlarmInfoBySms ( deviceId , alarmRule ) ;
} else {
logger . warn ( "设备:{};报警信息发送失败,联系方式为空" , deviceId ) ;
}
break ;
}
}
}
}
}
}
}
} catch ( Exception e ) {
logger . error ( "设备:{};报警逻辑处理异常:{}" , deviceId , e . getMessage ( ) ) ;
}
}
}
}
/ * *
* 过 滤 指 定 时 间 段 内 的 数 据 , 返 回 最 大 最 小 的 差 值 , 过 滤 0
* @param deviceId
* @param fieldName
* @param triggerTimeFrame
* @return 差 值
* /
private double FilterDeviceValue ( Long deviceId , String fieldName , long triggerTimeFrame ) {
double differenceValue = 0.00 ;
String databaseName = TdEngineConstants . getDatabaseName ( ) ;
String tableName = TdEngineConstants . DEFAULT_TABLE_NAME_PREFIX + deviceId ;
long currentTimeMillis = System . currentTimeMillis ( ) ;
long fiveMinutesInMillis = triggerTimeFrame * 60 * 1000 ;
long beforeTimeMillis = currentTimeMillis - fiveMinutesInMillis ;
TdHistorySelectDto tdHistorySelectDto = new TdHistorySelectDto ( ) ;
tdHistorySelectDto . setDatabaseName ( databaseName ) ;
tdHistorySelectDto . setTableName ( tableName ) ;
tdHistorySelectDto . setStartTime ( beforeTimeMillis ) ;
tdHistorySelectDto . setEndTime ( currentTimeMillis ) ;
tdHistorySelectDto . setFirstFieldName ( "ts" ) ;
R < ? > result = this . remoteTdEngineService . getHistoryData ( tdHistorySelectDto , SecurityConstants . INNER ) ;
if ( result . getCode ( ) = = ResultEnums . SUCCESS . getCode ( ) ) {
logger . info ( "Get history data result: {}" , result . getCode ( ) ) ;
TdReturnDataVo data = ( TdReturnDataVo ) result . getData ( ) ;
List < Map < String , Object > > dataList = data . getDataList ( ) ;
List < Double > value = dataList . stream ( ) . map ( map - > ( Double ) map . get ( "value1" ) ) . distinct ( ) . filter ( x - > x > 0 ) . collect ( Collectors . toList ( ) ) ;
System . out . println ( triggerTimeFrame + "分钟内数据:" + JSONArray . toJSON ( value ) ) ;
OptionalDouble maxOptional = value . stream ( ) . mapToDouble ( Double : : doubleValue ) . max ( ) ;
OptionalDouble minOptional = value . stream ( ) . mapToDouble ( Double : : doubleValue ) . min ( ) ;
if ( maxOptional . isPresent ( ) & & minOptional . isPresent ( ) ) {
double maxValue = maxOptional . getAsDouble ( ) ;
double minValue = minOptional . getAsDouble ( ) ;
differenceValue = maxValue - minValue ;
System . out . println ( "Difference between max and min values: " + differenceValue ) ;
} else {
System . out . println ( "No values found to calculate difference." ) ;
}
} else {
logger . error ( "Get history data Exception: {}" , result . getMsg ( ) ) ;
}
return differenceValue ;
}
/ * *
/ * *
* 新 增 报 警 详 情 信 息 信 息
* 新 增 报 警 详 情 信 息 信 息
*
*
@ -516,6 +577,26 @@ public class DataProcessServiceImpl extends CommanHandleService implements IData
}
}
}
}
/ * *
* 发 送 报 警 短 信
* @param deviceId
* @param alarmRule
* /
private void SendAlarmInfoBySms ( Long deviceId , HwAlarmRule alarmRule ) {
try {
HwDevice hwDevice = hwDeviceMapper . GetDeviceById ( deviceId ) ;
if ( hwDevice ! = null ) {
AlarmMsgTemplateParam alarmMsgTemplateParam = new AlarmMsgTemplateParam ( ) ;
alarmMsgTemplateParam . setWarning ( alarmRule . getAlarmRuleName ( ) ) ;
alarmMsgTemplateParam . setParentname ( hwDevice . getMonitorUnitName ( ) ) ;
alarmMsgTemplateParam . setSensorID ( deviceId . toString ( ) ) ;
AliSmsHandle ( alarmRule . getPhoneNumbers ( ) , alarmMsgTemplateParam ) ;
}
} catch ( ExecutionException | InterruptedException e ) {
throw new InvalidOperationException ( String . format ( "报警信息发送异常:%e" , e . getMessage ( ) ) ) ;
}
}
/ * *
/ * *
* 报 警 短 信 推 送
* 报 警 短 信 推 送
* @param phoneNumbers
* @param phoneNumbers
@ -523,7 +604,7 @@ public class DataProcessServiceImpl extends CommanHandleService implements IData
* @throws ExecutionException
* @throws ExecutionException
* @throws InterruptedException
* @throws InterruptedException
* /
* /
private void SendAlarmInfoToSms ( String phoneNumbers , AlarmMsgTemplateParam alarmMsgTemplateParam ) throws ExecutionException , InterruptedException {
private void AliSmsHandle ( String phoneNumbers , AlarmMsgTemplateParam alarmMsgTemplateParam ) throws ExecutionException , InterruptedException {
StaticCredentialProvider provider = StaticCredentialProvider . create ( Credential . builder ( )
StaticCredentialProvider provider = StaticCredentialProvider . create ( Credential . builder ( )
. accessKeyId ( AccessKeyId )
. accessKeyId ( AccessKeyId )
. accessKeySecret ( AccessKeySecret )
. accessKeySecret ( AccessKeySecret )
@ -546,7 +627,7 @@ public class DataProcessServiceImpl extends CommanHandleService implements IData
. templateCode ( "SMS_468740027" )
. templateCode ( "SMS_468740027" )
. templateParam ( jsonString )
. templateParam ( jsonString )
. build ( ) ;
. build ( ) ;
logger . info ( "向:{};发送报警信息:{}" , phoneNumbers , jsonString ) ;
CompletableFuture < SendSmsResponse > response = client . sendSms ( sendSmsRequest ) ;
CompletableFuture < SendSmsResponse > response = client . sendSms ( sendSmsRequest ) ;
SendSmsResponse resp = response . get ( ) ;
SendSmsResponse resp = response . get ( ) ;