use of org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto in project open-smart-grid-platform by OSGP.
the class GetActualMeterReadsCommandExecutor method execute.
@Override
public MeterReadsResponseDto execute(final DlmsConnectionManager conn, final DlmsDevice device, final ActualMeterReadsQueryDto actualMeterReadsQuery, final MessageMetadata messageMetadata) throws ProtocolAdapterException {
if (actualMeterReadsQuery != null && actualMeterReadsQuery.isMbusQuery()) {
throw new IllegalArgumentException("ActualMeterReadsQuery object for energy reads should not be about gas.");
}
conn.getDlmsMessageListener().setDescription("GetActualMeterReads retrieve attributes: " + JdlmsObjectToStringUtil.describeAttributes(ATTRIBUTE_ADDRESSES));
LOGGER.info("Retrieving actual energy reads");
final List<GetResult> getResultList = this.dlmsHelper.getAndCheck(conn, device, "retrieve actual meter reads", ATTRIBUTE_ADDRESSES);
final CosemDateTimeDto cosemDateTime = this.dlmsHelper.readDateTime(getResultList.get(INDEX_TIME), "Actual Energy Reads Time");
final DateTime time = cosemDateTime.asDateTime();
if (time == null) {
throw new ProtocolAdapterException("Unexpected null/unspecified value for Actual Energy Reads Time");
}
final DlmsMeterValueDto activeEnergyImport = this.dlmsHelper.getScaledMeterValue(getResultList.get(INDEX_ACTIVE_ENERGY_IMPORT), getResultList.get(INDEX_ACTIVE_ENERGY_IMPORT_SCALER_UNIT), "Actual Energy Reads +A");
final DlmsMeterValueDto activeEnergyExport = this.dlmsHelper.getScaledMeterValue(getResultList.get(INDEX_ACTIVE_ENERGY_EXPORT), getResultList.get(INDEX_ACTIVE_ENERGY_EXPORT_SCALER_UNIT), "Actual Energy Reads -A");
final DlmsMeterValueDto activeEnergyImportRate1 = this.dlmsHelper.getScaledMeterValue(getResultList.get(INDEX_ACTIVE_ENERGY_IMPORT_RATE_1), getResultList.get(INDEX_ACTIVE_ENERGY_IMPORT_RATE_1_SCALER_UNIT), "Actual Energy Reads +A rate 1");
final DlmsMeterValueDto activeEnergyImportRate2 = this.dlmsHelper.getScaledMeterValue(getResultList.get(INDEX_ACTIVE_ENERGY_IMPORT_RATE_2), getResultList.get(INDEX_ACTIVE_ENERGY_IMPORT_RATE_2_SCALER_UNIT), "Actual Energy Reads +A rate 2");
final DlmsMeterValueDto activeEnergyExportRate1 = this.dlmsHelper.getScaledMeterValue(getResultList.get(INDEX_ACTIVE_ENERGY_EXPORT_RATE_1), getResultList.get(INDEX_ACTIVE_ENERGY_EXPORT_RATE_1_SCALER_UNIT), "Actual Energy Reads -A rate 1");
final DlmsMeterValueDto activeEnergyExportRate2 = this.dlmsHelper.getScaledMeterValue(getResultList.get(INDEX_ACTIVE_ENERGY_EXPORT_RATE_2), getResultList.get(INDEX_ACTIVE_ENERGY_EXPORT_RATE_2_SCALER_UNIT), "Actual Energy Reads -A rate 2");
return new MeterReadsResponseDto(time.toDate(), new ActiveEnergyValuesDto(activeEnergyImport, activeEnergyExport, activeEnergyImportRate1, activeEnergyImportRate2, activeEnergyExportRate1, activeEnergyExportRate2));
}
use of org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto in project open-smart-grid-platform by OSGP.
the class DlmsHelperTest method testFromByteArrayWinterTime.
@Test
public void testFromByteArrayWinterTime() throws Exception {
final CosemDateTimeDto cosemDateTime = this.dlmsHelper.fromDateTimeValue(this.byteArrayWinterTime());
assertThat(cosemDateTime.isDateTimeSpecified()).isTrue();
final DateTime dateInWinterTime = cosemDateTime.asDateTime();
assertThat(ISODateTimeFormat.dateTime().print(dateInWinterTime)).isEqualTo("2015-02-21T14:53:07.230+01:00");
}
use of org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto in project open-smart-grid-platform by OSGP.
the class GetActualPowerQualityCommandExecutor method makeActualPowerQualityDataDto.
private ActualPowerQualityDataDto makeActualPowerQualityDataDto(final List<GetResult> resultList, final List<PowerQualityObjectMetadata> metadatas) throws ProtocolAdapterException {
final List<PowerQualityObjectDto> powerQualityObjects = new ArrayList<>();
final List<PowerQualityValueDto> powerQualityValues = new ArrayList<>();
int idx = 0;
for (final PowerQualityObjectMetadata metadata : metadatas) {
final PowerQualityObjectDto powerQualityObject;
final PowerQualityValueDto powerQualityValue;
if (metadata.getClassId() == CLASS_ID_CLOCK) {
final GetResult resultTime = resultList.get(idx++);
final CosemDateTimeDto cosemDateTime = this.dlmsHelper.readDateTime(resultTime, "Actual Power Quality - Time");
powerQualityObject = new PowerQualityObjectDto(metadata.name(), null);
powerQualityValue = new PowerQualityValueDto(cosemDateTime.asDateTime().toDate());
} else if (metadata.getClassId() == CLASS_ID_REGISTER) {
final GetResult resultValue = resultList.get(idx++);
final GetResult resultScalerUnit = resultList.get(idx++);
final DlmsMeterValueDto meterValue = this.dlmsHelper.getScaledMeterValue(resultValue, resultScalerUnit, "Actual Power Quality - " + metadata.getObisCode());
final BigDecimal value = meterValue != null ? meterValue.getValue() : null;
final String unit = meterValue != null ? meterValue.getDlmsUnit().getUnit() : null;
powerQualityValue = new PowerQualityValueDto(value);
powerQualityObject = new PowerQualityObjectDto(metadata.name(), unit);
} else if (metadata.getClassId() == CLASS_ID_DATA) {
final GetResult resultValue = resultList.get(idx++);
final Integer meterValue = this.dlmsHelper.readInteger(resultValue, "Actual Power Quality - " + metadata.getObisCode());
powerQualityValue = meterValue != null ? new PowerQualityValueDto(new BigDecimal(meterValue)) : null;
powerQualityObject = new PowerQualityObjectDto(metadata.name(), null);
} else {
throw new ProtocolAdapterException(String.format("Unsupported ClassId {} for logical name {}", metadata.getClassId(), metadata.obisCode));
}
powerQualityObjects.add(powerQualityObject);
powerQualityValues.add(powerQualityValue);
}
return new ActualPowerQualityDataDto(powerQualityObjects, powerQualityValues);
}
use of org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto in project open-smart-grid-platform by OSGP.
the class AbstractGetPowerQualityProfileHandler method makeDateProfileEntryValueDto.
private ProfileEntryValueDto makeDateProfileEntryValueDto(final DataObject dataObject, final ProfileEntryDto previousProfileEntryDto, final int timeInterval) {
final CosemDateTimeDto cosemDateTime = this.dlmsHelper.convertDataObjectToDateTime(dataObject);
if (cosemDateTime == null) {
// in case of null date, we calculate the date based on the always
// existing previous value plus interval
final Date previousDate = (Date) previousProfileEntryDto.getProfileEntryValues().get(0).getValue();
final LocalDateTime newLocalDateTime = Instant.ofEpochMilli(previousDate.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime().plusMinutes(timeInterval);
return new ProfileEntryValueDto(Date.from(newLocalDateTime.atZone(ZoneId.systemDefault()).toInstant()));
} else {
return new ProfileEntryValueDto(cosemDateTime.asDateTime().toDate());
}
}
use of org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto in project open-smart-grid-platform by OSGP.
the class AbstractPeriodicMeterReadsCommandExecutor method readClock.
/**
* Calculates/derives the date of the read buffered DataObject.
*
* @param ctx context elements for the buffered object conversion
* @param previousLogTime the log time of the previous meter read
* @param dlmsHelper dlms helper object
* @return the date of the buffered {@link DataObject} or null if it cannot be determined
* @throws ProtocolAdapterException
* @throws BufferedDateTimeValidationException in case the date is invalid or null
*/
Date readClock(final ConversionContext ctx, final Optional<Date> previousLogTime, final DlmsHelper dlmsHelper) throws ProtocolAdapterException, BufferedDateTimeValidationException {
final Date logTime;
final PeriodTypeDto queryPeriodType = ctx.periodicMeterReadsQuery.getPeriodType();
final DateTime from = new DateTime(ctx.periodicMeterReadsQuery.getBeginDate());
final DateTime to = new DateTime(ctx.periodicMeterReadsQuery.getEndDate());
final Integer clockIndex = ctx.attributeAddressForProfile.getIndex(DlmsObjectType.CLOCK, null);
CosemDateTimeDto cosemDateTime = null;
if (clockIndex != null) {
cosemDateTime = dlmsHelper.readDateTime(ctx.bufferedObjects.get(clockIndex), "Clock from " + queryPeriodType + " buffer");
}
final DateTime bufferedDateTime = cosemDateTime == null ? null : cosemDateTime.asDateTime();
if (bufferedDateTime != null) {
dlmsHelper.validateBufferedDateTime(bufferedDateTime, from, to);
logTime = bufferedDateTime.toDate();
} else {
logTime = this.calculateIntervalTimeBasedOnPreviousValue(ctx.periodicMeterReadsQuery.getPeriodType(), previousLogTime, ctx.intervalTime);
}
if (logTime == null) {
throw new BufferedDateTimeValidationException("Unable to calculate logTime");
}
return logTime;
}
Aggregations