Search in sources :

Example 11 with Featureofinterest

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

the class HomeController method getMeasuresByObsProperty.

@RequestMapping(value = "/{mydevice}/{observablePropertyId}/measures", params = { "dtstart", "dtend" }, method = RequestMethod.GET)
public ResponseEntity<String> getMeasuresByObsProperty(@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);
    }
    String sentData = null;
    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);
        ObservableMeasure observableMeasure = observationProperyService.getObservationData(observablePropertyId, users.getUser_id(), mydevice, from, to);
        if (observableMeasure == null) {
            sentData = "{\"unit\":\"\",\"measuredata\":[]}";
        } else {
            ObjectMapper mapper = new ObjectMapper();
            // Object to JSON in String
            sentData = mapper.writeValueAsString(observableMeasure);
        }
    } catch (Exception e) {
        e.printStackTrace();
        new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
    }
    return new ResponseEntity<String>(sentData, HttpStatus.OK);
}
Also used : 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) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ParserException(jdk.nashorn.internal.runtime.ParserException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 12 with Featureofinterest

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

the class HomeController method setIrrigationDates.

@RequestMapping(value = "/{mydevice}/irrigation/times", params = { "dtfrom", "dtto" }, method = RequestMethod.POST)
public ResponseEntity<?> setIrrigationDates(@PathVariable("mydevice") String mydevice, @RequestParam("dtfrom") String datefrom, @RequestParam("dtto") String dateto, HttpServletRequest request) {
    Users users = (Users) request.getSession().getAttribute("current_user");
    try {
        AutomaticWater automaticWater = new AutomaticWater(datefrom, dateto, new BigDecimal(0), mydevice);
        boolean sentData = featureofInterestService.setDeviceIrrigaDate(users.getUser_id(), mydevice, datefrom, dateto);
        Featureofinterest featureofinterest = this.featureofInterestService.getFeatureofinterestByIdentifier(mydevice);
        usersService.createNewNotification(users.getUser_id(), String.format("New irrigation event from: %1$s until %2$s for end device %3$s ", automaticWater.getFromtime(), automaticWater.getUntiltime(), featureofinterest.getName()), NotificationTypeEnum.IRRIGATION.getValue());
        if (!sentData)
            return new ResponseEntity(HttpStatus.BAD_REQUEST);
    } catch (Exception e) {
        e.printStackTrace();
        return new ResponseEntity(HttpStatus.BAD_REQUEST);
    }
    return new ResponseEntity(HttpStatus.OK);
}
Also used : Featureofinterest(com.webstart.model.Featureofinterest) ResponseEntity(org.springframework.http.ResponseEntity) Users(com.webstart.model.Users) BigDecimal(java.math.BigDecimal) ParserException(jdk.nashorn.internal.runtime.ParserException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 13 with Featureofinterest

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

the class MeasuresServiceImpl method findDailyMeasure.

public JSONArray findDailyMeasure(String id) {
    JSONArray finalDataList = new JSONArray();
    try {
        Timestamp timestampTo = observationJpaRepository.fiindlastdatetime(id);
        // substract one day
        Timestamp timestampFrom = new Timestamp(timestampTo.getTime() - 24 * 60 * 60 * 1000);
        List<Object[]> ojectMeasures = observationJpaRepository.findCurrentMeasure(id, timestampFrom, timestampTo);
        List<String> observableProperyList = observablePropertyJpaRepository.findallObsProperty();
        Featureofinterest featureofinterest = featureofInterestService.getFeatureofinterestByIdentifier(id);
        // Create a hash table with all observable properties
        Hashtable<String, JSONArray> obspropValues = new Hashtable<String, JSONArray>();
        for (String obsprop : observableProperyList) {
            obspropValues.put(obsprop, new JSONArray());
        }
        int offset = DateTimeZone.getDefault().getOffset(new Instant());
        Iterator itr = ojectMeasures.iterator();
        while (itr.hasNext()) {
            Object[] object = (Object[]) itr.next();
            JSONArray internvalues = new JSONArray();
            String value = String.valueOf(object[2]);
            DateTimeFormatter dtfInput = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
            HelperCls.ConvertToDateTime convertable = new HelperCls.ConvertToDateTime();
            DateTime dt = convertable.GetUTCDateTime(object[1].toString(), dtfInput, featureofinterest.getTimezone(), StatusTimeConverterEnum.TO_TIMEZONE);
            Timestamp time = new Timestamp(dt.getMillis() - offset);
            String strTd = String.valueOf(time.getTime() / 1000L);
            internvalues.add(strTd);
            internvalues.add(value);
            JSONArray jsonarr = obspropValues.get(String.valueOf(object[0]));
            if (jsonarr != null) {
                jsonarr.add(internvalues);
            }
        }
        List<JSONObject> tempJsonObjectsList = new ArrayList<JSONObject>();
        for (String obsprop : observableProperyList) {
            JSONArray jsonarr = obspropValues.get(obsprop);
            if (jsonarr != null) {
                JSONObject tmpObject = new JSONObject();
                tmpObject.put("key", obsprop);
                tmpObject.put("values", jsonarr);
                tempJsonObjectsList.add(tmpObject);
            }
        }
        JSONArray finalHumList = new JSONArray();
        JSONArray finalTempList = new JSONArray();
        for (JSONObject jsonObj : tempJsonObjectsList) {
            JSONObject newjsonobject = new JSONObject();
            newjsonobject.put("key", jsonObj.get("key").toString());
            newjsonobject.put("values", jsonObj.get("values"));
            if (jsonObj.get("key").toString().toLowerCase().contains("temperature")) {
                finalTempList.add(newjsonobject);
            } else {
                finalHumList.add(newjsonobject);
            }
        }
        JSONObject Temperatures = new JSONObject();
        JSONObject Humidities = new JSONObject();
        Temperatures.put("Temperatures", finalTempList);
        Humidities.put("Humidities", finalHumList);
        finalDataList.add(Temperatures);
        finalDataList.add(Humidities);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return finalDataList;
}
Also used : Featureofinterest(com.webstart.model.Featureofinterest) HelperCls(com.webstart.Helpers.HelperCls) Instant(org.joda.time.Instant) JSONArray(org.json.simple.JSONArray) Timestamp(java.sql.Timestamp) DateTime(org.joda.time.DateTime) LocalDateTime(org.joda.time.LocalDateTime) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 14 with Featureofinterest

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

the class UsersServiceImpl method getUserCounterNotifications.

public List<Notifications> getUserCounterNotifications(Integer userId) {
    List<Notifications> notifications = null;
    try {
        notifications = this.notificationsJpaRepository.getAllByUseridAndIsreaded(userId, false);
        // 
        List<Featureofinterest> enddevices = this.featureofinterestJpaRepository.getAllByUseridAndFeatureofinteresttypeid(userId, 3L);
        int offset = DateTimeZone.getDefault().getOffset(new Instant());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter dtfInput = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        // Convert Datetime created to user timezone
        for (Notifications notification : notifications) {
            HelperCls.ConvertToDateTime convertable = new HelperCls.ConvertToDateTime();
            DateTime dt = convertable.GetUTCDateTime(sdf.format(notification.getDatecreated()), dtfInput, enddevices.get(0).getTimezone(), StatusTimeConverterEnum.TO_TIMEZONE);
            Timestamp ts = new Timestamp(dt.getMillis() - offset);
            notification.setDatecreated(ts);
            // 
            logger.debug("params: dt={}, ts={}", dt, ts);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        return notifications;
    }
}
Also used : Featureofinterest(com.webstart.model.Featureofinterest) HelperCls(com.webstart.Helpers.HelperCls) Instant(org.joda.time.Instant) Timestamp(java.sql.Timestamp) DateTime(org.joda.time.DateTime) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Notifications(com.webstart.model.Notifications) SimpleDateFormat(java.text.SimpleDateFormat) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

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