100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
WIP Rules Editor
robindekkers2be1bfa401ef
diff --git a/ui/component/or-rules-editor/package.json b/ui/component/or-rules-editor/package.jsonindex a42c82269..0cb29fbf5 100644--- a/ui/component/or-rules-editor/package.json+++ b/ui/component/or-rules-editor/package.json@@ -11,11 +11,13 @@"author": "OpenRemote","license": "AGPL-3.0-or-later","dependencies": {+ "@openremote/core": "^1.0.0","@openremote/or-input": "^1.0.0","@openremote/or-select": "^1.0.0","@openremote/rest": "^1.0.0",- "@openremote/core": "^1.0.0",+ "@types/lodash-es": "^4.17.3","lit-element": "^2.0.1",+ "lodash-es": "^4.17.11","moment": "^2.24.0"},"devDependencies": {diff --git a/ui/component/or-rules-editor/src/index.ts b/ui/component/or-rules-editor/src/index.tsindex 2ffe664b2..a8e63457a 100644--- a/ui/component/or-rules-editor/src/index.ts+++ b/ui/component/or-rules-editor/src/index.ts@@ -1,7 +1,8 @@import {html, LitElement, property, customElement} from 'lit-element';import '@openremote/or-select';-import {Rule, JsonRulesetDefinition, Ruleset} from '@openremote/model';+import '@openremote/or-icon';+import {Rule, JsonRulesetDefinition, Ruleset, RulesetLang, TenantRuleset} from '@openremote/model';import openremote from "@openremote/core";import rest from "@openremote/rest";@@ -11,17 +12,21 @@ import './or-rule-then';import './or-rule-header';import {style} from "./style";+import findIndex from 'lodash-es/findIndex';const ruleModel:Rule = {name: "",when: undefined,then: undefined};-//TODO rules should be based on the type Rule-const rulesetModel:Ruleset = {- name: "",++const rulesetModel:TenantRuleset = {+ name: "New Rule",type: "tenant",- rules: ""+ lang: RulesetLang.JSON,+ realm: openremote.getRealm(),+ accessPublicRead: true,+ rules: JSON.stringify({rules: [ruleModel]})};@customElement('or-rules-editor')@@ -38,22 +43,25 @@ class OrRulesEditor extends LitElement {return html`<div class="rule-editor-container"><side-menu class="bg-white shadow">- <or-rule-list .rules="${this.rulesets}"></or-rule-list>+ <or-rule-list .rulesets="${this.rulesets}" .ruleset="${this.ruleset}"></or-rule-list><div class="bottom-toolbar">- <icon @click="${this.createRule}">[plus]</icon>+ <icon class="small-icon" @click="${this.deleteRule}"><or-icon icon="delete"></or-icon></icon>+ <icon class="small-icon" @click="${this.createRule}"><or-icon icon="plus"></or-icon></icon></div></side-menu>- ${this.ruleset && this.ruleset.rules ? html`+ ${this.ruleset ? html`<or-body>- <or-rule-header class="bg-white shadow" .rule="${this.ruleset}"></or-rule-header>- <div class="content">- <or-rule-when .rule="${this.ruleset.rules}"></or-rule-when>- <or-rule-then .rule="${this.ruleset.rules}"></or-rule-then>- </div>+ <or-rule-header class="bg-white shadow" .ruleset="${this.ruleset}"></or-rule-header>++ ${this.rule ? html`+ <div class="content">+ <or-rule-when .rule="${this.rule}"></or-rule-when>+ <or-rule-then .rule="${this.rule}"></or-rule-then>+ </div>+ ` : ``}+</or-body>- ` : html`-- `}+ ` : html``}</div>`;}@@ -62,10 +70,10 @@ class OrRulesEditor extends LitElement {private value: number = 0;@property({type: Array})- private rulesets?: Ruleset[];+ private rulesets?: TenantRuleset[];@property({type: Object})- private ruleset?: Ruleset;+ private ruleset?: TenantRuleset;@property({type: Array})private rules?: Rule[];@@ -78,91 +86,105 @@ class OrRulesEditor extends LitElement {super();this.readRules();this.addEventListener('rules:set-active-rule', this.setActiveRule);+ this.addEventListener('rules:create-rule', this.createRule);this.addEventListener('rules:update-rule', this.updateRule);this.addEventListener('rules:delete-rule', this.deleteRule);}readRules() {- rest.api.RulesResource.getTenantRulesets(openremote.config.realm).then((response: any) => {-- // TODO remove this when rules are in the project- response.data = [{- "type": "tenant",- "id": 1001,- "version": 6,- "createdOn": 1551799275058,- "lastModified": 1552321167681,- "name": "Geofence Test",- "enabled": true,- "rules": "{\n \"rules\": [\n {\n \"name\": \"Rich Home\",\n \"when\": {\n \"asset\": {\n \"types\": [\n {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"urn:openremote:asset:console\"\n }\n ],\n \"attributes\": {\n \"predicates\": [\n {\n \"name\": {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"location\"\n },\n \"value\": {\n \"predicateType\": \"radial\",\n \"radius\": 100,\n \"lat\": 51.765106,\n \"lng\": -2.233860\n }\n }\n ]\n }\n }\n },\n \"then\": [\n {\n \"action\": \"notification\",\n \"target\": {\n \"useAssetsFromWhen\": true\n },\n \"notification\": {\n \"name\": \"Rich Home\",\n \"message\": {\n \"type\": \"push\",\n \"title\": \"Duurzaamheid\",\n \"body\": \"Samenwerken aan duurzaamheid\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/samen-werken-aan-duurzaamheid\"\n },\n \"buttons\": [\n {\n \"title\": \"Ja, in app\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/samen-werken-aan-duurzaamheid\"\n }\n },\n {\n \"title\": \"Ja, in browser\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/samen-werken-aan-duurzaamheid\",\n \"openInBrowser\": true\n }\n }\n ]\n }\n }\n }\n ],\n \"reset\": {\n \"triggerNoLongerMatches\": true\n }\n },\n {\n \"name\": \"Pierre Home\",\n \"when\": {\n \"asset\": {\n \"types\": [\n {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"urn:openremote:asset:console\"\n }\n ],\n \"attributes\": {\n \"predicates\": [\n {\n \"name\": {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"location\"\n },\n \"value\": {\n \"predicateType\": \"radial\",\n \"radius\": 100,\n \"lat\": 51.416214,\n \"lng\": 5.483527\n }\n }\n ]\n }\n }\n },\n \"then\": [\n {\n \"action\": \"notification\",\n \"target\": {\n \"useAssetsFromWhen\": true\n },\n \"notification\": {\n \"name\": \"Pierre Home\",\n \"message\": {\n \"type\": \"push\",\n \"title\": \"Welkom thuis\",\n \"body\": \"Meer weten over de Vestdijk?\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/beleef-de-vestdijk\"\n },\n \"buttons\": [\n {\n \"title\": \"Ja, in app\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/beleef-de-vestdijk\"\n }\n },\n {\n \"title\": \"Ja, in browser\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/beleef-de-vestdijk\",\n \"openInBrowser\": true\n }\n }\n ]\n }\n }\n }\n ],\n \"reset\": {\n \"triggerNoLongerMatches\": true\n }\n },\n {\n \"name\": \"Vestdijk\",\n \"when\": {\n \"asset\": {\n \"types\": [\n {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"urn:openremote:asset:console\"\n }\n ],\n \"attributes\": {\n \"predicates\": [\n {\n \"name\": {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"location\"\n },\n \"value\": {\n \"predicateType\": \"radial\",\n \"radius\": 100,\n \"lat\": 51.438803,\n \"lng\": 5.481697\n }\n }\n ]\n }\n }\n },\n \"then\": [\n {\n \"action\": \"notification\",\n \"target\": {\n \"useAssetsFromWhen\": true\n },\n \"notification\": {\n \"name\": \"Vestdijk\",\n \"message\": {\n \"type\": \"push\",\n \"title\": \"Beleef de Vestdijk\",\n \"body\": \"Meer weten over de Vestdijk?\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/beleef-de-vestdijk\"\n },\n \"buttons\": [\n {\n \"title\": \"Ja, in app\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/beleef-de-vestdijk\"\n }\n },\n {\n \"title\": \"Ja, in browser\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/beleef-de-vestdijk\",\n \"openInBrowser\": true\n }\n }\n ]\n }\n }\n }\n ],\n \"reset\": {\n \"triggerNoLongerMatches\": true\n }\n },\n {\n \"name\": \"Boeiende Binnenstad\",\n \"when\": {\n \"asset\": {\n \"types\": [\n {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"urn:openremote:asset:console\"\n }\n ],\n \"attributes\": {\n \"predicates\": [\n {\n \"name\": {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"location\"\n },\n \"value\": {\n \"predicateType\": \"radial\",\n \"radius\": 100,\n \"lat\": 51.439887,\n \"lng\": 5.478430\n }\n }\n ]\n }\n }\n },\n \"then\": [\n {\n \"action\": \"notification\",\n \"target\": {\n \"useAssetsFromWhen\": true\n },\n \"notification\": {\n \"name\": \"Boeiende Binnenstad\",\n \"message\": {\n \"type\": \"push\",\n \"title\": \"Boeiende Binnenstad\",\n \"body\": \"Wat jullie vonden van de bestrating\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/boeiende-binnenstad/stapsgewijs-naar-een-nieuwe-inrichting\"\n },\n \"buttons\": [\n {\n \"title\": \"Bekijk, in app\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/boeiende-binnenstad/stapsgewijs-naar-een-nieuwe-inrichting\"\n }\n },\n {\n \"title\": \"Bekijk, in browser\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/boeiende-binnenstad/stapsgewijs-naar-een-nieuwe-inrichting\",\n \"openInBrowser\": true\n }\n }\n ]\n }\n }\n }\n ],\n \"reset\": {\n \"triggerNoLongerMatches\": true\n }\n },\n{\n \"name\": \"Michael Home\",\n \"when\": {\n \"asset\": {\n \"types\": [\n {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"urn:openremote:asset:console\"\n }\n ],\n \"attributes\": {\n \"predicates\": [\n {\n \"name\": {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"location\"\n },\n \"value\": {\n \"predicateType\": \"radial\",\n \"radius\": 100,\n \"lat\": 51.687741,\n \"lng\": 5.286913\n }\n }\n ]\n }\n }\n },\n \"then\": [\n {\n \"action\": \"notification\",\n \"target\": {\n \"useAssetsFromWhen\": true\n },\n \"notification\": {\n \"name\": \"Rich Home\",\n \"message\": {\n \"type\": \"push\",\n \"title\": \"Michael\",\n \"body\": \"Welcome to Den Bosch\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/samen-werken-aan-duurzaamheid\"\n },\n \"buttons\": [\n {\n \"title\": \"Ja, in app\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/samen-werken-aan-duurzaamheid\"\n }\n },\n {\n \"title\": \"Ja, in browser\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/samen-werken-aan-duurzaamheid\",\n \"openInBrowser\": true\n }\n }\n ]\n }\n }\n }\n ],\n \"reset\": {\n \"triggerNoLongerMatches\": true\n }\n }\n ]\n}",- "lang": "JSON",- "status": "DEPLOYED",- "realm": "eindhoven",- "accessPublicRead": true- }, {- "type": "tenant",- "id": 1002,- "version": 6,- "createdOn": 1551799275058,- "lastModified": 1552321167681,- "name": "Geofence Test 2",- "enabled": true,- "rules": "{\n \"rules\": [\n {\n \"name\": \"Rich Home\",\n \"when\": {\n \"asset\": {\n \"types\": [\n {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"urn:openremote:asset:console\"\n }\n ],\n \"attributes\": {\n \"predicates\": [\n {\n \"name\": {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"location\"\n },\n \"value\": {\n \"predicateType\": \"radial\",\n \"radius\": 100,\n \"lat\": 51.765106,\n \"lng\": -2.233860\n }\n }\n ]\n }\n }\n },\n \"then\": [\n {\n \"action\": \"notification\",\n \"target\": {\n \"useAssetsFromWhen\": true\n },\n \"notification\": {\n \"name\": \"Rich Home\",\n \"message\": {\n \"type\": \"push\",\n \"title\": \"Duurzaamheid\",\n \"body\": \"Samenwerken aan duurzaamheid\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/samen-werken-aan-duurzaamheid\"\n },\n \"buttons\": [\n {\n \"title\": \"Ja, in app\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/samen-werken-aan-duurzaamheid\"\n }\n },\n {\n \"title\": \"Ja, in browser\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/samen-werken-aan-duurzaamheid\",\n \"openInBrowser\": true\n }\n }\n ]\n }\n }\n }\n ],\n \"reset\": {\n \"triggerNoLongerMatches\": true\n }\n },\n {\n \"name\": \"Pierre Home\",\n \"when\": {\n \"asset\": {\n \"types\": [\n {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"urn:openremote:asset:console\"\n }\n ],\n \"attributes\": {\n \"predicates\": [\n {\n \"name\": {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"location\"\n },\n \"value\": {\n \"predicateType\": \"radial\",\n \"radius\": 100,\n \"lat\": 51.416214,\n \"lng\": 5.483527\n }\n }\n ]\n }\n }\n },\n \"then\": [\n {\n \"action\": \"notification\",\n \"target\": {\n \"useAssetsFromWhen\": true\n },\n \"notification\": {\n \"name\": \"Pierre Home\",\n \"message\": {\n \"type\": \"push\",\n \"title\": \"Welkom thuis\",\n \"body\": \"Meer weten over de Vestdijk?\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/beleef-de-vestdijk\"\n },\n \"buttons\": [\n {\n \"title\": \"Ja, in app\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/beleef-de-vestdijk\"\n }\n },\n {\n \"title\": \"Ja, in browser\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/beleef-de-vestdijk\",\n \"openInBrowser\": true\n }\n }\n ]\n }\n }\n }\n ],\n \"reset\": {\n \"triggerNoLongerMatches\": true\n }\n },\n {\n \"name\": \"Vestdijk\",\n \"when\": {\n \"asset\": {\n \"types\": [\n {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"urn:openremote:asset:console\"\n }\n ],\n \"attributes\": {\n \"predicates\": [\n {\n \"name\": {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"location\"\n },\n \"value\": {\n \"predicateType\": \"radial\",\n \"radius\": 100,\n \"lat\": 51.438803,\n \"lng\": 5.481697\n }\n }\n ]\n }\n }\n },\n \"then\": [\n {\n \"action\": \"notification\",\n \"target\": {\n \"useAssetsFromWhen\": true\n },\n \"notification\": {\n \"name\": \"Vestdijk\",\n \"message\": {\n \"type\": \"push\",\n \"title\": \"Beleef de Vestdijk\",\n \"body\": \"Meer weten over de Vestdijk?\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/beleef-de-vestdijk\"\n },\n \"buttons\": [\n {\n \"title\": \"Ja, in app\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/beleef-de-vestdijk\"\n }\n },\n {\n \"title\": \"Ja, in browser\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/beleef-de-vestdijk\",\n \"openInBrowser\": true\n }\n }\n ]\n }\n }\n }\n ],\n \"reset\": {\n \"triggerNoLongerMatches\": true\n }\n },\n {\n \"name\": \"Boeiende Binnenstad\",\n \"when\": {\n \"asset\": {\n \"types\": [\n {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"urn:openremote:asset:console\"\n }\n ],\n \"attributes\": {\n \"predicates\": [\n {\n \"name\": {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"location\"\n },\n \"value\": {\n \"predicateType\": \"radial\",\n \"radius\": 100,\n \"lat\": 51.439887,\n \"lng\": 5.478430\n }\n }\n ]\n }\n }\n },\n \"then\": [\n {\n \"action\": \"notification\",\n \"target\": {\n \"useAssetsFromWhen\": true\n },\n \"notification\": {\n \"name\": \"Boeiende Binnenstad\",\n \"message\": {\n \"type\": \"push\",\n \"title\": \"Boeiende Binnenstad\",\n \"body\": \"Wat jullie vonden van de bestrating\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/boeiende-binnenstad/stapsgewijs-naar-een-nieuwe-inrichting\"\n },\n \"buttons\": [\n {\n \"title\": \"Bekijk, in app\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/boeiende-binnenstad/stapsgewijs-naar-een-nieuwe-inrichting\"\n }\n },\n {\n \"title\": \"Bekijk, in browser\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/boeiende-binnenstad/stapsgewijs-naar-een-nieuwe-inrichting\",\n \"openInBrowser\": true\n }\n }\n ]\n }\n }\n }\n ],\n \"reset\": {\n \"triggerNoLongerMatches\": true\n }\n },\n{\n \"name\": \"Michael Home\",\n \"when\": {\n \"asset\": {\n \"types\": [\n {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"urn:openremote:asset:console\"\n }\n ],\n \"attributes\": {\n \"predicates\": [\n {\n \"name\": {\n \"predicateType\": \"string\",\n \"match\": \"EXACT\",\n \"value\": \"location\"\n },\n \"value\": {\n \"predicateType\": \"radial\",\n \"radius\": 100,\n \"lat\": 51.687741,\n \"lng\": 5.286913\n }\n }\n ]\n }\n }\n },\n \"then\": [\n {\n \"action\": \"notification\",\n \"target\": {\n \"useAssetsFromWhen\": true\n },\n \"notification\": {\n \"name\": \"Rich Home\",\n \"message\": {\n \"type\": \"push\",\n \"title\": \"Michael\",\n \"body\": \"Welcome to Den Bosch\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/samen-werken-aan-duurzaamheid\"\n },\n \"buttons\": [\n {\n \"title\": \"Ja, in app\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/samen-werken-aan-duurzaamheid\"\n }\n },\n {\n \"title\": \"Ja, in browser\",\n \"action\": {\n \"url\": \"https://www.eindhoven.nl/projecten/samen-werken-aan-duurzaamheid\",\n \"openInBrowser\": true\n }\n }\n ]\n }\n }\n }\n ],\n \"reset\": {\n \"triggerNoLongerMatches\": true\n }\n }\n ]\n}",- "lang": "JSON",- "status": "DEPLOYED",- "realm": "eindhoven",- "accessPublicRead": true- }];-+ rest.api.RulesResource.getTenantRulesets(openremote.config.realm, { fullyPopulate: true }).then((response: any) => {if (response && response.data) {- response.data.forEach((rulesSet: any) => {- let parsedRules = JSON.parse(rulesSet.rules);- // Only get first element of rules for now- // TODO how should this work if a ruleset has multiple rules?- rulesSet.rules = parsedRules.rules[0];- });+ this.rulesets = response.data;+ if(this.ruleset && this.rulesets) {+ const index = findIndex(this.rulesets, ['id', this.ruleset.id]);+ let updatedRuleset;++ // ID is not found when a new ruleset is added+ if(index > 0) {+ updatedRuleset = this.rulesets[index];+ } else {+ updatedRuleset = this.rulesets[this.rulesets.length-1];+ }++ this.ruleset = updatedRuleset;+ this.computeRuleset();+ }+ this.requestUpdate();}-- this.rulesets = response.data;}).catch((reason: any) => {console.log("Error:" + reason);-});}createRule() {if(this.rulesets) {- this.rulesets = [...this.rulesets, rulesetModel]- }- // rest.api.RulesResource.createTenantRuleset(this.ruleset).then((response:any)=> {- // console.log(response);- // });+ const newRule = rulesetModel;+ this.rulesets = [...this.rulesets, rulesetModel];+ this.ruleset = newRule;+ this.computeRuleset();++ rest.api.RulesResource.createTenantRuleset(newRule).then((response:any)=> {+ this.readRules();+ });+ }}updateRule(e:any) {- this.ruleset = e.detail.rule;- console.log(this.ruleset);- // if(this.ruleset){- // this.ruleset.rules = JSON.stringify(this.rules);- // console.log(this.ruleset);- // rest.api.RulesResource.updateTenantRuleset(this.ruleset.id, this.ruleset).then((response:any)=> {- // console.log(response);- // });- // }+ this.ruleset = e.detail.ruleset;+ if(this.ruleset && this.ruleset.id && this.rule){+ delete this.ruleset.lastModified;+ delete this.ruleset.createdOn;+ delete this.ruleset.status;+ delete this.ruleset.error;++ // Parse rule to string of array of rules+ this.rule.name = this.ruleset.name;+ this.ruleset.rules = JSON.stringify({"rules": [this.rule] });++ //this.ruleset.rules = JSON.stringify(this.rules);+ rest.api.RulesResource.updateTenantRuleset(this.ruleset.id, this.ruleset).then((response:any)=> {+ this.readRules();+ });+ }}deleteRule() {+ if(this.ruleset && this.ruleset.id) {+ rest.api.RulesResource.deleteTenantRuleset(this.ruleset.id).then((response: any) => {++ if(this.rulesets && this.ruleset) {+ const index = findIndex(this.rulesets, ['id', this.ruleset.id]);+ this.rulesets.splice(index, 1);+ this.rulesets = [...this.rulesets];+ this.ruleset = undefined;+ this.rule = undefined;+ this.requestUpdate();+ }++ });+ }+ }++ computeRuleset () {+ if(this.ruleset && this.ruleset.rules){+ this.rule = JSON.parse(this.ruleset.rules).rules[0];+ }+ else if(this.ruleset && !this.ruleset.rules){+ this.rule = ruleModel;+ this.rule.name = this.ruleset.name;+ }+ this.requestUpdate();}setActiveRule(e:any) {- this.ruleset = e.detail.rule;- console.log(this.ruleset);- this.requestUpdate();+ this.ruleset = e.detail.ruleset;++ this.computeRuleset();}}diff --git a/ui/component/or-rules-editor/src/or-rule-header/index.ts b/ui/component/or-rules-editor/src/or-rule-header/index.tsindex cea19477b..5b787d3f4 100644--- a/ui/component/or-rules-editor/src/or-rule-header/index.ts+++ b/ui/component/or-rules-editor/src/or-rule-header/index.ts@@ -4,14 +4,15 @@ import '../selects/or-select-asset-attribute';import '@openremote/or-input';import '@openremote/or-select';-import {Rule} from '@openremote/model';+import '@openremote/or-icon';+import {TenantRuleset} from '@openremote/model';import {style} from './style';@customElement('or-rule-header')class OrRuleHeader extends LitElement {@property({type: Object})- rule?: Rule;+ ruleset?: TenantRuleset;@property({type: Boolean})editmode: boolean = false;@@ -26,19 +27,19 @@ class OrRuleHeader extends LitElement {return html`<div class="rule-container">- ${this.editmode ? html`+<div class="layout horizontal">- <or-input type="text" value="${this.rule ? this.rule.name : null}"></or-input>- <span @click="${this.toggleEditmode}">edit</span>+ ${this.editmode ? html`+ <or-input type="text" value="${this.ruleset ? this.ruleset.name : null}"></or-input>+ ` : html`+ <h1>${this.ruleset ? this.ruleset.name : ''}</h1>+ `}++ <span @click="${this.toggleEditmode}"><or-icon style="margin:10px;" icon="pencil-outline"></or-icon></span>+ <span class="rule-status ${this.ruleset && this.ruleset.enabled ? 'bg-green' : 'bg-red'}"></span>+ <or-toggle @click="${this.toggleEnabled}">toggle</or-toggle><button @click="${this.updateRule}">opslaan</button></div>- ` : html`- <div class="layout horizontal">- <h1>${this.rule ? this.rule.name : ''}</h1>- <span @click="${this.toggleEditmode}">edit</span>- <button @click="${this.updateRule}">opslaan</button>- </div>- `}</div>`;}@@ -50,21 +51,30 @@ class OrRuleHeader extends LitElement {changeName (e:any) {const value = e.detail.value;- if(this.rule) {- this.rule.name = value;- console.log(this.rule);+ if(this.ruleset) {+ this.ruleset.name = value;+ console.log(this.ruleset);}}updateRule() {let event = new CustomEvent('rules:update-rule', {- detail: {rule: this.rule},+ detail: {ruleset: this.ruleset},bubbles: true,composed: true});this.dispatchEvent(event);}+ toggleEnabled () {+ if(this.ruleset){+ this.ruleset.enabled = !this.ruleset.enabled;++ this.updateRule();+ this.requestUpdate();+ }+ }+toggleEditmode() {this.editmode = !this.editmode;}diff --git a/ui/component/or-rules-editor/src/or-rule-header/style.ts b/ui/component/or-rules-editor/src/or-rule-header/style.tsindex c84a55da8..9450c711a 100644--- a/ui/component/or-rules-editor/src/or-rule-header/style.ts+++ b/ui/component/or-rules-editor/src/or-rule-header/style.ts@@ -1,11 +1,10 @@import {css} from 'lit-element';-+// language=CSSexport const style = css`:host {display: block;width: 100%;- height: 300px;z-index: 1;}@@ -35,4 +34,23 @@ export const style = css`color: var(--app-white-color, #FFF);font-weight: bold;}++ or-input {+ padding: 10px;+ }++ .rule-status {+ width: 8px;+ height: 8px;+ border-radius: 8px;+ margin: 15px+ }++ .bg-green {+ background-color: green;+ }++ .bg-red {+ background-color: red+ }`;\ No newline at end of filediff --git a/ui/component/or-rules-editor/src/or-rule-list/index.ts b/ui/component/or-rules-editor/src/or-rule-list/index.tsindex 0d2c3d84e..9aea645da 100644--- a/ui/component/or-rules-editor/src/or-rule-list/index.ts+++ b/ui/component/or-rules-editor/src/or-rule-list/index.ts@@ -1,9 +1,12 @@import {html, LitElement, property, PropertyValues} from 'lit-element';-import {Rule} from '@openremote/model';+import {TenantRuleset} from '@openremote/model';class OrRulesList extends LitElement {@property({type: Array})- rules: Rule[] = [];+ rulesets: TenantRuleset[] = [];++ @property({type: Object})+ ruleset?: TenantRuleset;protected render() {@@ -38,6 +41,7 @@ class OrRulesList extends LitElement {cursor: pointer;}+ .list-item[selected],.list-item:hover {border-left-color: var(--app-primary-color);background-color: var(--app-lightgrey-color);@@ -57,14 +61,18 @@ class OrRulesList extends LitElement {.bg-green {background-color: green;}++ .bg-red {+ background-color: red+ }</style><div class="list-container">- ${this.rules && this.rules.map((rule: Rule, index:number) => {+ ${this.rulesets && this.rulesets.map((ruleset: TenantRuleset, index:number) => {return html`- <a class="d-flex list-item" @click="${()=> this.setActiveRule(this.rules[index])}">- <span class="rule-status ${!rule.name ? '' : 'bg-green'}"></span>+ <a ?selected="${this.ruleset && ruleset.id === this.ruleset.id}" class="d-flex list-item" @click="${()=> this.setActiveRule(this.rulesets[index])}">+ <span class="rule-status ${ruleset.enabled ? 'bg-green' : 'bg-red'}"></span><div class="flex">- <span>${rule.name}</span>+ <span>${ruleset.name}</span></div></a>`@@ -73,10 +81,10 @@ class OrRulesList extends LitElement {`;}- setActiveRule (rule:Rule) {+ setActiveRule (ruleset:TenantRuleset) {this.requestUpdate();let event = new CustomEvent('rules:set-active-rule', {- detail: {rule: rule},+ detail: {ruleset: ruleset},bubbles: true,composed: true});diff --git a/ui/component/or-rules-editor/src/or-rule-then-condition/index.ts b/ui/component/or-rules-editor/src/or-rule-then-condition/index.tsindex ecc72eca2..6f542db6e 100644--- a/ui/component/or-rules-editor/src/or-rule-then-condition/index.ts+++ b/ui/component/or-rules-editor/src/or-rule-then-condition/index.ts@@ -4,11 +4,14 @@ import '../selects/or-select-asset-attribute';import '@openremote/or-input';import '@openremote/or-select';+import '@openremote/or-icon';import {style} from './style';-import {RuleActionUnion} from "@openremote/model";+import {RuleActionUnion, RuleActionWriteAttribute} from "@openremote/model";++// TODO use create select option to@customElement('or-rule-then-condition')class OrRuleThenCondition extends LitElement {@@ -22,23 +25,36 @@ class OrRuleThenCondition extends LitElement {return html`<div class="rule-container">- <or-select-asset-type></or-select-asset-type>- <or-select-asset-attribute type="${this.condition}"></or-select-asset-attribute>- <or-select-operator type="${this.condition}"></or-select-operator>- <or-input type="text"></or-input>+ ${this.condition ? html`+ <or-select-asset-attribute disabled type="${this.condition}" value="${this.condition.attributeName}"></or-select-asset-attribute>+ <or-select-operator disabled type="${this.condition}" value="EQUAL"></or-select-operator>++ <or-input type="text" value="${this.condition.value}"></or-input>+ ` : ``}</div>`;}@property({type: Object})- condition?: RuleActionUnion;+ condition?: RuleActionWriteAttribute;@property({type: Number})private value: number = 0;+ setValue(e:any) {+ const value = e.detail.value;++ if(this.condition && this.condition.value) {+ this.condition.value = value;+ this.requestUpdate();+ }+ }+constructor() {super();++ this.addEventListener('or-input:changed', this.setValue);}diff --git a/ui/component/or-rules-editor/src/or-rule-then-condition/style.ts b/ui/component/or-rules-editor/src/or-rule-then-condition/style.tsindex 4d4d8e8d8..c5fac89bb 100644--- a/ui/component/or-rules-editor/src/or-rule-then-condition/style.ts+++ b/ui/component/or-rules-editor/src/or-rule-then-condition/style.ts@@ -8,9 +8,10 @@ export const style = css`.rule-container {display: flex;- padding: 20px;+ margin-bottom: 10px;+ padding: 15px 20px;- border-radius: 3px;- border: 1px solid var(--lightgrey-color);+ border-radius: 5px;+ border: 1px solid var(--app-lightgrey-color, #F5F5F5);}`;\ No newline at end of filediff --git a/ui/component/or-rules-editor/src/or-rule-then/index.ts b/ui/component/or-rules-editor/src/or-rule-then/index.tsindex 7b72cd460..cbcbf74f9 100644--- a/ui/component/or-rules-editor/src/or-rule-then/index.ts+++ b/ui/component/or-rules-editor/src/or-rule-then/index.ts@@ -1,13 +1,25 @@-import {html, LitElement, property, customElement} from 'lit-element';+import {html, LitElement, property, customElement, PropertyValues} from 'lit-element';import {style} from './style';-import {Rule, RuleTrigger, RuleActionUnion} from "@openremote/model";+import {Rule, RuleTrigger, RuleActionUnion, NewAssetQuery, BaseAssetQueryMatch} from "@openremote/model";import '../or-rule-then-condition';let defaultThen:RuleActionUnion[] = [];++const defaultAssetType:NewAssetQuery = {+ "types": [{+ "predicateType": "string",+ "match": BaseAssetQueryMatch.EXACT,+ "value": "urn:openremote:asset:kmar:flight"+ }]+};+let defaultThenCondition:RuleActionUnion = {- action: "write-attribute"+ action: "write-attribute",+ attributeName: 'profileName',+ value: 'profiel naam',+ target: { "useAssetsFromWhen": true}};@customElement('or-rule-then')@@ -31,10 +43,9 @@ class OrRuleThen extends LitElement {<span class="rule-additional">&</span>`}) : ``}- <a class="button-add" @click="${this.addThenRule}">+</a>+ <a class="button-add" @click="${this.addThenCondition}">+</a></div></div>-`;}@@ -45,10 +56,16 @@ class OrRuleThen extends LitElement {super();}- addThenRule () {+ protected updated(_changedProperties: PropertyValues): void {+ super.updated(_changedProperties);+ if(this.rule && !this.rule.then) {+ this.rule.then = defaultThen;+ }+ }++ addThenCondition () {if(this.rule && !this.rule.then) {this.rule.then = defaultThen;- console.log(this.rule);}diff --git a/ui/component/or-rules-editor/src/or-rule-then/style.ts b/ui/component/or-rules-editor/src/or-rule-then/style.tsindex 9ed929a33..fa6f88819 100644--- a/ui/component/or-rules-editor/src/or-rule-then/style.ts+++ b/ui/component/or-rules-editor/src/or-rule-then/style.ts@@ -20,15 +20,18 @@ export const style = css`.button-add {display: block;cursor: pointer;- padding: 15px 40px;+ padding: 10px 40px;font-size: 20px;font-weight: bold;- box-sizing: border-box;+ height: 32px;+ box-sizing: content-box;border: 3px dashed var(--app-lightgrey-color, #e4e4e4);}.rule-additional {- padding: 15px 20px;+ height: 38px;+ line-height: 38px;+ padding: 10px 20px;font-size: 20px;font-weight: bold;}diff --git a/ui/component/or-rules-editor/src/or-rule-when-condition/index.ts b/ui/component/or-rules-editor/src/or-rule-when-condition/index.tsindex dec31634a..5069e0bae 100644--- a/ui/component/or-rules-editor/src/or-rule-when-condition/index.ts+++ b/ui/component/or-rules-editor/src/or-rule-when-condition/index.ts@@ -4,6 +4,7 @@ import '../selects/or-select-operator';import '@openremote/or-input';import '@openremote/or-select';+import '@openremote/or-icon';import {style} from './style';import {AttributePredicate} from "@openremote/model";@@ -23,19 +24,30 @@ class OrRuleWhenCondition extends LitElement {<div class="rule-container">${this.predicate ? html`<or-select-asset-type>- <i class="material-icons">- ${this.predicate.name ? this.predicate.name.value : 'defaultIcon'}- </i>+ <or-icon icon="${this.predicate.name ? 'airplane' : 'airplane'}">+ </or-icon></or-select-asset-type>+${this.predicate.name ? html`<or-select-asset-attribute icon="${this.predicate}" value="${this.predicate.name.value}"></or-select-asset-attribute>- ` : ``}-- ${this.predicate.value && this.predicate.value.predicateType === 'string' ? html`- <or-select-operator type="${this.predicate.value.predicateType}" value="${this.predicate.value.match}"></or-select-operator>-- <or-input type="text" value="${this.predicate.value.value}"></or-input>+ ${this.predicate.value && this.predicate.value.predicateType === 'string' ? html`+ ${this.predicate.name.value ? html`+ <or-select-operator type="${this.predicate.value.predicateType}" value="${this.predicate.value.match}"></or-select-operator>+ ` : ``}++ ${this.predicate.value.match ? html`+ <or-input type="text" value="${this.predicate.value.value ? this.predicate.value.value : ''}"></or-input>+ ` : ``}++ ${this.predicate.value.value ? html`+ <a @click="${this.deleteCondition}">+ <or-icon class="small-icon" icon="delete"></or-icon>+ </a>+ ` : ``}++ ` : ``}+` : ``}` : ``}@@ -46,6 +58,10 @@ class OrRuleWhenCondition extends LitElement {@property({type: Object})predicate?: AttributePredicate;+ @property({type: Number})+ index?: number;++// setAssetType(value:string) {// const assetType = {// "predicateType": "string",@@ -65,6 +81,7 @@ class OrRuleWhenCondition extends LitElement {if(this.predicate && this.predicate.name) {this.predicate.name.value = value;console.log(this.predicate);+ this.requestUpdate();}}@@ -73,6 +90,7 @@ class OrRuleWhenCondition extends LitElement {if(this.predicate && this.predicate.value && this.predicate.value.predicateType === 'string') {this.predicate.value.match = value;console.log(this.predicate);+ this.requestUpdate();}}@@ -82,9 +100,22 @@ class OrRuleWhenCondition extends LitElement {if(this.predicate && this.predicate.value && this.predicate.value.predicateType === 'string') {this.predicate.value.value = value;console.log(this.predicate);+ this.requestUpdate();}}+ deleteCondition (){++ let event = new CustomEvent('when-condition:delete', {+ detail: { index: this.index },+ bubbles: true,+ composed: true });++ this.dispatchEvent(event);++ this.requestUpdate();+ }+constructor() {super();diff --git a/ui/component/or-rules-editor/src/or-rule-when-condition/style.ts b/ui/component/or-rules-editor/src/or-rule-when-condition/style.tsindex 3643fb0ab..2eebff3ed 100644--- a/ui/component/or-rules-editor/src/or-rule-when-condition/style.ts+++ b/ui/component/or-rules-editor/src/or-rule-when-condition/style.ts@@ -1,6 +1,6 @@import {css} from 'lit-element';-+// language=CSSexport const style = css`:host {display: block;@@ -8,9 +8,12 @@ export const style = css`.rule-container {display: flex;- padding: 20px;+ margin-bottom: 10px;+ padding: 15px 20px;- border-radius: 3px;- border: 1px solid var(--grey-color);+ border-radius: 5px;+ border: 1px solid var(--app-lightgrey-color, #F5F5F5);}++`;\ No newline at end of filediff --git a/ui/component/or-rules-editor/src/or-rule-when/index.ts b/ui/component/or-rules-editor/src/or-rule-when/index.tsindex f26644ad6..b3b1848e0 100644--- a/ui/component/or-rules-editor/src/or-rule-when/index.ts+++ b/ui/component/or-rules-editor/src/or-rule-when/index.ts@@ -1,13 +1,25 @@-import {html, LitElement, property, customElement} from 'lit-element';+import {html, LitElement, property, customElement, PropertyValues} from 'lit-element';import {style} from './style';-import {Rule, RuleTrigger, AttributePredicate, BaseAssetQueryMatch} from "@openremote/model";+import {Rule, RuleTrigger, AttributePredicate, BaseAssetQueryMatch, NewAssetQuery} from "@openremote/model";import '../or-rule-when-condition';++const ruleModel:Rule = {+ name: "",+ when: undefined,+ then: undefined+};+const defaultWhenCondition:RuleTrigger = {"asset": {- "types": [],+ "types": [{+ "predicateType": "string",+ "match": BaseAssetQueryMatch.EXACT,+ "value": "urn:openremote:asset:kmar:flight"+ }+ ],"attributes": {"predicates": []}@@ -20,9 +32,7 @@ const defaultPredicate:AttributePredicate = {match: BaseAssetQueryMatch.EXACT},value: {- predicateType: "string",- match: BaseAssetQueryMatch.EXACT,- value: ""+ predicateType: "string"}};@@ -43,14 +53,14 @@ class OrRuleWhen extends LitElement {<h3>Als..</h3><div class="rule-when-container bg-white shadow">${this.rule && this.rule.when && this.rule.when.asset && this.rule.when.asset.attributes && this.rule.when.asset.attributes.predicates ? html`- ${this.rule.when.asset.attributes.predicates.map((predicate:AttributePredicate) => {+ ${this.rule.when.asset.attributes.predicates.map((predicate:AttributePredicate, index) => {return html`- <or-rule-when-condition .predicate="${predicate}"></or-rule-when-condition>+ <or-rule-when-condition index="${index}" .predicate="${predicate}"></or-rule-when-condition><span class="rule-additional">&</span>`})}`: ``}- <a class="button-add" @click="${this.addWhenRule}">+</a>+ <a class="button-add" @click="${this.addWhenCondition}">+</a></div></div>`;@@ -64,9 +74,18 @@ class OrRuleWhen extends LitElement {constructor() {super();+ this.addEventListener('when-condition:delete', this.deleteWhenCondition);++ }++ protected updated(_changedProperties: PropertyValues): void {+ super.updated(_changedProperties);+ if (!this.rule) {+ this.rule = ruleModel;+ }}- addWhenRule () {+ addWhenCondition () {if(this.rule && !this.rule.when) {this.rule.when = defaultWhenCondition;}@@ -78,5 +97,14 @@ class OrRuleWhen extends LitElement {}+ 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);+ this.requestUpdate();+ }+ }}diff --git a/ui/component/or-rules-editor/src/or-rule-when/style.ts b/ui/component/or-rules-editor/src/or-rule-when/style.tsindex 71c2da382..15bb5c5fc 100644--- a/ui/component/or-rules-editor/src/or-rule-when/style.ts+++ b/ui/component/or-rules-editor/src/or-rule-when/style.ts@@ -2,7 +2,6 @@ import {css} from 'lit-element';// language=CSSexport const style = css`-.rule-content-section {flex-grow: 1;flex-basis: 1;@@ -20,15 +19,18 @@ export const style = css`.button-add {display: block;cursor: pointer;- padding: 15px 40px;+ padding: 10px 40px;font-size: 20px;- font-weight: bold;- box-sizing: border-box;+ font-weight: bold;+ height: 32px;+ box-sizing: content-box;border: 3px dashed var(--app-lightgrey-color, #e4e4e4);}.rule-additional {- padding: 15px 20px;+ height: 38px;+ line-height: 38px;+ padding: 10px 20px;font-size: 20px;font-weight: bold;}diff --git a/ui/component/or-rules-editor/src/selects/or-select-asset-attribute/index.ts b/ui/component/or-rules-editor/src/selects/or-select-asset-attribute/index.tsindex ed8116270..d19dc1b9a 100644--- a/ui/component/or-rules-editor/src/selects/or-select-asset-attribute/index.ts+++ b/ui/component/or-rules-editor/src/selects/or-select-asset-attribute/index.ts@@ -19,14 +19,17 @@ class OrSelectAssetAttribute extends LitElement {@property({type: String})value: any;+ @property({type: Boolean})+ disabled: boolean = false;+protected render() {// TODO types should be based on rules-configreturn html`- <select id="or-select-asset-attribute" @change="${this.onChange}">+ <select ?disabled="${this.disabled}" id="or-select-asset-attribute" @change="${this.onChange}">${this.attributeDescriptors ? this.attributeDescriptors.map((attribute:AttributeDescriptor) => {return html`- <option value="${attribute.name}">${attribute.name}</option>+ <option ?selected="${attribute.name === this.value}" value="${attribute.name}">${attribute.name}</option>`}) : ``}</select>@@ -53,6 +56,8 @@ class OrSelectAssetAttribute extends LitElement {// TODO what is the attribute name of profile flights?this.attributeDescriptors = [{ name: 'flightProfile', valueType: AttributeValueType.STRING },+ { name: 'profileName', valueType: AttributeValueType.STRING },+ { name: 'profileColor', valueType: AttributeValueType.STRING },{ name: 'airportIata', valueType: AttributeValueType.STRING },{ name: 'airlineIata', valueType: AttributeValueType.STRING },{ name: 'originRegion', valueType: AttributeValueType.STRING },diff --git a/ui/component/or-rules-editor/src/selects/or-select-operator/index.ts b/ui/component/or-rules-editor/src/selects/or-select-operator/index.tsindex 878fa27c2..0e59fae03 100644--- a/ui/component/or-rules-editor/src/selects/or-select-operator/index.ts+++ b/ui/component/or-rules-editor/src/selects/or-select-operator/index.ts@@ -10,11 +10,14 @@ class OrRuleWhen extends LitElement {@property({type: String})value?: BaseAssetQueryMatch;+ @property({type: Boolean})+ disabled: boolean = false;+protected render() {return html`${this.type ? html`- <select id="or-select-operator" @change="${this.onChange}">+ <select ?disabled="${this.disabled}" id="or-select-operator" @change="${this.onChange}"><option ?selected="${this.value === BaseAssetQueryMatch.EXACT}" value="${BaseAssetQueryMatch.EXACT}">=</option><option ?selected="${this.value === BaseAssetQueryMatch.NOT_EXACT}" value="${BaseAssetQueryMatch.NOT_EXACT}">!=</option>${this.type === AttributeValueType.NUMBER ? html`diff --git a/ui/component/or-rules-editor/src/style.ts b/ui/component/or-rules-editor/src/style.tsindex 262f4d503..abd0a3fe2 100644--- a/ui/component/or-rules-editor/src/style.ts+++ b/ui/component/or-rules-editor/src/style.ts@@ -9,7 +9,7 @@ export const style = css`--bg-white: #FFFFFF;--bg-grey: #F5F5F5;- --app-lightgrey-color: #e4e4e4;+ --app-lightgrey-color: #f2f2f2;}.rule-editor-container {@@ -37,30 +37,30 @@ export const style = css`display: flex;flex-direction: column;}-+or-rule-list {flex-grow: 1;}-+.bottom-toolbar {display: flex;-+border-top: 1px solid var(--app-lightgrey-color);}-+icon {cursor: pointer;- padding: 10px;+ padding: 5px;margin: 10px;border: 1px solid var(--app-lightgrey-color);border-radius: 3px;}-+icon:hover {background-color: var(--app-primary-color);color: var(--app-white-color, #FFF);}-+or-body,.content {display: flex;@@ -70,11 +70,11 @@ export const style = css`or-body {flex-direction: column;}-+.content {flex-direction: row;}-+or-rule-then,or-rule-when {display: flex;@@ -83,4 +83,9 @@ export const style = css`background-color: var(--app-lightgrey-color);max-width: 50%;}++ .small-icon {+ --or-icon-height: 16px;+ }+`;\ No newline at end of file