From 2a995deeb4ca1c590e8a9b08bf413312e69d55c7 Mon Sep 17 00:00:00 2001 From: charitha Date: Fri, 3 Aug 2018 22:31:24 +0530 Subject: [PATCH] Prevent duplicate alerts & added owner field --- .../mgt/common/DeviceManagementConstants.java | 2 + .../impl/DeviceInformationManagerImpl.java | 3 +- .../GeoLocationProviderServiceImpl.java | 7 ++- .../Geo-ExecutionPlan-Exit_alert.siddhiql | 31 +++++++--- ...Geo-ExecutionPlan-Proximity_alert.siddhiql | 56 +++++++++---------- ...n-Proximity_alert_for_GeoClusters.siddhiql | 2 +- .../Geo-ExecutionPlan-Speed_alert.siddhiql | 24 +++++--- ...nPlan-Speed_alert_for_GeoClusters.siddhiql | 2 +- ...eo-ExecutionPlan-Stationery_alert.siddhiql | 36 +++++++----- .../Geo-ExecutionPlan-Traffic_alert.siddhiql | 8 +-- .../Geo-ExecutionPlan-Within_alert.siddhiql | 31 +++++++--- .../public/js/geo_remote.js | 19 ------- .../public/js/geo_remote.js | 23 +------- 13 files changed, 131 insertions(+), 113 deletions(-) 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 e5b1183673..f826aacb5f 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 @@ -113,6 +113,8 @@ public final class DeviceManagementConstants { public static final String FLUCTUATION_RADIUS = "fluctuationRadius"; public static final String QUERY_NAME = "queryName"; public static final String AREA_NAME = "areaName"; + public static final String EXECUTION_PLAN_NAME = "executionPlanName"; + public static final String DEVICE_OWNER = "owner"; public static final String GEO_FENCE_GEO_JSON = "geoFenceGeoJSON"; public static final String SPEED_ALERT_VALUE = "speedAlertValue"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 8c9f357b19..f265509910 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -198,9 +198,8 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); deviceDetailsDAO.deleteDeviceLocation(deviceLocation.getDeviceId(), device.getEnrolmentInfo().getId()); deviceDetailsDAO.addDeviceLocation(deviceLocation, device.getEnrolmentInfo().getId()); - //TODO: This has to be fixed with enrollment id or username should include in the stream def. if (DeviceManagerUtil.isPublishLocationResponseEnabled()) { - Object[] metaData = {device.getDeviceIdentifier(), device.getType()}; + Object[] metaData = {device.getDeviceIdentifier(), device.getEnrolmentInfo().getOwner(), device.getType()}; Object[] payload = new Object[]{ deviceLocation.getUpdatedTime().getTime(), deviceLocation.getLatitude(), diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java index 1fd4700b39..c5bcec41c2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java @@ -376,6 +376,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic ExecutionPlanConfigurationDto[] allActiveExecutionPlanConfigs = null; String activeExecutionPlan = null; String executionPlanName = getExecutionPlanName(alertType, alert.getQueryName()); + parseMap.put(GeoServices.EXECUTION_PLAN_NAME, executionPlanName); eventprocessorStub = getEventProcessorAdminServiceStub(); String parsedTemplate = parseTemplateForGeoClusters(alertType, parseMap); String validationResponse = eventprocessorStub.validateExecutionPlan(parsedTemplate); @@ -483,6 +484,8 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic ExecutionPlanConfigurationDto[] allActiveExecutionPlanConfigs = null; String activeExecutionPlan = null; String executionPlanName = getExecutionPlanName(alertType, alert.getQueryName(), identifier.getId(), owner); + parseMap.put(GeoServices.EXECUTION_PLAN_NAME, executionPlanName); + parseMap.put(GeoServices.DEVICE_OWNER, owner); eventprocessorStub = getEventProcessorAdminServiceStub(); String parsedTemplate = parseTemplate(alertType, parseMap); String validationResponse = eventprocessorStub.validateExecutionPlan(parsedTemplate); @@ -600,8 +603,10 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic } private String getExecutionPlanName(String alertType, String queryName, String deviceId, String owner) { - if ("Traffic".equals(alertType)) { + if (GeoServices.ALERT_TYPE_TRAFFIC.equals(alertType)) { return "Geo-ExecutionPlan-Traffic_" + queryName + "_alert"; + } else if (GeoServices.ALERT_TYPE_SPEED.equals(alertType)) { + return "Geo-ExecutionPlan-" + alertType + "---_" + owner + "_" + deviceId + "_alert"; } else { return "Geo-ExecutionPlan-" + alertType + "_" + queryName + "---_" + owner + "_" + deviceId + "_alert"; } 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 index f4e47bb2fa..982cb09cb3 100644 --- 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 @@ -7,14 +7,31 @@ /* 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); +define stream dataIn (id string, owner 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); +@Export('iot.per.device.stream.geo.FusedSpatialEvent:1.0.0') +define stream dataOut (id string, owner string, latitude double, longitude double, timeStamp long, type string, speed float, heading float, state string, information string, notify bool); -from dataIn[geo:within(longitude,latitude,"$geoFenceGeoJSON")==false and id == "$deviceId"] -select id , latitude, longitude,timeStamp, type, speed, heading ,eventId , "ALERTED" as state, str:concat(str:concat(str:concat(type," device "),id), " is outside $areaName area!!!") as information +@Export('iot.per.device.stream.geo.AlertNotifications:1.0.0') +define stream alertsOut (id string, owner string, state string, information string, timeStamp long, latitude double, longitude double, type string); + +/* Check if the device is within the geo fence. */ +from dataIn[id == "$deviceId" and owner == "$owner"] +select id, owner, latitude, longitude, timeStamp, type, speed, heading, eventId, geo:within(longitude,latitude,"$geoFenceGeoJSON") as isWithin +insert into withinStream; + +from withinStream[isWithin == false] +select id, owner, latitude, longitude,timeStamp, type, speed, heading, "ALERTED" as state, str:concat(str:concat(str:concat(type," device "),id), " is outside $areaName area!!!") as information, true as notify 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 + +from withinStream[isWithin == true] +select id, owner, latitude, longitude,timeStamp, type, speed, heading, "NORMAL" as state, "" as information, false as notify insert into dataOut; + +from every fs1=withinStream, fs2=withinStream[fs1.isWithin != isWithin] +select fs2.id, fs2.owner, fs2.latitude, fs2.longitude, fs2.timeStamp, fs2.type, fs2.speed, fs2.heading, fs2.eventId, fs2.isWithin +insert into crossedStream; + +from crossedStream[isWithin == false] +select id, owner, "ALERTED" as state, str:concat(str:concat(str:concat(type," device "),id), " is outside $areaName area!!!") as information, timeStamp, latitude, longitude, type +insert into alertsOut; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Proximity_alert.siddhiql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Proximity_alert.siddhiql index 58ac836c47..55a33c2438 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Proximity_alert.siddhiql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Proximity_alert.siddhiql @@ -1,5 +1,5 @@ /* Enter a unique ExecutionPlan */ -@Plan:name('Geo-ExecutionPlan-Proximity_alert') +@Plan:name('$executionPlanName') /* Enter a unique description for ExecutionPlan */ -- @Plan:description('ExecutionPlan') @@ -7,10 +7,10 @@ /* 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 ); +define stream dataIn (id string, owner 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 ); +@Export('iot.per.device.stream.geo.FusedSpatialEvent:1.0.0') +define stream dataOut (id string, owner string, latitude double, longitude double, timeStamp long, type string, speed float, heading float, state string, information string, notify bool); @IndexBy('id') define table ProximityTable(id string, timeStamp long); @@ -19,11 +19,11 @@ define table ProximityTable(id string, timeStamp long); define table AlertsTable(id string , proximityWith string, eventId string); from dataIn -select id, latitude, longitude, timeStamp, type, speed, heading, eventId +select id, owner, latitude, longitude, timeStamp, type, speed, heading, eventId insert into initialStream; from initialStream[type == 'STOP'] -select id , latitude, longitude,timeStamp, type, speed, heading ,eventId , "" as proximityInfo ,"false" as isProximity +select id, owner, latitude, longitude,timeStamp, type, speed, heading ,eventId , "" as proximityInfo ,"false" as isProximity insert into dataOutStream; from initialStream[type != 'STOP'] @@ -31,51 +31,51 @@ select * insert into objectInitialStream; from objectInitialStream#geo:proximity(id,longitude,latitude, $proximityDistance) -select id, latitude, longitude, timeStamp, type, speed, heading, eventId,inCloseProximity,proximityWith +select id, owner, latitude, longitude, timeStamp, type, speed, heading, eventId,inCloseProximity,proximityWith insert into proxymityStream; from proxymityStream[AlertsTable.id == proxymityStream.id in AlertsTable] -select id, latitude, longitude, timeStamp, type, speed, heading, eventId,inCloseProximity,proximityWith,true as inAlertTable +select id, owner, latitude, longitude, timeStamp, type, speed, heading, eventId,inCloseProximity,proximityWith,true as inAlertTable insert into innerStreamOne; from proxymityStream[not(AlertsTable.id == proxymityStream.id in AlertsTable)] -select id, latitude, longitude, timeStamp, type, speed, heading, eventId,inCloseProximity,proximityWith,false as inAlertTable +select id, owner, latitude, longitude, timeStamp, type, speed, heading, eventId,inCloseProximity,proximityWith,false as inAlertTable insert into innerStreamOne; from proxymityStream[AlertsTable.id == proxymityStream.proximityWith in AlertsTable] -select id, latitude, longitude, timeStamp, type, speed, heading, eventId,inCloseProximity,proximityWith,true as inAlertTable +select id, owner, latitude, longitude, timeStamp, type, speed, heading, eventId, inCloseProximity, proximityWith, true as inAlertTable insert into innerStreamSeven; from proxymityStream[not(AlertsTable.id == proxymityStream.proximityWith in AlertsTable)] -select id, latitude, longitude, timeStamp, type, speed, heading, eventId,inCloseProximity,proximityWith,false as inAlertTable +select id, owner, latitude, longitude, timeStamp, type, speed, heading, eventId,inCloseProximity,proximityWith,false as inAlertTable insert into innerStreamSeven; from innerStreamOne[inCloseProximity == true AND not(inAlertTable)] -select id,str:concat(",",proximityWith) as proximityWith , eventId +select id, owner, str:concat(",",proximityWith) as proximityWith , eventId insert into AlertsTable; from innerStreamSeven[inCloseProximity == true AND not(inAlertTable)] -select proximityWith as id,str:concat(",",id) as proximityWith , eventId +select proximityWith as id, owner, str:concat(",",id) as proximityWith , eventId insert into AlertsTable; from innerStreamOne[innerStreamOne.inCloseProximity == true AND inAlertTable]#window.length(0) join AlertsTable on innerStreamOne.id == AlertsTable.id -select innerStreamOne.id as id, str:concat(",", innerStreamOne.proximityWith, AlertsTable.proximityWith) as proximityWith, innerStreamOne.eventId as eventId +select innerStreamOne.id as id, innerStreamOne.owner as owner, str:concat(",", innerStreamOne.proximityWith, AlertsTable.proximityWith) as proximityWith, innerStreamOne.eventId as eventId insert into updateStream; from innerStreamSeven[innerStreamSeven.inCloseProximity == true AND inAlertTable]#window.length(0) join AlertsTable on innerStreamSeven.proximityWith == AlertsTable.id -select innerStreamSeven.proximityWith as id, str:concat(",", innerStreamSeven.id, AlertsTable.proximityWith) as proximityWith, innerStreamSeven.eventId as eventId +select innerStreamSeven.proximityWith as id, innerStreamSeven.owner as owner, str:concat(",", innerStreamSeven.id, AlertsTable.proximityWith) as proximityWith, innerStreamSeven.eventId as eventId insert into updateStream; from innerStreamOne[innerStreamOne.inCloseProximity == false AND inAlertTable]#window.length(0) join AlertsTable on innerStreamOne.id == AlertsTable.id -select innerStreamOne.id as id, str:replaceAll(AlertsTable.proximityWith, str:concat(",", innerStreamOne.proximityWith), "") as proximityWith, innerStreamOne.eventId as eventId +select innerStreamOne.id as id, innerStreamOne.owner as owner, str:replaceAll(AlertsTable.proximityWith, str:concat(",", innerStreamOne.proximityWith), "") as proximityWith, innerStreamOne.eventId as eventId insert into updateStream; from innerStreamSeven[innerStreamSeven.inCloseProximity == false AND inAlertTable]#window.length(0) join AlertsTable on innerStreamSeven.proximityWith == AlertsTable.id -select innerStreamSeven.proximityWith as id, str:replaceAll(AlertsTable.proximityWith, str:concat(",", innerStreamSeven.id), "") as proximityWith, innerStreamSeven.eventId as eventId +select innerStreamSeven.proximityWith as id, innerStreamSeven.owner as owner, str:replaceAll(AlertsTable.proximityWith, str:concat(",", innerStreamSeven.id), "") as proximityWith, innerStreamSeven.eventId as eventId insert into updateStream; from updateStream @@ -88,23 +88,23 @@ delete AlertsTable on id== AlertsTable.id; from objectInitialStream[AlertsTable.id == objectInitialStream.id in AlertsTable] -select id, latitude, longitude, timeStamp, type, speed, heading, eventId, true as inAlertTable +select id, owner, latitude, longitude, timeStamp, type, speed, heading, eventId, true as inAlertTable insert into publishStream; from objectInitialStream[not(AlertsTable.id == objectInitialStream.id in AlertsTable)] -select id, latitude, longitude, timeStamp, type, speed, heading, eventId, false as inAlertTable +select id, owner, latitude, longitude, timeStamp, type, speed, heading, eventId, false as inAlertTable insert into publishStream; from publishStream[inAlertTable == true]#window.length(0) join AlertsTable on publishStream.id== AlertsTable.id -select publishStream.id as id, publishStream.latitude as latitude, publishStream.longitude as longitude, publishStream.timeStamp as timeStamp, publishStream.type as type, publishStream.speed as speed, publishStream.heading as heading, publishStream.eventId as eventId, AlertsTable.proximityWith as proximityInfo +select publishStream.id as id, publishStream.owner as owner, publishStream.latitude as latitude, publishStream.longitude as longitude, publishStream.timeStamp as timeStamp, publishStream.type as type, publishStream.speed as speed, publishStream.heading as heading, publishStream.eventId as eventId, AlertsTable.proximityWith as proximityInfo insert into innerStreamTwo; from publishStream[inAlertTable == false] delete ProximityTable on ProximityTable.id==id; from publishStream[inAlertTable == false] -select id , latitude, longitude,timeStamp, type, speed, heading ,eventId , "" as proximityInfo ,"false" as isProximity +select id, owner, latitude, longitude,timeStamp, type, speed, heading ,eventId , "" as proximityInfo ,"false" as isProximity insert into dataOutStream; from innerStreamTwo[ProximityTable.id == innerStreamTwo.id in ProximityTable] @@ -112,29 +112,29 @@ insert into innerStreamThree; from innerStreamThree#window.length(0) join ProximityTable on innerStreamThree.id == ProximityTable.id -select innerStreamThree.id , innerStreamThree.latitude, innerStreamThree.longitude,innerStreamThree.timeStamp, innerStreamThree.type, innerStreamThree.speed, innerStreamThree.heading ,innerStreamThree.eventId, ProximityTable.timeStamp as storedTime, innerStreamThree.proximityInfo as proximityInfo +select innerStreamThree.id, innerStreamThree.owner, innerStreamThree.latitude, innerStreamThree.longitude,innerStreamThree.timeStamp, innerStreamThree.type, innerStreamThree.speed, innerStreamThree.heading ,innerStreamThree.eventId, ProximityTable.timeStamp as storedTime, innerStreamThree.proximityInfo as proximityInfo insert into innerStreamFour; from innerStreamFour[(timeStamp - storedTime) >= $proximityTime] -select id , latitude, longitude,timeStamp, type, speed, heading ,eventId ,proximityInfo,"true" as isProximity +select id, owner, latitude, longitude,timeStamp, type, speed, heading ,eventId ,proximityInfo, "true" as isProximity insert into dataOutStream; from innerStreamFour[(timeStamp - storedTime) < $proximityTime] -select id , latitude, longitude,timeStamp, type, speed, heading ,eventId , proximityInfo ,"false" as isProximity +select id, owner, latitude, longitude,timeStamp, type, speed, heading ,eventId, proximityInfo, "false" as isProximity insert into dataOutStream; from innerStreamTwo[not(ProximityTable.id == innerStreamTwo.id in ProximityTable)] -select innerStreamTwo.id, innerStreamTwo.timeStamp +select innerStreamTwo.id, innerStreamTwo.owner, innerStreamTwo.timeStamp insert into ProximityTable; from innerStreamTwo[not(ProximityTable.id == innerStreamTwo.id in ProximityTable)] -select id , latitude, longitude,timeStamp, type, speed, heading ,eventId , "" as proximityInfo ,"false" as isProximity +select id, owner, latitude, longitude,timeStamp, type, speed, heading ,eventId , "" as proximityInfo ,"false" as isProximity insert into dataOutStream; from dataOutStream[isProximity == 'true'] -select id, latitude, longitude, timeStamp, type, speed, heading, eventId,"WARNING" as state,str:concat("Proximity with "," ",proximityInfo) as information +select id, owner, latitude, longitude, timeStamp, type, speed, heading, "WARNING" as state,str:concat("Proximity with "," ",proximityInfo) as information, true as notify insert into dataOut; from dataOutStream[isProximity == 'false'] -select id , latitude, longitude,timeStamp, type, speed, heading ,eventId ,"NORMAL" as state,"" as information +select id, owner, latitude, longitude,timeStamp, type, speed, heading, "NORMAL" as state,"" as information, false as notify insert into dataOut; \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Proximity_alert_for_GeoClusters.siddhiql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Proximity_alert_for_GeoClusters.siddhiql index 58ac836c47..73c5ac0616 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Proximity_alert_for_GeoClusters.siddhiql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Proximity_alert_for_GeoClusters.siddhiql @@ -1,5 +1,5 @@ /* Enter a unique ExecutionPlan */ -@Plan:name('Geo-ExecutionPlan-Proximity_alert') +@Plan:name('$executionPlanName') /* Enter a unique description for ExecutionPlan */ -- @Plan:description('ExecutionPlan') diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Speed_alert.siddhiql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Speed_alert.siddhiql index 6cc4581394..022d915220 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Speed_alert.siddhiql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Speed_alert.siddhiql @@ -1,5 +1,5 @@ /* Enter a unique ExecutionPlan */ -@Plan:name('Geo-ExecutionPlan-Speed---$deviceId_alert') +@Plan:name('$executionPlanName') /* Enter a unique description for ExecutionPlan */ -- @Plan:description('ExecutionPlan') @@ -7,14 +7,22 @@ /* 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); +define stream dataIn (id string, owner 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); +@Export('iot.per.device.stream.geo.FusedSpatialEvent:1.0.0') +define stream dataOut (id string, owner string, latitude double, longitude double, timeStamp long, type string, speed float, heading float, state string, information string, notify bool ); -from dataIn[speed >= $speedAlertValue and id == "$deviceId"] -select id , latitude, longitude,timeStamp, type ,speed, heading ,eventId , "ALERTED" as state, str:concat(str:concat(str:concat(str:concat("Movement of ",type), " device "), id), " is not normal!!") as information +@Export('iot.per.device.stream.geo.AlertNotifications:1.0.0') +define stream alertsOut (id string, owner string, state string, information string, timeStamp long, latitude double, longitude double, type string); + +from dataIn[speed >= $speedAlertValue and id == "$deviceId" and owner == "$owner"] +select id, owner, latitude, longitude, timeStamp, type, speed, heading, "ALERTED" as state, str:concat(str:concat(str:concat(str:concat("Speed of ",type), " device "), id), " is not normal!!") as information, true as notify insert into dataOut; -from dataIn[speed < $speedAlertValue and id == "$deviceId"] -select id , latitude, longitude,timeStamp, type ,speed, heading ,eventId , "NORMAL" as state, str:concat(str:concat(str:concat(str:concat("Movement of ",type), " device "), id), " is normal") as information + +from dataIn[speed < $speedAlertValue and id == "$deviceId" and owner == "$owner"] +select id, owner, latitude, longitude, timeStamp, type, speed, heading, "NORMAL" as state, str:concat(str:concat(str:concat(str:concat("Speed of ",type), " device "), id), " is normal") as information, false as notify insert into dataOut; + +from dataOut[notify == true] +select id, owner, state, information, timeStamp, latitude, longitude, type +insert into alertsOut; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Speed_alert_for_GeoClusters.siddhiql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Speed_alert_for_GeoClusters.siddhiql index 7ecd9948cd..59973ad7ea 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Speed_alert_for_GeoClusters.siddhiql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Speed_alert_for_GeoClusters.siddhiql @@ -1,5 +1,5 @@ /* Enter a unique ExecutionPlan */ -@Plan:name('Geo-ExecutionPlan-Speed---_alert') +@Plan:name('$executionPlanName') /* Enter a unique description for ExecutionPlan */ -- @Plan:description('ExecutionPlan') diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Stationery_alert.siddhiql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Stationery_alert.siddhiql index f29f50be68..00673bb203 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Stationery_alert.siddhiql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Stationery_alert.siddhiql @@ -7,11 +7,13 @@ /* 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); +define stream dataIn (id string, owner string, latitude double, longitude double, timeStamp long, type string ,speed float, heading float, eventId string); +@Export('iot.per.device.stream.geo.FusedSpatialEvent:1.0.0') +define stream dataOut (id string, owner string, latitude double, longitude double, timeStamp long, type string, speed float, heading float, state string, information string, notify bool ); -@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); +@Export('iot.per.device.stream.geo.AlertNotifications:1.0.0') +define stream alertsOut (id string, owner string, state string, information string, timeStamp long, latitude double, longitude double, type string); @IndexBy('id') define table StationeryTable(id string, timeStamp long); @@ -20,18 +22,18 @@ define table StationeryTable(id string, timeStamp long); define table AlertsTable(id string, stationary bool); from dataIn -select id, latitude, longitude, timeStamp, type, speed, heading, eventId,geo:within(longitude,latitude,"$geoFenceGeoJSON") as isWithin +select id, owner, latitude, longitude, timeStamp, type, speed, heading, eventId,geo:within(longitude,latitude,"$geoFenceGeoJSON") as isWithin insert into innerStreamOne; from innerStreamOne[isWithin == false] delete StationeryTable on StationeryTable.id==id; from innerStreamOne[isWithin == false] -select id , latitude, longitude,timeStamp, type, speed, heading ,eventId , "false" as isStationary +select id, owner, latitude, longitude,timeStamp, type, speed, heading ,eventId , "false" as isStationary insert into dataOutStream; from innerStreamOne[isWithin == true]#geo:stationary(id,longitude,latitude, $fluctuationRadius) -select id, latitude, longitude, timeStamp, type, speed, heading, eventId,stationary +select id, owner, latitude, longitude, timeStamp, type, speed, heading, eventId,stationary insert into innerStreamTwo; from innerStreamTwo[innerStreamTwo.stationary == true] @@ -45,7 +47,7 @@ from innerStreamTwo[innerStreamTwo.stationary == false] delete StationeryTable on StationeryTable.id==id; from innerStreamOne[isWithin == true AND not(AlertsTable.id == innerStreamOne.id in AlertsTable)] -select id , latitude, longitude,timeStamp, type, speed, heading ,eventId , "false" as isStationary +select id, owner, latitude, longitude,timeStamp, type, speed, heading ,eventId , "false" as isStationary insert into dataOutStream; from innerStreamOne[isWithin == true AND AlertsTable.id == innerStreamOne.id in AlertsTable] @@ -53,7 +55,7 @@ insert into innerStreamThree; from innerStreamThree#window.length(0) join AlertsTable on innerStreamThree.id == AlertsTable.id -select innerStreamThree.id , innerStreamThree.latitude, innerStreamThree.longitude,innerStreamThree.timeStamp, innerStreamThree.type, innerStreamThree.speed, innerStreamThree.heading ,innerStreamThree.eventId +select innerStreamThree.id, innerStreamThree.owner, innerStreamThree.latitude, innerStreamThree.longitude,innerStreamThree.timeStamp, innerStreamThree.type, innerStreamThree.speed, innerStreamThree.heading ,innerStreamThree.eventId insert into innerStreamFour; from innerStreamFour[not(StationeryTable.id == innerStreamFour.id in StationeryTable)] @@ -61,7 +63,7 @@ select innerStreamFour.id, innerStreamFour.timeStamp insert into StationeryTable; from innerStreamOne[isWithin == true AND not(StationeryTable.id == innerStreamOne.id in StationeryTable)] -select id , latitude, longitude,timeStamp, type, speed, heading ,eventId , "false" as isStationary +select id, owner, latitude, longitude,timeStamp, type, speed, heading ,eventId , "false" as isStationary insert into dataOutStream; from innerStreamOne[isWithin == true AND StationeryTable.id == innerStreamOne.id in StationeryTable] @@ -69,21 +71,25 @@ insert into innerStreamFive; from innerStreamFive#window.length(0) join StationeryTable on innerStreamFive.id == StationeryTable.id -select innerStreamFive.id , innerStreamFive.latitude, innerStreamFive.longitude,innerStreamFive.timeStamp, innerStreamFive.type, innerStreamFive.speed, innerStreamFive.heading ,innerStreamFive.eventId, StationeryTable.timeStamp as storedTime +select innerStreamFive.id, innerStreamFive.owner, innerStreamFive.latitude, innerStreamFive.longitude,innerStreamFive.timeStamp, innerStreamFive.type, innerStreamFive.speed, innerStreamFive.heading ,innerStreamFive.eventId, StationeryTable.timeStamp as storedTime insert into innerStreamSix; from innerStreamSix[(timeStamp - storedTime) >= $stationeryTime] -select id , latitude, longitude,timeStamp, type, speed, heading ,eventId ,"true" as isStationary +select id, owner, latitude, longitude,timeStamp, type, speed, heading ,eventId ,"true" as isStationary insert into dataOutStream; from innerStreamSix[(timeStamp - storedTime) < $stationeryTime] -select id , latitude, longitude,timeStamp, type, speed, heading ,eventId ,"false" as isStationary +select id, owner, latitude, longitude,timeStamp, type, speed, heading ,eventId ,"false" as isStationary insert into dataOutStream; from dataOutStream[isStationary == 'true'] -select id ,latitude, longitude,timeStamp, type, speed, heading ,eventId ,"ALERTED" as state, str:concat(str:concat(str:concat(type," device "),id),"is in $stationeryName area!!!") as information +select id, owner, latitude, longitude,timeStamp, type, speed, heading, "ALERTED" as state, str:concat(str:concat(str:concat(type," device "),id),"is in $stationeryName area!!!") as information, true as notify insert into dataOut; from dataOutStream[isStationary == 'false'] -select id , latitude, longitude,timeStamp, type, speed, heading ,eventId ,"NORMAL" as state,"" as information -insert into dataOut; \ No newline at end of file +select id, owner, latitude, longitude,timeStamp, type, speed, heading, "NORMAL" as state,"" as information, false as notify +insert into dataOut; + +from dataOut[notify == true] +select id, owner, state, information, timeStamp, latitude, longitude, type +insert into alertsOut; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Traffic_alert.siddhiql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Traffic_alert.siddhiql index 5e6bb5b1a8..c5ce1187ee 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Traffic_alert.siddhiql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Traffic_alert.siddhiql @@ -7,11 +7,11 @@ /* define streams/tables and write queries here ... */ @Import('rawGeoStream:1.0.0') -define stream dataIn (id string, timeStamp long, geometry string, state string, information string); +define stream dataIn (id string, owner string, timeStamp long, geometry string, state string, information string); @Export('AlertsNotifications:1.0.0') -define stream dataOut (id string, state string, information string, timeStamp long, latitude double, longitude double); +define stream dataOut (id string, owner string, state string, information string, timeStamp long, latitude double, longitude double); -from dataIn[geo:intersects(geometry, "$geoFenceGeoJSON")==true and geodashboard:needToNotify(id, str:concat(information, state), "sendFirst") == true and id == $deviceId] -select id, state, str:concat("Traffic alert in $areaName. State: ", state, " ", information) as information, timeStamp, 0.0 as latitude, 0.0 as longitude +from dataIn[geo:intersects(geometry, "$geoFenceGeoJSON")==true and geodashboard:needToNotify(id, str:concat(information, state), "sendFirst") == true and id == "$deviceId" and owner == "$owner"] +select id, owner, state, str:concat("Traffic alert in $areaName. State: ", state, " ", information) as information, timeStamp, 0.0 as latitude, 0.0 as longitude insert into dataOut \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Within_alert.siddhiql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Within_alert.siddhiql index f88e1a8d24..2ce27e7f3b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Within_alert.siddhiql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/resources/alerts/Geo-ExecutionPlan-Within_alert.siddhiql @@ -7,14 +7,31 @@ /* 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); +define stream dataIn (id string, owner 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); +@Export('iot.per.device.stream.geo.FusedSpatialEvent:1.0.0') +define stream dataOut (id string, owner string, latitude double, longitude double, timeStamp long, type string, speed float, heading float, state string, information string, notify bool ); -from dataIn[geo:within(longitude,latitude,"$geoFenceGeoJSON")==true and id == "$deviceId"] -select id , latitude, longitude,timeStamp, type, speed, heading ,eventId , "ALERTED" as state, str:concat(str:concat(str:concat(type," device "), id), " is in $areaName restricted area!!!") as information +@Export('iot.per.device.stream.geo.AlertNotifications:1.0.0') +define stream alertsOut (id string, owner string, state string, information string, timeStamp long, latitude double, longitude double, type string); + +/* Check if the device is within the geo fence. */ +from dataIn[id == "$deviceId" and owner == "$owner"] +select id, owner, latitude, longitude, timeStamp, type, speed, heading, eventId, geo:within(longitude,latitude,"$geoFenceGeoJSON") as isWithin +insert into withinStream; + +from withinStream[isWithin == true] +select id, owner, latitude, longitude,timeStamp, type, speed, heading, "ALERTED" as state, str:concat(str:concat(str:concat(type," device "),id), " is in $areaName restricted area!!!") as information, true as notify insert into dataOut; -from dataIn[geo:within(longitude,latitude,"$geoFenceGeoJSON")!=true and id == "$deviceId"] -select id , latitude, longitude,timeStamp, type, speed, heading ,eventId , "NORMAL" as state, "" as information + +from withinStream[isWithin == false] +select id, owner, latitude, longitude,timeStamp, type, speed, heading, "NORMAL" as state, "" as information, false as notify insert into dataOut; + +from every fs1=withinStream, fs2=withinStream[fs1.isWithin != isWithin] +select fs2.id, fs2.owner, fs2.latitude, fs2.longitude, fs2.timeStamp, fs2.type, fs2.speed, fs2.heading, fs2.eventId, fs2.isWithin +insert into crossedStream; + +from crossedStream[isWithin == true] +select id, owner, "ALERTED" as state, str:concat(str:concat(str:concat(type," device "),id), " is outside $areaName area!!!") as information, timeStamp, latitude, longitude, type +insert into alertsOut; 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/geo_remote.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.geo-dashboard/public/js/geo_remote.js index b6e5cba7e3..136e455453 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.geo-dashboard/public/js/geo_remote.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.geo-dashboard/public/js/geo_remote.js @@ -248,7 +248,6 @@ function setWithinAlert(leafletId) { var data = { 'parseData': JSON.stringify({ 'geoFenceGeoJSON': selectedAreaGeoJson, - 'executionPlanName': createExecutionPlanName(queryName, "WithIn", deviceId), 'areaName': areaName, 'deviceId': deviceId }), @@ -307,7 +306,6 @@ function setExitAlert(leafletId) { var data = { 'parseData': JSON.stringify({ 'geoFenceGeoJSON': selectedAreaGeoJson, - 'executionPlanName': createExecutionPlanName(queryName, "Exit", deviceId), 'areaName': areaName, 'deviceId': deviceId }), @@ -379,7 +377,6 @@ function setStationeryAlert(leafletId) { var data = { 'parseData': JSON.stringify({ 'geoFenceGeoJSON': selectedProcessedAreaGeoJson, - 'executionPlanName': createExecutionPlanName(queryName, "Stationery", deviceId), 'stationeryName': stationeryName, 'stationeryTime': time, 'fluctuationRadius': fluctuationRadius @@ -492,7 +489,6 @@ function setTrafficAlert(leafletId) { var data = { 'parseData': JSON.stringify({ 'geoFenceGeoJSON': selectedProcessedAreaGeoJson, - 'executionPlanName': createExecutionPlanName(queryName, "Traffic", deviceId), 'areaName': areaName }), 'executionPlan': 'Traffic', @@ -634,21 +630,6 @@ function setProximityAlert() { } } -// TODO:this is not a remote call , move this to application.js -function createExecutionPlanName(queryName, id, deviceId) { - - if (id == "WithIn") { - return 'Geo-ExecutionPlan-Within' + (queryName ? '_' + queryName : '') + "---" + (deviceId ? '_' + deviceId : '') + '_alert'; // TODO: value of the `queryName` can't be empty, because it will cause name conflicts in CEP, have to do validation(check not empty String) - } else if (id == "Exit") { - return 'Geo-ExecutionPlan-Exit' + (queryName ? '_' + queryName : '') + "---" + (deviceId ? '_' + deviceId : '') + '_alert'; // TODO: value of the `queryName` can't be empty, because it will cause name conflicts in CEP, have to do validation(check not empty String) - } else if (id == "Stationery") { - return 'Geo-ExecutionPlan-Stationery' + (queryName ? '_' + queryName : '') + "---" + (deviceId ? '_' + deviceId : '') + '_alert'; // TODO: value of the `queryName` can't be empty, because it will cause name conflicts in CEP, have to do validation(check not empty String) - } else if (id == "Traffic") { - return 'Geo-ExecutionPlan-Traffic' + (queryName ? '_' + queryName : '') + '_alert'; // TODO: value of the `queryName` can't be empty, because it will cause name conflicts in CEP, have to do validation(check not empty String) - } - -} - // TODO:this is not a remote call , move this to application.js function closeAll() { $('.modal').modal('hide'); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.geo-devices/public/js/geo_remote.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.geo-devices/public/js/geo_remote.js index af47b6db21..ce78a0a66b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.geo-devices/public/js/geo_remote.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.geo-devices/public/js/geo_remote.js @@ -165,7 +165,9 @@ function setSpeedAlert() { noty({text: message, type: 'error'}); } else { data = { - 'parseData': JSON.stringify({'speedAlertValue': speedAlertValue, 'deviceId': deviceId}), // parseKey : parseValue pair , this key pair is replace with the key in the template file + 'parseData': JSON.stringify({ + 'speedAlertValue': speedAlertValue, + 'deviceId': deviceId}), // parseKey : parseValue pair , this key pair is replace with the key in the template file 'executionPlan': 'Speed', 'customName': null, 'cepAction': 'edit', @@ -221,7 +223,6 @@ function setWithinAlert(leafletId) { var data = { 'parseData': JSON.stringify({ 'geoFenceGeoJSON': selectedAreaGeoJson, - 'executionPlanName': createExecutionPlanName(queryName, "WithIn", deviceId), 'areaName': areaName, 'deviceId': deviceId }), @@ -280,7 +281,6 @@ function setExitAlert(leafletId) { var data = { 'parseData': JSON.stringify({ 'geoFenceGeoJSON': selectedAreaGeoJson, - 'executionPlanName': createExecutionPlanName(queryName, "Exit", deviceId), 'areaName': areaName, 'deviceId': deviceId }), @@ -352,7 +352,6 @@ function setStationeryAlert(leafletId) { var data = { 'parseData': JSON.stringify({ 'geoFenceGeoJSON': selectedProcessedAreaGeoJson, - 'executionPlanName': createExecutionPlanName(queryName, "Stationery", deviceId), 'stationeryName': stationeryName, 'stationeryTime': time, 'fluctuationRadius': fluctuationRadius @@ -427,7 +426,6 @@ function setTrafficAlert(leafletId) { var data = { 'parseData': JSON.stringify({ 'geoFenceGeoJSON': selectedProcessedAreaGeoJson, - 'executionPlanName': createExecutionPlanName(queryName, "Traffic", deviceId), 'areaName': areaName }), 'executionPlan': 'Traffic', @@ -569,21 +567,6 @@ function setProximityAlert() { } } -// TODO:this is not a remote call , move this to application.js -function createExecutionPlanName(queryName, id) { - - if (id == "WithIn") { - return 'Geo-ExecutionPlan-Within' + (queryName ? '_' + queryName : '') + "---" + '_alert'; // TODO: value of the `queryName` can't be empty, because it will cause name conflicts in CEP, have to do validation(check not empty String) - } else if (id == "Exit") { - return 'Geo-ExecutionPlan-Exit' + (queryName ? '_' + queryName : '') + "---" + '_alert'; // TODO: value of the `queryName` can't be empty, because it will cause name conflicts in CEP, have to do validation(check not empty String) - } else if (id == "Stationery") { - return 'Geo-ExecutionPlan-Stationery' + (queryName ? '_' + queryName : '') + "---" + '_alert'; // TODO: value of the `queryName` can't be empty, because it will cause name conflicts in CEP, have to do validation(check not empty String) - } else if (id == "Traffic") { - return 'Geo-ExecutionPlan-Traffic' + (queryName ? '_' + queryName : '') + '_alert'; // TODO: value of the `queryName` can't be empty, because it will cause name conflicts in CEP, have to do validation(check not empty String) - } - -} - // TODO:this is not a remote call , move this to application.js function closeAll() { $('.modal').modal('hide');