@Override public PresenceState getPresenceState(final UnitType unitType) throws NotAvailableException { PresenceState.Builder builder = PresenceState.newBuilder().setValue(PresenceState.State.ABSENT); builder.getLastPresenceBuilder().setTime(0); for (PresenceStateProviderService provider : getServices(unitType)) { if (!((UnitRemote) provider).isDataAvailable()) { continue; } if (provider.getPresenceState().getValue() == PresenceState.State.PRESENT) { builder.setValue(PresenceState.State.PRESENT).build(); builder.getLastPresenceBuilder().setTime(Math.max(builder.getLastPresence().getTime(), provider.getPresenceState().getLastPresence().getTime())); } } return builder.build(); }
private synchronized void updatePresenceState(final PresenceStateOrBuilder presenceState) throws CouldNotPerformException { // TODO? // so wird das timeout durch das erste present setzten des detectors selbst nochmal restarted... // vorher nach motion state filtern (inklusive lastMotion) und wenn gleich das presence update skippen? // update Timestemp and reset timer if (presenceState.getValue() == PresenceState.State.PRESENT && this.presenceState.getLastPresence() != presenceState.getLastPresence()) { presenceTimeout.restart(); this.presenceState.getLastPresenceBuilder().setTime(Math.max(this.presenceState.getLastPresence().getTime(), presenceState.getLastPresence().getTime())); } // filter non state changes if (this.presenceState.getValue() == presenceState.getValue()) { return; } // update value TimestampProcessor.updateTimestampWithCurrentTime(presenceState, logger); this.presenceState.setValue(presenceState.getValue()); // notify try { presenceStateObservable.notifyObservers(this.presenceState.build()); } catch (CouldNotPerformException ex) { ExceptionPrinter.printHistory(new CouldNotPerformException("Could not update MotionState!", ex), logger, LogLevel.ERROR); } }
@Override public void expired() { try { // if motion is still detected just restart the timeout. if (locationDataProvider.getData().getMotionState().getValue() == MotionState.State.MOTION) { GlobalCachedExecutorService.submit(() -> { try { presenceTimeout.restart(); } catch (final CouldNotPerformException ex) { ExceptionPrinter.printHistory("Could not setup presence timeout!", ex, logger); } }); return; } updatePresenceState(PresenceState.newBuilder().setValue(PresenceState.State.ABSENT)); } catch (CouldNotPerformException ex) { ExceptionPrinter.printHistory(new CouldNotPerformException("Could not notify absent by timer!", ex), logger); } } };
private synchronized void updateMotionState(final MotionStateOrBuilder motionState) throws CouldNotPerformException { // Filter rush motion predictions. if (motionState.getValue() == MotionState.State.NO_MOTION) { return; } if (motionState.getValue() == MotionState.State.MOTION) { updatePresenceState(PresenceState.newBuilder().setValue(PresenceState.State.PRESENT).setLastPresence(motionState.getLastMotion())); } }