From d3eb32b5ccb10e137ec8797e340e80cc9f8c4ed7 Mon Sep 17 00:00:00 2001 From: Gathika94 Date: Mon, 13 Nov 2017 15:37:20 +0530 Subject: [PATCH] working on ui features --- .../impl/GeoLocationBasedServiceImpl.java | 109 ++++++++---------- .../carbon/device/mgt/core/geo/GeoGrid.java | 7 +- .../device/mgt/core/geo/GeoRectangle.java | 93 ++++++++++----- .../cdmf.unit.geo-dashboard/public/js/app.js | 1 - 4 files changed, 116 insertions(+), 94 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java index 3d05ffe9b88..7e24fd2b582 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java @@ -35,11 +35,7 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementConstants.GeoServices; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; -import org.wso2.carbon.device.mgt.common.geo.service.Alert; -import org.wso2.carbon.device.mgt.common.geo.service.Event; -import org.wso2.carbon.device.mgt.common.geo.service.GeoFence; -import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationBasedServiceException; -import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationProviderService; +import org.wso2.carbon.device.mgt.common.geo.service.*; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException; @@ -48,26 +44,15 @@ import org.wso2.carbon.device.mgt.core.geo.GeoGrid; import org.wso2.carbon.device.mgt.core.geo.GeoRectangle; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; -import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; -import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.api.GeoLocationBasedService; -import org.wso2.carbon.device.mgt.jaxrs.service.api.GroupManagementService; import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtUtil; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; +import javax.ws.rs.*; import javax.ws.rs.core.Response; import java.util.ArrayList; import java.util.HashMap; @@ -81,6 +66,15 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { private static Log log = LogFactory.getLog(GeoLocationBasedServiceImpl.class); + private static Event getEventBean(Record record) { + Event eventBean = new Event(); + eventBean.setId(record.getId()); + eventBean.setTableName(record.getTableName()); + eventBean.setTimestamp(record.getTimestamp()); + eventBean.setValues(record.getValues()); + return eventBean; + } + @Path("stats/{deviceType}/{deviceId}") @GET @Consumes("application/json") @@ -163,7 +157,7 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { Map locationHashMap = new HashMap<>(); for (Device device : devices) { - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(),device.getType()); + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); locationHashMap.put(device.getId(), deviceInformationManagerService.getDeviceLocation(deviceIdentifier)); } @@ -173,8 +167,8 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); } catch (DeviceDetailsMgtException e) { - String msg = "Exception occurred while retrieving device location."+groupId; - log.error(msg,e); + String msg = "Exception occurred while retrieving device location." + groupId; + log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); } @@ -191,39 +185,44 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { @QueryParam("minLong") double minLong, @QueryParam("maxLong") double maxLong) { - try { - if (!DeviceManagerUtil.isPublishOperationResponseEnabled()) { - return Response.status(Response.Status.BAD_REQUEST.getStatusCode()) - .entity("Operation publishing does not exists").build(); - } - } catch (DeviceManagementException e) { - return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(e.getMessage()).build(); - } - DeviceManagementProviderService deviceManagementService = DeviceMgtAPIUtils.getDeviceManagementService(); - DeviceInformationManager deviceInformationManagerService = DeviceMgtAPIUtils.getDeviceInformationManagerService(); - GeoGrid geoGrid = new GeoGrid(horizontalDivisions,verticalDivisions,minLat,maxLat,minLong,maxLong); + DeviceManagementProviderService deviceManagementService = DeviceMgtAPIUtils.getDeviceManagementService(); + GeoGrid geoGrid = new GeoGrid(horizontalDivisions, verticalDivisions, minLat, maxLat, minLong, maxLong); try { - List devices =deviceManagementService.getAllDevices(); - ArrayList devicesInGeoGrid =geoGrid.getDevicesInGeoGrid(devices); - List geoRectangles=geoGrid.placeDevicesInGeoRectangles(devicesInGeoGrid); - Map,Map> details = new HashMap<>(); - for(GeoRectangle geoRectangle:geoRectangles){ - Map center = geoRectangle.getCenter(); - if(geoRectangle.getDeviceCount()==0){ - details.put(center,null); - }else if(geoRectangle.getDeviceCount()==1){ - Map deviceDetails = new HashMap<>(); - Device device=geoRectangle.getDevices().get(0); - deviceDetails.put("deviceID",device.getDeviceIdentifier()); - details.put(center,deviceDetails); - }else{ - Map deviceCountDetails = new HashMap<>(); - int deviceCount=geoRectangle.getDeviceCount(); - deviceCountDetails.put("count",Integer.toString(deviceCount)); - details.put(center,deviceCountDetails); + List devices = deviceManagementService.getAllDevices(); + ArrayList devicesInGeoGrid = geoGrid.getDevicesInGeoGrid(devices); + List geoRectangles = geoGrid.placeDevicesInGeoRectangles(devicesInGeoGrid); + Map> details = new HashMap<>(); + for (GeoRectangle geoRectangle : geoRectangles) { + Map rectangleDetails = new HashMap<>(); + Map rectangleCoordinates = geoRectangle.getCoordinates(); + String rectangleLat = rectangleCoordinates.get("Lat").toString(); + String rectangleLong = rectangleCoordinates.get("Long").toString(); + String minRectangleLat = geoRectangle.getMinLat().toString(); + String maxRectangleLat = geoRectangle.getMaxLat().toString(); + String minRectangleLong = geoRectangle.getMinLong().toString(); + String maxRectangleLong = geoRectangle.getMaxLong().toString(); + rectangleDetails.put("rectangleLat", rectangleLat); + rectangleDetails.put("rectangleLong", rectangleLong); + rectangleDetails.put("minLat",minRectangleLat); + rectangleDetails.put("maxLat",maxRectangleLat); + rectangleDetails.put("minLong",minRectangleLong); + rectangleDetails.put("maxLong",maxRectangleLong); + if (geoRectangle.getDeviceCount() == 0) { + + rectangleDetails.put("count", "0"); + rectangleDetails.put("deviceId", null); + + } else if (geoRectangle.getDeviceCount() == 1) { + Device device = geoRectangle.getDevices().get(0); + rectangleDetails.put("count", "1"); + rectangleDetails.put("deviceID", device.getDeviceIdentifier()); + } else { + int deviceCount = geoRectangle.getDeviceCount(); + rectangleDetails.put("count", Integer.toString(deviceCount)); + rectangleDetails.put("deviceID", null); } - + details.put(geoRectangle.getId().toString(), rectangleDetails); } return Response.ok().entity(details).build(); } catch (DeviceManagementException e) { @@ -234,7 +233,6 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { } - @Path("alerts/{alertType}/{deviceType}/{deviceId}") @POST @Consumes("application/json") @@ -475,13 +473,4 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { } return ids; } - - private static Event getEventBean(Record record) { - Event eventBean = new Event(); - eventBean.setId(record.getId()); - eventBean.setTableName(record.getTableName()); - eventBean.setTimestamp(record.getTimestamp()); - eventBean.setValues(record.getValues()); - return eventBean; - } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/GeoGrid.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/GeoGrid.java index 86753673acf..603347c0e6d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/GeoGrid.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/GeoGrid.java @@ -48,6 +48,7 @@ public class GeoGrid { double maxRectangleLat; double minRectangleLong; double maxRectangleLong; + Integer id = 0; for (int i = 0; i < verticalDivisions; i++) { minRectangleLong = this.minLong + i * longIncrement; @@ -59,7 +60,11 @@ public class GeoGrid { for (int m = 0; m < horizontalDivisions; m++) { minRectangleLat = this.minLat + m * latIncrement; - maxRectangleLat = this.minLat + (m + 1) * latIncrement; + if(m+1==horizontalDivisions){ + maxRectangleLat=this.maxLat; + }else { + maxRectangleLat = this.minLat + (m + 1) * latIncrement; + } geoRectangles.add(new GeoRectangle(minRectangleLat, maxRectangleLat, minRectangleLong, maxRectangleLong)); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/GeoRectangle.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/GeoRectangle.java index bdc36234fa3..ae91090dd3b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/GeoRectangle.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/GeoRectangle.java @@ -33,68 +33,93 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -public class GeoRectangle{ +public class GeoRectangle { + private static int count = 0; + private static Log log = LogFactory.getLog(GeoRectangle.class); + DeviceInformationManager deviceInformationManagerService = new DeviceInformationManagerImpl(); private double minLat; private double maxLat; private double minLong; private double maxLong; - private ArrayList devices=new ArrayList<>(); - - - DeviceInformationManager deviceInformationManagerService = new DeviceInformationManagerImpl(); - private static Log log = LogFactory.getLog(GeoRectangle.class); - - public GeoRectangle(double minLat, double maxLat, double minLong, double maxLong){ - this.minLat=minLat; - this.maxLat=maxLat; - this.minLong=minLong; - this.maxLong=maxLong; + private double centerLat; + private double centerLong; + private double rectangleLat; + private double rectangleLong; + private double deviantLatValue = 1000; + private double deviantLongValue = 1000; + private int id; + private ArrayList devices = new ArrayList<>(); + + public GeoRectangle(double minLat, double maxLat, double minLong, double maxLong) { + this.minLat = minLat; + this.maxLat = maxLat; + this.minLong = minLong; + this.maxLong = maxLong; + this.centerLat = (this.minLat + this.maxLat) / 2; + this.centerLong = (this.minLong + this.maxLong) / 2; + this.rectangleLat = this.centerLat; + this.rectangleLong = this.centerLong; + count++; + this.id = count; } - public Map getCenter(){ - Map centerCordinates = new HashMap(); - double centerLat = (minLat+maxLat)/2; - double centerLong = (minLong+maxLong)/2; - centerCordinates.put("Lat",centerLat); - centerCordinates.put("Long",centerLong); - return centerCordinates; + public Map getCoordinates() { + Map rectangleCoordinates = new HashMap(); + + rectangleCoordinates.put("Lat", rectangleLat); + rectangleCoordinates.put("Long", rectangleLong); + return rectangleCoordinates; } - public boolean isDeviceInGeoRectangle(Device device){ - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(),device.getType()); + public boolean isDeviceInGeoRectangle(Device device) { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); try { DeviceLocation location = deviceInformationManagerService.getDeviceLocation(deviceIdentifier); double locationLat = location.getLatitude(); double locationLong = location.getLongitude(); - if(locationLat >= minLat && locationLat < maxLat && locationLong >= minLong && locationLong= minLat && locationLat < maxLat && locationLong >= minLong && locationLong < maxLong) { + checkLocationDeviants(locationLat, locationLong); return true; - }else{ + } else { return false; } } catch (DeviceDetailsMgtException e) { - String msg = "Exception occurred while retrieving device location."+deviceIdentifier; - log.error(msg,e); + String msg = "Exception occurred while retrieving device location." + deviceIdentifier; + log.error(msg, e); return false; } } - public void addDevice(Device device){ + private void checkLocationDeviants(double locationLat, double locationLong) { + double latDeviant = Math.abs(centerLat - locationLat); + double longDeviant = Math.abs(centerLong - locationLong); + if (latDeviant <= deviantLatValue) { + deviantLatValue = latDeviant; + rectangleLat = locationLat; + } + if (longDeviant <= deviantLongValue) { + deviantLongValue = longDeviant; + rectangleLong = locationLong; + } + } + public void addDevice(Device device) { devices.add(device); } - public List getDevices(){ + + public List getDevices() { return devices; } - public int getDeviceCount(){ + public int getDeviceCount() { return devices.size(); } - public double getMinLat() { + public Double getMinLat() { return minLat; } @@ -102,7 +127,7 @@ public class GeoRectangle{ this.minLat = minLat; } - public double getMaxLat() { + public Double getMaxLat() { return maxLat; } @@ -110,7 +135,7 @@ public class GeoRectangle{ this.maxLat = maxLat; } - public double getMinLong() { + public Double getMinLong() { return minLong; } @@ -118,7 +143,7 @@ public class GeoRectangle{ this.minLong = minLong; } - public double getMaxLong() { + public Double getMaxLong() { return maxLong; } @@ -126,4 +151,8 @@ public class GeoRectangle{ this.maxLong = maxLong; } + public Integer getId() { + return id; + } + } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.geo-dashboard/public/js/app.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.geo-dashboard/public/js/app.js index 1d93c73a2f4..9c9632f3df1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.geo-dashboard/public/js/app.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.geo-dashboard/public/js/app.js @@ -105,7 +105,6 @@ function updateAttribution(e) { } }); } - var attributionControl; var geoAlertsBar; var groupedOverlays;