use of org.thingsboard.server.transport.coap.CoapTransportService.MEASUREMENTS in project thingsboard by thingsboard.
the class CoapEfentoTransportResource method getEfentoMeasurements.
private List<EfentoMeasurements> getEfentoMeasurements(MeasurementsProtos.ProtoMeasurements protoMeasurements, UUID sessionId) {
String serialNumber = CoapEfentoUtils.convertByteArrayToString(protoMeasurements.getSerialNum().toByteArray());
boolean batteryStatus = protoMeasurements.getBatteryStatus();
int measurementPeriodBase = protoMeasurements.getMeasurementPeriodBase();
int measurementPeriodFactor = protoMeasurements.getMeasurementPeriodFactor();
int signal = protoMeasurements.getSignal();
List<MeasurementsProtos.ProtoChannel> channelsList = protoMeasurements.getChannelsList();
Map<Long, JsonObject> valuesMap = new TreeMap<>();
if (!CollectionUtils.isEmpty(channelsList)) {
int channel = 0;
JsonObject values;
for (MeasurementsProtos.ProtoChannel protoChannel : channelsList) {
channel++;
boolean isBinarySensor = false;
MeasurementTypeProtos.MeasurementType measurementType = protoChannel.getType();
String measurementTypeName = measurementType.name();
if (measurementType.equals(MeasurementTypeProtos.MeasurementType.OK_ALARM) || measurementType.equals(MeasurementTypeProtos.MeasurementType.FLOODING)) {
isBinarySensor = true;
}
if (measurementPeriodFactor == 0 && isBinarySensor) {
measurementPeriodFactor = 14;
} else {
measurementPeriodFactor = 1;
}
int measurementPeriod = measurementPeriodBase * measurementPeriodFactor;
long measurementPeriodMillis = TimeUnit.SECONDS.toMillis(measurementPeriod);
long nextTransmissionAtMillis = TimeUnit.SECONDS.toMillis(protoMeasurements.getNextTransmissionAt());
int startPoint = protoChannel.getStartPoint();
int startTimestamp = protoChannel.getTimestamp();
long startTimestampMillis = TimeUnit.SECONDS.toMillis(startTimestamp);
List<Integer> sampleOffsetsList = protoChannel.getSampleOffsetsList();
if (!CollectionUtils.isEmpty(sampleOffsetsList)) {
int sampleOfssetsListSize = sampleOffsetsList.size();
for (int i = 0; i < sampleOfssetsListSize; i++) {
int sampleOffset = sampleOffsetsList.get(i);
Integer previousSampleOffset = isBinarySensor && i > 0 ? sampleOffsetsList.get(i - 1) : null;
if (sampleOffset == -32768) {
log.warn("[{}],[{}] Sensor error value! Ignoring.", sessionId, sampleOffset);
} else {
switch(measurementType) {
case TEMPERATURE:
values = valuesMap.computeIfAbsent(startTimestampMillis, k -> CoapEfentoUtils.setDefaultMeasurements(serialNumber, batteryStatus, measurementPeriod, nextTransmissionAtMillis, signal, k));
values.addProperty("temperature_" + channel, ((double) (startPoint + sampleOffset)) / 10f);
startTimestampMillis = startTimestampMillis + measurementPeriodMillis;
break;
case WATER_METER:
values = valuesMap.computeIfAbsent(startTimestampMillis, k -> CoapEfentoUtils.setDefaultMeasurements(serialNumber, batteryStatus, measurementPeriod, nextTransmissionAtMillis, signal, k));
values.addProperty("pulse_counter_water_" + channel, ((double) (startPoint + sampleOffset)));
startTimestampMillis = startTimestampMillis + measurementPeriodMillis;
break;
case HUMIDITY:
values = valuesMap.computeIfAbsent(startTimestampMillis, k -> CoapEfentoUtils.setDefaultMeasurements(serialNumber, batteryStatus, measurementPeriod, nextTransmissionAtMillis, signal, k));
values.addProperty("humidity_" + channel, (double) (startPoint + sampleOffset));
startTimestampMillis = startTimestampMillis + measurementPeriodMillis;
break;
case ATMOSPHERIC_PRESSURE:
values = valuesMap.computeIfAbsent(startTimestampMillis, k -> CoapEfentoUtils.setDefaultMeasurements(serialNumber, batteryStatus, measurementPeriod, nextTransmissionAtMillis, signal, k));
values.addProperty("pressure_" + channel, (double) (startPoint + sampleOffset) / 10f);
startTimestampMillis = startTimestampMillis + measurementPeriodMillis;
break;
case DIFFERENTIAL_PRESSURE:
values = valuesMap.computeIfAbsent(startTimestampMillis, k -> CoapEfentoUtils.setDefaultMeasurements(serialNumber, batteryStatus, measurementPeriod, nextTransmissionAtMillis, signal, k));
values.addProperty("pressure_diff_" + channel, (double) (startPoint + sampleOffset));
startTimestampMillis = startTimestampMillis + measurementPeriodMillis;
break;
case OK_ALARM:
boolean currentIsOk = sampleOffset < 0;
if (previousSampleOffset != null) {
boolean previousIsOk = previousSampleOffset < 0;
boolean isOk = previousIsOk && currentIsOk;
boolean isAlarm = !previousIsOk && !currentIsOk;
if (isOk || isAlarm) {
break;
}
}
String data = currentIsOk ? "OK" : "ALARM";
long sampleOffsetMillis = TimeUnit.SECONDS.toMillis(sampleOffset);
long measurementTimestamp = startTimestampMillis + Math.abs(sampleOffsetMillis);
values = valuesMap.computeIfAbsent(measurementTimestamp - 1000, k -> CoapEfentoUtils.setDefaultMeasurements(serialNumber, batteryStatus, measurementPeriod, nextTransmissionAtMillis, signal, k));
values.addProperty("ok_alarm_" + channel, data);
break;
case NO_SENSOR:
case UNRECOGNIZED:
log.trace("[{}][{}] Sensor error value! Ignoring.", sessionId, measurementTypeName);
break;
default:
log.trace("[{}],[{}] Unsupported measurementType! Ignoring.", sessionId, measurementTypeName);
break;
}
}
}
} else {
log.trace("[{}][{}] sampleOffsetsList list is empty!", sessionId, measurementTypeName);
}
}
} else {
throw new IllegalStateException("[" + sessionId + "]: Failed to get Efento measurements, reason: channels list is empty!");
}
if (!CollectionUtils.isEmpty(valuesMap)) {
List<EfentoMeasurements> efentoMeasurements = new ArrayList<>();
for (Long ts : valuesMap.keySet()) {
EfentoMeasurements measurement = new EfentoMeasurements(ts, valuesMap.get(ts));
efentoMeasurements.add(measurement);
}
return efentoMeasurements;
} else {
throw new IllegalStateException("[" + sessionId + "]: Failed to collect Efento measurements, reason, values map is empty!");
}
}
Aggregations