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