浏览代码

Added service for getting last timestamps for given sensor list
Fix logging and database settings

mkepka 4 年之前
父节点
当前提交
93c745a87c

+ 1 - 0
src/main/java/cz/hsrs/rest/ParamsList.java

@@ -37,6 +37,7 @@ public class ParamsList {
      * Unit
      */    
     public static final String UNIT_ID = "unit_id";
+    public static final String UNIT_IDs = "unit_ids";
     
     /*
      * User

+ 19 - 4
src/main/java/cz/hsrs/rest/beans/LastObsTimestampBean.java

@@ -12,7 +12,7 @@ public class LastObsTimestampBean {
 	public int groupId;
 	public String groupName;
 	public long unitId;
-	public String description;
+	public String unitName;
 	public long sensorId;
 	public String sensorName;
 	public String lastTimest;
@@ -35,7 +35,22 @@ public class LastObsTimestampBean {
 		this.groupId = groupId;
 		this.groupName = groupName;
 		this.unitId = unitId;
-		this.description = description;
+		this.unitName = description;
+		this.sensorId = sensorId;
+		this.sensorName = sensorName;
+		this.lastTimest = lastTimest;
+	}
+
+	/**
+	 * @param unitId
+	 * @param description
+	 * @param sensorId
+	 * @param sensorName
+	 * @param lastTimest
+	 */
+	public LastObsTimestampBean(long unitId, String description, long sensorId, String sensorName, String lastTimest) {
+		this.unitId = unitId;
+		this.unitName = description;
 		this.sensorId = sensorId;
 		this.sensorName = sensorName;
 		this.lastTimest = lastTimest;
@@ -43,8 +58,8 @@ public class LastObsTimestampBean {
 
 	@Override
 	public String toString() {
-		return "[groupId=" + groupId + ", groupName=" + groupName + ", unitId=" + unitId + ", description="
-				+ description + ", sensorId=" + sensorId + ", sensorName=" + sensorName + ", lastTimest=" + lastTimest
+		return "[groupId=" + groupId + ", groupName=" + groupName + ", unitId=" + unitId + ", unitName="
+				+ unitName + ", sensorId=" + sensorId + ", sensorName=" + sensorName + ", lastTimest=" + lastTimest
 				+ "]";
 	}	
 }

+ 72 - 0
src/main/java/cz/hsrs/rest/provider/WatchDogRest.java

@@ -100,4 +100,76 @@ public class WatchDogRest {
                     .build();
         }
 	}
+	
+	/**
+	 * Service provides list of observation last time stamps for given unit or list of units
+	 * e.g.: /rest/watchdog/unit?unit_id=1305167549144045 
+	 * e.g.: /rest/watchdog/unit?unit_id=1305167549144045,1305167549149707
+	 * @param unitId
+	 * @param req
+	 * @return
+	 */
+	@Path("/unit/")
+	@GET
+	@Produces(MediaType.APPLICATION_JSON)
+	public Response getLastTimestampsPerUnit(@QueryParam(ParamsList.UNIT_ID) String unitId,
+										     @Context HttpServletRequest req) {
+		try {
+			LoginUser loggedUser = AuthUtil.getAuthenticatedLoginUser(req);
+			
+			/* to check last values needs to have superuser rights */
+			if(loggedUser.getRightsId() < 0) {
+				List<LastObsTimestampBean> obsList = ObservationRestUtil.getLastTimestampPerUnit(unitId);
+				return Response.ok().entity(obsList)
+	                    .build();
+			}
+			else {
+				throw new AuthenticationException();
+			}
+		} catch (AuthenticationException e1) {
+            return Response.status(HttpStatus.ORDINAL_401_Unauthorized)
+                    .entity(new ExceptionBean(e1.getClass().getName(), "Authentication failure for request!"))
+                    .build();
+		} catch (SQLException e) {
+            return Response.status(HttpStatus.ORDINAL_500_Internal_Server_Error)
+                    .entity(new ExceptionBean(e.getClass().getName(), e.getLocalizedMessage()))
+                    .build();
+        }
+	}
+	
+	/**
+	 * Service provides list of observation last time stamps for given sensor or list of sensors
+	 * e.g.: /rest/watchdog/sensor?sensor_id=560030000
+	 * e.g.: /rest/watchdog/sensor?sensor_id=560030000,360200000
+	 * @param sensorId
+	 * @param req
+	 * @return
+	 */
+	@Path("/sensor/")
+	@GET
+	@Produces(MediaType.APPLICATION_JSON)
+	public Response getLastTimestampsPerSensor(@QueryParam(ParamsList.SENSOR_ID) String sensorId,
+										     @Context HttpServletRequest req) {
+		try {
+			LoginUser loggedUser = AuthUtil.getAuthenticatedLoginUser(req);
+			
+			/* to check last values needs to have superuser rights */
+			if(loggedUser.getRightsId() < 0) {
+				List<LastObsTimestampBean> obsList = ObservationRestUtil.getLastTimestampPerSensor(sensorId);
+				return Response.ok().entity(obsList)
+	                    .build();
+			}
+			else {
+				throw new AuthenticationException();
+			}
+		} catch (AuthenticationException e1) {
+            return Response.status(HttpStatus.ORDINAL_401_Unauthorized)
+                    .entity(new ExceptionBean(e1.getClass().getName(), "Authentication failure for request!"))
+                    .build();
+		} catch (SQLException e) {
+            return Response.status(HttpStatus.ORDINAL_500_Internal_Server_Error)
+                    .entity(new ExceptionBean(e.getClass().getName(), e.getLocalizedMessage()))
+                    .build();
+        }
+	}
 }

+ 72 - 2
src/main/java/cz/hsrs/rest/util/ObservationRestUtil.java

@@ -26,14 +26,14 @@ public class ObservationRestUtil {
 	 */
 	public static List<LastObsTimestampBean> getLastTimestampPerGroup(String groupName, Integer groupId) throws SQLException {
 		String query;
-		if(groupName.isEmpty() && groupId != null) {
+		if(groupName == null && groupId != null) {
 			query = "SELECT * FROM public.units_sensors_lastobs WHERE id = "+groupId+";";
 		}
 		else if(!groupName.isEmpty() && groupId == null) {
 			query = "SELECT * FROM public.units_sensors_lastobs WHERE group_name = '"+groupName+"';";
 		}
 		else {
-			query = "SELECT * FROM public.units_sensors_lastobs WHERE id = "+groupId+" AND group_name = '"+groupName+"';";
+			throw new SQLException("Group identification must be provided!");
 		}
 		List<LastObsTimestampBean> obsList = new LinkedList<LastObsTimestampBean>();
 		
@@ -54,4 +54,74 @@ public class ObservationRestUtil {
 		}
 		return obsList;
 	}
+	
+	/**
+	 * 
+	 * @param unitIds
+	 * @return
+	 * @throws SQLException
+	 */
+	public static List<LastObsTimestampBean> getLastTimestampPerUnit(String unitIds) throws SQLException {
+		String query;
+		if(unitIds != null) {
+			query = "SELECT DISTINCT ON (unit_id, sensor_id) unit_id, description, sensor_id, sensor_name, last_timestamp"
+					+ " FROM public.units_sensors_lastobs"
+					+ " WHERE unit_id IN ("+unitIds+")"
+					+ " ORDER BY unit_id, sensor_id;";
+		}
+		else {
+			throw new SQLException("Unit identification must be provided!");
+		}
+		List<LastObsTimestampBean> obsList = new LinkedList<LastObsTimestampBean>();
+		
+		ResultSet res = SQLExecutor.getInstance().executeQuery(query);
+		if(res != null) {
+			while (res.next()) {
+				LastObsTimestampBean obs = new LastObsTimestampBean(
+						res.getLong("unit_id"),
+						res.getString("description"),
+						res.getLong("sensor_id"),
+						res.getString("sensor_name"),
+						res.getString("last_timestamp")
+						);
+				obsList.add(obs);
+			}
+		}
+		return obsList;
+	}
+	
+	/**
+	 * 
+	 * @param sensorIds
+	 * @return
+	 * @throws SQLException
+	 */
+	public static List<LastObsTimestampBean> getLastTimestampPerSensor(String sensorIds) throws SQLException {
+		String query;
+		if(sensorIds != null) {
+			query = "SELECT DISTINCT ON (unit_id, sensor_id) unit_id, description, sensor_id, sensor_name, last_timestamp"
+					+ " FROM public.units_sensors_lastobs"
+					+ " WHERE sensor_id IN ("+sensorIds+")"
+					+ " ORDER BY unit_id, sensor_id;";
+		}
+		else {
+			throw new SQLException("Sensor identification must be provided!");
+		}
+		List<LastObsTimestampBean> obsList = new LinkedList<LastObsTimestampBean>();
+		
+		ResultSet res = SQLExecutor.getInstance().executeQuery(query);
+		if(res != null) {
+			while (res.next()) {
+				LastObsTimestampBean obs = new LastObsTimestampBean(
+						res.getLong("unit_id"),
+						res.getString("description"),
+						res.getLong("sensor_id"),
+						res.getString("sensor_name"),
+						res.getString("last_timestamp")
+						);
+				obsList.add(obs);
+			}
+		}
+		return obsList;
+	}
 }

+ 3 - 3
src/main/webapp/WEB-INF/database.properties

@@ -15,14 +15,14 @@ UnitsPositions_table= mercator_positions
 
 
 # foodie server
-#Address=jdbc:postgresql://foodie.lesprojekt.cz:5432/foodie
+#Address=jdbc:postgresql://10.0.0.24:5432/senslog1
 #Username = maplog
 #Password = map.LOG_16
 
 # FIE20 - tunnel
-Address=jdbc:postgresql://localhost:5433/senslog1
+#Address=jdbc:postgresql://localhost:5433/senslog1
 # FIE20 - local
-#Address=jdbc:postgresql://localhost:5432/senslog1
+Address=jdbc:postgresql://localhost:5432/senslog1
 Username = senslog_app
 Password = SENSlog
 

+ 3 - 3
src/main/webapp/WEB-INF/logging.properties

@@ -3,7 +3,7 @@ formatters=java.util.logging.SimpleFormatter
 
 .level = ALL
 java.util.logging.ConsoleHandler.level = SEVERE
-java.util.logging.FileHandler.level = FINEST
+java.util.logging.FileHandler.level = WARN
 
 java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
 
@@ -11,5 +11,5 @@ java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
 #java.util.logging.FileHandler.pattern = /data/tomcat/webapps/medlov/log.log
 #java.util.logging.FileHandler.pattern = /data/tomcat/webapps/DBService/DBService.log
 #java.util.logging.FileHandler.pattern = /usr/share/tomcat5.5/webapps/DBService/maplog.log
-#java.util.logging.FileHandler.pattern = /var/log/maplog_ot.log
-java.util.logging.FileHandler.pattern = d://tmp//senslog15.log
+java.util.logging.FileHandler.pattern = /var/log/senslog/senslog15.log
+#java.util.logging.FileHandler.pattern = c://tmp//senslog15.log