use of org.openremote.container.timer.TimerService in project openremote by openremote.
the class Main method main.
public static void main(String[] args) throws Exception {
List<ContainerService> services = new ArrayList<ContainerService>() {
{
addAll(Arrays.asList(new TimerService(), new ManagerExecutorService(), new I18NService(), new ManagerPersistenceService(), new MessageBrokerSetupService(), new ManagerIdentityService(), new SetupService(), new ClientEventService(), new RulesetStorageService(), new RulesService(), new AssetStorageService(), new AssetDatapointService(), new AssetAttributeLinkingService(), new AssetProcessingService(), new MessageBrokerService()));
ServiceLoader.load(Protocol.class).forEach(this::add);
addAll(Arrays.asList(new AgentService(), new SimulatorService(), new MapService(), new NotificationService(), new ConsoleAppService(), new ManagerWebService()));
}
};
new Container(services).startBackground();
}
use of org.openremote.container.timer.TimerService in project openremote by openremote.
the class AssetProcessingService method init.
@Override
public void init(Container container) throws Exception {
timerService = container.getService(TimerService.class);
identityService = container.getService(ManagerIdentityService.class);
persistenceService = container.getService(PersistenceService.class);
rulesService = container.getService(RulesService.class);
agentService = container.getService(AgentService.class);
assetStorageService = container.getService(AssetStorageService.class);
assetDatapointService = container.getService(AssetDatapointService.class);
assetAttributeLinkingService = container.getService(AssetAttributeLinkingService.class);
messageBrokerService = container.getService(MessageBrokerService.class);
clientEventService = container.getService(ClientEventService.class);
clientEventService.addSubscriptionAuthorizer((auth, subscription) -> {
if (!subscription.isEventType(AttributeEvent.class)) {
return false;
}
// Always must have a filter, as you can't subscribe to ALL asset attribute events
if (subscription.getFilter() != null && subscription.getFilter() instanceof AttributeEvent.EntityIdFilter) {
AttributeEvent.EntityIdFilter filter = (AttributeEvent.EntityIdFilter) subscription.getFilter();
// Superuser can get attribute events for any asset
if (auth.isSuperUser())
return true;
// Regular user must have role
if (!auth.hasResourceRole(ClientRole.READ_ASSETS.getValue(), Constants.KEYCLOAK_CLIENT_ID)) {
return false;
}
boolean isRestrictedUser = identityService.getIdentityProvider().isRestrictedUser(auth.getUserId());
// Client can subscribe to several assets
for (String assetId : filter.getEntityId()) {
Asset asset = assetStorageService.find(assetId);
// If the asset doesn't exist, subscription must fail
if (asset == null)
return false;
if (isRestrictedUser) {
// Restricted users can only get attribute events for their linked assets
if (!assetStorageService.isUserAsset(auth.getUserId(), assetId))
return false;
// TODO Restricted clients should only receive events for RESTRICTED_READ attributes!
} else {
// Regular users can only get attribute events for assets in their realm
if (!asset.getTenantRealm().equals(auth.getAuthenticatedRealm()))
return false;
}
}
return true;
}
return false;
});
processors.add(agentService);
processors.add(rulesService);
processors.add(assetDatapointService);
processors.add(assetAttributeLinkingService);
container.getService(MessageBrokerSetupService.class).getContext().addRoutes(this);
}
use of org.openremote.container.timer.TimerService in project openremote by openremote.
the class AbstractProtocol method start.
@Override
public void start(Container container) throws Exception {
timerService = container.getService(TimerService.class);
executorService = container.getExecutorService();
assetService = container.getService(ProtocolAssetService.class);
predictedAssetService = container.getService(ProtocolPredictedAssetService.class);
messageBrokerContext = container.getService(MessageBrokerService.class).getContext();
withLock(getProtocolName() + "::start", () -> {
try {
messageBrokerContext.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from(ACTUATOR_TOPIC).routeId("Actuator-" + getProtocolName() + getAgent().getId()).process(exchange -> {
Protocol<?> protocolInstance = exchange.getIn().getHeader(ACTUATOR_TOPIC_TARGET_PROTOCOL, Protocol.class);
if (protocolInstance != AbstractProtocol.this) {
return;
}
AttributeEvent event = exchange.getIn().getBody(AttributeEvent.class);
Attribute<?> linkedAttribute = getLinkedAttributes().get(event.getAttributeRef());
if (linkedAttribute == null) {
LOG.info("Attempt to write to attribute that is not actually linked to this protocol '" + AbstractProtocol.this + "': " + linkedAttribute);
return;
}
processLinkedAttributeWrite(event);
});
}
});
doStart(container);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
});
this.producerTemplate = container.getService(MessageBrokerService.class).getProducerTemplate();
}
use of org.openremote.container.timer.TimerService in project openremote by openremote.
the class ClientEventService method init.
@Override
public void init(Container container) throws Exception {
timerService = container.getService(TimerService.class);
messageBrokerService = container.getService(MessageBrokerService.class);
identityService = container.getService(ManagerIdentityService.class);
gatewayService = container.getService(GatewayService.class);
eventSubscriptions = new EventSubscriptions(container.getService(TimerService.class));
messageBrokerService.getContext().getTypeConverterRegistry().addTypeConverters(new EventTypeConverters());
// TODO: Remove prefix and just use event type then use a subscription wrapper to pass subscription ID around
messageBrokerService.getContext().addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("websocket://" + WEBSOCKET_EVENTS).routeId("FromClientWebsocketEvents").process(exchange -> exchange.getIn().setHeader(HEADER_CONNECTION_TYPE, HEADER_CONNECTION_TYPE_WEBSOCKET)).to(ClientEventService.CLIENT_EVENT_QUEUE).end();
from(ClientEventService.CLIENT_EVENT_QUEUE).routeId("ClientEvents").choice().when(header(ConnectionConstants.SESSION_OPEN)).process(exchange -> {
String sessionKey = getSessionKey(exchange);
sessionKeyInfoMap.put(sessionKey, createSessionInfo(sessionKey, exchange));
passToInterceptors(exchange);
}).stop().when(or(header(ConnectionConstants.SESSION_CLOSE), header(ConnectionConstants.SESSION_CLOSE_ERROR))).process(exchange -> {
String sessionKey = getSessionKey(exchange);
sessionKeyInfoMap.remove(sessionKey);
eventSubscriptions.cancelAll(sessionKey);
passToInterceptors(exchange);
}).stop().end().process(exchange -> {
// Do basic formatting of exchange
EventRequestResponseWrapper<?> requestResponse = null;
if (exchange.getIn().getBody() instanceof EventRequestResponseWrapper) {
requestResponse = exchange.getIn().getBody(EventRequestResponseWrapper.class);
} else if (exchange.getIn().getBody() instanceof String && exchange.getIn().getBody(String.class).startsWith(EventRequestResponseWrapper.MESSAGE_PREFIX)) {
requestResponse = exchange.getIn().getBody(EventRequestResponseWrapper.class);
}
if (requestResponse != null) {
SharedEvent event = requestResponse.getEvent();
exchange.getIn().setHeader(HEADER_REQUEST_RESPONSE_MESSAGE_ID, requestResponse.getMessageId());
exchange.getIn().setBody(event);
}
if (exchange.getIn().getBody() instanceof String) {
String bodyStr = exchange.getIn().getBody(String.class);
if (bodyStr.startsWith(EventSubscription.SUBSCRIBE_MESSAGE_PREFIX)) {
exchange.getIn().setBody(exchange.getIn().getBody(EventSubscription.class));
} else if (bodyStr.startsWith(CancelEventSubscription.MESSAGE_PREFIX)) {
exchange.getIn().setBody(exchange.getIn().getBody(CancelEventSubscription.class));
} else if (bodyStr.startsWith(SharedEvent.MESSAGE_PREFIX)) {
exchange.getIn().setBody(exchange.getIn().getBody(SharedEvent.class));
}
}
if (exchange.getIn().getBody() instanceof SharedEvent) {
SharedEvent event = exchange.getIn().getBody(SharedEvent.class);
// If there is no timestamp in event, set to system time
if (event.getTimestamp() <= 0) {
event.setTimestamp(timerService.getCurrentTimeMillis());
}
}
}).process(exchange -> passToInterceptors(exchange)).choice().when(body().isInstanceOf(EventSubscription.class)).process(exchange -> {
String sessionKey = getSessionKey(exchange);
EventSubscription<?> subscription = exchange.getIn().getBody(EventSubscription.class);
AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);
boolean restrictedUser = identityService.getIdentityProvider().isRestrictedUser(authContext);
boolean anonymousUser = authContext == null;
String username = authContext == null ? "anonymous" : authContext.getUsername();
String realm = exchange.getIn().getHeader(Constants.REALM_PARAM_NAME, String.class);
if (authorizeEventSubscription(realm, authContext, subscription)) {
eventSubscriptions.createOrUpdate(sessionKey, restrictedUser, anonymousUser, subscription);
subscription.setSubscribed(true);
sendToSession(sessionKey, subscription);
} else {
LOG.warning("Unauthorized subscription from '" + username + "' in realm '" + realm + "': " + subscription);
sendToSession(sessionKey, new UnauthorizedEventSubscription<>(subscription));
}
}).stop().when(body().isInstanceOf(CancelEventSubscription.class)).process(exchange -> {
String sessionKey = getSessionKey(exchange);
eventSubscriptions.cancel(sessionKey, exchange.getIn().getBody(CancelEventSubscription.class));
}).stop().when(body().isInstanceOf(SharedEvent.class)).choice().when(// Inbound messages from clients
header(HEADER_CONNECTION_TYPE).isNotNull()).to(ClientEventService.CLIENT_EVENT_TOPIC).stop().when(// Outbound message to clients
header(HEADER_CONNECTION_TYPE).isNull()).split(method(eventSubscriptions, "splitForSubscribers")).process(exchange -> {
String sessionKey = getSessionKey(exchange);
sendToSession(sessionKey, exchange.getIn().getBody());
}).stop().endChoice().otherwise().process(exchange -> LOG.info("Unsupported message body: " + exchange.getIn().getBody())).end();
}
});
// Add pending internal subscriptions
if (!pendingInternalSubscriptions.isEmpty()) {
pendingInternalSubscriptions.forEach(subscription -> eventSubscriptions.createOrUpdate(INTERNAL_SESSION_KEY, false, false, subscription));
}
pendingInternalSubscriptions = null;
}
use of org.openremote.container.timer.TimerService in project openremote by openremote.
the class AssetStorageService method init.
@Override
public void init(Container container) throws Exception {
timerService = container.getService(TimerService.class);
persistenceService = container.getService(PersistenceService.class);
identityService = container.getService(ManagerIdentityService.class);
clientEventService = container.getService(ClientEventService.class);
gatewayService = container.getService(GatewayService.class);
EventSubscriptionAuthorizer assetEventAuthorizer = AssetStorageService.assetInfoAuthorizer(identityService, this);
clientEventService.addSubscriptionAuthorizer((realm, auth, subscription) -> {
if (!subscription.isEventType(AssetEvent.class)) {
return false;
}
return assetEventAuthorizer.authorise(realm, auth, subscription);
});
container.getService(ManagerWebService.class).addApiSingleton(new AssetResourceImpl(container.getService(TimerService.class), identityService, this, container.getService(MessageBrokerService.class)));
container.getService(ManagerWebService.class).addApiSingleton(new ConsoleResourceImpl(container.getService(TimerService.class), identityService, this, clientEventService));
container.getService(MessageBrokerService.class).getContext().addRoutes(this);
}
Aggregations