Search in sources :

Example 1 with NumericValue

use of com.webstart.model.NumericValue in project FarmCloud by vratsasg.

the class MeasuresServiceImpl method saveMeasure.

public void saveMeasure(AutomaticWater automaticWater) {
    try {
        Long seriesid = seriesJpaRepository.findSeriesIdByEndDevice(automaticWater.getIdentifier()).get(0);
        BigDecimal waterCons = featureofinterestJpaRepository.getWaterConsumption(automaticWater.getIdentifier()).get(0);
        DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyyMMddHHmmss");
        // DateTime Convertable
        Featureofinterest featureofinterest = featureofInterestService.getFeatureofinterestByIdentifier(automaticWater.getIdentifier());
        DateTimeFormatter dtfInput = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        HelperCls.ConvertToDateTime convertable = new HelperCls.ConvertToDateTime();
        DateTime from = convertable.GetUTCDateTime(automaticWater.getFromtime(), dtfInput, featureofinterest.getTimezone(), StatusTimeConverterEnum.TO_UTC);
        DateTime to = convertable.GetUTCDateTime(automaticWater.getUntiltime(), dtfInput, featureofinterest.getTimezone(), StatusTimeConverterEnum.TO_UTC);
        int offset = DateTimeZone.getDefault().getOffset(new Instant());
        // 
        Observation observation = new Observation();
        observation.setSeriesid(seriesid);
        observation.setPhenomenontimestart(new Timestamp(from.getMillis() - offset));
        observation.setPhenomenontimeend(new Timestamp(to.getMillis() - offset));
        observation.setIdentifier(dtf.print(new DateTime(DateTimeZone.UTC)) + "-" + java.util.UUID.randomUUID());
        observation.setUnitid((long) 22);
        // 
        logger.debug("params: FROM dt_from={}, ts_from={} TO dt_to={}, ts_to={}", from, observation.getPhenomenontimestart(), to, observation.getPhenomenontimeend());
        // 
        observationJpaRepository.save(observation);
        NumericValue numericValue = new NumericValue();
        numericValue.setObservationid(observation.getObservationid());
        float diffmilliSec = to.getMillis() - from.getMillis();
        numericValue.setValue(new BigDecimal((diffmilliSec / 1000.0 / 3600.0) * waterCons.doubleValue()));
        numericValueJpaRepository.save(numericValue);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Featureofinterest(com.webstart.model.Featureofinterest) HelperCls(com.webstart.Helpers.HelperCls) Instant(org.joda.time.Instant) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) LocalDateTime(org.joda.time.LocalDateTime) Observation(com.webstart.model.Observation) NumericValue(com.webstart.model.NumericValue) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 2 with NumericValue

use of com.webstart.model.NumericValue in project FarmCloud by vratsasg.

the class MeasuresServiceImpl method saveMeasure.

public void saveMeasure(Long seriesId, EmbeddedData embeddedData) {
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        // DateTime Convertable
        Featureofinterest featureofinterest = featureofInterestService.getFeatureofinterestByIdentifier(embeddedData.getZigbeeAddress());
        DateTimeFormatter dtfInput = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        HelperCls.ConvertToDateTime convertable = new HelperCls.ConvertToDateTime();
        DateTime dt = convertable.GetUTCDateTime(embeddedData.getDatetimeMeasure(), dtfInput, featureofinterest.getTimezone(), StatusTimeConverterEnum.TO_UTC);
        Timestamp ts = new Timestamp(dt.getMillis());
        Observation observation = new Observation();
        observation.setSeriesid(seriesId);
        observation.setPhenomenontimestart(ts);
        observation.setPhenomenontimeend(ts);
        // observation.setIdentifier(dtf.print(new DateTime(DateTimeZone.UTC)) + "-" + java.util.UUID.randomUUID());
        observation.setIdentifier(sdf.format(ts) + "-" + java.util.UUID.randomUUID());
        observation.setUnitid((long) embeddedData.getUnitId());
        observationJpaRepository.save(observation);
        NumericValue numericValue = new NumericValue();
        numericValue.setObservationid(observation.getObservationid());
        numericValue.setValue(embeddedData.getMeasureValue());
        numericValueJpaRepository.save(numericValue);
        logger.debug("params: dt={}, ts={}", dt, ts);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Featureofinterest(com.webstart.model.Featureofinterest) HelperCls(com.webstart.Helpers.HelperCls) Observation(com.webstart.model.Observation) NumericValue(com.webstart.model.NumericValue) SimpleDateFormat(java.text.SimpleDateFormat) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Timestamp(java.sql.Timestamp) DateTime(org.joda.time.DateTime) LocalDateTime(org.joda.time.LocalDateTime)

Aggregations

HelperCls (com.webstart.Helpers.HelperCls)2 Featureofinterest (com.webstart.model.Featureofinterest)2 NumericValue (com.webstart.model.NumericValue)2 Observation (com.webstart.model.Observation)2 Timestamp (java.sql.Timestamp)2 DateTime (org.joda.time.DateTime)2 LocalDateTime (org.joda.time.LocalDateTime)2 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)2 BigDecimal (java.math.BigDecimal)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Instant (org.joda.time.Instant)1