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;
}
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);
}
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();
}
}
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();
}
}
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);
}
Aggregations