Search in sources :

Example 21 with RuleChain

use of org.thingsboard.server.common.data.rule.RuleChain in project thingsboard by thingsboard.

the class EdgeServiceImpl method findMissingToRelatedRuleChains.

@Override
public String findMissingToRelatedRuleChains(TenantId tenantId, EdgeId edgeId) {
    List<RuleChain> edgeRuleChains = findEdgeRuleChains(tenantId, edgeId);
    List<RuleChainId> edgeRuleChainIds = edgeRuleChains.stream().map(IdBased::getId).collect(Collectors.toList());
    ObjectNode result = mapper.createObjectNode();
    for (RuleChain edgeRuleChain : edgeRuleChains) {
        List<RuleChainConnectionInfo> connectionInfos = ruleChainService.loadRuleChainMetaData(edgeRuleChain.getTenantId(), edgeRuleChain.getId()).getRuleChainConnections();
        if (connectionInfos != null && !connectionInfos.isEmpty()) {
            List<RuleChainId> connectedRuleChains = connectionInfos.stream().map(RuleChainConnectionInfo::getTargetRuleChainId).collect(Collectors.toList());
            List<String> missingRuleChains = new ArrayList<>();
            for (RuleChainId connectedRuleChain : connectedRuleChains) {
                if (!edgeRuleChainIds.contains(connectedRuleChain)) {
                    RuleChain ruleChainById = ruleChainService.findRuleChainById(tenantId, connectedRuleChain);
                    missingRuleChains.add(ruleChainById.getName());
                }
            }
            if (!missingRuleChains.isEmpty()) {
                ArrayNode array = mapper.createArrayNode();
                for (String missingRuleChain : missingRuleChains) {
                    array.add(missingRuleChain);
                }
                result.set(edgeRuleChain.getName(), array);
            }
        }
    }
    return result.toString();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) ArrayList(java.util.ArrayList) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Validator.validateString(org.thingsboard.server.dao.service.Validator.validateString) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) RuleChainConnectionInfo(org.thingsboard.server.common.data.rule.RuleChainConnectionInfo)

Example 22 with RuleChain

use of org.thingsboard.server.common.data.rule.RuleChain in project thingsboard by thingsboard.

the class EdgeServiceImpl method findEdgeRuleChains.

private List<RuleChain> findEdgeRuleChains(TenantId tenantId, EdgeId edgeId) {
    List<RuleChain> result = new ArrayList<>();
    PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE);
    PageData<RuleChain> pageData;
    do {
        pageData = ruleChainService.findRuleChainsByTenantIdAndEdgeId(tenantId, edgeId, pageLink);
        if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
            result.addAll(pageData.getData());
            if (pageData.hasNext()) {
                pageLink = pageLink.nextPageLink();
            }
        }
    } while (pageData != null && pageData.hasNext());
    return result;
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) Validator.validatePageLink(org.thingsboard.server.dao.service.Validator.validatePageLink)

Example 23 with RuleChain

use of org.thingsboard.server.common.data.rule.RuleChain in project thingsboard by thingsboard.

the class BaseRuleChainServiceTest method testGetDefaultEdgeRuleChains.

@Test
public void testGetDefaultEdgeRuleChains() throws Exception {
    RuleChainId ruleChainId = saveRuleChainAndSetAutoAssignToEdge("Default Edge Rule Chain 1");
    saveRuleChainAndSetAutoAssignToEdge("Default Edge Rule Chain 2");
    PageData<RuleChain> result = ruleChainService.findAutoAssignToEdgeRuleChainsByTenantId(tenantId, new PageLink(100));
    Assert.assertEquals(2, result.getData().size());
    ruleChainService.unsetAutoAssignToEdgeRuleChain(tenantId, ruleChainId);
    result = ruleChainService.findAutoAssignToEdgeRuleChainsByTenantId(tenantId, new PageLink(100));
    Assert.assertEquals(1, result.getData().size());
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) PageLink(org.thingsboard.server.common.data.page.PageLink) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Test(org.junit.Test)

Example 24 with RuleChain

use of org.thingsboard.server.common.data.rule.RuleChain in project thingsboard by thingsboard.

the class BaseRuleChainServiceTest method testDeleteRuleChain.

@Test
public void testDeleteRuleChain() {
    RuleChain ruleChain = new RuleChain();
    ruleChain.setTenantId(tenantId);
    ruleChain.setName("My RuleChain");
    RuleChain savedRuleChain = ruleChainService.saveRuleChain(ruleChain);
    RuleChain foundRuleChain = ruleChainService.findRuleChainById(tenantId, savedRuleChain.getId());
    Assert.assertNotNull(foundRuleChain);
    ruleChainService.deleteRuleChainById(tenantId, savedRuleChain.getId());
    foundRuleChain = ruleChainService.findRuleChainById(tenantId, savedRuleChain.getId());
    Assert.assertNull(foundRuleChain);
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) Test(org.junit.Test)

Example 25 with RuleChain

use of org.thingsboard.server.common.data.rule.RuleChain in project thingsboard by thingsboard.

the class BaseRuleChainServiceTest method testSaveRuleChainWithEmptyName.

@Test(expected = DataValidationException.class)
public void testSaveRuleChainWithEmptyName() {
    RuleChain ruleChain = new RuleChain();
    ruleChain.setTenantId(tenantId);
    ruleChainService.saveRuleChain(ruleChain);
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) Test(org.junit.Test)

Aggregations

RuleChain (org.thingsboard.server.common.data.rule.RuleChain)67 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)26 Test (org.junit.Test)20 RuleNode (org.thingsboard.server.common.data.rule.RuleNode)19 ArrayList (java.util.ArrayList)16 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)15 RuleChainMetaData (org.thingsboard.server.common.data.rule.RuleChainMetaData)14 ApiOperation (io.swagger.annotations.ApiOperation)13 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)13 Edge (org.thingsboard.server.common.data.edge.Edge)13 TenantId (org.thingsboard.server.common.data.id.TenantId)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)12 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)11 PageLink (org.thingsboard.server.common.data.page.PageLink)11 RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)10 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)10 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)9 EdgeId (org.thingsboard.server.common.data.id.EdgeId)8 List (java.util.List)7 Device (org.thingsboard.server.common.data.Device)7