protected Object compute(SensorEvent<T> event) { // transformation is not going to change, but this design makes it easier to support changing config in future. // if it's an efficiency hole we can switch to populate the transformation at start. U result = getTransformation().apply(event); if (LOG.isTraceEnabled()) LOG.trace("Enricher "+this+" computed "+result+" from "+event); return result; } }
super.setEntity(entity); this.producer = getConfig(PRODUCER) == null ? entity: getConfig(PRODUCER); this.sourceSensor = (Sensor<T>) getConfig(SOURCE_SENSOR); Sensor<?> targetSensorSpecified = getConfig(TARGET_SENSOR); List<? extends Sensor<?>> triggerSensorsSpecified = getConfig(TRIGGER_SENSORS); List<? extends Sensor<?>> triggerSensors = triggerSensorsSpecified != null ? triggerSensorsSpecified : ImmutableList.<Sensor<?>>of(); this.targetSensor = targetSensorSpecified!=null ? (Sensor<U>) targetSensorSpecified : (Sensor<U>) this.sourceSensor; boolean allowCyclicPublishing = Boolean.TRUE.equals(getConfig(ALLOW_CYCLIC_PUBLISHING)); if (allowCyclicPublishing) { LOG.debug("Permitting cyclic publishing, though detected enricher will read and publish on the same sensor: "+ subscriptions().subscribe(MutableMap.of("notifyOfInitialValue", true), producer, sourceSensor, this); subscriptions().subscribe(MutableMap.of("notifyOfInitialValue", true), producer, (Sensor<?>)sensor, triggerListener); highlightTriggers(MutableList.<Sensor<?>>of(sourceSensor).appendAll(triggerSensors), producer);
@Override public void setEntity(EntityLocal entity) { super.setEntity(entity); // Check that sourceSensor has been set (rather than triggerSensors) getRequiredConfig(SOURCE_SENSOR); }
@Override public void onEvent(SensorEvent<Object> event) { if (sourceSensor != null) { // Simulate an event, as though our sourceSensor changed Object value = producer.getAttribute((AttributeSensor<?>)sourceSensor); AbstractTransformer.this.onEvent(new BasicSensorEvent(sourceSensor, producer, value, event.getTimestamp())); } else { // Assume the transform doesn't care about the value - otherwise it would // have declared a sourceSensor! AbstractTransformer.this.onEvent(null); } } };
@Override public void setEntity(EntityLocal entity) { super.setEntity(entity); // Check that sourceSensor has been set (rather than triggerSensors) getRequiredConfig(SOURCE_SENSOR); }