Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

UI updates: fixed or-rules-editor to work with latest or components

Rich Turner636aafca1094
diff --git a/ui/component/core/src/asset-mixin.ts b/ui/component/core/src/asset-mixin.ts
index 8a5cac663..4147b772d 100644
--- a/ui/component/core/src/asset-mixin.ts
+++ b/ui/component/core/src/asset-mixin.ts
@@ -61,13 +61,14 @@ export const subscribe = (eventProviderFactory: EventProviderFactory) => <T exte
return;
}
+ const isAttributes = !!this._attributeRefs;
const ids: string[] | AttributeRef[] | undefined = this._attributeRefs ? this._attributeRefs : this._assetIds;
if (ids && ids.length > 0) {
- this._assetSubscriptionId = await eventProviderFactory.getEventProvider()!.subscribeAssetEvents(ids, true, (event) => this._onAssetEvent(event));
- this._attributeSubscriptionId = await eventProviderFactory.getEventProvider()!.subscribeAttributeEvents(ids, false, (event) => this._onAttributeEvent(event));
- } else if (this._attributeRefs && this._attributeRefs.length > 0) {
-
+ if (!isAttributes) {
+ this._assetSubscriptionId = await eventProviderFactory.getEventProvider()!.subscribeAssetEvents(ids, true, (event) => this._onAssetEvent(event));
+ }
+ this._attributeSubscriptionId = await eventProviderFactory.getEventProvider()!.subscribeAttributeEvents(ids, isAttributes, (event) => this._onAttributeEvent(event));
}
this.onStatusChange(EventProviderStatus.CONNECTED);
}
diff --git a/ui/component/core/src/event.ts b/ui/component/core/src/event.ts
index 0aa54de92..9ae0c414d 100644
--- a/ui/component/core/src/event.ts
+++ b/ui/component/core/src/event.ts
@@ -219,16 +219,16 @@ abstract class EventProviderImpl implements EventProvider {
eventType: "asset"
};
- if (ids && ids.length > 0) {
+ const isAttributeRef = ids && typeof ids[0] !== "string";
+ const assetIds = isAttributeRef ? (ids as AttributeRef[]).map((id) => id.entityId!) : ids as string[] | null;
+
+ if (assetIds && assetIds.length > 0) {
subscription.filter = {
filterType: "asset-id",
assetIds: ids
};
}
- const isAttributeRef = ids && typeof ids[0] !== "string";
- const assetIds = isAttributeRef ? (ids as AttributeRef[]).map((id) => id.entityId!) : ids as string[] | null;
-
let subscriptionId: string | null = null;
try {
@@ -278,7 +278,7 @@ abstract class EventProviderImpl implements EventProvider {
try {
subscriptionId = await this.subscribe(subscription, (evt) => {
- if(attributes) {
+ if (attributes) {
// Filter events for these attributes
const eventRef = evt.attributeState!.attributeRef!;
if (attributes.findIndex((attrRef) => eventRef.entityId === attrRef.entityId && eventRef.attributeName === attrRef.attributeName) >= 0) {
diff --git a/ui/component/core/src/index.ts b/ui/component/core/src/index.ts
index ff769a234..bf3ec18d5 100644
--- a/ui/component/core/src/index.ts
+++ b/ui/component/core/src/index.ts
@@ -7,7 +7,8 @@ import {EventProvider, EventProviderFactory, EventProviderStatus, WebSocketEvent
import i18next from "i18next";
import i18nextXhr from "i18next-xhr-backend";
import {AssetDescriptor, AttributeDescriptor, AttributeValueDescriptor, MetaItemDescriptor, Asset, AssetAttribute,
- MetaItem} from "@openremote/model";
+ MetaItem,
+ Attribute} from "@openremote/model";
import * as Util from "./util";
export declare type KeycloakPromise<T> = {
@@ -185,7 +186,11 @@ export class AssetModelUtil {
});
}
- public static getAttributeValueDescriptorFromAsset(name: string, assetType?: string, attributeName?: string) : AttributeValueDescriptor | undefined {
+ public static getAttributeValueDescriptorFromAsset(name: string | undefined, assetType?: string, attributeName?: string): AttributeValueDescriptor | undefined {
+ if (!name) {
+ return;
+ }
+
if (attributeName) {
const attributeDescriptor = this.getAttributeDescriptorFromAsset(attributeName, assetType);
if (attributeDescriptor) {
@@ -216,15 +221,18 @@ export class AssetModelUtil {
return attributeValueDescriptor1.name === attributeValueDescriptor2.name && attributeValueDescriptor1.valueType === attributeValueDescriptor2.valueType;
}
- public static getMetaValue(metaItemUrn: string | MetaItemDescriptor, metaItems: MetaItem[] | undefined): any {
- if (!metaItems) {
- return;
- }
-
+ public static getMetaValue(metaItemUrn: string | MetaItemDescriptor, attribute: Attribute | undefined, descriptor: AttributeDescriptor | undefined): any {
const urn = typeof metaItemUrn === "string" ? metaItemUrn : (metaItemUrn as MetaItemDescriptor).urn;
- const mi = metaItems.find((mi) => mi.name === urn);
- return mi ? mi.value : undefined;
+ if (attribute && attribute.meta) {
+ const metaItem = attribute.meta.find((mi) => mi.name === urn);
+ return metaItem ? metaItem.value : undefined;
+ }
+
+ if (descriptor && descriptor.metaItemDescriptors) {
+ const metaItemDescriptor = descriptor.metaItemDescriptors.find((mid) => mid.urn === urn);
+ return metaItemDescriptor ? metaItemDescriptor.initialValue : undefined;
+ }
}
public static getMetaInitialValueFromMetaDescriptors(metaItemUrn: MetaItemDescriptor | string, metaItemDescriptors: MetaItemDescriptor[] | undefined): any | undefined {
diff --git a/ui/component/or-attribute-input/src/index.ts b/ui/component/or-attribute-input/src/index.ts
index c2f0bf466..157a9b305 100644
--- a/ui/component/or-attribute-input/src/index.ts
+++ b/ui/component/or-attribute-input/src/index.ts
@@ -3,14 +3,14 @@ import i18next from "i18next";
import {translate} from "@openremote/or-translate/dist/translate-mixin";
import {
AssetAttribute,
+ Attribute,
AttributeDescriptor,
AttributeEvent,
AttributeRef,
AttributeValueDescriptor,
AttributeValueType,
MetaItemType,
- ValueType,
- Attribute
+ ValueType
} from "@openremote/model";
import openremote, {AssetModelUtil} from "@openremote/core";
import {subscribe} from "@openremote/core/dist/asset-mixin";
@@ -18,21 +18,26 @@ import "@openremote/or-select";
import "@openremote/or-input";
import {InputType, OrInput, OrInputChangedEvent} from "@openremote/or-input";
-export function getAttributeLabel(attribute: Attribute, fallback?: string): string {
- const labelMetaValue = AssetModelUtil.getMetaValue(MetaItemType.LABEL, attribute.meta);
- return i18next.t([attribute!.name!, fallback || labelMetaValue || attribute!.name!]);
+export function getAttributeLabel(attribute: Attribute | undefined, descriptor: AttributeDescriptor | undefined, fallback?: string): string {
+ if (!attribute && !descriptor) {
+ return fallback || "";
+ }
+
+ const labelMetaValue = AssetModelUtil.getMetaValue(MetaItemType.LABEL, attribute, descriptor);
+ const name = attribute ? attribute.name : descriptor!.attributeName;
+ return i18next.t([name, fallback || labelMetaValue || name]);
}
// Allows consuming applications to provide custom UI for certain attributes; if the value is changed the custom control
// must call the valueChangeNotifier.
class AttributeInputProviders {
- public _providers: Array<(assetType: string, attribute: AssetAttribute, attributeDescriptor: AttributeDescriptor | undefined, valueDescriptor: AttributeValueDescriptor | undefined, valueChangeNotifier: (value: any | undefined) => void, readonly: boolean, disabled: boolean) => TemplateResult | undefined> = [];
+ public _providers: Array<(assetType: string | undefined, attribute: AssetAttribute | undefined, attributeDescriptor: AttributeDescriptor | undefined, valueDescriptor: AttributeValueDescriptor | undefined, valueChangeNotifier: (value: any | undefined) => void, readonly: boolean, disabled: boolean) => TemplateResult | undefined> = [];
- public addInputProvider(callback: (assetType: string, attribute: AssetAttribute, attributeDescriptor: AttributeDescriptor | undefined, valueDescriptor: AttributeValueDescriptor | undefined, valueChangeNotifier: (value: any | undefined) => void, readonly: boolean, disabled: boolean) => TemplateResult | undefined) {
+ public addInputProvider(callback: (assetType: string | undefined, attribute: AssetAttribute | undefined, attributeDescriptor: AttributeDescriptor | undefined, valueDescriptor: AttributeValueDescriptor | undefined, valueChangeNotifier: (value: any | undefined) => void, readonly: boolean, disabled: boolean) => TemplateResult | undefined) {
this._providers.push(callback);
}
- public removeInputProvider(callback: (assetType: string, attribute: AssetAttribute, attributeDescriptor: AttributeDescriptor | undefined, valueDescriptor: AttributeValueDescriptor | undefined, valueChangeNotifier: (value: any | undefined) => void, readonly: boolean, disabled: boolean) => TemplateResult | undefined) {
+ public removeInputProvider(callback: (assetType: string | undefined, attribute: AssetAttribute | undefined, attributeDescriptor: AttributeDescriptor | undefined, valueDescriptor: AttributeValueDescriptor | undefined, valueChangeNotifier: (value: any | undefined) => void, readonly: boolean, disabled: boolean) => TemplateResult | undefined) {
const i = this._providers.indexOf(callback);
if (i <= 0) {
this._providers.splice(i, 1);
@@ -43,6 +48,7 @@ class AttributeInputProviders {
const inputProviders = new AttributeInputProviders();
export default inputProviders;
+// TODO: Add support for attribute not found and attribute deletion/addition
@customElement("or-attribute-input")
export class OrAttributeInput extends subscribe(openremote)(translate(i18next)(LitElement)) {
@@ -93,15 +99,18 @@ export class OrAttributeInput extends subscribe(openremote)(translate(i18next)(L
protected _step?: number;
protected _label?: string;
protected _readonly?: boolean;
- protected _allowedValues?: string[] | [string, string][];
+ protected _options?: string[] | [string, string][];
protected _template?: TemplateResult;
@query("#or-input")
protected _input!: OrInput;
- shouldUpdate(_changedProperties: PropertyValues): boolean {
+ public shouldUpdate(_changedProperties: PropertyValues): boolean {
+ const shouldUpdate = super.shouldUpdate(_changedProperties);
- if (_changedProperties.has("attribute")) {
+ if (_changedProperties.has("attribute")
+ || _changedProperties.has("attributeRef")
+ || _changedProperties.has("assetType")) {
this._inputType = undefined;
this._valueInboundConverter = undefined;
this._valueOutboundConverter = undefined;
@@ -110,140 +119,150 @@ export class OrAttributeInput extends subscribe(openremote)(translate(i18next)(L
this._step = undefined;
this._label = undefined;
this._readonly = undefined;
- this._allowedValues = undefined;
+ this._options = undefined;
+
+ if (!this.attribute && !this.attributeRef) {
+ return shouldUpdate;
+ }
+
+ let attributeDescriptor: AttributeDescriptor | undefined;
+ let valueDescriptor: AttributeValueDescriptor | undefined;
+ let attributeName: string;
+ let valueType: string | undefined;
- if (this.assetType && this.attribute) {
+ if (this.attribute) {
+ attributeName = this.attribute.name!;
// Type is actually the name of the type descriptor not the type descriptor itself
- const type = this.attribute.type as string;
- const attributeDescriptor = AssetModelUtil.getAttributeDescriptorFromAsset(this.attribute.name!, this.assetType);
- const valueDescriptor = AssetModelUtil.getAttributeValueDescriptorFromAsset(type, this.assetType, this.attribute.name);
-
- for (const inputProvider of inputProviders._providers) {
- this._template = inputProvider(this.assetType, this.attribute, attributeDescriptor, valueDescriptor, (newValue: any) => this.onValueChange(newValue), this.readonly, this.disabled);
- if (this._template) {
- break;
- }
+ valueType = this.attribute.type as string;
+ } else {
+ attributeName = this.attributeRef!.attributeName!;
+ }
+
+ attributeDescriptor = AssetModelUtil.getAttributeDescriptorFromAsset(attributeName, this.assetType);
+ valueDescriptor = AssetModelUtil.getAttributeValueDescriptorFromAsset(valueType, this.assetType, attributeName);
+
+ for (const inputProvider of inputProviders._providers) {
+ this._template = inputProvider(this.assetType, this.attribute, attributeDescriptor, valueDescriptor, (newValue: any) => this.onValueChange(newValue), this.readonly, this.disabled);
+ if (this._template) {
+ break;
}
+ }
- if (!this._template) {
- if (valueDescriptor) {
-
- switch (valueDescriptor.name) {
- case AttributeValueType.GEO_JSON_POINT.name:
- break;
- case AttributeValueType.BOOLEAN.name:
- case AttributeValueType.SWITCH_TOGGLE.name:
- this._inputType = InputType.SWITCH;
- break;
- case AttributeValueType.NUMBER.name:
- this._inputType = InputType.NUMBER;
- break;
- case AttributeValueType.STRING.name:
- this._inputType = InputType.TEXT;
- break;
- case AttributeValueType.OBJECT.name:
- case AttributeValueType.ARRAY.name:
- this._inputType = InputType.JSON;
- break;
- case AttributeValueType.BRIGHTNESS_LUX.name:
- this._inputType = InputType.NUMBER;
- this._step = 1;
- this._min = 0;
- break;
- default:
- // Use value type
- switch (valueDescriptor.valueType) {
- case ValueType.STRING:
- this._inputType = InputType.TEXT;
- break;
- case ValueType.NUMBER:
- this._inputType = InputType.NUMBER;
- break;
- case ValueType.BOOLEAN:
- this._inputType = InputType.SWITCH;
- break;
- default:
- this._inputType = InputType.JSON;
- break;
- }
- break;
- }
+ if (!this._template) {
+ if (valueDescriptor) {
+
+ switch (valueDescriptor.name) {
+ case AttributeValueType.GEO_JSON_POINT.name:
+ break;
+ case AttributeValueType.BOOLEAN.name:
+ case AttributeValueType.SWITCH_TOGGLE.name:
+ this._inputType = InputType.SWITCH;
+ break;
+ case AttributeValueType.NUMBER.name:
+ this._inputType = InputType.NUMBER;
+ break;
+ case AttributeValueType.STRING.name:
+ this._inputType = InputType.TEXT;
+ break;
+ case AttributeValueType.OBJECT.name:
+ case AttributeValueType.ARRAY.name:
+ this._inputType = InputType.JSON;
+ break;
+ case AttributeValueType.BRIGHTNESS_LUX.name:
+ this._inputType = InputType.NUMBER;
+ this._step = 1;
+ break;
+ default:
+ // Use value type
+ switch (valueDescriptor.valueType) {
+ case ValueType.STRING:
+ this._inputType = InputType.TEXT;
+ break;
+ case ValueType.NUMBER:
+ this._inputType = InputType.NUMBER;
+ break;
+ case ValueType.BOOLEAN:
+ this._inputType = InputType.SWITCH;
+ break;
+ default:
+ this._inputType = InputType.JSON;
+ break;
+ }
+ break;
}
+ }
- if (!this._inputType) {
- const value = this.attribute.value;
-
- if (value) {
- if (typeof value === "number") {
- this._inputType = InputType.NUMBER;
- } else if (typeof value === "string") {
- this._inputType = InputType.TEXT;
- } else if (typeof value === "boolean") {
- this._inputType = InputType.SWITCH;
- } else {
- this._inputType = InputType.JSON;
- }
+ if (!this._inputType) {
+ const value = this.attribute ? this.attribute.value : undefined;
+
+ if (value) {
+ if (typeof value === "number") {
+ this._inputType = InputType.NUMBER;
+ } else if (typeof value === "string") {
+ this._inputType = InputType.TEXT;
+ } else if (typeof value === "boolean") {
+ this._inputType = InputType.SWITCH;
+ } else {
+ this._inputType = InputType.JSON;
}
}
+ }
- if (this._inputType) {
- // TODO: Update to use 'effective' meta so not dependent on meta items being defined on the asset itself
- this._min = AssetModelUtil.getMetaValue(MetaItemType.RANGE_MIN, this.attribute.meta);
- this._max = AssetModelUtil.getMetaValue(MetaItemType.RANGE_MAX, this.attribute.meta);
- this._label = getAttributeLabel(this.attribute);
- this._readonly = AssetModelUtil.getMetaValue(MetaItemType.READ_ONLY, this.attribute.meta);
- this._allowedValues = AssetModelUtil.getMetaValue(MetaItemType.ALLOWED_VALUES, this.attribute.meta);
-
- if (this._inputType === InputType.JSON) {
- const outConverter = this._valueOutboundConverter;
- const inConverter = this._valueInboundConverter;
-
- this._valueOutboundConverter = (value) => {
- if (outConverter) {
- // @ts-ignore
- value = outConverter(value);
- }
- if (value !== null && typeof(value) !== "string") {
- value = JSON.stringify(value, null, 2);
- this._valueInboundConverter = (value) => {
- if (inConverter) {
- // @ts-ignore
- value = inConverter(value);
- }
- if (typeof(value) === "string") {
- value = JSON.parse(value);
- }
- return value;
+ if (this._inputType) {
+ // TODO: Update to use 'effective' meta so not dependent on meta items being defined on the asset itself
+ this._min = AssetModelUtil.getMetaValue(MetaItemType.RANGE_MIN, this.attribute, attributeDescriptor);
+ this._max = AssetModelUtil.getMetaValue(MetaItemType.RANGE_MAX, this.attribute, attributeDescriptor);
+ this._label = getAttributeLabel(this.attribute, attributeDescriptor);
+ this._readonly = AssetModelUtil.getMetaValue(MetaItemType.READ_ONLY, this.attribute, attributeDescriptor);
+ this._options = AssetModelUtil.getMetaValue(MetaItemType.ALLOWED_VALUES, this.attribute, attributeDescriptor);
+
+ if (this._inputType === InputType.JSON) {
+ const outConverter = this._valueOutboundConverter;
+ const inConverter = this._valueInboundConverter;
+
+ this._valueOutboundConverter = (value) => {
+ if (outConverter) {
+ // @ts-ignore
+ value = outConverter(value);
+ }
+ if (value !== null && typeof(value) !== "string") {
+ value = JSON.stringify(value, null, 2);
+ this._valueInboundConverter = (v) => {
+ if (inConverter) {
+ // @ts-ignore
+ v = inConverter(v);
+ }
+ if (typeof(v) === "string") {
+ v = JSON.parse(v);
}
- }
+ return v;
+ };
+ }
- return value;
- };
- }
+ return value;
+ };
}
}
}
}
- return super.shouldUpdate(_changedProperties);
+ return shouldUpdate;
}
- updated(_changedProperties: PropertyValues): void {
+ public updated(_changedProperties: PropertyValues): void {
super.updated(_changedProperties);
- if (_changedProperties.has("attributeRef")) {
- this.attribute = undefined;
-
- if (this.attributeRef) {
+ if (_changedProperties.has("attribute") || _changedProperties.has("attributeRef")) {
+ if (this.attribute || this.attributeRef) {
this._loading = true;
- super.attributeRefs = [this.attributeRef];
+ super.attributeRefs = this.attribute ? [{entityId: this.attribute!.assetId!, attributeName: this.attribute!.name!}] : [this.attributeRef!];
} else {
super.attributeRefs = undefined;
}
}
}
- render() {
+ public render() {
if (this._loading) {
return html`
@@ -251,52 +270,57 @@ export class OrAttributeInput extends subscribe(openremote)(translate(i18next)(L
`;
}
- if ((!this.attribute && !this.attributeRef) || !this.assetType) {
- return html``;
- }
-
- if (!this.attribute) {
- return html`
- <div><or-translate value="notFound"></or-translate></div>
- `;
- }
-
if (this._template) {
return html`${this._template}`;
}
if (this._inputType) {
- let value: any = this.attribute.value;
+ let value: any = this.attribute ? this.attribute.value : undefined;
if (this._valueOutboundConverter) {
// Convert the value before setting it on the input
- value = this._valueOutboundConverter(this.attribute.value);
+ value = this._valueOutboundConverter(value);
}
- return html`<or-input .type="${this._inputType}" .label="${this._label}" .value="${value}" .allowedValues="${this._allowedValues}" .min="${this._min}" .max="${this._max}" .readonly="${this.readonly || this._readonly}" .disabled="${this.disabled}" .dense="${this.dense}" @or-input-changed="${(e: OrInputChangedEvent) => this.onValueChange(e.detail.value)}"></or-input>`;
+ return html`<or-input .type="${this._inputType}" .label="${this._label}" .value="${value}" .allowedValues="${this._options}" .min="${this._min}" .max="${this._max}" .readonly="${this.readonly || this._readonly}" .disabled="${this.disabled}" .dense="${this.dense}" @or-input-changed="${(e: OrInputChangedEvent) => this.onValueChange(e.detail.value)}"></or-input>`;
}
return html`<span>INPUT TYPE NOT IMPLEMENTED</span>`;
}
- protected onValueChange(newValue: any) {
+ public onAttributeEvent(event: AttributeEvent) {
+ const oldValue = this.attribute ? this.attribute.value : undefined;
if (this.attribute) {
+ this.attribute.value = event.attributeState!.value;
+ super.requestUpdate();
+ }
+ this.dispatchEvent(new OrInputChangedEvent(event.attributeState!.value, oldValue));
+ }
+
+ protected onValueChange(newValue: any) {
+ if (this.attribute || this.attributeRef) {
if (this._valueInboundConverter) {
newValue = this._valueInboundConverter(newValue);
}
if (!this.preventWrite && !this.disabled && !this.readonly) {
+
+ let attributeRef = this.attributeRef;
+
+ if (!this.attributeRef) {
+ attributeRef = {
+ entityId: this.attribute!.assetId,
+ attributeName: this.attribute!.name
+ };
+ }
+
super._sendEvent({
eventType: "attribute",
attributeState: {
- attributeRef: {
- entityId: this.attribute.assetId,
- attributeName: this.attribute.name
- },
+ attributeRef: attributeRef,
value: newValue
}
} as AttributeEvent);
- this.dispatchEvent(new OrInputChangedEvent(newValue, this.attribute.value));
}
}
}
diff --git a/ui/component/or-input/src/index.ts b/ui/component/or-input/src/index.ts
index 55fee0f73..bbc3cb95c 100644
--- a/ui/component/or-input/src/index.ts
+++ b/ui/component/or-input/src/index.ts
@@ -496,12 +496,16 @@ export class OrInput extends LitElement {
case InputType.JSON:
case InputType.NUMBER:
case InputType.RANGE:
- newValue = JSON.parse(newValue);
+ newValue = newValue !== "" ? JSON.parse(newValue) : null;
break;
case InputType.DATETIME:
- const date = Date.parse(newValue);
- const offset = (new Date()).getTimezoneOffset() * 60000;
- newValue = date + offset;
+ if (newValue === "") {
+ newValue = null;
+ } else {
+ const date = Date.parse(newValue);
+ const offset = (new Date()).getTimezoneOffset() * 60000;
+ newValue = date + offset;
+ }
break;
}
}
diff --git a/ui/component/or-rules-editor/src/index.ts b/ui/component/or-rules-editor/src/index.ts
index b5dcdadf8..946d4748e 100644
--- a/ui/component/or-rules-editor/src/index.ts
+++ b/ui/component/or-rules-editor/src/index.ts
@@ -109,7 +109,7 @@ export interface RulesConfig {
hideWhenAddGroup?: boolean;
hideWhenGroupOutline?: boolean;
};
- inputProvider?: (assetType: string, attributeName: string, attributeDescriptor: AttributeDescriptor | undefined, value: any | undefined, valueChangeNotifier: (value: any) => void, readonly: boolean) => TemplateResult | undefined;
+ inputProvider?: (assetType: string, attributeName: string, attributeDescriptor: AttributeDescriptor | undefined, value: any | undefined, valueChangeNotifier: (value: any) => void, readonly: boolean, disabled: boolean) => TemplateResult | undefined;
descriptors?: {
all?: RulesDescriptorSection;
when?: RulesDescriptorSection;
diff --git a/ui/component/or-rules-editor/src/or-rule-actions.ts b/ui/component/or-rules-editor/src/or-rule-actions.ts
index 08128913e..ee2cd38f7 100644
--- a/ui/component/or-rules-editor/src/or-rule-actions.ts
+++ b/ui/component/or-rules-editor/src/or-rule-actions.ts
@@ -11,9 +11,7 @@ import {
RuleActionUnion, AssetDescriptor, AttributeDescriptor, RuleActionWriteAttribute
} from "@openremote/model";
import i18next from "i18next";
-import {OrSelectChangedEvent} from "@openremote/or-select";
-import {OrAttributeInput} from "@openremote/or-attribute-input";
-import {OrInputChangedEvent} from "@openremote/or-input/dist";
+import {OrInputChangedEvent, InputType} from "@openremote/or-input";
@customElement("or-rule-actions")
class OrRuleActions extends LitElement {
@@ -58,7 +56,7 @@ class OrRuleActions extends LitElement {
if (action.target.ruleConditionTag) {
currentTarget = ActionTargetType.TAGGED_ASSETS;
if (!hideTargetOptions) {
- targetRhsTemplate = html`<or-select @or-select-changed="${(e: OrSelectChangedEvent) => this.setActionTriggerTag(action, e.detail.value)}" ?readonly="${this.readonly}" options="${this.getTriggerTags}" value="${action.target.ruleConditionTag}"></or-select>`;
+ targetRhsTemplate = html`<or-input type="${InputType.SELECT}" @or-input-changed="${(e: OrInputChangedEvent) => this.setActionTriggerTag(action, e.detail.value)}" ?readonly="${this.readonly}" .options="${this.getTriggerTags}" .value="${action.target.ruleConditionTag}"></or-input>`;
}
} else if (action.target.users) {
currentTarget = ActionTargetType.USERS;
@@ -77,7 +75,7 @@ class OrRuleActions extends LitElement {
const translatedAllowedTargets = allowedTargets.map((at) => [at, i18next.t(at)] as [string, string]);
targetTemplate = html`
<div class="rule-action-target">
- <span><or-translate value="target"></or-translate>: </span><or-select id="actionTargetSelect" @or-select-changed="${(e: OrSelectChangedEvent) => this.setActionTarget(action, e.detail.value as ActionTargetType)}" ?readonly="${this.readonly}" .options="${translatedAllowedTargets}" value="${currentTarget}"></or-select>
+ <span><or-translate value="target"></or-translate>: </span><or-select id="actionTargetSelect" type="${InputType.SELECT}" @or-input-changed="${(e: OrInputChangedEvent) => this.setActionTarget(action, e.detail.value as ActionTargetType)}" ?readonly="${this.readonly}" .options="${translatedAllowedTargets}" .value="${currentTarget}"></or-select>
${targetRhsTemplate}
</div>
`;
@@ -122,11 +120,11 @@ class OrRuleActions extends LitElement {
const attributeNames = attributeDescriptors.map((ad) => [ad.attributeName, i18next.t(ad.attributeName!)]);
const assetType = assetTypes.length > 0 ? assetTypes[0][0] : undefined;
- const attributeInput = this.config && this.config.inputProvider && action.attributeName ? this.config.inputProvider(assetType!, action.attributeName, attributeDescriptors.find((ad) => ad.attributeName === action.attributeName)!, action.value, (value: any) => this.setActionAttributeValue(action, value), this.readonly || false) : undefined;
+ const attributeInput = this.config && this.config.inputProvider && action.attributeName ? this.config.inputProvider(assetType!, action.attributeName, attributeDescriptors.find((ad) => ad.attributeName === action.attributeName)!, action.value, (value: any) => this.setActionAttributeValue(action, value), this.readonly || false, false) : undefined;
actionTemplate = html`
- <or-select @or-select-changed="${(e: OrSelectChangedEvent) => this.setActionAttributeName(action, e.detail.value)}" ?readonly="${this.readonly}" .options="${attributeNames}" .value="${action.attributeName}"></or-select>
- ${action.attributeName ? attributeInput ? attributeInput : html`<or-attribute-input @or-input-changed="${(e: OrInputChangedEvent) => this.setActionAttributeValue(action, e.detail.value)}" ?readonly="${this.readonly}" .assetType="${assetType}" .attributeName="${action.attributeName}" .value="${action.value}"></or-attribute-input>` : ``}
+ <or-input type="${InputType.SELECT}" @or-input-changed="${(e: OrInputChangedEvent) => this.setActionAttributeName(action, e.detail.value)}" ?readonly="${this.readonly}" .options="${attributeNames}" .value="${action.attributeName}"></or-input>
+ ${action.attributeName ? attributeInput ? attributeInput : html`<or-input @or-input-changed="${(e: OrInputChangedEvent) => this.setActionAttributeValue(action, e.detail.value)}" ?readonly="${this.readonly}" .assetType="${assetType}" .attributeName="${action.attributeName}" .value="${action.value}"></or-input>` : ``}
`;
if (action.action === "update-attribute" && !hideUpdateOptions) {
diff --git a/ui/component/or-rules-editor/src/or-rule-asset-query.ts b/ui/component/or-rules-editor/src/or-rule-asset-query.ts
index 7c906c7a2..26f18c5a5 100644
--- a/ui/component/or-rules-editor/src/or-rule-asset-query.ts
+++ b/ui/component/or-rules-editor/src/or-rule-asset-query.ts
@@ -11,7 +11,8 @@ import {
AttributeValueDescriptor,
AssetDescriptor,
LogicGroupOperator,
- LogicGroup
+ LogicGroup,
+ MetaItemType
} from "@openremote/model";
import {
AssetQueryOperator, AssetTypeAttributeName,
@@ -21,11 +22,12 @@ import {
RulesConfigAttribute
} from "./index";
import {OrSelectChangedEvent} from "@openremote/or-select";
-import "@openremote/or-attribute-input";
+import "@openremote/or-input";
import {InputType, OrInputChangedEvent} from "@openremote/or-input";
import {getEnumKeyAsString} from "@openremote/core/dist/util";
import {assetQueryEditorStyle} from "./style";
import i18next from "i18next";
+import {AssetModelUtil} from "@openremote/core";
@customElement("or-rule-asset-query")
class OrRuleAssetQuery extends LitElement {
@@ -68,29 +70,36 @@ class OrRuleAssetQuery extends LitElement {
protected attributePredicateValueEditorTemplate(assetDescriptor: AssetDescriptor, attributePredicate: AttributePredicate) {
- if (!attributePredicate.value) {
- return ``;
- }
-
const valuePredicate = attributePredicate.value;
- const assetType = getAssetTypeFromQuery(this.query);
const attributeName = this.getAttributeName(attributePredicate);
+ const assetType = getAssetTypeFromQuery(this.query);
const descriptor = assetDescriptor.attributeDescriptors ? assetDescriptor.attributeDescriptors.find((ad) => ad.attributeName === attributeName) : undefined;
// @ts-ignore
- const value = valuePredicate.value ? valuePredicate.value : null;
+ const value = valuePredicate && valuePredicate.value ? valuePredicate.value : null;
- const attributeInput = this.config && this.config.inputProvider ? this.config.inputProvider(assetType!, attributeName!, descriptor, value, (value: any) => this.setValuePredicateProperty(valuePredicate, "value", value), this.readonly || false) : undefined;
+ const attributeInput = this.config && this.config.inputProvider ? this.config.inputProvider(assetType!, attributeName!, descriptor, value, (v: any) => this.setValuePredicateProperty(valuePredicate, "value", v), this.readonly || false, false) : undefined;
if (attributeInput) {
return attributeInput;
}
+ if (!assetDescriptor || !valuePredicate) {
+ return ``;
+ }
+
+ // TODO: Update to use 'effective' meta so not dependent on meta items being defined on the asset itself
+ const min = AssetModelUtil.getMetaValue(MetaItemType.RANGE_MIN, undefined, descriptor);
+ const max = AssetModelUtil.getMetaValue(MetaItemType.RANGE_MAX, undefined, descriptor);
+ let label = AssetModelUtil.getMetaValue(MetaItemType.LABEL, undefined, descriptor);
+ label = i18next.t([attributeName, label || attributeName]);
+ const readonly = AssetModelUtil.getMetaValue(MetaItemType.READ_ONLY, undefined, descriptor);
+ const options = AssetModelUtil.getMetaValue(MetaItemType.ALLOWED_VALUES, undefined, descriptor);
+
switch (valuePredicate.predicateType) {
case "string":
return html`
- <or-attribute-input required type="${InputType.TEXT}" @or-input-changed="${(e: OrInputChangedEvent) => this.setValuePredicateProperty(valuePredicate, "value", e.detail.value)}" .assetType="${assetType}" .attributeName="${attributeName}" .attributeDescriptor="${descriptor}" .value="${value}" ?readonly="${this.readonly}"></or-attribute-input>
+ <or-input required type="${options ? InputType.SELECT : InputType.TEXT}" @or-input-changed="${(e: OrInputChangedEvent) => this.setValuePredicateProperty(valuePredicate, "value", e.detail.value)}" .options="${options}" .label="${label}" .value="${valuePredicate.value ? valuePredicate.value : null}" ?readonly="${readonly || this.readonly}"></or-input>
`;
- break;
case "boolean":
return html `<span>NOT IMPLEMENTED</span>`;
case "datetime":
@@ -98,13 +107,13 @@ class OrRuleAssetQuery extends LitElement {
case "number":
if (valuePredicate.operator === AQO.BETWEEN) {
return html`
- <or-attribute-input required type="${InputType.NUMBER}" @or-input-changed="${(e: OrInputChangedEvent) => this.setValuePredicateProperty(valuePredicate, "value", e.detail.value)}" .assetType="${assetType}" .attributeName="${attributeName}" .attributeDescriptor="${descriptor}" .value="${value}" ?readonly="${this.readonly}"></or-attribute-input>
+ <or-input required type="${InputType.NUMBER}" @or-input-changed="${(e: OrInputChangedEvent) => this.setValuePredicateProperty(valuePredicate, "value", e.detail.value)}" .min="${min}" .max="${max}" .label="${label}" .value="${valuePredicate.value ? valuePredicate.value : null}" ?readonly="${readonly || this.readonly}"></or-input>
<span style="display: inline-flex; align-items: center;">&</span>
- <or-attribute-input required type="${InputType.NUMBER}" @or-input-changed="${(e: OrInputChangedEvent) => this.setValuePredicateProperty(valuePredicate, "rangeValue", e.detail.value)}" .assetType="${assetType}" .attributeName="${attributeName}" .attributeDescriptor="${descriptor}" .value="${valuePredicate.rangeValue ? valuePredicate.rangeValue : null}" ?readonly="${this.readonly}"></or-attribute-input>
+ <or-input required type="${InputType.NUMBER}" @or-input-changed="${(e: OrInputChangedEvent) => this.setValuePredicateProperty(valuePredicate, "rangeValue", e.detail.value)}" .min="${min}" .max="${max}" .label="${label}" .value="${valuePredicate.rangeValue ? valuePredicate.rangeValue : null}" ?readonly="${readonly || this.readonly}"></or-input>
`;
}
return html`
- <or-attribute-input required type="${InputType.NUMBER}" @or-input-changed="${(e: OrInputChangedEvent) => this.setValuePredicateProperty(valuePredicate, "value", e.detail.value)}" .assetType="${assetType}" .attributeName="${attributeName}" .attributeDescriptor="${descriptor}" .value="${value}" ?readonly="${this.readonly}"></or-attribute-input>
+ <or-input required type="${InputType.NUMBER}" @or-input-changed="${(e: OrInputChangedEvent) => this.setValuePredicateProperty(valuePredicate, "value", e.detail.value)}" .min="${min}" .max="${max}" .label="${label}" .value="${valuePredicate.value ? valuePredicate.value : null}" ?readonly="${readonly || this.readonly}"></or-input>
`;
case "string-array":
return html `<span>NOT IMPLEMENTED</span>`;
@@ -121,7 +130,7 @@ class OrRuleAssetQuery extends LitElement {
case "array":
// TODO: Update once we can determine inner type of array
// Assume string array
- return html`<or-attribute-input required type="${InputType.TEXT}" @or-input-changed="${(e: OrInputChangedEvent) => this.setValuePredicateProperty(valuePredicate, "value", e.detail.value)}" .assetType="${assetType}" .attributeName="${attributeName}" .attributeDescriptor="${descriptor}" .value="${value}" ?readonly="${this.readonly}"></or-attribute-input>`;
+ return html`<or-input required type="${options ? InputType.SELECT : InputType.TEXT}" @or-input-changed="${(e: OrInputChangedEvent) => this.setValuePredicateProperty(valuePredicate, "value", e.detail.value)}" .options="${options}" .label="${label}" .value="${valuePredicate.value ? valuePredicate.value : null}" ?readonly="${readonly || this.readonly}"></or-input>`;
default:
return html `<span>NOT IMPLEMENTED</span>`;
}
@@ -562,7 +571,11 @@ class OrRuleAssetQuery extends LitElement {
? this.query.attributes.items[0] : undefined;
}
- protected setValuePredicateProperty(valuePredicate: ValuePredicateUnion, propertyName: string, value: any) {
+ protected setValuePredicateProperty(valuePredicate: ValuePredicateUnion | undefined, propertyName: string, value: any) {
+ if (!valuePredicate) {
+ return;
+ }
+
(valuePredicate as any)[propertyName] = value;
this.dispatchEvent(new OrRuleChangedEvent());
this.requestUpdate();
diff --git a/ui/component/or-select/src/index.ts b/ui/component/or-select/src/index.ts
index 114874d50..ccd352901 100644
--- a/ui/component/or-select/src/index.ts
+++ b/ui/component/or-select/src/index.ts
@@ -93,16 +93,13 @@ export class OrSelect extends LitElement {
if (_changedProperties.has("options") || _changedProperties.has("value")) {
let val = this.value;
- if (this.options.length === 1 || (!this.noEmpty && !val)) {
+ let index: number;
+ if (this.options.length === 1 || (this.noEmpty && !val)) {
const opt = this.options[0];
const firstValue = Array.isArray(opt) ? opt[0] : opt;
if (this.value !== firstValue) {
val = firstValue;
}
- }
-
- let index: number;
- if (this.options.length === 1 || (this.noEmpty && !val)) {
index = 0;
} else {
index = this.options.findIndex((opt: string | [string, string]) => {
@@ -110,12 +107,12 @@ export class OrSelect extends LitElement {
return value === val;
});
- if(!this.noEmpty) {
+ if (!this.noEmpty) {
index = index + 1;
}
}
- const indexChanged = this._select.selectedIndex != index;
+ const indexChanged = this._select.selectedIndex !== index;
this._select.selectedIndex = index;
if (val !== this.value) {