Search in sources :

Example 1 with SharedEvent

use of org.openremote.model.event.shared.SharedEvent in project openremote by openremote.

the class AdminSyslogActivity method fetchEvents.

protected void fetchEvents() {
    adminContent.setFormBusy(true);
    environment.getApp().getRequests().sendAndReturn(sharedEventArrayMapper, requestParams -> syslogResource.getEvents(requestParams, filterLevel, filterLimit), 200, events -> {
        List<SyslogEvent> syslogEvents = new ArrayList<>();
        for (SharedEvent event : events) {
            if (event instanceof SyslogEvent) {
                SyslogEvent syslogEvent = (SyslogEvent) event;
                syslogEvents.add(syslogEvent);
            }
        }
        adminContent.showEvents(syslogEvents.toArray(new SyslogEvent[syslogEvents.size()]));
        adminContent.setFormBusy(false);
        environment.getEventService().subscribe(SyslogEvent.class, new SyslogEvent.LevelCategoryFilter(filterLevel));
    });
}
Also used : SyslogEvent(org.openremote.model.syslog.SyslogEvent) ArrayList(java.util.ArrayList) SharedEvent(org.openremote.model.event.shared.SharedEvent)

Example 2 with SharedEvent

use of org.openremote.model.event.shared.SharedEvent in project openremote by openremote.

the class GatewayClientService method onCentralManagerMessage.

protected void onCentralManagerMessage(GatewayConnection connection, String message) {
    String messageId = null;
    SharedEvent event = null;
    if (message.startsWith(EventRequestResponseWrapper.MESSAGE_PREFIX)) {
        EventRequestResponseWrapper<?> wrapper = messageFromString(message, EventRequestResponseWrapper.MESSAGE_PREFIX, EventRequestResponseWrapper.class);
        messageId = wrapper.getMessageId();
        event = wrapper.getEvent();
    }
    if (message.startsWith(SharedEvent.MESSAGE_PREFIX)) {
        event = messageFromString(message, SharedEvent.MESSAGE_PREFIX, SharedEvent.class);
    }
    if (event != null) {
        if (event instanceof GatewayDisconnectEvent) {
            if (((GatewayDisconnectEvent) event).getReason() == GatewayDisconnectEvent.Reason.PERMANENT_ERROR) {
                LOG.info("Central manager requested disconnect due to permanent error (likely this version of the edge gateway software is not compatible with that manager version)");
                destroyGatewayClient(connection, clientRealmMap.get(connection.getLocalRealm()));
                clientRealmMap.put(connection.getLocalRealm(), null);
            }
        } else if (event instanceof AttributeEvent) {
            assetProcessingService.sendAttributeEvent((AttributeEvent) event, AttributeEvent.Source.INTERNAL);
        } else if (event instanceof AssetEvent) {
            AssetEvent assetEvent = (AssetEvent) event;
            if (assetEvent.getCause() == AssetEvent.Cause.CREATE || assetEvent.getCause() == AssetEvent.Cause.UPDATE) {
                Asset asset = assetEvent.getAsset();
                asset.setRealm(connection.getLocalRealm());
                LOG.finer("Request from central manager to create/update an asset: Realm=" + connection.getLocalRealm() + ", Asset<?> ID=" + asset.getId());
                try {
                    asset = assetStorageService.merge(asset, true);
                } catch (Exception e) {
                    LOG.log(Level.INFO, "Request from central manager to create/update an asset failed: Realm=" + connection.getLocalRealm() + ", Asset<?> ID=" + asset.getId(), e);
                }
            }
        } else if (event instanceof DeleteAssetsRequestEvent) {
            DeleteAssetsRequestEvent deleteRequest = (DeleteAssetsRequestEvent) event;
            LOG.finer("Request from central manager to delete asset(s): Realm=" + connection.getLocalRealm() + ", Asset<?> IDs=" + Arrays.toString(deleteRequest.getAssetIds().toArray()));
            boolean success = false;
            try {
                success = assetStorageService.delete(deleteRequest.getAssetIds());
            } catch (Exception e) {
                LOG.log(Level.INFO, "Request from central manager to create/update an asset failed: Realm=" + connection.getLocalRealm() + ", Asset<?> IDs=" + Arrays.toString(deleteRequest.getAssetIds().toArray()), e);
            } finally {
                sendCentralManagerMessage(connection.getLocalRealm(), messageToString(EventRequestResponseWrapper.MESSAGE_PREFIX, new EventRequestResponseWrapper<>(messageId, new DeleteAssetsResponseEvent(success, deleteRequest.getAssetIds()))));
            }
        } else if (event instanceof ReadAssetsEvent) {
            ReadAssetsEvent readAssets = (ReadAssetsEvent) event;
            AssetQuery query = readAssets.getAssetQuery();
            // Force realm to be the one that this client is associated with
            query.tenant(new TenantPredicate(connection.getLocalRealm()));
            List<Asset<?>> assets = assetStorageService.findAll(readAssets.getAssetQuery());
            sendCentralManagerMessage(connection.getLocalRealm(), messageToString(EventRequestResponseWrapper.MESSAGE_PREFIX, new EventRequestResponseWrapper<>(messageId, new AssetsEvent(assets))));
        }
    }
}
Also used : GatewayDisconnectEvent(org.openremote.model.gateway.GatewayDisconnectEvent) AssetQuery(org.openremote.model.query.AssetQuery) TenantPredicate(org.openremote.model.query.filter.TenantPredicate) AttributeEvent(org.openremote.model.attribute.AttributeEvent) SharedEvent(org.openremote.model.event.shared.SharedEvent) EventRequestResponseWrapper(org.openremote.model.event.shared.EventRequestResponseWrapper)

Example 3 with SharedEvent

use of org.openremote.model.event.shared.SharedEvent in project openremote by openremote.

the class DefaultMQTTHandler method doSubscribe.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void doSubscribe(MqttConnection connection, Topic topic, InterceptSubscribeMessage msg) {
    boolean isAssetTopic = isAssetTopic(topic);
    // Use topic as subscription ID
    String subscriptionId = msg.getTopicFilter();
    AssetFilter filter = buildAssetFilter(connection, topic);
    Class subscriptionClass = isAssetTopic ? AssetEvent.class : AttributeEvent.class;
    if (filter == null) {
        LOG.fine("Invalid event filter generated for topic '" + topic + "': " + connection);
        return;
    }
    Consumer<SharedEvent> eventConsumer = getSubscriptionEventConsumer(connection, topic, msg.getRequestedQos());
    EventSubscription subscription = new EventSubscription(subscriptionClass, filter, subscriptionId, eventConsumer);
    Map<String, Object> headers = prepareHeaders(connection);
    messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, subscription, headers);
}
Also used : CancelEventSubscription(org.openremote.model.event.shared.CancelEventSubscription) EventSubscription(org.openremote.model.event.shared.EventSubscription) SharedEvent(org.openremote.model.event.shared.SharedEvent) AssetFilter(org.openremote.model.asset.AssetFilter)

Example 4 with SharedEvent

use of org.openremote.model.event.shared.SharedEvent in project openremote by openremote.

the class EventSubscriptions method splitForSubscribers.

@SuppressWarnings({ "unchecked", "unused" })
public <T extends SharedEvent> List<Message> splitForSubscribers(Exchange exchange) {
    List<Message> messageList = new ArrayList<>();
    T event = (T) exchange.getIn().getBody(SharedEvent.class);
    if (event == null)
        return messageList;
    Set<Map.Entry<String, SessionSubscriptions>> sessionSubscriptionsSet;
    synchronized (this.sessionSubscriptionIdMap) {
        sessionSubscriptionsSet = new HashSet<>(sessionSubscriptionIdMap.entrySet());
    }
    for (Map.Entry<String, SessionSubscriptions> entry : sessionSubscriptionsSet) {
        String sessionKey = entry.getKey();
        SessionSubscriptions subscriptions = entry.getValue();
        for (SessionSubscription<?> sessionSubscription : subscriptions) {
            if (!sessionSubscription.matches(event))
                continue;
            SessionSubscription<T> sessionSub = (SessionSubscription<T>) sessionSubscription;
            if (sessionSub.subscription.getFilter() == null || sessionSub.subscription.getFilter().apply(event)) {
                LOG.finer("Creating message for subscribed session '" + sessionKey + "': " + event);
                List<T> events = Collections.singletonList(event);
                TriggeredEventSubscription<T> triggeredEventSubscription = new TriggeredEventSubscription<>(events, sessionSub.subscriptionId);
                if (sessionSub.subscription.getInternalConsumer() == null) {
                    Message msg = new DefaultMessage();
                    // Don't copy the event, use same reference
                    msg.setBody(triggeredEventSubscription);
                    // Copy headers
                    msg.setHeaders(new HashMap<>(exchange.getIn().getHeaders()));
                    msg.setHeader(ConnectionConstants.SESSION_KEY, sessionKey);
                    messageList.add(msg);
                } else {
                    if (triggeredEventSubscription.getEvents() != null) {
                        triggeredEventSubscription.getEvents().forEach(e -> sessionSub.subscription.getInternalConsumer().accept(e));
                    }
                }
            }
        }
    }
    return messageList;
}
Also used : DefaultMessage(org.apache.camel.impl.DefaultMessage) Message(org.apache.camel.Message) DefaultMessage(org.apache.camel.impl.DefaultMessage) SharedEvent(org.openremote.model.event.shared.SharedEvent) TriggeredEventSubscription(org.openremote.model.event.TriggeredEventSubscription)

Example 5 with SharedEvent

use of org.openremote.model.event.shared.SharedEvent in project openremote by openremote.

the class DefaultMQTTHandler method doUnsubscribe.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void doUnsubscribe(MqttConnection connection, Topic topic, InterceptUnsubscribeMessage msg) {
    String subscriptionId = topic.toString();
    boolean isAssetTopic = subscriptionId.startsWith(ASSET_TOPIC);
    Map<String, Object> headers = prepareHeaders(connection);
    Class<SharedEvent> subscriptionClass = (Class) (isAssetTopic ? AssetEvent.class : AttributeEvent.class);
    CancelEventSubscription cancelEventSubscription = new CancelEventSubscription(subscriptionClass, subscriptionId);
    messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, cancelEventSubscription, headers);
}
Also used : CancelEventSubscription(org.openremote.model.event.shared.CancelEventSubscription) SharedEvent(org.openremote.model.event.shared.SharedEvent)

Aggregations

SharedEvent (org.openremote.model.event.shared.SharedEvent)5 CancelEventSubscription (org.openremote.model.event.shared.CancelEventSubscription)2 ArrayList (java.util.ArrayList)1 Message (org.apache.camel.Message)1 DefaultMessage (org.apache.camel.impl.DefaultMessage)1 AssetFilter (org.openremote.model.asset.AssetFilter)1 AttributeEvent (org.openremote.model.attribute.AttributeEvent)1 TriggeredEventSubscription (org.openremote.model.event.TriggeredEventSubscription)1 EventRequestResponseWrapper (org.openremote.model.event.shared.EventRequestResponseWrapper)1 EventSubscription (org.openremote.model.event.shared.EventSubscription)1 GatewayDisconnectEvent (org.openremote.model.gateway.GatewayDisconnectEvent)1 AssetQuery (org.openremote.model.query.AssetQuery)1 TenantPredicate (org.openremote.model.query.filter.TenantPredicate)1 SyslogEvent (org.openremote.model.syslog.SyslogEvent)1