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
UI updates: Improvements to page-map in Manager UI
Rich Turner76bb25dae2e8
diff --git a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java b/manager/src/main/java/org/openremote/manager/event/ClientEventService.javaindex a55246fa4..3c7e73834 100644--- a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java+++ b/manager/src/main/java/org/openremote/manager/event/ClientEventService.java@@ -65,12 +65,7 @@ import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;* <dd><p>* The payload is a serialized representation of {@link EventSubscription} with an optional* {@link org.openremote.model.event.shared.EventFilter}. Clients can subscribe to receive {@link SharedEvent}s- * when they are published on the server. Subscriptions are handled by {@link SharedEvent#getEventType}, there- * can only be one active subscription for a particular event type and any new subscription for the same event- * type will replace any currently active subscription. The <code>SUBSCRIBE</code> message must be send- * repeatedly to renew the subscription,or the server will expire the subscription. The default expiration- * time is {@link EventSubscription#RENEWAL_PERIOD_SECONDS}; it is recommended clients renew the subscription- * in shorter periods to allow for processing time of the renewal.+ * when they are published on the server. Subscriptions are handled by {@link SharedEvent#getEventType}.* </p></dd>* <dt><code>UNSUBSCRIBE:{...}</code></dt>* <dd><p>diff --git a/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java b/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.javaindex 75b5916ee..59918b18e 100644--- a/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java+++ b/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java@@ -46,17 +46,6 @@ public class EventSubscriptions {final protected Map<String, SessionSubscriptions> sessionSubscriptionIdMap = new HashMap<>();class SessionSubscriptions extends HashSet<SessionSubscription> {- public void removeExpired() {- removeIf(sessionSubscription -> {- boolean expired = sessionSubscription.isExpired();- if (expired) {- LOG.fine("Removing expired; " + sessionSubscription.subscription);- }- return expired;- }- );- }-public void createOrUpdate(boolean restrictedUser, EventSubscription<?> eventSubscription) {if (TextUtil.isNullOrEmpty(eventSubscription.getSubscriptionId())) {@@ -103,14 +92,6 @@ public class EventSubscriptions {public boolean matches(boolean accessibleForRestrictedUsers, SharedEvent event) {return (!restrictedUser || accessibleForRestrictedUsers) && subscription.getEventType().equals(event.getEventType());}-- /**- * Subscriptions with internal consumer never expire- */- public boolean isExpired() {- return subscription.getInternalConsumer() == null- && timestamp + (EventSubscription.RENEWAL_PERIOD_SECONDS * 1000) < timerService.getCurrentTimeMillis();- }}public EventSubscriptions(TimerService timerService) {diff --git a/model/src/main/java/org/openremote/model/event/shared/EventSubscription.java b/model/src/main/java/org/openremote/model/event/shared/EventSubscription.javaindex 1742b42ef..6d89db077 100644--- a/model/src/main/java/org/openremote/model/event/shared/EventSubscription.java+++ b/model/src/main/java/org/openremote/model/event/shared/EventSubscription.java@@ -30,9 +30,6 @@ import java.util.function.Consumer;* type of event it wants to receive as well as filter criteria to restrict the* events to an interesting subset.* <p>- * Subscriptions must be refreshed by the client every {@link #RENEWAL_PERIOD_SECONDS}- * or the server will expire and remove the subscription.- * <p>* A subscription can optionally contain a {@link #subscriptionId} which allows a client* to have multiple subscriptions for the same event type.*/@@ -40,7 +37,6 @@ public class EventSubscription<E extends SharedEvent> {public static final String SUBSCRIBE_MESSAGE_PREFIX = "SUBSCRIBE:";public static final String SUBSCRIBED_MESSAGE_PREFIX = "SUBSCRIBED:";- public static final int RENEWAL_PERIOD_SECONDS = 300;protected String eventType;protected EventFilter<E> filter;diff --git a/ui/app/manager/src/pages/page-map.ts b/ui/app/manager/src/pages/page-map.tsindex d835808ef..1e566c122 100644--- a/ui/app/manager/src/pages/page-map.ts+++ b/ui/app/manager/src/pages/page-map.ts@@ -35,14 +35,18 @@ const pageMapSlice = createSlice({initialState: INITIAL_STATE,reducers: {assetEventReceived(state: MapState, action: PayloadAction<AssetEvent>) {- let assets = state.assets.filter((asst) => asst.id !== action.payload.asset.id);- if (action.payload.cause !== AssetEventCause.DELETE) {- assets.push(action.payload.asset);++ if (action.payload.cause === AssetEventCause.CREATE) {+ // Update and delete handled by attribute handler++ const asset = action.payload.asset;+ const locationAttr = asset.attributes && asset.attributes.hasOwnProperty(WellknownAttributes.LOCATION) ? asset.attributes[WellknownAttributes.LOCATION] as Attribute<GeoJSONPoint> : undefined;+ if (locationAttr && (!locationAttr.meta || locationAttr.meta && (!locationAttr.meta.hasOwnProperty(WellknownMetaItems.SHOWONDASHBOARD) || !!locationAttr.meta[WellknownMetaItems.SHOWONDASHBOARD]))) {+ state.assets.push(action.payload.asset);+ }}- return {- ...state,- assets: assets- };++ return state;},attributeEventReceived(state: MapState, action: PayloadAction<AttributeEvent>) {let assets = state.assets;@@ -54,16 +58,12 @@ const pageMapSlice = createSlice({return state;}- asset = Util.updateAsset(asset, action.payload);-- return {- ...state,- assets: [- ...assets.slice(0, index),- asset,- ...assets.slice(index + 1)- ]- };+ if (action.payload.attributeState.deleted) {+ assets.splice(index, 1);+ } else {+ assets[index] = Util.updateAsset({...asset}, action.payload);+ }+ return state;},setAssets(state, action: PayloadAction<Asset[]>) {return {@@ -220,11 +220,7 @@ export class PageMap<S extends MapStateKeyed> extends Page<S> {dispatch(setAssets(assets));- const ids = result.data.map(- asset => asset.id- );-- const assetSubscriptionId = await manager.events.subscribeAssetEvents(ids, false, undefined, (event) => {+ const assetSubscriptionId = await manager.events.subscribeAssetEvents(undefined, false, undefined, (event) => {dispatch(assetEventReceived(event));});@@ -235,7 +231,7 @@ export class PageMap<S extends MapStateKeyed> extends Page<S> {this.assetSubscriptionId = assetSubscriptionId;- const attributeSubscriptionId = await manager.events.subscribeAttributeEvents(ids, false, (event) => {+ const attributeSubscriptionId = await manager.events.subscribeAttributeEvents(undefined, false, (event) => {dispatch(attributeEventReceived(event));});diff --git a/ui/component/core/src/event.ts b/ui/component/core/src/event.tsindex 1b46c0e49..3114c2c01 100644--- a/ui/component/core/src/event.ts+++ b/ui/component/core/src/event.ts@@ -41,7 +41,7 @@ export interface EventProvider {/*** Subscribe to {@link AssetEvent}s for the specified {@link Asset}s for all {@link Asset}s*/- subscribeAssetEvents(ids: string[] | AttributeRef[] | null, requestCurrentValues: boolean, filter: AssetFilter | undefined, callback: (event: AssetEvent) => void): Promise<string>;+ subscribeAssetEvents(ids: string[] | AttributeRef[] | undefined, requestCurrentValues: boolean, filter: AssetFilter | undefined, callback: (event: AssetEvent) => void): Promise<string>;/*** Subscribe to {@link AttributeEvent}s for the specified {@link Asset}s and if {@link AttributeRef}s are provided@@ -267,38 +267,30 @@ abstract class EventProviderImpl implements EventProvider {return this._doSendWithReply(event);}- public async subscribeAssetEvents(ids: string[] | AttributeRef[] | null, requestCurrentValues: boolean, filter: AssetFilter | undefined, callback: (event: AssetEvent) => void): Promise<string> {+ public async subscribeAssetEvents(ids: string[] | AttributeRef[] | undefined, requestCurrentValues: boolean, filter: AssetFilter | undefined, callback: (event: AssetEvent) => void): Promise<string> {const isAttributeRef = ids && typeof ids[0] !== "string";- const assetIds = isAttributeRef ? (ids as AttributeRef[]).map((ref) => ref.id!) : ids as string[] | null;+ const assetIds = isAttributeRef ? (ids as AttributeRef[]).map((ref) => ref.id!) : ids as string[] | undefined;const subscriptionId = "AssetEvent" + EventProviderImpl._subscriptionCounter++;// If not already done then create a single global subscription for asset events and filter for each callbackif (!this._assetEventPromise) {const subscription: EventSubscription<AssetEvent> = {- eventType: "asset"+ eventType: "asset",};+ if (assetIds) {+ subscription.filter = {+ filterType: "asset",+ assetIds: assetIds+ };+ }this._assetEventPromise = this.subscribe(subscription, (event) => {this._assetEventCallbackMap.forEach((callback) => callback(event));});}- // Build a filter to only respond to the callback for the requested assets- const eventFilter = (e: AssetEvent) => {- if (!assetIds) {- callback(e);- return;- }- if (!e.asset) {- return;- }- if (assetIds.find((id) => id === e.asset!.id)) {- callback(e);- }- };-- this._assetEventCallbackMap.set(subscriptionId, eventFilter);+ this._assetEventCallbackMap.set(subscriptionId, callback);return this._assetEventPromise.then(() => {@@ -328,7 +320,7 @@ abstract class EventProviderImpl implements EventProvider {asset: asset,cause: AssetEventCause.READ};- eventFilter(assetEvent);+ callback(assetEvent);});});}@@ -344,14 +336,10 @@ abstract class EventProviderImpl implements EventProvider {});}- public async subscribeAttributeEvents(ids: string[] | AttributeRef[], requestCurrentValues: boolean, callback: (event: AttributeEvent) => void): Promise<string> {+ public async subscribeAttributeEvents(ids: string[] | AttributeRef[] | undefined, requestCurrentValues: boolean, callback: (event: AttributeEvent) => void): Promise<string> {- if (!ids || ids.length === 0) {- throw new Error("At least one ID must be provided");- }-- const isAttributeRef = typeof ids[0] !== "string";- const assetIds = isAttributeRef ? [...new Set((ids as AttributeRef[]).map((ref) => ref.id!))] : [...new Set(ids as string[])];+ const isAttributeRef = ids && typeof ids[0] !== "string";+ const assetIds = isAttributeRef ? (ids as AttributeRef[]).map((ref) => ref.id!) : ids as string[] | undefined;const attributes = isAttributeRef ? ids as AttributeRef[] : undefined;const subscriptionId = "AttributeEvent" + EventProviderImpl._subscriptionCounter++;@@ -368,18 +356,16 @@ abstract class EventProviderImpl implements EventProvider {// Build a filter to only respond to the callback for the requested attributesconst eventFilter = (e: AttributeEvent) => {- ids.forEach((idOrRef: string | AttributeRef) => {- if (typeof (idOrRef) === "string") {- if (e.attributeState!.ref!.id === idOrRef) {- callback(e);- }- } else {+ if (isAttributeRef) {+ (ids as AttributeRef[]).forEach((ref: AttributeRef) => {const eventRef = e.attributeState!.ref!;- if (eventRef.id === idOrRef.id && eventRef.name === idOrRef.name) {+ if (eventRef.id === ref.id && eventRef.name === ref.name) {callback(e);}- }- });+ });+ } else {+ callback(e);+ }};this._attributeEventCallbackMap.set(subscriptionId, eventFilter);@@ -388,7 +374,7 @@ abstract class EventProviderImpl implements EventProvider {try {// Get the current state of the attributes if requested- if (requestCurrentValues) {+ if (requestCurrentValues && assetIds) {// Just request the whole asset(s) and let the event filter do the workconst readRequest: EventRequestResponseWrapper<ReadAssetsEvent> = {messageId: "read-assets:" + assetIds.join(",") + ":" + subscriptionId,diff --git a/ui/component/or-asset-tree/src/index.ts b/ui/component/or-asset-tree/src/index.tsindex 72c9522d5..ab1335828 100644--- a/ui/component/or-asset-tree/src/index.ts+++ b/ui/component/or-asset-tree/src/index.ts@@ -784,7 +784,7 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {public async _addEventSubscriptions(): Promise<void> {if (!this.disableSubscribe) {// Subscribe to asset events for all assets in the realm- this._subscriptionIds = [await manager.getEventProvider()!.subscribeAssetEvents(null, false, undefined, (event) => this._onEvent(event))];+ this._subscriptionIds = [await manager.getEventProvider()!.subscribeAssetEvents(undefined, false, undefined, (event) => this._onEvent(event))];}}