mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Add location extraction from device properties and deice type specific location retrieval
This commit is contained in:
parent
bccb88ab22
commit
2bfd598c67
@ -198,6 +198,10 @@ public interface GeoLocationBasedService {
|
||||
response = Response.class)
|
||||
})
|
||||
Response getGeoDeviceLocations(
|
||||
@ApiParam(
|
||||
name = "deviceType",
|
||||
value = "Optional Device type name.")
|
||||
@QueryParam("deviceType") String deviceType,
|
||||
@ApiParam(
|
||||
name = "minLat",
|
||||
value = "Define the minimum latitude of the geofence.",
|
||||
|
||||
@ -125,6 +125,7 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
|
||||
@Consumes("application/json")
|
||||
@Produces("application/json")
|
||||
public Response getGeoDeviceLocations(
|
||||
@QueryParam("deviceType") String deviceType,
|
||||
@QueryParam("minLat") double minLat,
|
||||
@QueryParam("maxLat") double maxLat,
|
||||
@QueryParam("minLong") double minLong,
|
||||
@ -138,7 +139,7 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
|
||||
DeviceManagementProviderService deviceManagementService = DeviceMgtAPIUtils.getDeviceManagementService();
|
||||
List<GeoCluster> geoClusters;
|
||||
try {
|
||||
geoClusters = deviceManagementService.findGeoClusters(southWest, northEast, geohashLength);
|
||||
geoClusters = deviceManagementService.findGeoClusters(deviceType, southWest, northEast, geohashLength);
|
||||
} catch (DeviceManagementException e) {
|
||||
String msg = "Error occurred while retrieving geo clusters ";
|
||||
log.error(msg, e);
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
package org.wso2.carbon.device.mgt.jaxrs.service.impl;
|
||||
|
||||
|
||||
import org.mockito.Mockito;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
@ -33,8 +32,8 @@ public class GeoLocationBasedServiceImplTest {
|
||||
"in the given map boundaries")
|
||||
public void testGetGeoDeviceLocations1() throws DeviceManagementException {
|
||||
Mockito.doReturn(new ArrayList<GeoCluster>()).when(deviceManagementProviderService)
|
||||
.findGeoClusters(Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt());
|
||||
Response response = geoLocationBasedService.getGeoDeviceLocations(0.4, 15, 75.6,
|
||||
.findGeoClusters(null, Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt());
|
||||
Response response = geoLocationBasedService.getGeoDeviceLocations(null, 0.4, 15, 75.6,
|
||||
90.1, 6);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
|
||||
"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,
|
||||
"t1gd", "swerty12s", "android", "1234"));
|
||||
Mockito.doReturn(geoClusters).when(deviceManagementProviderService)
|
||||
.findGeoClusters(Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt());
|
||||
Response response = geoLocationBasedService.getGeoDeviceLocations(0.4, 15, 75.6,
|
||||
.findGeoClusters(null, Mockito.any(GeoCoordinate.class), Mockito.any(GeoCoordinate.class), Mockito.anyInt());
|
||||
Response response = geoLocationBasedService.getGeoDeviceLocations(null, 0.4, 15, 75.6,
|
||||
90.1, 6);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
|
||||
"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
|
||||
* boundaries.
|
||||
*
|
||||
* @param deviceType Optional device type name.
|
||||
* @param southWest the coordinates of southWest corner of the map.
|
||||
* @param northEast the coordinates of northEast corner of the map.
|
||||
* @param tenantId tenant id.
|
||||
* @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;
|
||||
}
|
||||
|
||||
public List<GeoCluster> findGeoClusters(GeoCoordinate southWest, GeoCoordinate northEast,
|
||||
public List<GeoCluster> findGeoClusters(String deviceType, GeoCoordinate southWest, GeoCoordinate northEast,
|
||||
int geohashLength, int tenantId) throws DeviceManagementDAOException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
@ -1082,8 +1082,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
||||
"WHERE DEVICE_LOCATION.LATITUDE BETWEEN ? AND ? AND " +
|
||||
"DEVICE_LOCATION.LONGITUDE BETWEEN ? AND ? AND " +
|
||||
"DEVICE.TENANT_ID=? AND " +
|
||||
"DEVICE.ID=DEVICE_LOCATION.DEVICE_ID AND DEVICE.DEVICE_TYPE_ID=DEVICE_TYPE.ID" +
|
||||
" GROUP BY GEOHASH_PREFIX";
|
||||
"DEVICE.ID=DEVICE_LOCATION.DEVICE_ID AND DEVICE.DEVICE_TYPE_ID=DEVICE_TYPE.ID";
|
||||
if (deviceType != null && !deviceType.isEmpty()) {
|
||||
sql += " AND DEVICE_TYPE.NAME=?";
|
||||
}
|
||||
sql += " GROUP BY GEOHASH_PREFIX";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, geohashLength);
|
||||
stmt.setDouble(2, southWest.getLatitude());
|
||||
@ -1091,6 +1094,9 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
|
||||
stmt.setDouble(4, southWest.getLongitude());
|
||||
stmt.setDouble(5, northEast.getLongitude());
|
||||
stmt.setDouble(6,tenantId);
|
||||
if (deviceType != null && !deviceType.isEmpty()) {
|
||||
stmt.setString(7, deviceType);
|
||||
}
|
||||
rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
double latitude = rs.getDouble("LATITUDE");
|
||||
|
||||
@ -625,6 +625,6 @@ public interface DeviceManagementProviderService {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -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.DeviceTypeDAO;
|
||||
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.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.DeviceTypeServiceIdentifier;
|
||||
import org.wso2.carbon.device.mgt.core.geo.GeoCluster;
|
||||
@ -297,10 +299,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
||||
addDeviceToGroups(deviceIdentifier, device.getEnrolmentInfo().getOwnership());
|
||||
addInitialOperations(deviceIdentifier, device.getType());
|
||||
}
|
||||
extractDeviceLocationToUpdate(device);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean modifyEnrollment(Device device) throws DeviceManagementException {
|
||||
if (device == null) {
|
||||
@ -352,6 +354,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
||||
} finally {
|
||||
DeviceManagementDAOFactory.closeConnection();
|
||||
}
|
||||
extractDeviceLocationToUpdate(device);
|
||||
return status;
|
||||
}
|
||||
|
||||
@ -2614,13 +2617,18 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
||||
}
|
||||
|
||||
@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()) {
|
||||
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 {
|
||||
DeviceManagementDAOFactory.openConnection();
|
||||
return deviceDAO.findGeoClusters(southWest, northEast, geohashLength, this.getTenantId());
|
||||
return deviceDAO.findGeoClusters(deviceType, southWest, northEast, geohashLength, this.getTenantId());
|
||||
} catch (DeviceManagementDAOException e) {
|
||||
String msg = "Error occurred while retrieving the geo clusters.";
|
||||
log.error(msg, e);
|
||||
@ -2637,4 +2645,37 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
||||
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();
|
||||
List<GeoCluster> geoClusters;
|
||||
try {
|
||||
geoClusters = deviceManagementService.findGeoClusters(southWest, northEast, geohashLength);
|
||||
geoClusters = deviceManagementService.findGeoClusters(null, southWest, northEast, geohashLength);
|
||||
} catch (DeviceManagementException e) {
|
||||
String msg = "Error occurred while retrieving geo clusters ";
|
||||
log.error(msg, e);
|
||||
|
||||
@ -19,7 +19,6 @@
|
||||
|
||||
package org.wso2.carbon.device.mgt.jaxrs.service.impl;
|
||||
|
||||
|
||||
import org.mockito.Mockito;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
@ -52,7 +51,7 @@ public class GeoLocationBasedServiceImplTest {
|
||||
"in the given map boundaries")
|
||||
public void testGetGeoDeviceLocations1() throws DeviceManagementException {
|
||||
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,
|
||||
90.1, 6);
|
||||
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,
|
||||
"t1gd", "swerty12s", "android", "1234"));
|
||||
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,
|
||||
90.1, 6);
|
||||
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
|
||||
|
||||
Loading…
Reference in New Issue
Block a user