Skip to content

Commit

Permalink
Model gwt-jackson updates/bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
richturner committed Aug 13, 2019
1 parent 03fbde4 commit 18eedbf
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 37 deletions.
Expand Up @@ -34,6 +34,7 @@
import org.openremote.app.client.event.EventServiceImpl;
import org.openremote.app.client.style.WidgetStyle;
import org.openremote.model.event.bus.EventBus;
import org.openremote.model.event.shared.SharedEvent;
import org.openremote.model.security.TenantResource;

public class ManagerModule extends AbstractGinModule {
Expand Down Expand Up @@ -91,15 +92,17 @@ public EventService getEventService(OpenRemoteApp app,
SharedEventArrayMapper sharedEventArrayMapper,
EventSubscriptionMapper eventSubscriptionMapper,
CancelEventSubscriptionMapper cancelEventSubscriptionMapper,
UnauthorizedEventSubscriptionMapper unauthorizedEventSubscriptionMapper) {
UnauthorizedEventSubscriptionMapper unauthorizedEventSubscriptionMapper,
TriggeredEventSubscriptionMapper triggeredEventSubscriptionMapper) {
return new EventServiceImpl(
app,
eventBus,
sharedEventMapper,
sharedEventArrayMapper,
eventSubscriptionMapper,
cancelEventSubscriptionMapper,
unauthorizedEventSubscriptionMapper
unauthorizedEventSubscriptionMapper,
triggeredEventSubscriptionMapper
);
}

Expand Down
Expand Up @@ -21,9 +21,11 @@

import elemental2.dom.DomGlobal;
import org.openremote.app.client.OpenRemoteApp;
import org.openremote.model.event.TriggeredEventSubscription;
import org.openremote.model.event.bus.EventBus;
import org.openremote.model.event.shared.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -36,6 +38,7 @@ public class EventServiceImpl implements EventService {
final protected EventSubscriptionMapper eventSubscriptionMapper;
final protected CancelEventSubscriptionMapper cancelEventSubscriptionMapper;
final protected UnauthorizedEventSubscriptionMapper unauthorizedEventSubscriptionMapper;
final protected TriggeredEventSubscriptionMapper triggeredEventSubscriptionMapper;

final protected Map<String, Double> activeSubscriptions = new HashMap<>();

Expand All @@ -45,14 +48,16 @@ public EventServiceImpl(OpenRemoteApp app,
SharedEventArrayMapper sharedEventArrayMapper,
EventSubscriptionMapper eventSubscriptionMapper,
CancelEventSubscriptionMapper cancelEventSubscriptionMapper,
UnauthorizedEventSubscriptionMapper unauthorizedEventSubscriptionMapper) {
UnauthorizedEventSubscriptionMapper unauthorizedEventSubscriptionMapper,
TriggeredEventSubscriptionMapper triggeredEventSubscriptionMapper) {
this.app = app;
this.eventBus = eventBus;
this.sharedEventMapper = sharedEventMapper;
this.sharedEventArrayMapper = sharedEventArrayMapper;
this.eventSubscriptionMapper = eventSubscriptionMapper;
this.cancelEventSubscriptionMapper = cancelEventSubscriptionMapper;
this.unauthorizedEventSubscriptionMapper = unauthorizedEventSubscriptionMapper;
this.triggeredEventSubscriptionMapper = triggeredEventSubscriptionMapper;

this.app.addServiceMessageConsumer(this::onServiceMessageReceived);

Expand Down Expand Up @@ -123,8 +128,13 @@ protected void onServiceMessageReceived(String data) {
} else if (data.startsWith(SharedEvent.MESSAGE_PREFIX)) {
data = data.substring(SharedEvent.MESSAGE_PREFIX.length());
if (data.startsWith("{")) {
SharedEvent event = sharedEventMapper.read(data);
eventBus.dispatch(event);
TriggeredEventSubscription triggered = triggeredEventSubscriptionMapper.read(data);
if (triggered.getEvents() == null) {
SharedEvent event = sharedEventMapper.read(data);
eventBus.dispatch(event);
} else {
Arrays.stream(triggered.getEvents()).forEach(eventBus::dispatch);
}
} else if (data.startsWith("[")) {
// Handle array of events
SharedEvent[] events = sharedEventArrayMapper.read(data);
Expand Down
@@ -0,0 +1,31 @@
/*
* Copyright 2016, OpenRemote Inc.
*
* See the CONTRIBUTORS.txt file in the distribution for a
* full listing of individual contributors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.openremote.app.client.event;

import com.github.nmorel.gwtjackson.client.ObjectMapper;
import com.github.nmorel.gwtjackson.client.annotation.JsonMixIns;
import org.openremote.app.client.interop.jackson.DefaultJsonMixin;
import org.openremote.model.event.TriggeredEventSubscription;
import org.openremote.model.event.shared.SharedEvent;

@JsonMixIns({@JsonMixIns.JsonMixIn(target = TriggeredEventSubscription.class, mixIn = DefaultJsonMixin.class)})
public interface TriggeredEventSubscriptionMapper extends ObjectMapper<TriggeredEventSubscription> {

}
Expand Up @@ -349,7 +349,7 @@ metaItemDisplayName[urnopenremoteassetmetadisabled]=Disabled
metaItemDisplayName[urnopenremoteassetmetaexecutable]=Executable
metaItemDisplayName[urnopenremoteassetmetaallowedValues]=Allowed values
metaItemDisplayName[urnopenremoteassetmetadataPointsMaxAgeDays]=Data points max age (days)
metaItemDisplayName[urnopenremoteprotocolvalueFilters]=Value filters
metaItemDisplayName[urnopenremoteassetmetavalueFilters]=Value filters
metaItemDisplayName[urnopenremoteprotocolpollingMillis]=Polling interval (ms)
metaItemDisplayName[urnopenremoteprotocolwriteValue]=Write value
metaItemDisplayName[urnopenremoteprotocolcharset]=Charset
Expand Down
Expand Up @@ -1540,13 +1540,13 @@ protected void replyWithAttributeEvents(String sessionKey, String subscriptionId
.filter(Optional::isPresent)
.map(Optional::get)
.toArray(AttributeEvent[]::new);
TriggeredEventSubscription<AttributeEvent> triggeredEventSubscription = new TriggeredEventSubscription<>(events, subscriptionId);
TriggeredEventSubscription triggeredEventSubscription = new TriggeredEventSubscription(events, subscriptionId);
clientEventService.sendToSession(sessionKey, triggeredEventSubscription);
}

protected void replyWithAssetEvent(String sessionKey, String subscriptionId, Asset asset) {
AssetEvent event = new AssetEvent(AssetEvent.Cause.READ,asset, null);
TriggeredEventSubscription<AssetEvent> triggeredEventSubscription = new TriggeredEventSubscription<>(new AssetEvent[] {event}, subscriptionId);
TriggeredEventSubscription triggeredEventSubscription = new TriggeredEventSubscription(new AssetEvent[] {event}, subscriptionId);
clientEventService.sendToSession(sessionKey, triggeredEventSubscription);
}

Expand Down
Expand Up @@ -198,7 +198,7 @@ public <T extends SharedEvent> List<Message> splitForSubscribers(Exchange exchan
LOG.fine("Creating message for subscribed session '" + sessionKey + "': " + event);
SharedEvent[] events = (SharedEvent[])Array.newInstance(event.getClass(), 1);
events[0] = event;
TriggeredEventSubscription triggeredEventSubscription = new TriggeredEventSubscription<>(events, sessionSubscription.subscriptionId);
TriggeredEventSubscription triggeredEventSubscription = new TriggeredEventSubscription(events, sessionSubscription.subscriptionId);

if (sessionSubscription.subscription.getInternalConsumer() == null) {
Message msg = new DefaultMessage();
Expand Down
Expand Up @@ -95,7 +95,6 @@ public Optional<ArrayValue> getValueAsArray() {
return objectValue.getArray(VALUE_FIELD_NAME);
}


@JsonProperty("value")
private Value getValueInternal() {
return getValue().orElse(null);
Expand Down
2 changes: 0 additions & 2 deletions model/src/main/java/org/openremote/model/ValueHolder.java
Expand Up @@ -19,7 +19,6 @@
*/
package org.openremote.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.openremote.model.value.ArrayValue;
import org.openremote.model.value.ObjectValue;
import org.openremote.model.value.Value;
Expand All @@ -46,7 +45,6 @@ enum ValueFailureReason implements ValidationFailure.Reason {

void clearValue();

@JsonProperty
Optional<Value> getValue();

Optional<String> getValueAsString();
Expand Down
Expand Up @@ -21,6 +21,7 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.gwt.regexp.shared.RegExp;
import org.openremote.model.AbstractValueTimestampHolder;
Expand Down Expand Up @@ -137,7 +138,7 @@ public boolean hasMetaItems() {
.isPresent();
}

@JsonProperty
@JsonIgnore
public Meta getMeta() {
if (meta == null) {
return new Meta(getObjectValue()
Expand All @@ -152,7 +153,11 @@ public Meta getMeta() {
return meta;
}

@JsonIgnore
@JsonProperty("meta")
private List<MetaItem> getMetaInternal() {
return getMeta();
}

public Stream<MetaItem> getMetaStream() {
return getMeta().stream();
}
Expand Down Expand Up @@ -181,7 +186,7 @@ public Optional<MetaItem> getMetaItem(HasUniqueResourceName hasUniqueResourceNam
return getMetaItem(hasUniqueResourceName.getUrn());
}

@JsonProperty
@JsonProperty("meta")
public void setMeta(List<MetaItem> metaItems) {
Meta meta;
if (metaItems == null) {
Expand Down
15 changes: 12 additions & 3 deletions model/src/main/java/org/openremote/model/attribute/MetaItem.java
Expand Up @@ -19,6 +19,9 @@
*/
package org.openremote.model.attribute;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gwt.regexp.shared.RegExp;
import org.openremote.model.AbstractValueHolder;
Expand Down Expand Up @@ -62,7 +65,8 @@ public MetaItem(ObjectValue objectValue) {
super(objectValue);
}

public MetaItem(String name, Value value) {
@JsonCreator
public MetaItem(@JsonProperty("name") String name, @JsonProperty("value") Value value) {
super(Values.createObject());
setName(name);
setValue(value);
Expand All @@ -78,12 +82,17 @@ public MetaItem(HasUniqueResourceName hasUniqueResourceName, Value value) {
this(hasUniqueResourceName.getUrn(), value);
}

@JsonProperty
@JsonIgnore
public Optional<String> getName() {
return getObjectValue().getString("name");
}

@JsonProperty
@JsonProperty("name")
// This has to be after the ignored getter or it won't work with GWT-JACKSON
private String getNameInternal() {
return getName().orElse(null);
}

public void setName(String name) {
getObjectValue().put("name", TextUtil.requireNonNullAndNonEmpty(name));
}
Expand Down
Expand Up @@ -25,18 +25,18 @@

import java.util.Arrays;

public class TriggeredEventSubscription<E extends SharedEvent> {
public class TriggeredEventSubscription {

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

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

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

Expand Down
Expand Up @@ -50,7 +50,7 @@ public class EventSubscription<E extends SharedEvent> {
* Optional only set when an internal subscription is made
*/
@JsonIgnore
protected Consumer<TriggeredEventSubscription<E>> internalConsumer;
protected Consumer<TriggeredEventSubscription> internalConsumer;

protected EventSubscription() {
}
Expand All @@ -72,13 +72,13 @@ public EventSubscription(String eventType, EventFilter<E> filter) {
this.filter = filter;
}

public EventSubscription(Class<E> eventClass, EventFilter<E> filter, Consumer<TriggeredEventSubscription<E>> internalConsumer) {
public EventSubscription(Class<E> eventClass, EventFilter<E> filter, Consumer<TriggeredEventSubscription> internalConsumer) {
this.eventType = Event.getEventType(eventClass);
this.filter = filter;
this.internalConsumer = internalConsumer;
}

public EventSubscription(Class<E> eventClass, EventFilter<E> filter, String subscriptionId, Consumer<TriggeredEventSubscription<E>> internalConsumer) {
public EventSubscription(Class<E> eventClass, EventFilter<E> filter, String subscriptionId, Consumer<TriggeredEventSubscription> internalConsumer) {
this.eventType = Event.getEventType(eventClass);
this.filter = filter;
this.subscriptionId = subscriptionId;
Expand All @@ -105,7 +105,7 @@ public boolean isEventType(Class<? extends Event> eventClass) {
return Event.getEventType(eventClass).equals(getEventType());
}

public Consumer<TriggeredEventSubscription<E>> getInternalConsumer() {
public Consumer<TriggeredEventSubscription> getInternalConsumer() {
return internalConsumer;
}

Expand Down
Expand Up @@ -57,13 +57,6 @@
include = JsonTypeInfo.As.PROPERTY,
property = "eventType"
)
@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.ANY,
creatorVisibility= JsonAutoDetect.Visibility.NONE,
getterVisibility= JsonAutoDetect.Visibility.NONE,
setterVisibility= JsonAutoDetect.Visibility.NONE,
isGetterVisibility= JsonAutoDetect.Visibility.NONE
)
public abstract class SharedEvent extends Event {

public static final String MESSAGE_PREFIX = "EVENT:";
Expand Down
Expand Up @@ -19,15 +19,14 @@
*/
package org.openremote.model.simulator;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.openremote.model.ValidationFailure;
import org.openremote.model.ValueHolder;
import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.attribute.AttributeValueDescriptor;
import org.openremote.model.attribute.AttributeValueDescriptorImpl;
import org.openremote.model.attribute.AttributeValueType;
import org.openremote.model.simulator.element.ColorSimulatorElement;
import org.openremote.model.simulator.element.NumberSimulatorElement;
Expand Down Expand Up @@ -55,8 +54,7 @@ public abstract class SimulatorElement implements ValueHolder {

public AttributeRef attributeRef;
public AttributeValueDescriptor expectedType;
// TODO GWT jackson doesn't like fields with the same name as getters in the hierarchy
@JsonProperty("value")
@JsonIgnore
public Value elementValue = null;

protected SimulatorElement() {
Expand All @@ -75,11 +73,18 @@ public AttributeValueDescriptor getExpectedType() {
return expectedType;
}

@JsonIgnore
@Override
public Optional<Value> getValue() {
return Optional.ofNullable(elementValue);
}

@JsonProperty("value")
private Value getValueInternal() {
return getValue().orElse(null);
}

@JsonProperty("value")
@Override
public void setValue(Value value) {
this.elementValue = value;
Expand Down

0 comments on commit 18eedbf

Please sign in to comment.