/** * Method returns the state source(s) result(s) (contains state value(s)) of the input colorState. * * @param colorState The ColorState. * @return state source(s) result(s) of the input state. */ private List<RdfNodeObject> colorStateSources(final ColorState colorState) { final List<RdfNodeObject> stateSources = new ArrayList<>(); if (colorState.getColor().hasHsbColor()) { final String hue = "\"" + colorState.getColor().getHsbColor().getHue() + "\"^^NS:Hue"; final String saturation = "\"" + colorState.getColor().getHsbColor().getSaturation() + "\"^^NS:Saturation"; final String brightness = "\"" + colorState.getColor().getHsbColor().getBrightness() + "\"^^NS:Brightness"; stateSources.add(new RdfNodeObject(new ArrayList<String>() {{addAll(Arrays.asList(hue, saturation, brightness));}}, true)); } else if (colorState.getColor().hasRgbColor()) { final int red = colorState.getColor().getRgbColor().getRed(); final int green = colorState.getColor().getRgbColor().getGreen(); final int blue = colorState.getColor().getRgbColor().getBlue(); final float[] hsb = Color.RGBtoHSB(red, green, blue, null); final String hue = "\"" + hsb[0] + "\"^^NS:Hue"; final String saturation = "\"" + hsb[1] + "\"^^NS:Saturation"; final String brightness = "\"" + hsb[2] + "\"^^NS:Brightness"; stateSources.add(new RdfNodeObject(new ArrayList<String>() {{addAll(Arrays.asList(brightness, saturation, hue));}}, true)); } else { LOGGER.error("Could not set colorValue of colorState. Color is not set!"); } return stateSources; }
/** * {@inheritDoc } * * @return {@inheritDoc } * @throws NotAvailableException {@inheritDoc } */ @Override protected ColorState getNextServiceState() throws NotAvailableException { return ColorState.newBuilder().setColor(Color.newBuilder().setHsbColor(HSBColorType.HSBColor.newBuilder() .setBrightness(RANDOM.nextInt(100)) .setSaturation(RANDOM.nextInt(100)) .setHue(RANDOM.nextInt(360)).build()).build()).build(); } }
@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 ColorState getColorState(final UnitType unitType) throws NotAvailableException { try { double averageRed = 0; double averageGreen = 0; double averageBlue = 0; int amount = getColorStateOperationServices().size(); long timestamp = 0; Collection<ColorStateOperationService> colorStateOperationServiceCollection = getServices(unitType); for (ColorStateOperationService service : colorStateOperationServiceCollection) { if (!((UnitRemote) service).isDataAvailable()) { amount--; continue; } RGBColor rgbColor = HSBColorToRGBColorTransformer.transform(service.getColorState().getColor().getHsbColor()); averageRed += rgbColor.getRed(); averageGreen += rgbColor.getGreen(); averageBlue += rgbColor.getBlue(); timestamp = Math.max(timestamp, service.getColorState().getTimestamp().getTime()); } averageRed = averageRed / amount; averageGreen = averageGreen / amount; averageBlue = averageBlue / amount; HSBColor hsbColor = HSBColorToRGBColorTransformer.transform(RGBColor.newBuilder().setRed((int) averageRed).setGreen((int) averageGreen).setBlue((int) averageBlue).build()); return TimestampProcessor.updateTimestamp(timestamp, ColorState.newBuilder().setColor(ColorType.Color.newBuilder().setType(ColorType.Color.Type.HSB).setHsbColor(hsbColor)), TimeUnit.MICROSECONDS, logger).build(); } catch (CouldNotTransformException ex) { throw new NotAvailableException("Could not transform from HSB to RGB or vice-versa!", ex); } }
@Override protected void applyDataUpdate(ColorableLightData.Builder internalBuilder, ServiceType serviceType) { switch (serviceType) { case COLOR_STATE_SERVICE: internalBuilder.getBrightnessStateBuilder().setBrightness(internalBuilder.getColorState().getColor().getHsbColor().getBrightness()); internalBuilder.getPowerStateBuilder().setValue(PowerState.State.ON); break; case BRIGHTNESS_STATE_SERVICE: HSBColor hsb = internalBuilder.getColorState().getColor().getHsbColor().toBuilder().setBrightness(internalBuilder.getBrightnessState().getBrightness()).build(); Color color = Color.newBuilder().setType(Color.Type.HSB).setHsbColor(hsb).build(); internalBuilder.setColorState(internalBuilder.getColorState().toBuilder().setColor(color).build()); if (internalBuilder.getBrightnessState().getBrightness() == 0) { internalBuilder.getPowerStateBuilder().setValue(PowerState.State.OFF); } else { internalBuilder.getPowerStateBuilder().setValue(PowerState.State.ON); } break; } } }
@Override public UnitGroupRemote newInstance(final UnitConfig unitGroupUnitConfig) throws org.openbase.jul.exception.InstantiationException, InterruptedException { try { UnitGroupRemote unitGroupRemote = Units.getUnit(unitGroupUnitConfig, false, Units.UNITGROUP); unitGroupRemote.addDataObserver((final Observable<UnitGroupData> source, UnitGroupData 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(unitGroupUnitConfig, ServiceType.COLOR_STATE_SERVICE)).build()); } if (data.hasPowerState()) { openHABRemote.postUpdate(OpenHABCommandFactory.newOnOffCommand(data.getPowerState().getValue()).setItem(generateItemId(unitGroupUnitConfig, ServiceType.POWER_STATE_SERVICE)).build()); } }); return unitGroupRemote; } catch (CouldNotPerformException ex) { throw new org.openbase.jul.exception.InstantiationException(LocationRemote.class, ex); } }
default public RGBColor getRGBColor() throws NotAvailableException { return getColorState().getColor().getRgbColor(); }
default public Color getColor() throws NotAvailableException { return getColorState().getColor(); }
default public Future<ActionFuture> setColor(final Color color) throws CouldNotPerformException { return setColorState(ColorState.newBuilder().setColor(color).build()); }
default public HSBColor getHSBColor() throws NotAvailableException { return getColorState().getColor().getHsbColor(); }
@Override public Future<ActionFuture> setColorState(final ColorState colorState) throws CouldNotPerformException { lastCommand = OpenHABCommandFactory.newHSBCommand(colorState.getColor().getHsbColor()); final Future future = executeCommand(lastCommand); if (autoRepeat) { repeatLastCommand(); } return future; } }
private void initColorStates() throws CouldNotPerformException { HSBColor color; for (ColorStateServiceRemote colorRemote : colorRemotes) { if (!colors.contains(colorRemote.getColorState().getColor().getHsbColor())) { color = colors.get(random.nextInt(colors.size())); colorRemote.setColor(color); } } }
@Override public Future<ActionFuture> setColorState(final ColorState state) throws CouldNotPerformException { try { Services.verifyOperationServiceState(state); } catch (VerificationFailedException ex) { return FutureProcessor.canceledFuture(ActionFuture.class, ex); } if (state.getColor().getType() == Color.Type.RGB) { return colorService.setColor(HSBColorToRGBColorTransformer.transform(state.getColor().getRgbColor())); } else { return colorService.setColorState(state); } }
public static HSBType.HSB transform(final ColorState colorState) throws CouldNotTransformException { try { HSBColor color = colorState.getColor().getHsbColor(); return HSBType.HSB.newBuilder().setHue(color.getHue()).setSaturation(color.getSaturation()).setBrightness(color.getBrightness()).build(); } catch (Exception ex) { throw new CouldNotTransformException("Could not transform " + HSBColor.class.getName() + " to " + HSBType.class.getName() + "!", ex); } } }
public static ColorState transform(final HSBType.HSB hsbColor) throws CouldNotTransformException { try { HSBColor hsbColorBuilder = HSBColor.newBuilder().setHue(hsbColor.getHue()).setSaturation(hsbColor.getSaturation()).setBrightness(hsbColor.getBrightness()).build(); Color color = Color.newBuilder().setHsbColor(hsbColorBuilder).setType(Color.Type.HSB).build(); return ColorState.newBuilder().setColor(color).build(); } catch (Exception ex) { throw new CouldNotTransformException("Could not transform " + HSBType.HSB.class.getName() + " to " + ColorState.class.getName() + "!", ex); } }