Permalink
Browse files

UI updates: event component fully implemented

  • Loading branch information...
richturner committed Feb 5, 2019
1 parent ecbc2a1 commit 23cbc2c9ca4ffa921737d08900a4b0a49522273e
@@ -34,7 +34,7 @@
value: value
}
}
openremote.INSTANCE.sendServiceMessage("EVENT" + JSON.stringify(attributeEvent));
openremote.INSTANCE.sendServiceMessage("EVENT:" + JSON.stringify(attributeEvent));
}
subscribeAttributeEvents(assetIds) {
@@ -37,7 +37,6 @@
import org.openremote.container.persistence.PersistenceService;
import org.openremote.container.security.AuthContext;
import org.openremote.container.timer.TimerService;
import org.openremote.container.web.WebService;
import org.openremote.manager.asset.console.ConsoleResourceImpl;
import org.openremote.manager.event.ClientEventService;
import org.openremote.manager.rules.AssetQueryPredicate;
@@ -50,6 +49,7 @@
import org.openremote.model.attribute.AttributeType;
import org.openremote.model.calendar.CalendarEvent;
import org.openremote.model.calendar.RecurrenceRule;
import org.openremote.model.event.TriggeredEventSubscription;
import org.openremote.model.event.shared.TenantFilter;
import org.openremote.model.query.AssetQuery;
import org.openremote.model.query.BaseAssetQuery;
@@ -67,9 +67,8 @@
import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;
import java.sql.*;
import java.time.Instant;
import java.util.*;
import java.util.Date;
import java.util.*;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -188,7 +187,7 @@ public void configure() throws Exception {
if (authContext.isSuperUser()) {
Asset asset = find(event.getAssetId(), true);
if (asset != null)
replyWithAttributeEvents(sessionKey, asset, event.getAttributeNames());
replyWithAttributeEvents(sessionKey, event.getSubscriptionId(), asset, event.getAttributeNames());
return;
}

@@ -203,7 +202,7 @@ public void configure() throws Exception {
identityService.getIdentityProvider().isRestrictedUser(authContext.getUserId()) ? RESTRICTED_READ : PRIVATE_READ
);
if (asset != null) {
replyWithAttributeEvents(sessionKey, asset, event.getAttributeNames());
replyWithAttributeEvents(sessionKey, event.getSubscriptionId(), asset, event.getAttributeNames());
}
});

@@ -1402,15 +1401,18 @@ protected void publishModificationEvents(PersistenceEvent<Asset> persistenceEven
}
}

protected void replyWithAttributeEvents(String sessionKey, Asset asset, String[] attributeNames) {
protected void replyWithAttributeEvents(String sessionKey, String subscriptionId, Asset asset, String[] attributeNames) {
List<String> names = attributeNames == null ? Collections.emptyList() : Arrays.asList(attributeNames);

// Client may want to read a subset or all attributes of the asset
clientEventService.sendToSession(sessionKey, asset.getAttributesStream()
.filter(attribute -> names.isEmpty() || attribute.getName().filter(names::contains).isPresent())
.map(AssetAttribute::getStateEvent)
.filter(Optional::isPresent)
.map(Optional::get).toArray(AttributeEvent[]::new));
AttributeEvent[] events = asset.getAttributesStream()
.filter(attribute -> names.isEmpty() || attribute.getName().filter(names::contains).isPresent())
.map(AssetAttribute::getStateEvent)
.filter(Optional::isPresent)
.map(Optional::get)
.toArray(AttributeEvent[]::new);
TriggeredEventSubscription<AttributeEvent> triggeredEventSubscription = new TriggeredEventSubscription<>(events, subscriptionId);
clientEventService.sendToSession(sessionKey, triggeredEventSubscription);
}

protected static boolean calendarEventPredicateMatches(CalendarEventActivePredicate eventActivePredicate, Asset asset) {
@@ -128,6 +128,7 @@ public void init(Container container) throws Exception {
new EventTypeConverters()
);

// TODO: Remove prefix and just use event type then use a subscription wrapper to pass subscription ID around
messageBrokerSetupService.getContext().addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
@@ -23,7 +23,6 @@
import org.apache.camel.Message;
import org.apache.camel.impl.DefaultMessage;
import org.openremote.container.timer.TimerService;
import org.openremote.container.util.UniqueIdentifierGenerator;
import org.openremote.container.web.socket.WebsocketConstants;
import org.openremote.manager.concurrent.ManagerExecutorService;
import org.openremote.model.event.TriggeredEventSubscription;
@@ -197,7 +196,7 @@ public void cancelAll(String sessionKey) {
|| sessionSubscription.subscription.getFilter().apply(event)) {
LOG.fine("Creating message for subscribed session '" + sessionKey + "': " + event);

TriggeredEventSubscription<SharedEvent> triggeredEventSubscription = new TriggeredEventSubscription<>(event, sessionSubscription.subscriptionId);
TriggeredEventSubscription<SharedEvent> triggeredEventSubscription = new TriggeredEventSubscription<>(new SharedEvent[] {event}, sessionSubscription.subscriptionId);

if (sessionSubscription.subscription.getInternalConsumer() == null) {
Message msg = new DefaultMessage();
@@ -68,10 +68,10 @@ public String writeEventArray(SharedEvent[] event, Exchange exchange) throws Exc
@Converter
public String writeTriggeredEventSubscription(TriggeredEventSubscription triggeredEventSubscription, Exchange exchange) throws Exception {
if (!TextUtil.isNullOrEmpty(triggeredEventSubscription.getSubscriptionId())) {
return SharedEvent.MESSAGE_PREFIX + triggeredEventSubscription.getSubscriptionId() + ":" + Container.JSON.writeValueAsString(triggeredEventSubscription.getEvent());
return SharedEvent.MESSAGE_PREFIX + triggeredEventSubscription.getSubscriptionId() + ":" + Container.JSON.writeValueAsString(triggeredEventSubscription.getEvents());
}

return SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(triggeredEventSubscription.getEvent());
return SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(triggeredEventSubscription.getEvents());
}

@Converter
@@ -40,6 +40,8 @@

protected String[] attributeNames;

protected String subscriptionId;

protected ReadAssetAttributesEvent() {
}

@@ -64,6 +66,14 @@ public void setAttributeNames(String[] attributeNames) {
this.attributeNames = attributeNames;
}

public String getSubscriptionId() {
return subscriptionId;
}

public void setSubscriptionId(String subscriptionId) {
this.subscriptionId = subscriptionId;
}

@Override
public String toString() {
return getClass().getSimpleName() + "{" +
@@ -23,19 +23,21 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import org.openremote.model.event.shared.SharedEvent;

import java.util.Arrays;

public class TriggeredEventSubscription<E extends SharedEvent> {

protected E event;
protected E[] events;
protected String subscriptionId;

@JsonCreator
public TriggeredEventSubscription(@JsonProperty("event") E event, @JsonProperty("subscriptionId") String subscriptionId) {
this.event = event;
public TriggeredEventSubscription(@JsonProperty("events") E[] events, @JsonProperty("subscriptionId") String subscriptionId) {
this.events = events;
this.subscriptionId = subscriptionId;
}

public E getEvent() {
return event;
public E[] getEvents() {
return events;
}

public String getSubscriptionId() {
@@ -45,7 +47,7 @@ public String getSubscriptionId() {
@Override
public String toString() {
return "TriggeredEventSubscription{" +
"event=" + event +
"events=" + Arrays.toString(events) +
", subscriptionId='" + subscriptionId + '\'' +
'}';
}
@@ -12,7 +12,6 @@ export interface ProviderMessage {
provider: string;
action: string;
data?: any;

[x: string]: any;
}

@@ -169,7 +168,7 @@ export class Console {
try {
// Get an ID for this console if it doesn't have one
if (!this._registration.id) {
await this.sendRegistration();
await this.sendRegistration(0);
}

if (this._registration.providers) {
@@ -190,6 +189,7 @@ export class Console {
}
} catch (e) {
console.error(e);
} finally {
this._initialiseInProgress = false;
}
}
@@ -290,13 +290,18 @@ export class Console {
}

// Uses a delayed mechanism to avoid excessive calls to the server during enabling providers
public async sendRegistration(): Promise<void> {
public sendRegistration(delay?: number): Promise<void> {

if (this._registrationTimer) {
window.clearTimeout(this._registrationTimer);
this._registrationTimer = null;
}

await new Promise(resolve => {
delay = delay !== undefined ? delay : 2000;

console.debug("Sending registration in: " + delay + "ms");

return new Promise((resolve, reject) => {
this._registrationTimer = window.setTimeout(() => {
this._registrationTimer = null;
console.debug("Console: updating registration");
@@ -311,11 +316,13 @@ export class Console {
console.debug("Console: registration successful");
console.debug("Console: updating locally stored registration");
window.localStorage.setItem("OpenRemoteConsole:" + this._realm, JSON.stringify(this._registration));
resolve();
});
} finally {
resolve();
} catch (e) {
console.error("Failed to register console");
reject("Failed to register console");
}
}, 2000);
},);
});
}

@@ -367,22 +374,25 @@ export class Console {

switch (msg.action.trim().toUpperCase()) {
case "PROVIDER_INIT":
this._handleProviderResponse(JSON.stringify({
let initResponse: ProviderInitialiseResponse = {
action: "PROVIDER_INIT",
provider: "push",
version: "web",
enabled: true,
hasPermission: true,
requiresPermission: false,
success: true
}));
};
this._handleProviderResponse(JSON.stringify(initResponse));
break;
case "PROVIDER_ENABLE":
this._handleProviderResponse(JSON.stringify({
let enableResponse: ProviderEnableResponse = {
action: "PROVIDER_ENABLE",
provider: "push",
hasPermission: true,
success: true
}));
};
this._handleProviderResponse(JSON.stringify(enableResponse));
break;
default:
throw new Error("Unsupported provider '" + msg.provider + "' and action '" + msg.action + "'");
@@ -394,22 +404,25 @@ export class Console {

switch (msg.action) {
case "PROVIDER_INIT":
this._handleProviderResponse(JSON.stringify({
let initResponse: ProviderInitialiseResponse = {
action: "PROVIDER_INIT",
provider: "storage",
version: "1.0.0",
enabled: true,
hasPermission: true,
requiresPermission: false,
success: true
}));
};
this._handleProviderResponse(JSON.stringify(initResponse));
break;
case "PROVIDER_ENABLE":
this._handleProviderResponse(JSON.stringify({
let enableResponse: ProviderEnableResponse = {
action: "PROVIDER_ENABLE",
provider: "storage",
hasPermission: true,
success: true
}));
};
this._handleProviderResponse(JSON.stringify(enableResponse));
break;
case "STORE":
let keyValue = msg.key ? msg.key.trim() : null;
Oops, something went wrong.

0 comments on commit 23cbc2c

Please sign in to comment.