Search in sources :

Example 1 with AssetRuleset

use of org.openremote.model.rules.AssetRuleset in project openremote by openremote.

the class RulesetResourceImpl method getAssetRulesets.

@Override
public AssetRuleset[] getAssetRulesets(@BeanParam RequestParams requestParams, String assetId) {
    Asset asset = assetStorageService.find(assetId, false);
    if (asset == null)
        return new AssetRuleset[0];
    if (!isRealmAccessibleByUser(asset.getTenantRealm())) {
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    }
    if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), assetId)) {
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    }
    List<AssetRuleset> result = rulesetStorageService.findAssetRulesets(asset.getRealmId(), assetId);
    return result.toArray(new AssetRuleset[result.size()]);
}
Also used : AssetRuleset(org.openremote.model.rules.AssetRuleset) WebApplicationException(javax.ws.rs.WebApplicationException) Asset(org.openremote.model.asset.Asset)

Example 2 with AssetRuleset

use of org.openremote.model.rules.AssetRuleset in project openremote by openremote.

the class RulesetResourceImpl method deleteAssetRuleset.

@Override
public void deleteAssetRuleset(@BeanParam RequestParams requestParams, Long id) {
    AssetRuleset ruleset = rulesetStorageService.findById(AssetRuleset.class, id);
    if (ruleset == null) {
        return;
    }
    Asset asset = assetStorageService.find(ruleset.getAssetId(), false);
    if (asset == null) {
        throw new WebApplicationException(NOT_FOUND);
    }
    if (!isRealmAccessibleByUser(asset.getTenantRealm())) {
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    }
    if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), asset.getId())) {
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    }
    rulesetStorageService.delete(AssetRuleset.class, id);
}
Also used : AssetRuleset(org.openremote.model.rules.AssetRuleset) WebApplicationException(javax.ws.rs.WebApplicationException) Asset(org.openremote.model.asset.Asset)

Example 3 with AssetRuleset

use of org.openremote.model.rules.AssetRuleset in project openremote by openremote.

the class RulesetResourceImpl method getAssetRuleset.

@Override
public AssetRuleset getAssetRuleset(@BeanParam RequestParams requestParams, Long id) {
    AssetRuleset ruleset = rulesetStorageService.findById(AssetRuleset.class, id);
    if (ruleset == null) {
        throw new WebApplicationException(NOT_FOUND);
    }
    Asset asset = assetStorageService.find(ruleset.getAssetId(), false);
    if (asset == null) {
        throw new WebApplicationException(NOT_FOUND);
    }
    if (!isRealmAccessibleByUser(asset.getTenantRealm())) {
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    }
    if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), asset.getId())) {
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    }
    return ruleset;
}
Also used : AssetRuleset(org.openremote.model.rules.AssetRuleset) WebApplicationException(javax.ws.rs.WebApplicationException) Asset(org.openremote.model.asset.Asset)

Example 4 with AssetRuleset

use of org.openremote.model.rules.AssetRuleset in project openremote by openremote.

the class RulesDemoSetup method onStart.

@Override
public void onStart() throws Exception {
    KeycloakDemoSetup keycloakDemoSetup = setupService.getTaskOfType(KeycloakDemoSetup.class);
    ManagerDemoSetup managerDemoSetup = setupService.getTaskOfType(ManagerDemoSetup.class);
    LOG.info("Importing demo rulesets");
    // Apartment 1
    try (InputStream inputStream = RulesDemoSetup.class.getResourceAsStream("/demo/rules/DemoResidencePresenceDetection.groovy")) {
        String rules = IOUtils.toString(inputStream, Charset.forName("utf-8"));
        Ruleset ruleset = new AssetRuleset("Demo Residence - Presence Detection with motion and CO2 sensors", managerDemoSetup.apartment1Id, rules, Ruleset.Lang.GROOVY);
        apartmentActionsRulesetId = rulesetStorageService.merge(ruleset).getId();
    }
    try (InputStream inputStream = RulesDemoSetup.class.getResourceAsStream("/demo/rules/DemoResidenceVacationMode.groovy")) {
        String rules = IOUtils.toString(inputStream, Charset.forName("utf-8"));
        Ruleset ruleset = new AssetRuleset("Demo Residence - Vacation Mode", managerDemoSetup.apartment1Id, rules, Ruleset.Lang.GROOVY);
        apartmentActionsRulesetId = rulesetStorageService.merge(ruleset).getId();
    }
    try (InputStream inputStream = RulesDemoSetup.class.getResourceAsStream("/demo/rules/DemoResidenceAutoVentilation.groovy")) {
        String rules = IOUtils.toString(inputStream, Charset.forName("utf-8"));
        Ruleset ruleset = new AssetRuleset("Demo Residence - Auto Ventilation", managerDemoSetup.apartment1Id, rules, Ruleset.Lang.GROOVY);
        apartmentActionsRulesetId = rulesetStorageService.merge(ruleset).getId();
    }
    try (InputStream inputStream = RulesDemoSetup.class.getResourceAsStream("/demo/rules/DemoResidenceNotifyAlarmTrigger.groovy")) {
        String rules = IOUtils.toString(inputStream, Charset.forName("utf-8"));
        Ruleset ruleset = new AssetRuleset("Demo Residence - Notify Alarm Trigger", managerDemoSetup.apartment1Id, rules, Ruleset.Lang.GROOVY);
        apartmentActionsRulesetId = rulesetStorageService.merge(ruleset).getId();
    }
    try (InputStream inputStream = RulesDemoSetup.class.getResourceAsStream("/demo/rules/DemoResidenceSmartSwitch.groovy")) {
        String rules = IOUtils.toString(inputStream, Charset.forName("utf-8"));
        Ruleset ruleset = new AssetRuleset("Demo Residence - Smart Start Switch", managerDemoSetup.apartment1Id, rules, Ruleset.Lang.GROOVY);
        apartmentActionsRulesetId = rulesetStorageService.merge(ruleset).getId();
    }
    // Apartment 2
    try (InputStream inputStream = RulesDemoSetup.class.getResourceAsStream("/demo/rules/DemoResidenceAllLightsOff.js")) {
        String rules = IOUtils.toString(inputStream, Charset.forName("utf-8"));
        Ruleset ruleset = new AssetRuleset("Demo Residence - All Lights Off", managerDemoSetup.apartment2Id, rules, Ruleset.Lang.JAVASCRIPT);
        apartmentActionsRulesetId = rulesetStorageService.merge(ruleset).getId();
    }
}
Also used : Ruleset(org.openremote.model.rules.Ruleset) AssetRuleset(org.openremote.model.rules.AssetRuleset) AssetRuleset(org.openremote.model.rules.AssetRuleset) InputStream(java.io.InputStream)

Example 5 with AssetRuleset

use of org.openremote.model.rules.AssetRuleset in project openremote by openremote.

the class RulesetStorageService method mapResultTuple.

@SuppressWarnings("unchecked")
protected <T extends Ruleset> T mapResultTuple(Class<T> rulesetType, RulesetQuery query, ResultSet rs) throws SQLException {
    T ruleset;
    if (rulesetType == GlobalRuleset.class) {
        ruleset = (T) new GlobalRuleset();
    } else if (rulesetType == TenantRuleset.class) {
        TenantRuleset tenantRuleset = new TenantRuleset();
        tenantRuleset.setRealm(rs.getString("REALM"));
        tenantRuleset.setAccessPublicRead(rs.getBoolean("ACCESS_PUBLIC_READ"));
        ruleset = (T) tenantRuleset;
    } else if (rulesetType == AssetRuleset.class) {
        AssetRuleset assetRuleset = new AssetRuleset();
        assetRuleset.setAssetId(rs.getString("ASSET_ID"));
        assetRuleset.setRealm(rs.getString("REALM"));
        assetRuleset.setAccessPublicRead(rs.getBoolean("ACCESS_PUBLIC_READ"));
        ruleset = (T) assetRuleset;
    } else {
        throw new UnsupportedOperationException("Ruleset type not supported: " + rulesetType);
    }
    ruleset.setName(rs.getString("NAME"));
    ruleset.setId(rs.getLong("ID"));
    ruleset.setVersion(rs.getLong("VERSION"));
    ruleset.setLang(Ruleset.Lang.valueOf(rs.getString("RULES_LANG")));
    ruleset.setEnabled(rs.getBoolean("ENABLED"));
    ruleset.setLastModified(rs.getTimestamp("LAST_MODIFIED"));
    ruleset.setCreatedOn(rs.getTimestamp("CREATED_ON"));
    if (rs.getString("META") != null) {
        ruleset.setMeta(ValueUtil.parse(rs.getString("META"), MetaMap.class).orElse(null));
    }
    if (query.fullyPopulate) {
        ruleset.setRules(rs.getString("RULES"));
    }
    return ruleset;
}
Also used : GlobalRuleset(org.openremote.model.rules.GlobalRuleset) AssetRuleset(org.openremote.model.rules.AssetRuleset) TenantRuleset(org.openremote.model.rules.TenantRuleset)

Aggregations

AssetRuleset (org.openremote.model.rules.AssetRuleset)7 WebApplicationException (javax.ws.rs.WebApplicationException)4 Asset (org.openremote.model.asset.Asset)4 TenantRuleset (org.openremote.model.rules.TenantRuleset)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 InputStream (java.io.InputStream)1 GlobalRuleset (org.openremote.model.rules.GlobalRuleset)1 Ruleset (org.openremote.model.rules.Ruleset)1