@ -371,10 +371,12 @@ class DefaultTbContext implements TbContext {
return TbMsg . transformMsgOriginator ( origMsg , originator ) ;
}
@Override
public TbMsg customerCreatedMsg ( Customer customer , RuleNodeId ruleNodeId ) {
return entityActionMsg ( customer , customer . getId ( ) , ruleNodeId , ENTITY_CREATED ) ;
}
@Override
public TbMsg deviceCreatedMsg ( Device device , RuleNodeId ruleNodeId ) {
DeviceProfile deviceProfile = null ;
if ( device . getDeviceProfileId ( ) ! = null ) {
@ -383,6 +385,7 @@ class DefaultTbContext implements TbContext {
return entityActionMsg ( device , device . getId ( ) , ruleNodeId , ENTITY_CREATED , deviceProfile ) ;
}
@Override
public TbMsg assetCreatedMsg ( Asset asset , RuleNodeId ruleNodeId ) {
AssetProfile assetProfile = null ;
if ( asset . getAssetProfileId ( ) ! = null ) {
@ -391,18 +394,33 @@ class DefaultTbContext implements TbContext {
return entityActionMsg ( asset , asset . getId ( ) , ruleNodeId , ENTITY_CREATED , assetProfile ) ;
}
@Override
public TbMsg alarmActionMsg ( Alarm alarm , RuleNodeId ruleNodeId , String action ) {
EntityId originator = alarm . getOriginator ( ) ;
HasRuleEngineProfile profile = getRuleEngineProfile ( originator ) ;
return entityActionMsg ( alarm , originator , ruleNodeId , action , profile ) ;
}
@Override
public TbMsg alarmActionMsg ( Alarm alarm , RuleNodeId ruleNodeId , TbMsgType actionMsgType ) {
EntityId originator = alarm . getOriginator ( ) ;
HasRuleEngineProfile profile = getRuleEngineProfile ( originator ) ;
return entityActionMsg ( alarm , originator , ruleNodeId , actionMsgType , profile ) ;
}
private HasRuleEngineProfile getRuleEngineProfile ( EntityId originator ) {
HasRuleEngineProfile profile = null ;
if ( EntityType . DEVICE . equals ( alarm . getOriginator ( ) . getEntityType ( ) ) ) {
DeviceId deviceId = new DeviceId ( alarm . getOriginator ( ) . getId ( ) ) ;
if ( EntityType . DEVICE . equals ( o riginator. getEntityType ( ) ) ) {
DeviceId deviceId = new DeviceId ( o riginator. getId ( ) ) ;
profile = mainCtx . getDeviceProfileCache ( ) . get ( getTenantId ( ) , deviceId ) ;
} else if ( EntityType . ASSET . equals ( alarm . getOriginator ( ) . getEntityType ( ) ) ) {
AssetId assetId = new AssetId ( alarm . getOriginator ( ) . getId ( ) ) ;
} else if ( EntityType . ASSET . equals ( o riginator. getEntityType ( ) ) ) {
AssetId assetId = new AssetId ( o riginator. getId ( ) ) ;
profile = mainCtx . getAssetProfileCache ( ) . get ( getTenantId ( ) , assetId ) ;
}
return entityActionMsg ( alarm , alarm . getOriginator ( ) , ruleNodeId , actionMsgType , profile ) ;
return profile ;
}
@Override
public TbMsg attributesUpdatedActionMsg ( EntityId originator , RuleNodeId ruleNodeId , String scope , List < AttributeKvEntry > attributes ) {
ObjectNode entityNode = JacksonUtil . newObjectNode ( ) ;
if ( attributes ! = null ) {
@ -411,6 +429,7 @@ class DefaultTbContext implements TbContext {
return attributesActionMsg ( originator , ruleNodeId , scope , ATTRIBUTES_UPDATED , JacksonUtil . toString ( entityNode ) ) ;
}
@Override
public TbMsg attributesDeletedActionMsg ( EntityId originator , RuleNodeId ruleNodeId , String scope , List < String > keys ) {
ObjectNode entityNode = JacksonUtil . newObjectNode ( ) ;
ArrayNode attrsArrayNode = entityNode . putArray ( "attributes" ) ;
@ -423,14 +442,7 @@ class DefaultTbContext implements TbContext {
private TbMsg attributesActionMsg ( EntityId originator , RuleNodeId ruleNodeId , String scope , TbMsgType actionMsgType , String msgData ) {
TbMsgMetaData tbMsgMetaData = getActionMetaData ( ruleNodeId ) ;
tbMsgMetaData . putValue ( "scope" , scope ) ;
HasRuleEngineProfile profile = null ;
if ( EntityType . DEVICE . equals ( originator . getEntityType ( ) ) ) {
DeviceId deviceId = new DeviceId ( originator . getId ( ) ) ;
profile = mainCtx . getDeviceProfileCache ( ) . get ( getTenantId ( ) , deviceId ) ;
} else if ( EntityType . ASSET . equals ( originator . getEntityType ( ) ) ) {
AssetId assetId = new AssetId ( originator . getId ( ) ) ;
profile = mainCtx . getAssetProfileCache ( ) . get ( getTenantId ( ) , assetId ) ;
}
HasRuleEngineProfile profile = getRuleEngineProfile ( originator ) ;
return entityActionMsg ( originator , tbMsgMetaData , msgData , actionMsgType , profile ) ;
}
@ -443,6 +455,26 @@ class DefaultTbContext implements TbContext {
return entityActionMsg ( entity , id , ruleNodeId , actionMsgType , null ) ;
}
@Deprecated ( since = "3.5.2" , forRemoval = true )
public < E , I extends EntityId , K extends HasRuleEngineProfile > TbMsg entityActionMsg ( E entity , I id , RuleNodeId ruleNodeId , String action , K profile ) {
try {
return entityActionMsg ( id , getActionMetaData ( ruleNodeId ) , JacksonUtil . toString ( JacksonUtil . valueToTree ( entity ) ) , action , profile ) ;
} catch ( IllegalArgumentException e ) {
throw new RuntimeException ( "Failed to process " + id . getEntityType ( ) . name ( ) . toLowerCase ( ) + " " + action + " msg: " + e ) ;
}
}
@Deprecated ( since = "3.5.2" , forRemoval = true )
private < I extends EntityId , K extends HasRuleEngineProfile > TbMsg entityActionMsg ( I id , TbMsgMetaData msgMetaData , String msgData , String action , K profile ) {
String defaultQueueName = null ;
RuleChainId defaultRuleChainId = null ;
if ( profile ! = null ) {
defaultQueueName = profile . getDefaultQueueName ( ) ;
defaultRuleChainId = profile . getDefaultRuleChainId ( ) ;
}
return TbMsg . newMsg ( defaultQueueName , action , id , msgMetaData , msgData , defaultRuleChainId , null ) ;
}
public < E , I extends EntityId , K extends HasRuleEngineProfile > TbMsg entityActionMsg ( E entity , I id , RuleNodeId ruleNodeId , TbMsgType actionMsgType , K profile ) {
try {
return entityActionMsg ( id , getActionMetaData ( ruleNodeId ) , JacksonUtil . toString ( JacksonUtil . valueToTree ( entity ) ) , actionMsgType , profile ) ;
@ -878,10 +910,17 @@ class DefaultTbContext implements TbContext {
}
private static String getFailureMessage ( Throwable th ) {
if ( th = = null ) {
return null ;
String failureMessage ;
if ( th ! = null ) {
if ( ! StringUtils . isEmpty ( th . getMessage ( ) ) ) {
failureMessage = th . getMessage ( ) ;
} else {
failureMessage = th . getClass ( ) . getSimpleName ( ) ;
}
} else {
failureMessage = null ;
}
return StringUtils . isNotEmpty ( th . getMessage ( ) ) ? th . getMessage ( ) : th . getClass ( ) . getSimpleName ( ) ;
return failureMessage ;
}
private class SimpleTbQueueCallback implements TbQueueCallback {