@Override protected void updateDynamicComponents() { try { switch (getProviderService().getMotionState().getValue()) { case MOTION: motionStatusLabel.setForeground(Color.BLACK); break; default: throw new InvalidStateException("State[" + getProviderService().getMotionState().getValue() + "] is unknown."); motionStatusLabel.setText(StringProcessor.transformUpperCaseToCamelCase(getProviderService().getMotionState().getValue().name())); try { if(getProviderService().getMotionState().getLastMotion().getTime() <= 0) { lastMovementValueLabel.setText("Never"); } else { lastMovementValueLabel.setText(dateFormat.format(new Date(TimestampJavaTimeTransform.transform(getProviderService().getMotionState().getLastMotion())))); ExceptionPrinter.printHistory(new CouldNotPerformException("Could not format: [" + getProviderService().getMotionState().getLastMotion().getTime() + "]!", ex), logger, LogLevel.ERROR);
public static MotionState transform(final Double decimalType) throws CouldNotTransformException { MotionState.Builder motionState = MotionState.newBuilder(); try { if (decimalType.intValue() == 0) { motionState.setValue(State.NO_MOTION); } else { motionState.setValue(State.MOTION); } return motionState.build(); } catch (Exception ex) { throw new CouldNotTransformException("Could not transform " + Double.class.getName() + "! " + Double.class.getSimpleName() + "[" + decimalType + "] is unknown!", ex); } }
@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); } } };
@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(); } }
public static Double transform(final MotionState motionState) throws CouldNotTransformException { try { switch (motionState.getValue()) { case NO_MOTION: return 0d; case MOTION: return 1d; case UNKNOWN: throw new InvalidStateException("Unknown state is invalid!"); default: throw new TypeNotSupportedException(MotionState.State.class, Double.class); } } catch (CouldNotPerformException ex) { throw new CouldNotTransformException("Could not transform " + MotionState.State.class.getName() + "!", ex); } } }
/** * Method returns the state source(s) result(s) (contains state value(s)) of the input motionState. * * @param motionState The MotionState. * @return state source(s) result(s) of the input state. */ private List<RdfNodeObject> motionStateSources(final MotionState motionState) { final List<RdfNodeObject> stateSources = new ArrayList<>(); final String motionStateVal = motionState.getValue().toString(); stateSources.add(new RdfNodeObject(new ArrayList<String>() {{add(motionStateVal);}}, false)); return stateSources; }