Skip to content
Permalink
Browse files

Updated rules editor to work with latest JSON rules model

  • Loading branch information...
richturner committed May 10, 2019
1 parent e83dcbd commit a811f859f61ab2d59e80b3f7825257e46d7cea91
@@ -8,7 +8,8 @@ import {
RuleActionUnion,
RuleActionUpdateAttributeUpdateAction,
RuleTrigger,
AttributePredicate
AttributePredicate,
RuleCondition
} from "@openremote/model";
import {airlines, airports, countries, languages, regions} from "./resources";

@@ -49,7 +50,49 @@ export const rulesEditorConfig = {
export const ruleTemplate: Rule = {
name: "",
when: undefined,
then: undefined
then: undefined,
otherwise: [
{
action: "update-attribute",
updateAction: RuleActionUpdateAttributeUpdateAction.DELETE,
attributeName: "flightProfiles",
key: "%RULESET_ID%"
}
],
onStop: [
{
action: "update-attribute",
updateAction: RuleActionUpdateAttributeUpdateAction.DELETE,
attributeName: "flightProfiles",
key: "%RULESET_ID%",
target: {
assets: {
types: [
{
predicateType: "string",
match: BaseAssetQueryMatch.EXACT,
value: "urn:openremote:asset:kmar:flight"
}
],
attributes: {
predicates: [
{
name: {
predicateType: "string",
match: BaseAssetQueryMatch.EXACT,
value: "flightProfiles"
},
value: {
predicateType: "object-value-key",
key: "%RULESET_ID%"
}
}
]
}
}
}
}
]
};

export const rulesetTemplate: TenantRuleset = {
@@ -61,7 +104,6 @@ export const rulesetTemplate: TenantRuleset = {
rules: JSON.stringify({rules: [ruleTemplate]})
};


export const defaultAssetType: NewAssetQuery = {
types: [{
predicateType: "string",
@@ -78,24 +120,27 @@ export const defaultThenCondition: RuleActionUnion = {
value: {
profileName: "%RULESET_NAME%",
profileColor: "orange"
},
target: { useAssetsFromWhen: true}
}
};

export const defaultThen: RuleActionUnion[] = [defaultThenCondition];

export const defaultWhenCondition: RuleTrigger = {
asset: {
types: [{
predicateType: "string",
match: BaseAssetQueryMatch.EXACT,
value: "urn:openremote:asset:kmar:flight"
}
],
attributes: {
predicates: []
export const defaultWhenCondition: RuleCondition<RuleTrigger> = {
predicates: [
{
assets: {
types: [{
predicateType: "string",
match: BaseAssetQueryMatch.EXACT,
value: "urn:openremote:asset:kmar:flight"
}
],
attributes: {
predicates: []
}
}
}
}
]
};

export const defaultPredicate: AttributePredicate = {
@@ -109,4 +154,4 @@ export const defaultPredicate: AttributePredicate = {
match: BaseAssetQueryMatch.EXACT
}

};
};
@@ -24,7 +24,6 @@ class OrRuleWhen extends LitElement {
@property({type: Array})
public predicates?: AttributePredicate[] = [];


constructor() {
super();
this.addEventListener("when-condition:delete", this.deleteWhenCondition);
@@ -37,10 +36,10 @@ class OrRuleWhen extends LitElement {
<div class="rule-content-section">
<div class="rule-when-container bg-white shadow">
<strong>Als..</strong>
${this.rule && this.rule.when && this.rule.when.asset && this.rule.when.asset.types && this.rule.when.asset.attributes && this.rule.when.asset.attributes.predicates ? html`
${this.rule.when.asset.attributes.predicates.map((predicate: AttributePredicate, index) => {
${this.rule && this.rule.when && this.rule.when.predicates && this.rule.when.predicates.length > 0 && this.rule.when.predicates[0].assets && this.rule.when.predicates[0].assets.types && this.rule.when.predicates[0].assets.attributes && this.rule.when.predicates[0].assets.attributes.predicates ? html`
${this.rule.when.predicates[0].assets.attributes.predicates.map((predicate: AttributePredicate, index) => {
return html`
<or-rule-when-condition index="${index}" .assetType="${this.rule!.when!.asset!.types![0].value}" .predicate="${predicate}"></or-rule-when-condition>
<or-rule-when-condition index="${index}" .assetType="${this.rule!.when!.predicates![0]!.assets!.types![0].value}" .predicate="${predicate}"></or-rule-when-condition>
`; })}
` : ``}
@@ -59,7 +58,7 @@ class OrRuleWhen extends LitElement {
super.updated(_changedProperties);
if (this.rule && !this.rule.when) {
this.rule.when = cloneDeep(defaultWhenCondition);
if (this.rule!.when!.asset!.attributes!.predicates!.length === 0) {
if (this.rule.when.predicates![0].assets!.attributes!.predicates!.length === 0) {
this.addPredicate();
}
this.requestUpdate();
@@ -77,19 +76,19 @@ class OrRuleWhen extends LitElement {
}

private addPredicate() {
if (this.rule && this.rule.when && this.rule.when.asset && this.rule.when.asset.attributes && this.rule.when.asset.attributes.predicates) {
if (this.rule && this.rule.when && this.rule.when.predicates && this.rule.when.predicates.length > 0 && this.rule.when.predicates[0].assets && this.rule.when.predicates[0].assets.attributes && this.rule.when.predicates[0].assets.attributes.predicates) {
const newPredicate = cloneDeep(defaultPredicate);

this.rule.when.asset.attributes.predicates.push(newPredicate);
this.rule.when.predicates[0].assets.attributes.predicates.push(newPredicate);
}
}

private deleteWhenCondition(e: any) {

const index = e.detail.index;

if (this.rule && this.rule.when && this.rule.when.asset && this.rule.when.asset.attributes && this.rule.when.asset.attributes.predicates) {
this.rule.when.asset.attributes.predicates.splice(index, 1);
if (this.rule && this.rule.when && this.rule.when.predicates && this.rule.when.predicates.length > 0 && this.rule.when.predicates[0].assets && this.rule.when.predicates[0].assets.attributes && this.rule.when.predicates[0].assets.attributes.predicates) {
this.rule.when.predicates[0].assets.attributes.predicates.splice(index, 1);
this.requestUpdate();
}
}

0 comments on commit a811f85

Please sign in to comment.
You can’t perform that action at this time.