@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(); }
public PresenceDetector() { this.presenceState = PresenceState.newBuilder(); this.active = false; this.presenceStateObservable = new ObservableImpl<>(); this.presenceTimeout = new Timeout(PRESENCE_TIMEOUT) { @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); } } }; locationDataObserver = (Observable<LocationData> source, LocationData data) -> { updateMotionState(data.getMotionState()); }; }
@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())); } }