/** * 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(); } }
/** * Get the current in milli ampere from a PowerConsumptionState. * * @param powerConsumptionState the state * @return the current in milli ampere */ public static Double transform(PowerConsumptionState powerConsumptionState) { return powerConsumptionState.getCurrent() * MILLITOAMPERE; } }