@Override protected void applyDataUpdate(MotionDetectorData.Builder internalBuilder, ServiceType serviceType) { switch (serviceType) { case MOTION_STATE_SERVICE: MotionState.Builder motionState = internalBuilder.getMotionStateBuilder(); // Update timestamp if necessary if (motionState.getValue() == MotionState.State.MOTION) { if (!motionState.hasTimestamp()) { logger.warn("State[" + motionState.getClass().getSimpleName() + "] of " + this + " does not contain any state related timestampe!"); motionState = TimestampProcessor.updateTimestampWithCurrentTime(motionState, logger); } motionState.setLastMotion(motionState.getTimestamp()); } else if(motionState.getValue() == MotionState.State.NO_MOTION && internalBuilder.getMotionStateLast().hasLastMotion()) { motionState.setLastMotion(internalBuilder.getMotionStateLast().getLastMotion()); } break; } } }
@Override public MotionState getMotionState(UnitType unitType) throws NotAvailableException { MotionState.State motionValue = MotionState.State.NO_MOTION; long lastMotion = 0; long timestamp = 0; for (MotionStateProviderService service : getServices(unitType)) { if (!((UnitRemote) service).isDataAvailable()) { continue; } MotionState motionState = service.getMotionState(); if (motionState.getValue() == MotionState.State.MOTION) { motionValue = MotionState.State.MOTION; } if (motionState.hasLastMotion() && motionState.getLastMotion().getTime() > lastMotion) { lastMotion = motionState.getLastMotion().getTime(); } timestamp = Math.max(timestamp, motionState.getTimestamp().getTime()); } return TimestampProcessor.updateTimestamp(timestamp, MotionState.newBuilder().setValue(motionValue).setLastMotion(Timestamp.newBuilder().setTime(lastMotion)), TimeUnit.MICROSECONDS, logger).build(); } }