/** * Creates a new scheduled {@code serviceType} simulator to simulate the given {@code @unitController}. * * @param unitController the unit to simulate. * @param serviceType the service type to simulate. * @param changeRate the simulation update speed in milliseconds. */ public AbstractScheduledServiceSimulator(final UnitController unitController, final ServiceType serviceType, final long changeRate) { this.changeRate = (isBenchmarkDetected() ? BENCHMARK_CHANGE_RATE : changeRate); this.unitController = unitController; this.simulationTask = () -> { final SERVICE_STATE serviceState; try { serviceState = getNextServiceState(); } catch (NotAvailableException ex) { LOGGER.warn("No more further service states are available. Simulation task will be terminated."); if (simulationTaskFuture != null) { simulationTaskFuture.cancel(true); } return; } // apply random service manipulation try { // randomly select one of the registered service states, update the service state timestamp and apply the state update on unit controller. unitController.applyDataUpdate(TimestampProcessor.updateTimestampWithCurrentTime(serviceState), serviceType); } catch (CouldNotPerformException ex) { ExceptionPrinter.printHistory("Could not apply service modification!", ex, LOGGER); } }; }
public Consumer(final UnitController<?,?> unitController) { this.active = false; this.serviceStateObserver = (Observer) (final Observable source, final Object data) -> { try { ActionDescription responsibleAction = Services.getResponsibleAction(((GeneratedMessage) data)); // build consumer action out of responsible action ActionDescription.Builder consumerActionBuilder = responsibleAction.toBuilder(); consumerActionBuilder.getServiceStateDescriptionBuilder().setUnitId(unitController.getId()); consumerActionBuilder.getServiceStateDescriptionBuilder().setUnitType(unitController.getUnitType()); // apply new action unitController.applyAction(consumerActionBuilder.build()); } catch (CouldNotPerformException ex) { ExceptionPrinter.printHistory("Could not consume update!", ex, LOGGER); } }; }
private <D extends GeneratedMessage, DB extends D.Builder<DB>> void updateUnitSimulators(final Collection<UnitController<D, DB>> unitControllerList) throws InterruptedException { synchronized (UNIT_SIMULATOR_MONITOR) { final List<String> previousUnitKeyList = new ArrayList<>(unitSimulatorMap.keySet()); for (UnitController<?, ?> unitController : unitControllerList) { try { previousUnitKeyList.remove(unitController.getId()); // filter already registered controller if (unitSimulatorMap.containsKey(unitController.getId())) { continue; } // register new controller GenericUnitSimulator genericUnitSimulator = new GenericUnitSimulator(unitController); unitSimulatorMap.put(unitController.getId(), genericUnitSimulator); if (isActive()) { genericUnitSimulator.activate(); } } catch (CouldNotPerformException | NullPointerException ex) { ExceptionPrinter.printHistory("Could not handle " + unitController + " update!", ex, LOGGER); } } // remove outdated controller for (String unitId : previousUnitKeyList) { try { unitSimulatorMap.remove(unitId).deactivate(); } catch (CouldNotPerformException | NullPointerException ex) { ExceptionPrinter.printHistory("Could not deactivate simulation of Unit[" + unitId + "]", ex, LOGGER); } } } }
private void initServiceSimulators(final UnitController unitController) throws InitializationException, InterruptedException { try { final MultiException.ExceptionStack exceptionStack = new MultiException.ExceptionStack(); for (final ServiceDescription serviceDescription : unitController.getUnitTemplate().getServiceDescriptionList()) { try { serviceSimulatorList.add(serviceSimulatorFactory.newInstance(unitController, serviceDescription.getType())); } catch (final CouldNotPerformException ex) { MultiException.push(this, ex, exceptionStack); } } try { MultiException.checkAndThrow("Could not init all service simulators!", exceptionStack); } catch (CouldNotPerformException ex) { ExceptionPrinter.printHistory(ex, LOGGER); } } catch (CouldNotPerformException ex) { throw new InitializationException(this, ex); } }
public void receiveUpdate(OpenhabCommandType.OpenhabCommand command) throws CouldNotPerformException { LOGGER.info("receiveUpdate [" + command.getItem() + "=" + command.getType() + "]"); OpenhabCommandMetaData metaData = new OpenhabCommandMetaData(command); Object serviceData = OpenhabCommandTransformer.getServiceData(command, metaData.getServiceType()); if (serviceData == null) { return; } final UnitController unitController; try { unitController = unitControllerRegistry.getUnitByScope(metaData.getUnitScope()); } catch (NotAvailableException ex) { if (!unitControllerRegistry.isInitiallySynchronized()) { LOGGER.debug("ItemUpdate[" + command.getItem() + "=" + command.getType() + "] skipped because controller registry was not ready yet!"); return; } throw ex; } unitController.applyDataUpdate(serviceData, metaData.getServiceType()); } }