Search in sources :

Example 1 with MEASUREMENTS

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!");
    }
}
Also used : CONFIGURATION(org.thingsboard.server.transport.coap.CoapTransportService.CONFIGURATION) JsonObject(com.google.gson.JsonObject) CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange) Response(org.eclipse.californium.core.coap.Response) Exchange(org.eclipse.californium.core.network.Exchange) CoapDeviceAuthCallback(org.thingsboard.server.transport.coap.callback.CoapDeviceAuthCallback) MeasurementTypeProtos(org.thingsboard.server.gen.transport.coap.MeasurementTypeProtos) EfentoCoapDeviceTypeConfiguration(org.thingsboard.server.common.data.device.profile.EfentoCoapDeviceTypeConfiguration) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) CoapEfentoUtils(org.thingsboard.server.transport.coap.efento.utils.CoapEfentoUtils) Resource(org.eclipse.californium.core.server.resources.Resource) CURRENT_TIMESTAMP(org.thingsboard.server.transport.coap.CoapTransportService.CURRENT_TIMESTAMP) DEVICE_INFO(org.thingsboard.server.transport.coap.CoapTransportService.DEVICE_INFO) AbstractCoapTransportResource(org.thingsboard.server.transport.coap.AbstractCoapTransportResource) Map(java.util.Map) MEASUREMENTS(org.thingsboard.server.transport.coap.CoapTransportService.MEASUREMENTS) DeviceTransportType(org.thingsboard.server.common.data.DeviceTransportType) MeasurementsProtos(org.thingsboard.server.gen.transport.coap.MeasurementsProtos) CoapOkCallback(org.thingsboard.server.transport.coap.callback.CoapOkCallback) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) CoapTransportContext(org.thingsboard.server.transport.coap.CoapTransportContext) UUID(java.util.UUID) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) List(java.util.List) Request(org.eclipse.californium.core.coap.Request) CoapEfentoCallback(org.thingsboard.server.transport.coap.callback.CoapEfentoCallback) TreeMap(java.util.TreeMap) CoAP(org.eclipse.californium.core.coap.CoAP) CoapDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.CoapDeviceProfileTransportConfiguration) CollectionUtils(org.springframework.util.CollectionUtils) Data(lombok.Data) DeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.DeviceProfileTransportConfiguration) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) AllArgsConstructor(lombok.AllArgsConstructor) SessionInfoCreator(org.thingsboard.server.common.transport.auth.SessionInfoCreator) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) TreeMap(java.util.TreeMap) MeasurementTypeProtos(org.thingsboard.server.gen.transport.coap.MeasurementTypeProtos) MeasurementsProtos(org.thingsboard.server.gen.transport.coap.MeasurementsProtos)

Aggregations

JsonObject (com.google.gson.JsonObject)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 UUID (java.util.UUID)1 TimeUnit (java.util.concurrent.TimeUnit)1 AllArgsConstructor (lombok.AllArgsConstructor)1 Data (lombok.Data)1 Slf4j (lombok.extern.slf4j.Slf4j)1 CoAP (org.eclipse.californium.core.coap.CoAP)1 Request (org.eclipse.californium.core.coap.Request)1 Response (org.eclipse.californium.core.coap.Response)1 Exchange (org.eclipse.californium.core.network.Exchange)1 CoapExchange (org.eclipse.californium.core.server.resources.CoapExchange)1 Resource (org.eclipse.californium.core.server.resources.Resource)1 CollectionUtils (org.springframework.util.CollectionUtils)1 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)1 DeviceTransportType (org.thingsboard.server.common.data.DeviceTransportType)1