TrackRest.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. *
  3. */
  4. package cz.hsrs.rest.provider;
  5. import java.sql.SQLException;
  6. import java.text.ParseException;
  7. import java.util.logging.Logger;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.ws.rs.GET;
  10. import javax.ws.rs.Path;
  11. import javax.ws.rs.QueryParam;
  12. import javax.ws.rs.core.Context;
  13. import javax.ws.rs.core.HttpHeaders;
  14. import javax.ws.rs.core.MediaType;
  15. import javax.ws.rs.core.Response;
  16. import org.mortbay.jetty.HttpStatus;
  17. import cz.hsrs.db.util.DateUtil;
  18. import cz.hsrs.rest.ParamsList;
  19. import cz.hsrs.rest.beans.ExceptionBean;
  20. import cz.hsrs.rest.util.TrackRestUtil;
  21. /**
  22. * @author mkepka
  23. *
  24. */
  25. @Path("/data/track/")
  26. public class TrackRest {
  27. public static Logger logger = Logger.getLogger("TrackRest");
  28. /**
  29. * Empty constructor
  30. */
  31. public TrackRest() {
  32. super();
  33. }
  34. /**
  35. * /rest/track/dates?group_name=
  36. * /rest/track/dates?group_id=
  37. * /rest/track/dates?unit_id=
  38. */
  39. @GET @Path("/dates")
  40. public Response getDatesOfTracks(
  41. @QueryParam(ParamsList.UNIT_ID) String unitIds,
  42. @QueryParam(ParamsList.GROUP_ID) Integer groupId,
  43. @QueryParam(ParamsList.GROUP_NAME) String groupName,
  44. @Context HttpServletRequest req)
  45. {
  46. try {
  47. String jsonTimes;
  48. if(unitIds!= null && !unitIds.isEmpty()){
  49. jsonTimes = TrackRestUtil.getDatesByUnits(unitIds);
  50. }
  51. else if (groupId != null){
  52. jsonTimes = TrackRestUtil.getDatesByUnitsByGroup(groupId);
  53. }
  54. else if(groupName !=null && !groupName.isEmpty()) {
  55. jsonTimes = TrackRestUtil.getDatesByUnitsByGroup(groupName);
  56. }
  57. else {
  58. throw new SQLException("Wrong combination of parameters");
  59. }
  60. return Response.ok()
  61. .entity(jsonTimes)
  62. .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
  63. .build();
  64. } catch (SQLException e) {
  65. return Response.status(HttpStatus.ORDINAL_500_Internal_Server_Error)
  66. .entity(new ExceptionBean(e.getClass().getName(), e.getLocalizedMessage()))
  67. .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
  68. .build();
  69. }/* catch (AuthenticationException e) {
  70. return Response.status(HttpStatus.ORDINAL_401_Unauthorized)
  71. .entity(new ExceptionBean(e.getClass().getName(), e.getLocalizedMessage()))
  72. .build();
  73. }*/
  74. }
  75. /**
  76. * /rest/track/points
  77. * /rest/track/points?unit_id=352625691996753&from_time=2022-03-19 10:00:00&to_time=2022-03-29 12:00:00
  78. */
  79. @GET @Path("/points")
  80. public Response getPointsOfTrack(
  81. @QueryParam(ParamsList.UNIT_ID) Long unitId,
  82. @QueryParam(ParamsList.SENSOR_ID) String sensorIds,
  83. @QueryParam(ParamsList.FROM_TIME) String fromTime,
  84. @QueryParam(ParamsList.TO_TIME) String toTime,
  85. @Context HttpServletRequest req)
  86. {
  87. try {
  88. if(fromTime != null && toTime != null) {
  89. long weekInMilis = (7*24*60*60*1000);
  90. if((DateUtil.parseTimestamp(toTime).getTime() - DateUtil.parseTimestamp(fromTime).getTime()) <= weekInMilis) {
  91. String geoJson = TrackRestUtil.getTrackPoints(unitId, sensorIds, fromTime, toTime);
  92. return Response.ok()
  93. .entity(geoJson)
  94. .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
  95. .build();
  96. }
  97. else {
  98. throw new SQLException("Too long interval for data selection");
  99. }
  100. }
  101. else if(fromTime != null && toTime == null){
  102. String geoJson = TrackRestUtil.getTrackPoints(unitId, sensorIds, fromTime, toTime);
  103. return Response.ok()
  104. .entity(geoJson)
  105. .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
  106. .build();
  107. }
  108. else {
  109. throw new SQLException("Wrong combination of parameters");
  110. }
  111. } catch (SQLException e) {
  112. return Response.status(HttpStatus.ORDINAL_500_Internal_Server_Error)
  113. .entity(new ExceptionBean(e.getClass().getName(), e.getLocalizedMessage()))
  114. .build();
  115. } catch (ParseException e) {
  116. return Response.status(HttpStatus.ORDINAL_400_Bad_Request)
  117. .entity(new ExceptionBean(e.getClass().getName(), e.getLocalizedMessage()))
  118. .build();
  119. }
  120. }
  121. }