+ @Override
+ public Future<RpcResult<GetStatisticsWorkModeOutput>> getStatisticsWorkMode() {
+ GetStatisticsWorkModeOutputBuilder smModeOutputBld = new GetStatisticsWorkModeOutputBuilder();
+ smModeOutputBld.setMode(workMode);
+ return RpcResultBuilder.success(smModeOutputBld.build()).buildFuture();
+ }
+
+ @Override
+ public Future<RpcResult<Void>> changeStatisticsWorkMode(ChangeStatisticsWorkModeInput input) {
+ final Future<RpcResult<Void>> result;
+ // acquire exclusive access
+ if (workModeGuard.tryAcquire()) {
+ final StatisticsWorkMode targetWorkMode = input.getMode();
+ if (!workMode.equals(targetWorkMode)) {
+ shuttingDownStatisticsPolling = StatisticsWorkMode.FULLYDISABLED.equals(targetWorkMode);
+ // iterate through stats-ctx: propagate mode
+ for (Map.Entry<DeviceContext, StatisticsContext> contextEntry : contexts.entrySet()) {
+ final DeviceContext deviceContext = contextEntry.getKey();
+ final StatisticsContext statisticsContext = contextEntry.getValue();
+ switch (targetWorkMode) {
+ case COLLECTALL:
+ scheduleNextPolling(deviceContext, statisticsContext, new TimeCounter());
+ for (ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
+ lifeCycleSource.setItemLifecycleListener(null);
+ }
+ break;
+ case FULLYDISABLED:
+ final Optional<Timeout> pollTimeout = statisticsContext.getPollTimeout();
+ if (pollTimeout.isPresent()) {
+ pollTimeout.get().cancel();
+ }
+ for (ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
+ lifeCycleSource.setItemLifecycleListener(statisticsContext.getItemLifeCycleListener());
+ }
+ break;
+ default:
+ LOG.warn("statistics work mode not supported: {}", targetWorkMode);
+ }
+ }
+ workMode = targetWorkMode;
+ }
+ workModeGuard.release();
+ result = RpcResultBuilder.<Void>success().buildFuture();
+ } else {
+ result = RpcResultBuilder.<Void>failed()
+ .withError(RpcError.ErrorType.APPLICATION, "mode change already in progress")
+ .buildFuture();
+ }
+ return result;
+ }
+
+ @Override
+ public void close() {
+ if (controlServiceRegistration != null) {
+ controlServiceRegistration.close();
+ controlServiceRegistration = null;
+ }
+ }