Add location extraction from device properties and deice type specific location retrieval

revert-70aa11f8
charitha 6 years ago
parent bccb88ab22
commit 2bfd598c67

@ -198,6 +198,10 @@ public interface GeoLocationBasedService {
response = Response.class) response = Response.class)
}) })
Response getGeoDeviceLocations( Response getGeoDeviceLocations(
@ApiParam(
name = "deviceType",
value = "Optional Device type name.")
@QueryParam("deviceType") String deviceType,
@ApiParam( @ApiParam(
name = "minLat", name = "minLat",
value = "Define the minimum latitude of the geofence.", value = "Define the minimum latitude of the geofence.",

@ -125,6 +125,7 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
@Consumes("application/json") @Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public Response getGeoDeviceLocations( public Response getGeoDeviceLocations(
@QueryParam("deviceType") String deviceType,
@QueryParam("minLat") double minLat, @QueryParam("minLat") double minLat,
@QueryParam("maxLat") double maxLat, @QueryParam("maxLat") double maxLat,
@QueryParam("minLong") double minLong, @QueryParam("minLong") double minLong,
@ -138,7 +139,7 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
DeviceManagementProviderService deviceManagementService = DeviceMgtAPIUtils.getDeviceManagementService(); DeviceManagementProviderService deviceManagementService = DeviceMgtAPIUtils.getDeviceManagementService();
List<GeoCluster> geoClusters; List<GeoCluster> geoClusters;
try { try {
geoClusters = deviceManagementService.findGeoClusters(southWest, northEast, geohashLength); geoClusters = deviceManagementService.findGeoClusters(deviceType, southWest, northEast, geohashLength);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while retrieving geo clusters "; String msg = "Error occurred while retrieving geo clusters ";
log.error(msg, e); log.error(msg, e);

@ -1,6 +1,5 @@
package org.wso2.carbon.device.mgt.jaxrs.service.impl; package org.wso2.carbon.device.mgt.jaxrs.service.impl;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
@ -33,8 +32,8 @@ public class GeoLocationBasedServiceImplTest {
"in the given map boundaries") "in the given map boundaries")
public void testGetGeoDeviceLocations1() throws DeviceManagementException { public void testGetGeoDeviceLocations1() throws DeviceManagementException {
Mockito.doReturn(new ArrayList<GeoCluster>()).when(deviceManagementProviderService) Mockito.doReturn(new ArrayList<GeoCluster>()).when(deviceManagementProviderService)
.findGeoClusters(Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt()); .findGeoClusters(null, Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt());
Response response = geoLocationBasedService.getGeoDeviceLocations(0.4, 15, 75.6, Response response = geoLocationBasedService.getGeoDeviceLocations(null, 0.4, 15, 75.6,
90.1, 6); 90.1, 6);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
"getGeoDeviceLocations request failed with valid parameters"); "getGeoDeviceLocations request failed with valid parameters");
@ -51,8 +50,8 @@ public class GeoLocationBasedServiceImplTest {
new GeoCoordinate(9.8, 84.7), new GeoCoordinate(11.1, 88.1), 4, new GeoCoordinate(9.8, 84.7), new GeoCoordinate(11.1, 88.1), 4,
"t1gd", "swerty12s", "android", "1234")); "t1gd", "swerty12s", "android", "1234"));
Mockito.doReturn(geoClusters).when(deviceManagementProviderService) Mockito.doReturn(geoClusters).when(deviceManagementProviderService)
.findGeoClusters(Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt()); .findGeoClusters(null, Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt());
Response response = geoLocationBasedService.getGeoDeviceLocations(0.4, 15, 75.6, Response response = geoLocationBasedService.getGeoDeviceLocations(null, 0.4, 15, 75.6,
90.1, 6); 90.1, 6);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
"getGeoDeviceLocations request failed with valid parameters"); "getGeoDeviceLocations request failed with valid parameters");

@ -402,11 +402,13 @@ public interface DeviceDAO {
* This method is used to retrieve the details of geoclusters formed relatively to the zoom level and map * This method is used to retrieve the details of geoclusters formed relatively to the zoom level and map
* boundaries. * boundaries.
* *
* @param southWest the coordinates of southWest corner of the map. * @param deviceType Optional device type name.
* @param northEast the coordinates of northEast corner of the map. * @param southWest the coordinates of southWest corner of the map.
* @param tenantId tenant id. * @param northEast the coordinates of northEast corner of the map.
* @param tenantId tenant id.
* @return returns a list of enrolment info objects. * @return returns a list of enrolment info objects.
*/ */
List<GeoCluster> findGeoClusters(GeoCoordinate southWest, GeoCoordinate northEast, int geohashLength,int tenantId) throws DeviceManagementDAOException; List<GeoCluster> findGeoClusters(String deviceType, GeoCoordinate southWest, GeoCoordinate northEast,
int geohashLength,int tenantId) throws DeviceManagementDAOException;
} }

@ -1062,7 +1062,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
return tenants; return tenants;
} }
public List<GeoCluster> findGeoClusters(GeoCoordinate southWest, GeoCoordinate northEast, public List<GeoCluster> findGeoClusters(String deviceType, GeoCoordinate southWest, GeoCoordinate northEast,
int geohashLength, int tenantId) throws DeviceManagementDAOException { int geohashLength, int tenantId) throws DeviceManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -1082,8 +1082,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
"WHERE DEVICE_LOCATION.LATITUDE BETWEEN ? AND ? AND " + "WHERE DEVICE_LOCATION.LATITUDE BETWEEN ? AND ? AND " +
"DEVICE_LOCATION.LONGITUDE BETWEEN ? AND ? AND " + "DEVICE_LOCATION.LONGITUDE BETWEEN ? AND ? AND " +
"DEVICE.TENANT_ID=? AND " + "DEVICE.TENANT_ID=? AND " +
"DEVICE.ID=DEVICE_LOCATION.DEVICE_ID AND DEVICE.DEVICE_TYPE_ID=DEVICE_TYPE.ID" + "DEVICE.ID=DEVICE_LOCATION.DEVICE_ID AND DEVICE.DEVICE_TYPE_ID=DEVICE_TYPE.ID";
" GROUP BY GEOHASH_PREFIX"; if (deviceType != null && !deviceType.isEmpty()) {
sql += " AND DEVICE_TYPE.NAME=?";
}
sql += " GROUP BY GEOHASH_PREFIX";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, geohashLength); stmt.setInt(1, geohashLength);
stmt.setDouble(2, southWest.getLatitude()); stmt.setDouble(2, southWest.getLatitude());
@ -1091,6 +1094,9 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
stmt.setDouble(4, southWest.getLongitude()); stmt.setDouble(4, southWest.getLongitude());
stmt.setDouble(5, northEast.getLongitude()); stmt.setDouble(5, northEast.getLongitude());
stmt.setDouble(6,tenantId); stmt.setDouble(6,tenantId);
if (deviceType != null && !deviceType.isEmpty()) {
stmt.setString(7, deviceType);
}
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
double latitude = rs.getDouble("LATITUDE"); double latitude = rs.getDouble("LATITUDE");

@ -625,6 +625,6 @@ public interface DeviceManagementProviderService {
List<Integer> getDeviceEnrolledTenants() throws DeviceManagementException; List<Integer> getDeviceEnrolledTenants() throws DeviceManagementException;
List<GeoCluster> findGeoClusters(GeoCoordinate southWest, GeoCoordinate northEast, List<GeoCluster> findGeoClusters(String deviceType, GeoCoordinate southWest, GeoCoordinate northEast,
int geohashLength) throws DeviceManagementException; int geohashLength) throws DeviceManagementException;
} }

@ -67,8 +67,10 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dao.EnrollmentDAO; import org.wso2.carbon.device.mgt.core.dao.EnrollmentDAO;
import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager;
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO;
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException;
import org.wso2.carbon.device.mgt.core.device.details.mgt.impl.DeviceInformationManagerImpl;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.dto.DeviceTypeServiceIdentifier; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeServiceIdentifier;
import org.wso2.carbon.device.mgt.core.geo.GeoCluster; import org.wso2.carbon.device.mgt.core.geo.GeoCluster;
@ -297,10 +299,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
addDeviceToGroups(deviceIdentifier, device.getEnrolmentInfo().getOwnership()); addDeviceToGroups(deviceIdentifier, device.getEnrolmentInfo().getOwnership());
addInitialOperations(deviceIdentifier, device.getType()); addInitialOperations(deviceIdentifier, device.getType());
} }
extractDeviceLocationToUpdate(device);
return status; return status;
} }
@Override @Override
public boolean modifyEnrollment(Device device) throws DeviceManagementException { public boolean modifyEnrollment(Device device) throws DeviceManagementException {
if (device == null) { if (device == null) {
@ -352,6 +354,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} finally { } finally {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }
extractDeviceLocationToUpdate(device);
return status; return status;
} }
@ -2614,13 +2617,18 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
@Override @Override
public List<GeoCluster> findGeoClusters(GeoCoordinate southWest, GeoCoordinate northEast, int geohashLength) throws DeviceManagementException { public List<GeoCluster> findGeoClusters(String deviceType, GeoCoordinate southWest, GeoCoordinate northEast,
int geohashLength) throws DeviceManagementException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("get information about geo clusters"); if (deviceType == null || deviceType.isEmpty()) {
log.debug("get information about geo clusters.");
} else {
log.debug("get information about geo clusters for device type: " + deviceType);
}
} }
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
return deviceDAO.findGeoClusters(southWest, northEast, geohashLength, this.getTenantId()); return deviceDAO.findGeoClusters(deviceType, southWest, northEast, geohashLength, this.getTenantId());
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving the geo clusters."; String msg = "Error occurred while retrieving the geo clusters.";
log.error(msg, e); log.error(msg, e);
@ -2637,4 +2645,37 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }
} }
private void extractDeviceLocationToUpdate(Device device) {
List<Device.Property> properties = device.getProperties();
if (properties != null) {
String latitude = null;
String longitude = null;
for (Device.Property p : properties) {
if (p.getName().equalsIgnoreCase("latitude")) {
latitude = p.getValue();
}
if (p.getName().equalsIgnoreCase("longitude")) {
longitude = p.getValue();
}
}
if (latitude != null && longitude != null && !latitude.isEmpty() && !longitude.isEmpty()) {
DeviceLocation deviceLocation = new DeviceLocation();
deviceLocation.setDeviceId(device.getId());
deviceLocation.setDeviceIdentifier(new DeviceIdentifier(device.getDeviceIdentifier(),
device.getType()));
try {
deviceLocation.setLatitude(Double.parseDouble(latitude));
deviceLocation.setLongitude(Double.parseDouble(longitude));
DeviceInformationManager deviceInformationManager = new DeviceInformationManagerImpl();
deviceInformationManager.addDeviceLocation(deviceLocation);
} catch (Exception e) {
//We are not failing the execution since this is not critical for the functionality. But logging as
// an error for reference.
log.error("Exception occurred while trying to add device location.", e);
}
}
}
}
} }

@ -138,7 +138,7 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
DeviceManagementProviderService deviceManagementService = DeviceMgtAPIUtils.getDeviceManagementService(); DeviceManagementProviderService deviceManagementService = DeviceMgtAPIUtils.getDeviceManagementService();
List<GeoCluster> geoClusters; List<GeoCluster> geoClusters;
try { try {
geoClusters = deviceManagementService.findGeoClusters(southWest, northEast, geohashLength); geoClusters = deviceManagementService.findGeoClusters(null, southWest, northEast, geohashLength);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while retrieving geo clusters "; String msg = "Error occurred while retrieving geo clusters ";
log.error(msg, e); log.error(msg, e);

@ -19,7 +19,6 @@
package org.wso2.carbon.device.mgt.jaxrs.service.impl; package org.wso2.carbon.device.mgt.jaxrs.service.impl;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
@ -52,7 +51,7 @@ public class GeoLocationBasedServiceImplTest {
"in the given map boundaries") "in the given map boundaries")
public void testGetGeoDeviceLocations1() throws DeviceManagementException { public void testGetGeoDeviceLocations1() throws DeviceManagementException {
Mockito.doReturn(new ArrayList<GeoCluster>()).when(deviceManagementProviderService) Mockito.doReturn(new ArrayList<GeoCluster>()).when(deviceManagementProviderService)
.findGeoClusters(Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt()); .findGeoClusters(null, Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt());
Response response = geoLocationBasedService.getGeoDeviceLocations(0.4, 15, 75.6, Response response = geoLocationBasedService.getGeoDeviceLocations(0.4, 15, 75.6,
90.1, 6); 90.1, 6);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
@ -70,7 +69,7 @@ public class GeoLocationBasedServiceImplTest {
new GeoCoordinate(9.8, 84.7), new GeoCoordinate(11.1, 88.1), 4, new GeoCoordinate(9.8, 84.7), new GeoCoordinate(11.1, 88.1), 4,
"t1gd", "swerty12s", "android", "1234")); "t1gd", "swerty12s", "android", "1234"));
Mockito.doReturn(geoClusters).when(deviceManagementProviderService) Mockito.doReturn(geoClusters).when(deviceManagementProviderService)
.findGeoClusters(Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt()); .findGeoClusters(null,Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt());
Response response = geoLocationBasedService.getGeoDeviceLocations(0.4, 15, 75.6, Response response = geoLocationBasedService.getGeoDeviceLocations(0.4, 15, 75.6,
90.1, 6); 90.1, 6);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),

Loading…
Cancel
Save