Skip to content

armgit / core

Public Imported from GitHub
0

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

Added support for include & exclude wildcards in config (implements #251)

Rich Turner01bfb348e8e4
diff --git a/ui/component/core/src/util.ts b/ui/component/core/src/util.ts
index bbbb16ee7..2d4234ff8 100644
--- a/ui/component/core/src/util.ts
+++ b/ui/component/core/src/util.ts
@@ -239,6 +239,36 @@ export function arrayRemove<T>(arr: T[], item: T) {
}
}
+export function stringMatch(needle: string, haystack: string): boolean {
+
+ if (haystack === needle) {
+ return true;
+ }
+
+ const startsWith = needle.endsWith("*");
+ const endsWith = !startsWith && needle.startsWith("*");
+ const regExp = !startsWith && !endsWith && needle.startsWith("^") && needle.endsWith("$")
+
+ if (startsWith && haystack.startsWith(needle.substr(0, needle.length - 1))) {
+ return true;
+ }
+
+ if (endsWith && haystack.endsWith(needle.substr(1))) {
+ return true;
+ }
+
+ if (regExp) {
+ try {
+ const regexp = new RegExp(needle);
+ return regexp.test(haystack);
+ } catch(e) {
+ console.error("Failed to compile needle as a RegExp: " + e);
+ }
+ }
+
+ return false;
+}
+
export function enumContains(enm: object, val: string): boolean {
return enm && Object.values(enm).includes(val);
}
diff --git a/ui/component/or-asset-tree/src/index.ts b/ui/component/or-asset-tree/src/index.ts
index f20684bcc..ef73a784e 100644
--- a/ui/component/or-asset-tree/src/index.ts
+++ b/ui/component/or-asset-tree/src/index.ts
@@ -10,12 +10,12 @@ import {InputType, OrInputChangedEvent} from "@openremote/or-input";
import Qs from "qs";
import {getAssetDescriptorIconTemplate, OrIcon} from "@openremote/or-icon";
import "@openremote/or-mwc-components/dist/or-mwc-menu";
-import {getContentWithMenuTemplate, MenuItem, OrMwcMenuChangedEvent} from "@openremote/or-mwc-components/dist/or-mwc-menu";
+import {getContentWithMenuTemplate, MenuItem} from "@openremote/or-mwc-components/dist/or-mwc-menu";
import "@openremote/or-mwc-components/dist/or-mwc-list";
import {ListItem, OrMwcListChangedEvent} from "@openremote/or-mwc-components/dist/or-mwc-list";
import {i18next, OrTranslate} from "@openremote/or-translate";
import "@openremote/or-mwc-components/dist/or-mwc-dialog";
-import {DialogAction, OrMwcDialog, showDialog} from "@openremote/or-mwc-components/dist/or-mwc-dialog";
+import {showDialog} from "@openremote/or-mwc-components/dist/or-mwc-dialog";
export interface AssetTreeTypeConfig {
include?: string[];
@@ -449,11 +449,11 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
protected _onAddClicked() {
const includedAssetTypes = this.config && this.config.addAssetTypes && this.config.addAssetTypes.include ? this.config.addAssetTypes.include : undefined;
- const excludedAssetTypes = this.config && this.config.addAssetTypes && this.config.addAssetTypes.exclude ? this.config.addAssetTypes.exclude : [];
+ const excludedAssetTypes = this.config && this.config.addAssetTypes && this.config.addAssetTypes.exclude ? this.config.addAssetTypes.exclude : undefined;
const listItems: ListItem[] = AssetModelUtil.getAssetDescriptors()
- .filter((descriptor) => (!includedAssetTypes || includedAssetTypes.indexOf(descriptor.type!) >= 0)
- && (excludedAssetTypes.indexOf(descriptor.type!) < 0))
+ .filter((descriptor) => (!includedAssetTypes || includedAssetTypes.some((inc) => Util.stringMatch(inc, descriptor.type!)))
+ && (!excludedAssetTypes || !excludedAssetTypes.some((exc) => Util.stringMatch(exc, descriptor.type!))))
.sort(Util.sortByString((descriptor) => descriptor.name!))
.map((descriptor) => {
return {
diff --git a/ui/component/or-asset-viewer/src/index.ts b/ui/component/or-asset-viewer/src/index.ts
index 0429f5cfa..eaf29463e 100644
--- a/ui/component/or-asset-viewer/src/index.ts
+++ b/ui/component/or-asset-viewer/src/index.ts
@@ -132,10 +132,13 @@ export function getIncludedProperties(config?: InfoPanelConfig): string[] {
export function getIncludedAttributes(attributes: Attribute[], config?: InfoPanelConfig): Attribute[] {
const includedAttributes = config && config.attributes && config.attributes.include ? config.attributes.include : undefined;
- const excludedAttributes = config && config.attributes && config.attributes.exclude ? config.attributes.exclude : [];
- return attributes.filter((attr) =>
- (!includedAttributes || includedAttributes.indexOf(attr.name!) >= 0)
- && (!excludedAttributes || excludedAttributes.indexOf(attr.name!) < 0));
+ const excludedAttributes = config && config.attributes && config.attributes.exclude ? config.attributes.exclude : undefined;
+ if (includedAttributes || excludedAttributes) {
+ return attributes.filter((attr) =>
+ (!includedAttributes || includedAttributes.some((inc) => Util.stringMatch(inc, attr.name!)))
+ && (!excludedAttributes || !excludedAttributes.some((exc) => Util.stringMatch(exc, attr.name!))));
+ }
+ return attributes;
}
class EventHandler {
diff --git a/ui/component/or-asset-viewer/src/or-edit-asset-panel.ts b/ui/component/or-asset-viewer/src/or-edit-asset-panel.ts
index b5a5f9354..293f669d4 100644
--- a/ui/component/or-asset-viewer/src/or-edit-asset-panel.ts
+++ b/ui/component/or-asset-viewer/src/or-edit-asset-panel.ts
@@ -100,6 +100,9 @@ const style = css`
.button-clear.hidden {
visibility: hidden;
}
+ .padded-cell {
+ padding: 10px 0;
+ }
`;
export class OrEditAssetModifiedEvent extends CustomEvent<void> {
@@ -213,23 +216,21 @@ export class OrEditAssetPanel extends LitElement {
return html`
<tr class="mdc-data-table__row">
- <td class="mdc-data-table__cell expander-cell"><or-icon icon="chevron-right"></or-icon><span>${attribute.name}</span></td>
- <td class="mdc-data-table__cell">${attribute.type}</td>
- <td class="mdc-data-table__cell">
- <or-attribute-input compact .assetType="${assetType}" .label=${null} .readonly="${false}" .attribute="${attribute}" .hasHelperText="${false}" disableWrite disableSubscribe disableButton></or-attribute-input>
+ <td class="padded-cell mdc-data-table__cell expander-cell"><or-icon icon="chevron-right"></or-icon><span>${attribute.name}</span></td>
+ <td class="padded-cell mdc-data-table__cell">${attribute.type}</td>
+ <td class="padded-cell mdc-data-table__cell">
+ <or-attribute-input compact .assetType="${assetType}" .label=${null} .readonly="${false}" .attribute="${attribute}" hasHelperText disableWrite disableSubscribe disableButton></or-attribute-input>
</td>
- <td class="mdc-data-table__cell actions-cell"><or-input type="${InputType.BUTTON}" icon="delete" @click="${deleteAttribute}"></td>
+ <td class="padded-cell mdc-data-table__cell actions-cell"><or-input type="${InputType.BUTTON}" icon="delete" @click="${deleteAttribute}"></td>
</tr>
<tr class="attribute-meta-row">
<td colspan="4">
- <div>
- <div class="meta-item-container">
- <div>
- ${!attribute.meta ? `` : attribute.meta.sort(Util.sortByString((metaItem) => metaItem.name!)).map((metaItem) => this._getMetaItemTemplate(attribute, metaItem))}
- </div>
- <div class="item-add">
- <or-input .type="${InputType.BUTTON}" .label="${i18next.t("addMetaItems")}" icon="plus" @click="${() => this._addMetaItems(attribute)}"></or-input>
- </div>
+ <div class="meta-item-container">
+ <div>
+ ${!attribute.meta ? `` : attribute.meta.sort(Util.sortByString((metaItem) => metaItem.name!)).map((metaItem) => this._getMetaItemTemplate(attribute, metaItem))}
+ </div>
+ <div class="item-add">
+ <or-input .type="${InputType.BUTTON}" .label="${i18next.t("addMetaItems")}" icon="plus" @click="${() => this._addMetaItems(attribute)}"></or-input>
</div>
</div>
</td>
@@ -242,6 +243,11 @@ export class OrEditAssetPanel extends LitElement {
this.requestUpdate();
}
+ protected _onMetaItemModified(metaItem: MetaItem, value: any) {
+ metaItem.value = value;
+ this._onModified();
+ }
+
protected _getMetaItemTemplate(attribute: Attribute, metaItem: MetaItem): TemplateResult {
const removeMetaItem = () => {
@@ -290,7 +296,7 @@ export class OrEditAssetPanel extends LitElement {
const readonly = descriptor && !!descriptor.valueFixed;
return html`
<div class="meta-item-wrapper">
- <or-input .label=${Util.getMetaItemLabel(metaItem.name!)} .type="${inputType}" .value="${metaItem.value}" ?disabled="${readonly}"></or-input>
+ <or-input .label=${Util.getMetaItemLabel(metaItem.name!)} .type="${inputType}" .value="${metaItem.value}" @or-input-changed="${(e: OrInputChangedEvent) => this._onMetaItemModified(metaItem, e.detail.value)}" ?disabled="${readonly}"></or-input>
<button class="button-clear" @click="${removeMetaItem}"><or-icon icon="close-circle"></or-icon></input>
</div>
`;
diff --git a/ui/component/or-attribute-input/src/index.ts b/ui/component/or-attribute-input/src/index.ts
index 5b8f8ff7c..e06077b8e 100644
--- a/ui/component/or-attribute-input/src/index.ts
+++ b/ui/component/or-attribute-input/src/index.ts
@@ -180,14 +180,16 @@ export const GeoJsonPointInputTemplateProvider: AttributeInputCustomProvider = (
return (value, timestamp, loading, sending, error) => {
let pos: LngLat | undefined;
let center: number[] | undefined;
+
if (value) {
pos = MapUtil.getLngLat(value);
center = pos ? pos.toArray() : undefined;
}
+ const centerStr = center ? center.join(", ") : undefined;
centerControl.pos = pos;
coordinatesControl.readonly = !!attributeInput.disabled || !!attributeInput.readonly || sending || loading;
- coordinatesControl.value = center ? center.join(", ") : undefined;
+ coordinatesControl.value = centerStr;
const iconAndColor = getMarkerIconAndColorFromAssetType(assetType);
@@ -205,33 +207,52 @@ export const GeoJsonPointInputTemplateProvider: AttributeInputCustomProvider = (
or-map {
border: #e5e5e5 1px solid;
margin: 3px 0;
+ width: 100%;
+ height: 100%;
}
</style>
- <or-map class="or-map" @or-map-clicked="${(ev: OrMapClickedEvent) => clickHandler(ev.detail)}" .center="${center}" .controls="${[centerControl, [coordinatesControl, "top-left"]]}">
- <or-map-marker active .lng="${pos ? pos.lng : undefined}" .lat="${pos ? pos.lat : undefined}" .icon="${iconAndColor ? iconAndColor.icon : undefined}" .activeColor="${iconAndColor ? "#" + iconAndColor.color : undefined}" .color="${iconAndColor ? "#" + iconAndColor.color : undefined}"></or-map-marker>
- </or-map>
+ <div id="geo-json-point-input-wrapper">
+ <or-map class="or-map" @or-map-clicked="${(ev: OrMapClickedEvent) => clickHandler(ev.detail)}" .center="${center}" .controls="${[centerControl, [coordinatesControl, "top-left"]]}">
+ <or-map-marker active .lng="${pos ? pos.lng : undefined}" .lat="${pos ? pos.lat : undefined}" .icon="${iconAndColor ? iconAndColor.icon : undefined}" .activeColor="${iconAndColor ? "#" + iconAndColor.color : undefined}" .color="${iconAndColor ? "#" + iconAndColor.color : undefined}"></or-map-marker>
+ </or-map>
+ </div>
`;
if (attributeInput.compact) {
-
const mapContent = content;
const onClick = () => {
showDialog(
{
content: mapContent,
+ styles: html`
+ <style>
+ #geo-json-point-input-wrapper {
+ width: 600px;
+ height:600px;
+ }
+ </style>
+ `
});
};
content = html`
- <div>
- <or-input .type="${InputType.TEXT}" comfortable outlined .pattern="${CoordinatesRegexPattern}" @keyup="${(e: KeyboardEvent) => getCoordinatesInputKeyHandler(valueChangeNotifier)(e)}"></or-input>
- <or-input style="width: auto;" .type="${InputType.BUTTON}" compact action icon="crosshairs-gps" @clicked="${onClick}"></or-input>
+ <style>
+ #geo-json-point-input-compact-wrapper {
+ display: table-cell;
+ }
+ #geo-json-point-input-compact-wrapper > * {
+ vertical-align: middle;
+ }
+ </style>
+ <div id="geo-json-point-input-compact-wrapper">
+ <or-input .type="${InputType.TEXT}" .value="${centerStr}" .pattern="${CoordinatesRegexPattern}" @keyup="${(e: KeyboardEvent) => getCoordinatesInputKeyHandler(valueChangeNotifier)(e)}"></or-input>
+ <or-input style="width: auto;" .type="${InputType.BUTTON}" compact action icon="crosshairs-gps" @click="${onClick}"></or-input>
</div>
`;
}
- return getAttributeInputWrapper(content, loading, !!attributeInput.disabled, getHelperText(sending, false, timestamp), attributeInput.label, undefined);
+ return getAttributeInputWrapper(content, loading, !!attributeInput.disabled, attributeInput.hasHelperText ? getHelperText(sending, false, timestamp) : undefined, attributeInput.label, undefined);
}
}
@@ -919,6 +940,11 @@ export class OrAttributeInput extends subscribe(manager)(translate(i18next)(LitE
this._writeTimeoutHandler = window.setTimeout(() => this._onWriteTimeout(), this.writeTimeout);
} else {
+ if (this.attribute) {
+ this.attribute.value = newValue;
+ this.attribute.valueTimestamp = undefined; // Clear timestamp so server will set this
+ }
+
this.value = newValue;
this.dispatchEvent(new OrAttributeInputChangedEvent(newValue, oldValue));
}
diff --git a/ui/component/or-rules/src/index.ts b/ui/component/or-rules/src/index.ts
index e9ec2a25b..1f478f6f0 100644
--- a/ui/component/or-rules/src/index.ts
+++ b/ui/component/or-rules/src/index.ts
@@ -7,7 +7,8 @@ import manager, {
DefaultColor3,
DefaultColor4,
DefaultColor5,
- DefaultColor6
+ DefaultColor6,
+ Util
} from "@openremote/core";
import i18next from "i18next";
import "@openremote/or-icon";
@@ -305,19 +306,13 @@ export function getAssetDescriptors(config: RulesConfig | undefined, useActionCo
// Remove any excluded attributes
if (modifiedDescriptor.attributeDescriptors) {
- const inc = configDescriptor.includeAttributes !== undefined ? configDescriptor.includeAttributes : undefined;
- const exc = configDescriptor.excludeAttributes !== undefined ? configDescriptor.excludeAttributes : undefined;
+ const includedAttributes = configDescriptor.includeAttributes !== undefined ? configDescriptor.includeAttributes : undefined;
+ const excludedAttributes = configDescriptor.excludeAttributes !== undefined ? configDescriptor.excludeAttributes : undefined;
- if (inc || exc) {
- modifiedDescriptor.attributeDescriptors = modifiedDescriptor.attributeDescriptors.filter((mad) => {
- if (exc && exc.indexOf(mad.attributeName!) >= 0) {
- return false;
- }
- if (inc && inc.indexOf(mad.attributeName!) < 0) {
- return false;
- }
- return true;
- });
+ if (includedAttributes || excludedAttributes) {
+ modifiedDescriptor.attributeDescriptors = modifiedDescriptor.attributeDescriptors.filter((mad) =>
+ (!includedAttributes || includedAttributes.some((inc) => Util.stringMatch(inc, mad.attributeName!)))
+ && (!excludedAttributes || !excludedAttributes.some((exc) => Util.stringMatch(exc, mad.attributeName!))));
}
// Override any attribute descriptors