Search in sources :

Example 1 with TbEmail

use of org.thingsboard.rule.engine.api.TbEmail in project thingsboard by thingsboard.

the class TbMsgToEmailNode method onMsg.

@Override
public void onMsg(TbContext ctx, TbMsg msg) {
    try {
        TbEmail email = convert(msg);
        TbMsg emailMsg = buildEmailMsg(ctx, msg, email);
        ctx.tellNext(emailMsg, SUCCESS);
    } catch (Exception ex) {
        log.warn("Can not convert message to email " + ex.getMessage());
        ctx.tellFailure(msg, ex);
    }
}
Also used : TbEmail(org.thingsboard.rule.engine.api.TbEmail) TbMsg(org.thingsboard.server.common.msg.TbMsg) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) TbNodeException(org.thingsboard.rule.engine.api.TbNodeException)

Example 2 with TbEmail

use of org.thingsboard.rule.engine.api.TbEmail in project thingsboard by thingsboard.

the class TbMsgToEmailNodeTest method msgCanBeConverted.

@Test
public void msgCanBeConverted() throws IOException {
    initWithScript();
    metaData.putValue("username", "oreo");
    metaData.putValue("userEmail", "user@email.io");
    metaData.putValue("name", "temp");
    metaData.putValue("passed", "5");
    metaData.putValue("count", "100");
    TbMsg msg = TbMsg.newMsg("USER", originator, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId);
    emailNode.onMsg(ctx, msg);
    ArgumentCaptor<TbMsg> msgCaptor = ArgumentCaptor.forClass(TbMsg.class);
    ArgumentCaptor<String> typeCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<EntityId> originatorCaptor = ArgumentCaptor.forClass(EntityId.class);
    ArgumentCaptor<TbMsgMetaData> metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class);
    ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class);
    verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture());
    assertEquals("SEND_EMAIL", typeCaptor.getValue());
    assertEquals(originator, originatorCaptor.getValue());
    assertEquals("oreo", metadataCaptor.getValue().getValue("username"));
    assertNotSame(metaData, metadataCaptor.getValue());
    TbEmail actual = new ObjectMapper().readValue(dataCaptor.getValue().getBytes(), TbEmail.class);
    TbEmail expected = TbEmail.builder().from("test@mail.org").to("user@email.io").subject("Hi oreo there").body("temp is to high. Current 5 and 100").build();
    assertEquals(expected, actual);
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbEmail(org.thingsboard.rule.engine.api.TbEmail) TbMsg(org.thingsboard.server.common.msg.TbMsg) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with TbEmail

use of org.thingsboard.rule.engine.api.TbEmail in project thingsboard by thingsboard.

the class TbSendEmailNode method onMsg.

@Override
public void onMsg(TbContext ctx, TbMsg msg) {
    try {
        validateType(msg.getType());
        TbEmail email = getEmail(msg);
        withCallback(ctx.getMailExecutor().executeAsync(() -> {
            sendEmail(ctx, msg, email);
            return null;
        }), ok -> ctx.tellSuccess(msg), fail -> ctx.tellFailure(msg, fail));
    } catch (Exception ex) {
        ctx.tellFailure(msg, ex);
    }
}
Also used : TbEmail(org.thingsboard.rule.engine.api.TbEmail) IOException(java.io.IOException) TbNodeException(org.thingsboard.rule.engine.api.TbNodeException)

Aggregations

TbEmail (org.thingsboard.rule.engine.api.TbEmail)3 IOException (java.io.IOException)2 TbNodeException (org.thingsboard.rule.engine.api.TbNodeException)2 TbMsg (org.thingsboard.server.common.msg.TbMsg)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Test (org.junit.Test)1 EntityId (org.thingsboard.server.common.data.id.EntityId)1 TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)1