From 05b11a5099cb9e49b13f33ead63f757b2022415b Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Tue, 6 Jun 2017 14:55:04 +0530 Subject: [PATCH] Adding Exit Alerts feature --- .../jaxrs/service/impl/GeoServiceImpl.java | 3 ++ .../mgt/common/DeviceManagementConstants.java | 1 + .../mgt/common/geo/service/GeoService.java | 2 + .../geo/service/GeoServcieManagerImpl.java | 53 +++++++++++++++++++ .../Geo-ExecutionPlan-Exit_alert.siddhiql | 20 +++++++ 5 files changed, 79 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Exit_alert.siddhiql diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoServiceImpl.java index dca603ed99..f90bfbc6ac 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoServiceImpl.java @@ -244,6 +244,9 @@ public class GeoServiceImpl implements GeoService { if (GeoServices.EXECUTION_PLAN_TYPE_WITHIN.equals(executionPlanType)) { List alerts = geoService.getWithinAlerts(identifier); return Response.ok().entity(alerts).build(); + } else if (GeoServices.EXECUTION_PLAN_TYPE_EXIT.equals(executionPlanType)) { + List alerts = geoService.getExitAlerts(identifier); + return Response.ok().entity(alerts).build(); } else if (GeoServices.EXECUTION_PLAN_TYPE_SPEED.equals(executionPlanType)) { String result = geoService.getSpeedAlerts(identifier); return Response.ok().entity(result).build(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java index a3d50e2d12..25aa80354d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java @@ -101,6 +101,7 @@ public final class DeviceManagementConstants { public static final String EXECUTION_PLAN_TYPE_SPEED = "Speed"; public static final String EXECUTION_PLAN_TYPE_WITHIN = "Within"; + public static final String EXECUTION_PLAN_TYPE_EXIT = "Exit"; public static final String EXECUTION_PLAN_TYPE_PROXIMITY = "Proximity"; public static final String EXECUTION_PLAN_TYPE_STATIONARY = "Stationery"; public static final String EXECUTION_PLAN_TYPE_TRAFFIC = "Traffic"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoService.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoService.java index 1cc28e95f5..679fedb4ed 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoService.java @@ -30,6 +30,8 @@ public interface GeoService { List getWithinAlerts(DeviceIdentifier identifier) throws GeoServiceException; + List getExitAlerts(DeviceIdentifier identifier) throws GeoServiceException; + boolean createGeoAlert(Alert alert, DeviceIdentifier identifier, String executionPlanType) throws GeoServiceException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoServcieManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoServcieManagerImpl.java index f176c18ede..bad8a2c94c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoServcieManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoServcieManagerImpl.java @@ -157,6 +157,54 @@ public class GeoServcieManagerImpl implements GeoService { } } + @Override + public List getExitAlerts(DeviceIdentifier identifier) throws GeoServiceException { + + Registry registry = getGovernanceRegistry(); + String registryPath = GeoServices.REGISTRY_PATH_FOR_ALERTS + + GeoServices.EXECUTION_PLAN_TYPE_EXIT + "/" + identifier.getId() + "/"; + Resource resource; + try { + resource = registry.get(registryPath); + } catch (RegistryException e) { + log.error("Error while reading the registry path: " + registryPath); + return null; + } + + try { + List fences = new ArrayList<>(); + if (resource != null) { + Object contentObj = resource.getContent(); + if (contentObj instanceof String[]) { + String[] content = (String[]) contentObj; + for (String res : content) { + Resource childRes = registry.get(res); + Properties props = childRes.getProperties(); + + GeoFence geoFence = new GeoFence(); + + InputStream inputStream = childRes.getContentStream(); + StringWriter writer = new StringWriter(); + IOUtils.copy(inputStream, writer, "UTF-8"); + geoFence.setGeoJson(writer.toString()); + + List queryNameObj = (List) props.get(GeoServices.QUERY_NAME); + geoFence.setQueryName(queryNameObj != null ? queryNameObj.get(0).toString() : null); + List areaNameObj = (List) props.get(GeoServices.AREA_NAME); + geoFence.setAreaName(areaNameObj != null ? areaNameObj.get(0).toString() : null); + geoFence.setCreatedTime(childRes.getCreatedTime().getTime()); + fences.add(geoFence); + } + } + } + return fences; + } catch (RegistryException | IOException e) { + throw new GeoServiceException( + "Error occurred while getting the geo alerts for " + identifier.getType() + " with id: " + + identifier.getId(), e); + } + } + @Override public boolean createGeoAlert(Alert alert, DeviceIdentifier identifier, String executionPlanType) throws GeoServiceException { @@ -185,6 +233,11 @@ public class GeoServcieManagerImpl implements GeoService { options.put(GeoServices.AREA_NAME, alert.getCustomName()); content = parseMap.get(GeoServices.GEO_FENCE_GEO_JSON); + } else if (GeoServices.EXECUTION_PLAN_TYPE_EXIT.equals(executionPlanType)) { + options.put(GeoServices.QUERY_NAME, alert.getQueryName()); + options.put(GeoServices.AREA_NAME, alert.getCustomName()); + content = parseMap.get(GeoServices.GEO_FENCE_GEO_JSON); + } else if (GeoServices.EXECUTION_PLAN_TYPE_SPEED.equals(executionPlanType)) { content = parseMap.get(GeoServices.SPEED_ALERT_VALUE); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Exit_alert.siddhiql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Exit_alert.siddhiql new file mode 100644 index 0000000000..7f391b530b --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Exit_alert.siddhiql @@ -0,0 +1,20 @@ +/* Enter a unique ExecutionPlan */ +@Plan:name('$executionPlanName') + +/* Enter a unique description for ExecutionPlan */ +-- @Plan:description('ExecutionPlan') + +/* define streams/tables and write queries here ... */ + +@Import('org.wso2.geo.StandardSpatialEvents:1.0.0') +define stream dataIn (id string, latitude double, longitude double, timeStamp long, type string ,speed float, heading float, eventId string); + +@Export('org.wso2.geo.ProcessedSpatialEvents:1.0.0') +define stream dataOut (id string, latitude double, longitude double, timeStamp long, type string ,speed float, heading float, eventId string, state string, information string); + +from dataIn[geo:within(longitude,latitude,"$geoFenceGeoJSON")==false and id == "$deviceId"]#geodashboard:subscribe() +select id , latitude, longitude,timeStamp, type, speed, heading ,eventId , "ALERTED" as state, "This device is outside $areaName area!!!" as information +insert into dataOut; +from dataIn[geo:within(longitude,latitude,"$geoFenceGeoJSON")!=false and id == "$deviceId"] +select id , latitude, longitude,timeStamp, type, speed, heading ,eventId , "NORMAL" as state, "" as information +insert into dataOut;