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())); } } }
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 LocationRemote newInstance(final UnitConfig locationUnitConfig) throws org.openbase.jul.exception.InstantiationException, InterruptedException { try { LocationRemote locationRemote = Units.getUnit(locationUnitConfig, false, Units.LOCATION); locationRemote.addDataObserver((final Observable<LocationData> source, LocationData data) -> { // some locations do not have units for a given state so this state will not be set in LocationData and should not be updated in openhab if (data.hasColorState()) { openHABRemote.postUpdate(OpenHABCommandFactory.newHSBCommand(data.getColorState().getColor().getHsbColor()).setItem(generateItemId(locationUnitConfig, ServiceType.COLOR_STATE_SERVICE)).build()); } if (data.hasPowerState()) { openHABRemote.postUpdate(OpenHABCommandFactory.newOnOffCommand(data.getPowerState().getValue()).setItem(generateItemId(locationUnitConfig, ServiceType.POWER_STATE_SERVICE)).build()); } if (data.hasPowerConsumptionState()) { openHABRemote.postUpdate(OpenHABCommandFactory.newDecimalCommand(data.getPowerConsumptionState().getConsumption()).setItem(generateItemId(locationUnitConfig, ServiceType.POWER_CONSUMPTION_STATE_SERVICE)).build()); } if (data.hasMotionState()) { // post a 1 for motion and a 0 for no motion double number = data.getMotionState().getValue() == MotionStateType.MotionState.State.MOTION ? 1.0 : 0.0; openHABRemote.postUpdate(OpenHABCommandFactory.newDecimalCommand(number).setItem(generateItemId(locationUnitConfig, ServiceType.MOTION_STATE_SERVICE)).build()); } if (data.hasTemperatureState()) { openHABRemote.postUpdate(OpenHABCommandFactory.newDecimalCommand(data.getTemperatureState().getTemperature()).setItem(generateItemId(locationUnitConfig, ServiceType.TEMPERATURE_STATE_SERVICE)).build()); } }); return locationRemote; } catch (CouldNotPerformException ex) { throw new org.openbase.jul.exception.InstantiationException(LocationRemote.class, ex); } }
@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); } } } } }
@Override default PresenceState getPresenceState() throws NotAvailableException { try { return getData().getPresenceState(); } catch (CouldNotPerformException ex) { throw new NotAvailableException("PresenceState", ex); } }
@Override public void activate() throws CouldNotPerformException, InterruptedException { if (locationDataObserver == null) { throw new NotInitializedException(this); } active = true; locationDataProvider.addDataObserver(locationDataObserver); // start initial timeout presenceTimeout.start(); updateMotionState(locationDataProvider.getData().getMotionState()); }
@Override public StandbyState getStandbyState() throws NotAvailableException { try { return this.getData().getStandbyState(); } catch (CouldNotPerformException ex) { throw new NotAvailableException("StandbyState", ex); } } }
public PersonLightProviderAgent() throws InstantiationException, CouldNotPerformException, InterruptedException { super(PersonLightProviderAgent.class); locationObserver = (final Observable<LocationDataType.LocationData> source, LocationDataType.LocationData data) -> { try { notifyPresenceStateChanged(data.getPresenceState()); } catch (CouldNotPerformException ex) { ExceptionPrinter.printHistory(new CouldNotPerformException("Could not notify presence state change!", ex), logger); } }; }
@Override public StandbyState getStandbyState() throws NotAvailableException { try { return getData().getStandbyState(); } catch (CouldNotPerformException ex) { throw new NotAvailableException("StandbyState", ex); } }