/** * Method returns the state source(s) result(s) (contains state value(s)) of the input powerConsumptionState. * * @param powerConsumptionState The PowerConsumptionState. * @return state source(s) result(s) of the input state. */ private List<RdfNodeObject> powerConsumptionStateSources(final PowerConsumptionState powerConsumptionState) { final List<RdfNodeObject> stateSources = new ArrayList<>(); final String voltageVal = "\"" + String.valueOf(powerConsumptionState.getVoltage()) + "\"^^NS:Voltage"; final String consumptionVal = "\"" + String.valueOf(powerConsumptionState.getConsumption()) + "\"^^NS:Watt"; final String currentVal = "\"" + String.valueOf(powerConsumptionState.getCurrent()) + "\"^^NS:Ampere"; stateSources.add(new RdfNodeObject(new ArrayList<String>() {{addAll(Arrays.asList(voltageVal, consumptionVal, currentVal));}}, true)); return stateSources; }
@Override protected void updateDynamicComponents() { try { voltageValueLabel.setText(numberFormat.format(getProviderService().getPowerConsumptionState().getVoltage()) + " V"); currentValueLabel.setText(numberFormat.format(getProviderService().getPowerConsumptionState().getCurrent()) + " A"); consumptionBar.setString(numberFormat.format(getProviderService().getPowerConsumptionState().getConsumption()) + " W"); double level = (getProviderService().getPowerConsumptionState().getCurrent() / 16d); consumptionBar.setValue((int) (level * 100)); double hue = Math.min(180, Math.max(0, 180 - level * 180)); colorPanel.setBackground(AWTColorToHSBColorTransformer.transform(HSBColor.newBuilder().setBrightness(80).setSaturation(100).setHue(hue).build())); } catch (CouldNotPerformException ex) { ExceptionPrinter.printHistory(ex, logger); } } }
@Override public PowerConsumptionState getPowerConsumptionState(final UnitType unitType) throws NotAvailableException { double consumptionSum = 0; double averageCurrent = 0; double averageVoltage = 0; long timestamp = 0; Collection<PowerConsumptionStateProviderService> powerConsumptionStateProviderServices = getServices(unitType); int amount = powerConsumptionStateProviderServices.size(); for (PowerConsumptionStateProviderService service : powerConsumptionStateProviderServices) { if (!((UnitRemote) service).isDataAvailable()) { amount--; continue; } consumptionSum += service.getPowerConsumptionState().getConsumption(); averageCurrent += service.getPowerConsumptionState().getCurrent(); averageVoltage += service.getPowerConsumptionState().getVoltage(); timestamp = Math.max(timestamp, service.getPowerConsumptionState().getTimestamp().getTime()); } averageCurrent = averageCurrent / amount; averageVoltage = averageVoltage / amount; return TimestampProcessor.updateTimestamp(timestamp, PowerConsumptionState.newBuilder().setConsumption(consumptionSum).setCurrent(averageCurrent).setVoltage(averageVoltage), TimeUnit.MICROSECONDS, logger).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); } }