Search in sources :

Example 1 with Featureofinterest

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

the class ExtractController method getPDF.

@RequestMapping(value = "{mydevice}/{observablepropertyid}/pdf", params = { "dtstart", "dtend" }, method = RequestMethod.POST)
public ResponseEntity<byte[]> getPDF(@PathVariable("observablepropertyid") Long observablePropertyId, @PathVariable("mydevice") String mydevice, @RequestParam("dtstart") String datetimestart, @RequestParam("dtend") String datetimeend, HttpServletRequest request) {
    ObservableMeasure observableMeasure = null;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] contents;
    try {
        Users user = (Users) request.getSession().getAttribute("current_user");
        // DateTime Convertable
        Featureofinterest featureofinterest = featureofInterestService.getFeatureofinterestByIdentifier(mydevice);
        DateTimeFormatter dtfInput = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        HelperCls.ConvertToDateTime convertable = new HelperCls.ConvertToDateTime();
        DateTime from = convertable.GetUTCDateTime(datetimestart, dtfInput, featureofinterest.getTimezone(), StatusTimeConverterEnum.TO_UTC);
        DateTime to = convertable.GetUTCDateTime(datetimeend, dtfInput, featureofinterest.getTimezone(), StatusTimeConverterEnum.TO_UTC);
        // 
        observableMeasure = observationProperyService.getObservationData(observablePropertyId, user.getUser_id(), mydevice, from, to);
        // 
        com.itextpdf.text.Document doc = new com.itextpdf.text.Document();
        // Do this BEFORE document.open()
        PdfWriter.getInstance(doc, byteArrayOutputStream);
        doc.open();
        // Whatever function that you use to create your PDF
        doc = createPDF(observableMeasure, datetimestart, datetimeend, doc);
        doc.close();
        contents = byteArrayOutputStream.toByteArray();
    // generate the file
    } catch (Exception exc) {
        exc.printStackTrace();
        return null;
    }
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("application/pdf"));
    String filename = "measures.pdf";
    headers.setContentDispositionFormData(filename, filename);
    headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
    ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(contents, headers, HttpStatus.OK);
    return response;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Featureofinterest(com.webstart.model.Featureofinterest) HelperCls(com.webstart.Helpers.HelperCls) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Users(com.webstart.model.Users) DateTime(org.joda.time.DateTime) LocalDateTime(org.joda.time.LocalDateTime) ResponseEntity(org.springframework.http.ResponseEntity) ObservableMeasure(com.webstart.DTO.ObservableMeasure) com.itextpdf.text(com.itextpdf.text) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 2 with Featureofinterest

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

the class HomeController method getTotalMeasuresCounter.

@RequestMapping(value = "/{mydevice}/{observablePropertyId}/measures/counter", params = { "dtstart", "dtend" }, method = RequestMethod.GET)
public ResponseEntity<Long> getTotalMeasuresCounter(@PathVariable("observablePropertyId") Long observablePropertyId, @PathVariable("mydevice") String mydevice, @RequestParam("dtstart") String datetimestart, @RequestParam("dtend") String datetimeend, HttpServletRequest request) {
    Users users = (Users) request.getSession().getAttribute("current_user");
    if (users == null) {
        return new ResponseEntity(HttpStatus.UNAUTHORIZED);
    }
    Long sentData = 0L;
    try {
        Featureofinterest featureofinterest = featureofInterestService.getFeatureofinterestByIdentifier(mydevice);
        DateTimeFormatter dtfInput = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        HelperCls.ConvertToDateTime convertable = new HelperCls.ConvertToDateTime();
        DateTime from = convertable.GetUTCDateTime(datetimestart, dtfInput, featureofinterest.getTimezone(), StatusTimeConverterEnum.TO_UTC);
        DateTime to = convertable.GetUTCDateTime(datetimeend, dtfInput, featureofinterest.getTimezone(), StatusTimeConverterEnum.TO_UTC);
        sentData = observationProperyService.getObservationsCounter(observablePropertyId, users.getUser_id(), mydevice, from, to);
    } catch (ParserException parseExc) {
        parseExc.printStackTrace();
        new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
    } catch (Exception e) {
        e.printStackTrace();
        new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
    }
    return new ResponseEntity<Long>(sentData, HttpStatus.OK);
}
Also used : ParserException(jdk.nashorn.internal.runtime.ParserException) ResponseEntity(org.springframework.http.ResponseEntity) Featureofinterest(com.webstart.model.Featureofinterest) HelperCls(com.webstart.Helpers.HelperCls) Users(com.webstart.model.Users) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime) ParserException(jdk.nashorn.internal.runtime.ParserException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with Featureofinterest

use of com.webstart.model.Featureofinterest 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 4 with Featureofinterest

use of com.webstart.model.Featureofinterest 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)

Example 5 with Featureofinterest

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

the class EmbeddedController method saveAutomacticWateringObservation.

@RequestMapping(value = "irrigation", method = RequestMethod.POST)
public ResponseEntity saveAutomacticWateringObservation(@RequestBody AutomaticWater automaticWatering) {
    try {
        Featureofinterest featureofinterest = featureofInterestService.getFeatureofinterestByIdentifier(automaticWatering.getIdentifier());
        if (featureofinterest == null) {
            return new ResponseEntity(HttpStatus.BAD_REQUEST);
        }
        measureService.saveMeasure(automaticWatering);
        featureofInterestService.setFeatureWateringFalse(automaticWatering.getIdentifier());
        // TODO change using webockets
        usersService.createNewNotification(featureofinterest.getUserid(), String.format("New irrigation event from: %1$s until %2$s for end device %3$s ", automaticWatering.getFromtime(), automaticWatering.getUntiltime(), featureofinterest.getName()), NotificationTypeEnum.IRRIGATION.getValue());
    } catch (Exception exc) {
        exc.printStackTrace();
        return new ResponseEntity(exc.getMessage(), HttpStatus.BAD_REQUEST);
    }
    return new ResponseEntity(HttpStatus.OK);
}
Also used : Featureofinterest(com.webstart.model.Featureofinterest) ResponseEntity(org.springframework.http.ResponseEntity) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

Featureofinterest (com.webstart.model.Featureofinterest)14 HelperCls (com.webstart.Helpers.HelperCls)12 DateTime (org.joda.time.DateTime)12 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)12 Users (com.webstart.model.Users)9 LocalDateTime (org.joda.time.LocalDateTime)9 ResponseEntity (org.springframework.http.ResponseEntity)8 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Timestamp (java.sql.Timestamp)4 SimpleDateFormat (java.text.SimpleDateFormat)4 HttpHeaders (org.springframework.http.HttpHeaders)4 ObservableMeasure (com.webstart.DTO.ObservableMeasure)3 WateringMeasure (com.webstart.DTO.WateringMeasure)3 ParserException (jdk.nashorn.internal.runtime.ParserException)3 Instant (org.joda.time.Instant)3 com.itextpdf.text (com.itextpdf.text)2 WateringValueTime (com.webstart.DTO.WateringValueTime)2 NumericValue (com.webstart.model.NumericValue)2 Observation (com.webstart.model.Observation)2