/** * Method updates the timestamp field of the given message with the given timestamp. * * @param <M> the message type of the message to milliseconds. * @param milliseconds the time to update * @param messageOrBuilder the message * @return the updated message * @throws CouldNotPerformException */ public static <M extends MessageOrBuilder> M updateTimestamp(final long milliseconds, final M messageOrBuilder) throws CouldNotPerformException { return updateTimestamp(milliseconds, messageOrBuilder, TimeUnit.MILLISECONDS); }
/** * Method updates the timestamp field of the given message with the current time. * * @param <M> the message type of the message to update. * @param messageOrBuilder the message * @return the updated message * @throws CouldNotPerformException */ public static <M extends MessageOrBuilder> M updateTimestampWithCurrentTime(final M messageOrBuilder) throws CouldNotPerformException { return updateTimestamp(System.currentTimeMillis(), messageOrBuilder); }
/** * Method updates the timestamp field of the given message with the given timestamp. * In case of an error the original message is returned. * * @param <M> the message type of the message to update. * @param timestamp the timestamp to update * @param messageOrBuilder the message * @param timeUnit the timeUnit of the timeStamp * @param logger the logger which is used for printing the exception stack in case something went wrong. * @return the updated message or the original one in case of errors. */ public static <M extends MessageOrBuilder> M updateTimestamp(final long timestamp, final M messageOrBuilder, final TimeUnit timeUnit, final Logger logger) { try { return updateTimestamp(timestamp, messageOrBuilder, timeUnit); } catch (CouldNotPerformException ex) { ExceptionPrinter.printHistory(ex, logger); return messageOrBuilder; } }
/** * Method updates the timestamp field of the given message with the given timestamp. * In case of an error the original message is returned. * * @param <M> the message type of the message to update. * @param timestamp the timestamp to update * @param messageOrBuilder the message * @param logger the logger which is used for printing the exception stack in case something went wrong. * @return the updated message or the original one in case of errors. */ public static <M extends MessageOrBuilder> M updateTimestamp(final long timestamp, final M messageOrBuilder, final Logger logger) { try { return updateTimestamp(timestamp, messageOrBuilder); } catch (CouldNotPerformException ex) { ExceptionPrinter.printHistory(ex, logger); return messageOrBuilder; } }
@Override public BrightnessState getBrightnessState(final UnitType unitType) throws NotAvailableException { Collection<BrightnessStateOperationService> brightnessStateOperationServices = getServices(unitType); int serviceNumber = brightnessStateOperationServices.size(); Double average = 0d; long timestamp = 0; for (BrightnessStateOperationService service : brightnessStateOperationServices) { if (!((UnitRemote) service).isDataAvailable()) { serviceNumber--; continue; } average += service.getBrightnessState().getBrightness(); timestamp = Math.max(timestamp, service.getBrightnessState().getTimestamp().getTime()); } average /= serviceNumber; return TimestampProcessor.updateTimestamp(timestamp, BrightnessState.newBuilder().setBrightness(average), TimeUnit.MICROSECONDS, logger).build(); } }
@Override public TemperatureState getTargetTemperatureState(final UnitType unitType) throws NotAvailableException { Double average = 0d; Collection<TargetTemperatureStateOperationService> targetTemperatureStateOperationServices = getServices(unitType); int amount = targetTemperatureStateOperationServices.size(); long timestamp = 0; for (TargetTemperatureStateOperationService service : targetTemperatureStateOperationServices) { if (!((UnitRemote) service).isDataAvailable()) { amount--; continue; } average += service.getTargetTemperatureState().getTemperature(); timestamp = Math.max(timestamp, service.getTargetTemperatureState().getTimestamp().getTime()); } average /= amount; return TimestampProcessor.updateTimestamp(timestamp, TemperatureState.newBuilder().setTemperature(average), TimeUnit.MICROSECONDS, logger).build(); } }
@Override public IlluminanceState getIlluminanceState(final UnitType unitType) throws NotAvailableException { double averageIlluminance = 0; long timestamp = 0; Collection<IlluminanceStateProviderService> illuminanceStateProviderServices = getServices(unitType); int amount = illuminanceStateProviderServices.size(); for (IlluminanceStateProviderService service : illuminanceStateProviderServices) { if (!((UnitRemote) service).isDataAvailable()) { amount--; continue; } averageIlluminance += service.getIlluminanceState().getIlluminance(); timestamp = Math.max(timestamp, service.getIlluminanceState().getTimestamp().getTime()); } averageIlluminance = averageIlluminance / amount; return TimestampProcessor.updateTimestamp(timestamp, IlluminanceState.newBuilder().setIlluminance(averageIlluminance).setIlluminanceDataUnit(IlluminanceState.DataUnit.LUX), TimeUnit.MICROSECONDS, logger).build(); }
@Override public TemperatureState getTemperatureState(final UnitType unitType) throws NotAvailableException { Double average = 0d; Collection<TemperatureStateProviderService> temperatureStateProviderServices = getServices(unitType); int amount = temperatureStateProviderServices.size(); long timestamp = 0; for (TemperatureStateProviderService service : temperatureStateProviderServices) { if (!((UnitRemote) service).isDataAvailable()) { amount--; continue; } average += service.getTemperatureState().getTemperature(); timestamp = Math.max(timestamp, service.getTemperatureState().getTimestamp().getTime()); } average /= amount; return TimestampProcessor.updateTimestamp(timestamp, TemperatureState.newBuilder().setTemperature(average), TimeUnit.MICROSECONDS, logger).build(); } }
@Override public HandleState getHandleState(final UnitType unitType) throws NotAvailableException { // TODO: rethink position in handle state int position = 0; //boolean tilted = false; Collection<HandleStateProviderService> handleStateProviderServices = getServices(unitType); int amount = handleStateProviderServices.size(); long timestamp = 0; for (HandleStateProviderService service : handleStateProviderServices) { if (!((UnitRemote) service).isDataAvailable()) { amount--; continue; } position += service.getHandleState().getPosition(); timestamp = Math.max(timestamp, service.getHandleState().getTimestamp().getTime()); } position /= amount; return TimestampProcessor.updateTimestamp(timestamp, HandleState.newBuilder().setPosition(position), TimeUnit.MICROSECONDS, logger).build(); } }
@Override public AlarmState getSmokeAlarmState(final UnitType unitType) throws NotAvailableException { AlarmState.State alarmValue = AlarmState.State.NO_ALARM; long timestamp = 0; for (SmokeAlarmStateProviderService service : getServices(unitType)) { if (!((UnitRemote) service).isDataAvailable()) { continue; } if (service.getSmokeAlarmState().getValue() == AlarmState.State.ALARM) { alarmValue = AlarmState.State.ALARM; } timestamp = Math.max(timestamp, service.getSmokeAlarmState().getTimestamp().getTime()); } return TimestampProcessor.updateTimestamp(timestamp, AlarmState.newBuilder().setValue(alarmValue), TimeUnit.MICROSECONDS, logger).build(); } }
@Override public PowerState getPowerState(final UnitType unitType) throws NotAvailableException { PowerState.State powerStateValue = PowerState.State.OFF; long timestamp = 0; for (PowerStateOperationService service : getServices(unitType)) { if (!((UnitRemote) service).isDataAvailable()) { continue; } if (service.getPowerState().getValue() == PowerState.State.ON) { powerStateValue = PowerState.State.ON; } timestamp = Math.max(timestamp, service.getPowerState().getTimestamp().getTime()); } return TimestampProcessor.updateTimestamp(timestamp, PowerState.newBuilder().setValue(powerStateValue), TimeUnit.MICROSECONDS, logger).build(); }
@Override public ContactState getContactState(final UnitType unitType) throws NotAvailableException { ContactState.State contactValue = ContactState.State.CLOSED; long timestamp = 0; for (ContactStateProviderService service : getServices(unitType)) { if (!((UnitRemote) service).isDataAvailable()) { continue; } if (service.getContactState().getValue() == ContactState.State.OPEN) { contactValue = ContactState.State.OPEN; } timestamp = Math.max(timestamp, service.getContactState().getTimestamp().getTime()); } return TimestampProcessor.updateTimestamp(timestamp, ContactState.newBuilder().setValue(contactValue), TimeUnit.MICROSECONDS, logger).build(); } }
@Override public StandbyState getStandbyState(final UnitType unitType) throws NotAvailableException { StandbyState.State standbyValue = StandbyState.State.STANDBY; long timestamp = 0; for (StandbyStateOperationService service : getServices(unitType)) { if (!((UnitRemote) service).isDataAvailable()) { continue; } if (service.getStandbyState().getValue() == StandbyState.State.RUNNING) { standbyValue = StandbyState.State.RUNNING; } timestamp = Math.max(timestamp, service.getStandbyState().getTimestamp().getTime()); } return TimestampProcessor.updateTimestamp(timestamp, StandbyState.newBuilder().setValue(standbyValue), TimeUnit.MICROSECONDS, logger).build(); } }
return TimestampProcessor.updateTimestamp(timestamp, SmokeState.newBuilder().setValue(smokeValue).setSmokeLevel(averageSmokeLevel), TimeUnit.MICROSECONDS, logger).build();
@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 EmphasisState getEmphasisState(UnitType unitType) throws NotAvailableException { Collection<EmphasisStateOperationService> emphasisStateOperationServices = getServices(unitType); int serviceNumber = emphasisStateOperationServices.size(); Double averageComfort = 0d; Double averageEnergy = 0d; Double averageSecurity = 0d; long timestamp = 0; for (EmphasisStateOperationService service : emphasisStateOperationServices) { if (!((UnitRemote) service).isDataAvailable()) { serviceNumber--; continue; } averageComfort += service.getEmphasisState().getComfort(); averageEnergy += service.getEmphasisState().getEnergy(); averageSecurity += service.getEmphasisState().getSecurity(); timestamp = Math.max(timestamp, service.getEmphasisState().getTimestamp().getTime()); } averageComfort /= serviceNumber; averageEnergy /= serviceNumber; averageSecurity /= serviceNumber; return TimestampProcessor.updateTimestamp(timestamp, EmphasisState.newBuilder().setComfort(averageComfort).setEnergy(averageEnergy).setSecurity(averageSecurity), TimeUnit.MICROSECONDS, logger).build(); }
return TimestampProcessor.updateTimestamp(timestamp, BlindState.newBuilder().setMovementState(mostOccurrences).setOpeningRatio(openingRatioAverage), TimeUnit.MICROSECONDS, logger).build();
@Override public TamperState getTamperState(final UnitType unitType) throws NotAvailableException { TamperState.State tamperValue = TamperState.State.NO_TAMPER; long lastDetection = 0; long timestamp = 0; for (TamperStateProviderService service : getServices(unitType)) { if (!((UnitRemote) service).isDataAvailable()) { continue; } TamperState tamperState = service.getTamperState(); if (tamperState.getValue() == TamperState.State.TAMPER) { tamperValue = TamperState.State.TAMPER; } if (tamperState.getLastDetection().getTime() > lastDetection) { lastDetection = tamperState.getLastDetection().getTime(); } timestamp = Math.max(timestamp, tamperState.getTimestamp().getTime()); } return TimestampProcessor.updateTimestamp(timestamp, TamperState.newBuilder().setValue(tamperValue).setLastDetection(Timestamp.newBuilder().setTime(lastDetection)), TimeUnit.MICROSECONDS, logger).build(); } }
@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(); } }
@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); } }