@Override protected void updateDynamicComponents() { try { stateLabel.setText(StringProcessor.transformUpperCaseToCamelCase(getProviderService().getPresenceState().getValue().name())); } catch (CouldNotPerformException ex) { ExceptionPrinter.printHistory(ex, logger); switch (getProviderService().getPresenceState().getValue()) { case ABSENT: presenceStatePanel.setForeground(Color.LIGHT_GRAY); break; default: throw new InvalidStateException("State[" + getProviderService().getPresenceState().getValue() + "] is unknown."); if(getProviderService().getPresenceState().getLastPresence().getTime() <= 0) { lastPresenceValueLabel.setText("Never"); } else { lastPresenceValueLabel.setText(dateFormat.format(new Date(TimestampJavaTimeTransform.transform(getProviderService().getPresenceState().getLastPresence())))); ExceptionPrinter.printHistory(new CouldNotPerformException("Could not format: [" + getProviderService().getPresenceState().getLastPresence().getTime() + "]!", ex), logger, LogLevel.ERROR);
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); } } };
public void triggerPresenceChange(LocationDataType.LocationData data) throws InterruptedException { synchronized (standbySync) { if (data.getPresenceState().getValue().equals(PresenceStateType.PresenceState.State.PRESENT) && timeout.isActive()) { timeout.cancel(); try { locationRemote.setStandbyState(State.RUNNING); } catch (CouldNotPerformException ex) { ExceptionPrinter.printHistory(new CouldNotPerformException("Could not notify motion state change!", ex), logger); } } else if (data.getPresenceState().getValue().equals(PresenceStateType.PresenceState.State.ABSENT) && !timeout.isActive()) { try { timeout.start(); } catch (CouldNotPerformException ex) { ExceptionPrinter.printHistory(new CouldNotPerformException("Could not schedule presence timeout!", ex), logger); } } } } }
private void verifyCondition() { try { if (locationRemote.getData().getPresenceState().getValue().equals(PresenceState.State.PRESENT) && (connectionRemote.getDoorState().getValue().equals(DoorState.State.OPEN) || connectionRemote.getWindowState().getValue().equals(WindowState.State.OPEN))) { notifyChange(TimestampProcessor.updateTimestampWithCurrentTime(ActivationState.newBuilder().setValue(ActivationState.State.ACTIVE).build())); } else { notifyChange(TimestampProcessor.updateTimestampWithCurrentTime(ActivationState.newBuilder().setValue(ActivationState.State.DEACTIVE).build())); } } catch (CouldNotPerformException ex) { ExceptionPrinter.printHistory("Could not verify trigger state " + this, ex, LoggerFactory.getLogger(getClass())); } } }
@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 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())); } }
/** * Method returns the state source(s) result(s) (contains state value(s)) of the input presenceState. * * @param presenceState The PresenceState. * @return state source(s) result(s) of the input state. */ private List<RdfNodeObject> presenceStateSources(final PresenceState presenceState) { final List<RdfNodeObject> stateSources = new ArrayList<>(); final String presenceStateVal = presenceState.getValue().toString(); stateSources.add(new RdfNodeObject(new ArrayList<String>() {{add(presenceStateVal);}}, false)); return stateSources; }
@Override public void update(Observable<PresenceState> source, PresenceState data) throws Exception { try (ClosableDataBuilder<LocationData.Builder> dataBuilder = getDataBuilder(this)) { LOGGER.debug("Set " + this + " presence to [" + data.getValue() + "]"); dataBuilder.getInternalBuilder().setPresenceState(data); } catch (CouldNotPerformException ex) { throw new CouldNotPerformException("Could not apply presense state change!", ex); } } });