|
|
@ -25,10 +25,20 @@ import com.google.gson.Gson;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.http.HttpStatus;
|
|
|
|
import org.apache.http.HttpStatus;
|
|
|
|
|
|
|
|
import org.wso2.carbon.analytics.api.AnalyticsDataAPI;
|
|
|
|
|
|
|
|
import org.wso2.carbon.analytics.api.AnalyticsDataAPIUtil;
|
|
|
|
|
|
|
|
import org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse;
|
|
|
|
|
|
|
|
import org.wso2.carbon.analytics.dataservice.commons.SearchResultEntry;
|
|
|
|
|
|
|
|
import org.wso2.carbon.analytics.dataservice.commons.SortByField;
|
|
|
|
|
|
|
|
import org.wso2.carbon.analytics.dataservice.commons.SortType;
|
|
|
|
|
|
|
|
import org.wso2.carbon.analytics.datasource.commons.Record;
|
|
|
|
|
|
|
|
import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.context.CarbonContext;
|
|
|
|
import org.wso2.carbon.device.mgt.common.Device;
|
|
|
|
import org.wso2.carbon.device.mgt.common.Device;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants.GeoServices;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants.GeoServices;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
|
|
|
import org.wso2.carbon.device.mgt.common.PaginationRequest;
|
|
|
|
import org.wso2.carbon.device.mgt.common.PaginationRequest;
|
|
|
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
|
|
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
|
|
|
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
|
|
|
@ -37,23 +47,30 @@ import org.wso2.carbon.device.mgt.common.event.config.EventConfigurationExceptio
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.geo.service.Alert;
|
|
|
|
import org.wso2.carbon.device.mgt.common.geo.service.Alert;
|
|
|
|
import org.wso2.carbon.device.mgt.common.geo.service.AlertAlreadyExistException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.geo.service.AlertAlreadyExistException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.geo.service.Event;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.geo.service.GeoCluster;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.geo.service.GeoCoordinate;
|
|
|
|
import org.wso2.carbon.device.mgt.common.geo.service.GeoFence;
|
|
|
|
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.GeoLocationBasedServiceException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationProviderService;
|
|
|
|
import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationProviderService;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.geo.service.GeoQuery;
|
|
|
|
import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData;
|
|
|
|
import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData;
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
|
|
|
|
import org.wso2.carbon.device.mgt.core.geo.GeoCluster;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.geo.geoHash.geoHashStrategy.GeoHashLengthStrategy;
|
|
|
|
import org.wso2.carbon.device.mgt.core.geo.geoHash.geoHashStrategy.GeoHashLengthStrategy;
|
|
|
|
import org.wso2.carbon.device.mgt.core.geo.geoHash.geoHashStrategy.ZoomGeoHashLengthStrategy;
|
|
|
|
import org.wso2.carbon.device.mgt.core.geo.geoHash.geoHashStrategy.ZoomGeoHashLengthStrategy;
|
|
|
|
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
|
|
|
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.beans.EventAction;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.beans.EventAction;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.beans.GeofenceWrapper;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.beans.GeofenceWrapper;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.service.api.GeoLocationBasedService;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.service.api.GeoLocationBasedService;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.InputValidationException;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.InputValidationException;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
|
|
|
|
|
|
|
|
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.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.Consumes;
|
|
|
|
import javax.ws.rs.DELETE;
|
|
|
|
import javax.ws.rs.DELETE;
|
|
|
@ -66,79 +83,84 @@ import javax.ws.rs.Produces;
|
|
|
|
import javax.ws.rs.QueryParam;
|
|
|
|
import javax.ws.rs.QueryParam;
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* The api for
|
|
|
|
* The api for
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
|
|
|
|
public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
|
|
|
|
|
|
|
|
|
|
|
|
private static Log log = LogFactory.getLog(GeoLocationBasedServiceImpl.class);
|
|
|
|
private static final Log log = LogFactory.getLog(GeoLocationBasedServiceImpl.class);
|
|
|
|
|
|
|
|
|
|
|
|
//todo:amalka
|
|
|
|
@Path("stats/{deviceType}/{deviceId}")
|
|
|
|
// @Path("stats/{deviceType}/{deviceId}")
|
|
|
|
@GET
|
|
|
|
// @GET
|
|
|
|
@Consumes("application/json")
|
|
|
|
// @Consumes("application/json")
|
|
|
|
@Produces("application/json")
|
|
|
|
// @Produces("application/json")
|
|
|
|
public Response getGeoDeviceStats(@PathParam("deviceId") String deviceId,
|
|
|
|
// public Response getGeoDeviceStats(@PathParam("deviceId") String deviceId,
|
|
|
|
@PathParam("deviceType") String deviceType,
|
|
|
|
// @PathParam("deviceType") String deviceType,
|
|
|
|
@QueryParam("from") long from, @QueryParam("to") long to) {
|
|
|
|
// @QueryParam("from") long from, @QueryParam("to") long to) {
|
|
|
|
try {
|
|
|
|
// try {
|
|
|
|
if (!DeviceManagerUtil.isPublishLocationResponseEnabled()) {
|
|
|
|
// if (!DeviceManagerUtil.isPublishLocationResponseEnabled()) {
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST.getStatusCode())
|
|
|
|
// return Response.status(Response.Status.BAD_REQUEST.getStatusCode())
|
|
|
|
.entity("Unable to retrive Geo Device stats. Geo Data publishing does not enabled.").build();
|
|
|
|
// .entity("Unable to retrive Geo Device stats. Geo Data publishing does not enabled.").build();
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
// } catch (DeviceManagementException e) {
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(e.getMessage()).build();
|
|
|
|
// return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(e.getMessage()).build();
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
String tableName = "IOT_PER_DEVICE_STREAM_GEO_FUSEDSPATIALEVENT";
|
|
|
|
// String tableName = "IOT_PER_DEVICE_STREAM_GEO_FUSEDSPATIALEVENT";
|
|
|
|
String fromDate = String.valueOf(from);
|
|
|
|
// String fromDate = String.valueOf(from);
|
|
|
|
String toDate = String.valueOf(to);
|
|
|
|
// String toDate = String.valueOf(to);
|
|
|
|
String query = "id:" + deviceId + " AND type:" + deviceType;
|
|
|
|
// String query = "id:" + deviceId + " AND type:" + deviceType;
|
|
|
|
if (from != 0 || to != 0) {
|
|
|
|
// if (from != 0 || to != 0) {
|
|
|
|
query += " AND timeStamp : [" + fromDate + " TO " + toDate + "]";
|
|
|
|
// query += " AND timeStamp : [" + fromDate + " TO " + toDate + "]";
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
try {
|
|
|
|
// try {
|
|
|
|
if (!DeviceMgtAPIUtils.getDeviceAccessAuthorizationService().isUserAuthorized(
|
|
|
|
// if (!DeviceMgtAPIUtils.getDeviceAccessAuthorizationService().isUserAuthorized(
|
|
|
|
new DeviceIdentifier(deviceId, deviceType),
|
|
|
|
// new DeviceIdentifier(deviceId, deviceType),
|
|
|
|
DeviceGroupConstants.Permissions.DEFAULT_STATS_MONITOR_PERMISSIONS)) {
|
|
|
|
// DeviceGroupConstants.Permissions.DEFAULT_STATS_MONITOR_PERMISSIONS)) {
|
|
|
|
return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
|
|
|
|
// return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
List<SortByField> sortByFields = new ArrayList<>();
|
|
|
|
// List<SortByField> sortByFields = new ArrayList<>();
|
|
|
|
SortByField sortByField = new SortByField("timeStamp", SortType.ASC);
|
|
|
|
// SortByField sortByField = new SortByField("timeStamp", SortType.ASC);
|
|
|
|
sortByFields.add(sortByField);
|
|
|
|
// sortByFields.add(sortByField);
|
|
|
|
|
|
|
|
//
|
|
|
|
// this is the user who initiates the request
|
|
|
|
// // this is the user who initiates the request
|
|
|
|
String authorizedUser = MultitenantUtils.getTenantAwareUsername(
|
|
|
|
// String authorizedUser = MultitenantUtils.getTenantAwareUsername(
|
|
|
|
CarbonContext.getThreadLocalCarbonContext().getUsername());
|
|
|
|
// CarbonContext.getThreadLocalCarbonContext().getUsername());
|
|
|
|
|
|
|
|
//
|
|
|
|
try {
|
|
|
|
// try {
|
|
|
|
String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
|
|
|
|
// String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
|
|
|
|
int tenantId = DeviceMgtAPIUtils.getRealmService().getTenantManager().getTenantId(tenantDomain);
|
|
|
|
// int tenantId = DeviceMgtAPIUtils.getRealmService().getTenantManager().getTenantId(tenantDomain);
|
|
|
|
AnalyticsDataAPI analyticsDataAPI = DeviceMgtAPIUtils.getAnalyticsDataAPI();
|
|
|
|
// AnalyticsDataAPI analyticsDataAPI = DeviceMgtAPIUtils.getAnalyticsDataAPI();
|
|
|
|
List<SearchResultEntry> searchResults = analyticsDataAPI.search(tenantId, tableName, query,
|
|
|
|
// List<SearchResultEntry> searchResults = analyticsDataAPI.search(tenantId, tableName, query,
|
|
|
|
0,
|
|
|
|
// 0,
|
|
|
|
100,
|
|
|
|
// 100,
|
|
|
|
sortByFields);
|
|
|
|
// sortByFields);
|
|
|
|
List<Event> events = getEventBeans(analyticsDataAPI, tenantId, tableName, new ArrayList<String>(),
|
|
|
|
// List<Event> events = getEventBeans(analyticsDataAPI, tenantId, tableName, new ArrayList<String>(),
|
|
|
|
searchResults);
|
|
|
|
// searchResults);
|
|
|
|
return Response.ok().entity(events).build();
|
|
|
|
// return Response.ok().entity(null).build();
|
|
|
|
} catch (AnalyticsException | UserStoreException e) {
|
|
|
|
// } catch (AnalyticsException| UserStoreException e) {
|
|
|
|
log.error("Failed to perform search on table: " + tableName + " : " + e.getMessage(), e);
|
|
|
|
// log.error("Failed to perform search on table: " + tableName + " : " + e.getMessage(), e);
|
|
|
|
throw DeviceMgtUtil.buildBadRequestException(
|
|
|
|
// throw DeviceMgtUtil.buildBadRequestException(
|
|
|
|
Constants.ErrorMessages.STATUS_BAD_REQUEST_MESSAGE_DEFAULT);
|
|
|
|
// Constants.ErrorMessages.STATUS_BAD_REQUEST_MESSAGE_DEFAULT);
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
} catch (DeviceAccessAuthorizationException e) {
|
|
|
|
// } catch (DeviceAccessAuthorizationException e) {
|
|
|
|
log.error(e.getErrorMessage());
|
|
|
|
// log.error(e.getErrorMessage());
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
|
|
|
// return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Path("stats/device-locations")
|
|
|
|
@Path("stats/device-locations")
|
|
|
|
@GET
|
|
|
|
@GET
|
|
|
|
@Consumes("application/json")
|
|
|
|
@Consumes("application/json")
|
|
|
|
@Produces("application/json")
|
|
|
|
@Produces("application/json")
|
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
public Response getGeoDeviceLocations(
|
|
|
|
public Response getGeoDeviceLocations(
|
|
|
|
@QueryParam("deviceType") String deviceType,
|
|
|
|
@QueryParam("deviceType") String deviceType,
|
|
|
|
@QueryParam("minLat") double minLat,
|
|
|
|
@QueryParam("minLat") double minLat,
|
|
|
@ -146,6 +168,64 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
|
|
|
|
@QueryParam("minLong") double minLong,
|
|
|
|
@QueryParam("minLong") double minLong,
|
|
|
|
@QueryParam("maxLong") double maxLong,
|
|
|
|
@QueryParam("maxLong") double maxLong,
|
|
|
|
@QueryParam("zoom") int zoom) {
|
|
|
|
@QueryParam("zoom") int zoom) {
|
|
|
|
|
|
|
|
GeoHashLengthStrategy geoHashLengthStrategy = new ZoomGeoHashLengthStrategy();
|
|
|
|
|
|
|
|
GeoCoordinate southWest = new GeoCoordinate(minLat, minLong);
|
|
|
|
|
|
|
|
GeoCoordinate northEast = new GeoCoordinate(maxLat, maxLong);
|
|
|
|
|
|
|
|
int geohashLength = geoHashLengthStrategy.getGeohashLength(southWest, northEast, zoom);
|
|
|
|
|
|
|
|
DeviceManagementProviderService deviceManagementService = DeviceMgtAPIUtils.getDeviceManagementService();
|
|
|
|
|
|
|
|
GeoQuery geoQuery = new GeoQuery(southWest, northEast, geohashLength);
|
|
|
|
|
|
|
|
if (deviceType != null) {
|
|
|
|
|
|
|
|
geoQuery.setDeviceTypes(Collections.singletonList(deviceType));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
List<org.wso2.carbon.device.mgt.jaxrs.beans.GeoCluster> geoClusters = new ArrayList<>();
|
|
|
|
|
|
|
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
List<GeoCluster> newClusters = deviceManagementService.findGeoClusters(geoQuery);
|
|
|
|
|
|
|
|
org.wso2.carbon.device.mgt.jaxrs.beans.GeoCluster geoCluster;
|
|
|
|
|
|
|
|
String deviceIdentification = null;
|
|
|
|
|
|
|
|
String deviceName = null;
|
|
|
|
|
|
|
|
String lastSeen = null;
|
|
|
|
|
|
|
|
for (GeoCluster gc : newClusters) {
|
|
|
|
|
|
|
|
if (gc.getDevice() != null) {
|
|
|
|
|
|
|
|
deviceIdentification = gc.getDevice().getDeviceIdentifier();
|
|
|
|
|
|
|
|
deviceName = gc.getDevice().getName();
|
|
|
|
|
|
|
|
deviceType = gc.getDevice().getType();
|
|
|
|
|
|
|
|
lastSeen = simpleDateFormat.format(new Date(gc.getDevice()
|
|
|
|
|
|
|
|
.getEnrolmentInfo().getDateOfLastUpdate()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
geoCluster = new org.wso2.carbon.device.mgt.jaxrs.beans.GeoCluster(gc.getCoordinates(),
|
|
|
|
|
|
|
|
gc.getSouthWestBound(), gc.getNorthEastBound(), gc.getCount(), gc.getGeohashPrefix(),
|
|
|
|
|
|
|
|
deviceIdentification, deviceName, deviceType, lastSeen);
|
|
|
|
|
|
|
|
geoClusters.add(geoCluster);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
|
|
|
String msg = "Error occurred while retrieving geo clusters query: " + new Gson().toJson(geoQuery);
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return Response.ok().entity(geoClusters).build();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Path("stats/geo-view")
|
|
|
|
|
|
|
|
@GET
|
|
|
|
|
|
|
|
@Consumes("application/json")
|
|
|
|
|
|
|
|
@Produces("application/json")
|
|
|
|
|
|
|
|
public Response getGeoDeviceView(
|
|
|
|
|
|
|
|
@QueryParam("minLat") double minLat,
|
|
|
|
|
|
|
|
@QueryParam("maxLat") double maxLat,
|
|
|
|
|
|
|
|
@QueryParam("minLong") double minLong,
|
|
|
|
|
|
|
|
@QueryParam("maxLong") double maxLong,
|
|
|
|
|
|
|
|
@QueryParam("zoom") int zoom,
|
|
|
|
|
|
|
|
@QueryParam("deviceType") List<String> deviceTypes,
|
|
|
|
|
|
|
|
@QueryParam("deviceIdentifier") List<String> deviceIdentifiers,
|
|
|
|
|
|
|
|
@QueryParam("status") List<EnrolmentInfo.Status> statuses,
|
|
|
|
|
|
|
|
@QueryParam("ownership") List<String> ownerships,
|
|
|
|
|
|
|
|
@QueryParam("owner") List<String> owners,
|
|
|
|
|
|
|
|
@QueryParam("noClusters") boolean noClusters,
|
|
|
|
|
|
|
|
@QueryParam("createdBefore") long createdBefore,
|
|
|
|
|
|
|
|
@QueryParam("createdAfter") long createdAfter,
|
|
|
|
|
|
|
|
@QueryParam("updatedBefore") long updatedBefore,
|
|
|
|
|
|
|
|
@QueryParam("updatedAfter") long updatedAfter) {
|
|
|
|
|
|
|
|
|
|
|
|
GeoHashLengthStrategy geoHashLengthStrategy = new ZoomGeoHashLengthStrategy();
|
|
|
|
GeoHashLengthStrategy geoHashLengthStrategy = new ZoomGeoHashLengthStrategy();
|
|
|
|
GeoCoordinate southWest = new GeoCoordinate(minLat, minLong);
|
|
|
|
GeoCoordinate southWest = new GeoCoordinate(minLat, minLong);
|
|
|
@ -153,15 +233,25 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
|
|
|
|
int geohashLength = geoHashLengthStrategy.getGeohashLength(southWest, northEast, zoom);
|
|
|
|
int geohashLength = geoHashLengthStrategy.getGeohashLength(southWest, northEast, zoom);
|
|
|
|
DeviceManagementProviderService deviceManagementService = DeviceMgtAPIUtils.getDeviceManagementService();
|
|
|
|
DeviceManagementProviderService deviceManagementService = DeviceMgtAPIUtils.getDeviceManagementService();
|
|
|
|
List<GeoCluster> geoClusters;
|
|
|
|
List<GeoCluster> geoClusters;
|
|
|
|
|
|
|
|
GeoQuery geoQuery = new GeoQuery(southWest, northEast, geohashLength);
|
|
|
|
|
|
|
|
geoQuery.setDeviceTypes(deviceTypes);
|
|
|
|
|
|
|
|
geoQuery.setDeviceIdentifiers(deviceIdentifiers);
|
|
|
|
|
|
|
|
geoQuery.setStatuses(statuses);
|
|
|
|
|
|
|
|
geoQuery.setOwners(owners);
|
|
|
|
|
|
|
|
geoQuery.setOwnerships(ownerships);
|
|
|
|
|
|
|
|
geoQuery.setNoClusters(noClusters);
|
|
|
|
|
|
|
|
geoQuery.setCreatedBefore(createdBefore);
|
|
|
|
|
|
|
|
geoQuery.setCreatedAfter(createdAfter);
|
|
|
|
|
|
|
|
geoQuery.setUpdatedBefore(updatedBefore);
|
|
|
|
|
|
|
|
geoQuery.setUpdatedAfter(updatedAfter);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
geoClusters = deviceManagementService.findGeoClusters(deviceType, southWest, northEast, geohashLength);
|
|
|
|
geoClusters = deviceManagementService.findGeoClusters(geoQuery);
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
String msg = "Error occurred while retrieving geo clusters ";
|
|
|
|
String msg = "Error occurred while retrieving geo clusters for query: " + new Gson().toJson(geoQuery);
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Response.ok().entity(geoClusters).build();
|
|
|
|
return Response.ok().entity(geoClusters).build();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Path("alerts/{alertType}/{deviceType}/{deviceId}")
|
|
|
|
@Path("alerts/{alertType}/{deviceType}/{deviceId}")
|
|
|
@ -455,144 +545,142 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//todo:amalka
|
|
|
|
@Path("alerts/history/{deviceType}/{deviceId}")
|
|
|
|
// @Path("alerts/history/{deviceType}/{deviceId}")
|
|
|
|
@GET
|
|
|
|
// @GET
|
|
|
|
@Consumes("application/json")
|
|
|
|
// @Consumes("application/json")
|
|
|
|
@Produces("application/json")
|
|
|
|
// @Produces("application/json")
|
|
|
|
public Response getGeoAlertsHistory(@PathParam("deviceId") String deviceId,
|
|
|
|
// public Response getGeoAlertsHistory(@PathParam("deviceId") String deviceId,
|
|
|
|
@PathParam("deviceType") String deviceType,
|
|
|
|
// @PathParam("deviceType") String deviceType,
|
|
|
|
@QueryParam("from") long from, @QueryParam("to") long to) {
|
|
|
|
// @QueryParam("from") long from, @QueryParam("to") long to) {
|
|
|
|
String tableName = "IOT_PER_DEVICE_STREAM_GEO_ALERTNOTIFICATIONS";
|
|
|
|
// String tableName = "IOT_PER_DEVICE_STREAM_GEO_ALERTNOTIFICATIONS";
|
|
|
|
String fromDate = String.valueOf(from);
|
|
|
|
// String fromDate = String.valueOf(from);
|
|
|
|
String toDate = String.valueOf(to);
|
|
|
|
// String toDate = String.valueOf(to);
|
|
|
|
String query = "id:" + deviceId + " AND type:" + deviceType;
|
|
|
|
// String query = "id:" + deviceId + " AND type:" + deviceType;
|
|
|
|
if (from != 0 || to != 0) {
|
|
|
|
// if (from != 0 || to != 0) {
|
|
|
|
query += " AND timeStamp : [" + fromDate + " TO " + toDate + "]";
|
|
|
|
// query += " AND timeStamp : [" + fromDate + " TO " + toDate + "]";
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
try {
|
|
|
|
// try {
|
|
|
|
if (!DeviceMgtAPIUtils.getDeviceAccessAuthorizationService().isUserAuthorized(
|
|
|
|
// if (!DeviceMgtAPIUtils.getDeviceAccessAuthorizationService().isUserAuthorized(
|
|
|
|
new DeviceIdentifier(deviceId, deviceType),
|
|
|
|
// new DeviceIdentifier(deviceId, deviceType),
|
|
|
|
DeviceGroupConstants.Permissions.DEFAULT_STATS_MONITOR_PERMISSIONS)) {
|
|
|
|
// DeviceGroupConstants.Permissions.DEFAULT_STATS_MONITOR_PERMISSIONS)) {
|
|
|
|
return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
|
|
|
|
// return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
List<SortByField> sortByFields = new ArrayList<>();
|
|
|
|
// List<SortByField> sortByFields = new ArrayList<>();
|
|
|
|
SortByField sortByField = new SortByField("timeStamp", SortType.ASC);
|
|
|
|
// SortByField sortByField = new SortByField("timeStamp", SortType.ASC);
|
|
|
|
sortByFields.add(sortByField);
|
|
|
|
// sortByFields.add(sortByField);
|
|
|
|
|
|
|
|
//
|
|
|
|
// this is the user who initiates the request
|
|
|
|
// // this is the user who initiates the request
|
|
|
|
String authorizedUser = MultitenantUtils.getTenantAwareUsername(
|
|
|
|
// String authorizedUser = MultitenantUtils.getTenantAwareUsername(
|
|
|
|
CarbonContext.getThreadLocalCarbonContext().getUsername());
|
|
|
|
// CarbonContext.getThreadLocalCarbonContext().getUsername());
|
|
|
|
|
|
|
|
//
|
|
|
|
try {
|
|
|
|
// try {
|
|
|
|
String tenantDomain = MultitenantUtils.getTenantDomain(authorizedUser);
|
|
|
|
// String tenantDomain = MultitenantUtils.getTenantDomain(authorizedUser);
|
|
|
|
int tenantId = DeviceMgtAPIUtils.getRealmService().getTenantManager().getTenantId(tenantDomain);
|
|
|
|
// int tenantId = DeviceMgtAPIUtils.getRealmService().getTenantManager().getTenantId(tenantDomain);
|
|
|
|
AnalyticsDataAPI analyticsDataAPI = DeviceMgtAPIUtils.getAnalyticsDataAPI();
|
|
|
|
// AnalyticsDataAPI analyticsDataAPI = DeviceMgtAPIUtils.getAnalyticsDataAPI();
|
|
|
|
List<SearchResultEntry> searchResults = analyticsDataAPI.search(tenantId, tableName, query,
|
|
|
|
// List<SearchResultEntry> searchResults = analyticsDataAPI.search(tenantId, tableName, query,
|
|
|
|
0,
|
|
|
|
// 0,
|
|
|
|
100,
|
|
|
|
// 100,
|
|
|
|
sortByFields);
|
|
|
|
// sortByFields);
|
|
|
|
List<Event> events = getEventBeans(analyticsDataAPI, tenantId, tableName, new ArrayList<String>(),
|
|
|
|
// List<Event> events = getEventBeans(analyticsDataAPI, tenantId, tableName, new ArrayList<String>(),
|
|
|
|
searchResults);
|
|
|
|
// searchResults);
|
|
|
|
return Response.ok().entity(events).build();
|
|
|
|
// return Response.ok().entity(events).build();
|
|
|
|
} catch (AnalyticsException | UserStoreException e) {
|
|
|
|
// } catch (AnalyticsException | UserStoreException e) {
|
|
|
|
log.error("Failed to perform search on table: " + tableName + " : " + e.getMessage(), e);
|
|
|
|
// log.error("Failed to perform search on table: " + tableName + " : " + e.getMessage(), e);
|
|
|
|
throw DeviceMgtUtil.buildBadRequestException(
|
|
|
|
// throw DeviceMgtUtil.buildBadRequestException(
|
|
|
|
Constants.ErrorMessages.STATUS_BAD_REQUEST_MESSAGE_DEFAULT);
|
|
|
|
// Constants.ErrorMessages.STATUS_BAD_REQUEST_MESSAGE_DEFAULT);
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
} catch (DeviceAccessAuthorizationException e) {
|
|
|
|
// } catch (DeviceAccessAuthorizationException e) {
|
|
|
|
log.error(e.getErrorMessage());
|
|
|
|
// log.error(e.getErrorMessage());
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
|
|
|
// return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
@Path("alerts/history")
|
|
|
|
//todo:amalka
|
|
|
|
@GET
|
|
|
|
// @Path("alerts/history")
|
|
|
|
@Consumes("application/json")
|
|
|
|
// @GET
|
|
|
|
@Produces("application/json")
|
|
|
|
// @Consumes("application/json")
|
|
|
|
public Response getGeoAlertsHistoryForGeoClusters(@QueryParam("from") long from, @QueryParam("to") long to) {
|
|
|
|
// @Produces("application/json")
|
|
|
|
String tableName = "IOT_PER_DEVICE_STREAM_GEO_ALERTNOTIFICATIONS";
|
|
|
|
// public Response getGeoAlertsHistoryForGeoClusters(@QueryParam("from") long from, @QueryParam("to") long to) {
|
|
|
|
String fromDate = String.valueOf(from);
|
|
|
|
// String tableName = "IOT_PER_DEVICE_STREAM_GEO_ALERTNOTIFICATIONS";
|
|
|
|
String toDate = String.valueOf(to);
|
|
|
|
// String fromDate = String.valueOf(from);
|
|
|
|
String query = "";
|
|
|
|
// String toDate = String.valueOf(to);
|
|
|
|
if (from != 0 || to != 0) {
|
|
|
|
// String query = "";
|
|
|
|
query = "timeStamp : [" + fromDate + " TO " + toDate + "]";
|
|
|
|
// if (from != 0 || to != 0) {
|
|
|
|
}
|
|
|
|
// query = "timeStamp : [" + fromDate + " TO " + toDate + "]";
|
|
|
|
try {
|
|
|
|
// }
|
|
|
|
List<SortByField> sortByFields = new ArrayList<>();
|
|
|
|
// try {
|
|
|
|
SortByField sortByField = new SortByField("timeStamp", SortType.ASC);
|
|
|
|
// List<SortByField> sortByFields = new ArrayList<>();
|
|
|
|
sortByFields.add(sortByField);
|
|
|
|
// SortByField sortByField = new SortByField("timeStamp", SortType.ASC);
|
|
|
|
|
|
|
|
// sortByFields.add(sortByField);
|
|
|
|
// this is the user who initiates the request
|
|
|
|
//
|
|
|
|
String authorizedUser = MultitenantUtils.getTenantAwareUsername(
|
|
|
|
// // this is the user who initiates the request
|
|
|
|
CarbonContext.getThreadLocalCarbonContext().getUsername());
|
|
|
|
// String authorizedUser = MultitenantUtils.getTenantAwareUsername(
|
|
|
|
|
|
|
|
// CarbonContext.getThreadLocalCarbonContext().getUsername());
|
|
|
|
String tenantDomain = MultitenantUtils.getTenantDomain(authorizedUser);
|
|
|
|
//
|
|
|
|
int tenantId = DeviceMgtAPIUtils.getRealmService().getTenantManager().getTenantId(tenantDomain);
|
|
|
|
// String tenantDomain = MultitenantUtils.getTenantDomain(authorizedUser);
|
|
|
|
AnalyticsDataAPI analyticsDataAPI = DeviceMgtAPIUtils.getAnalyticsDataAPI();
|
|
|
|
// int tenantId = DeviceMgtAPIUtils.getRealmService().getTenantManager().getTenantId(tenantDomain);
|
|
|
|
List<SearchResultEntry> searchResults = analyticsDataAPI.search(tenantId, tableName, query,
|
|
|
|
// AnalyticsDataAPI analyticsDataAPI = DeviceMgtAPIUtils.getAnalyticsDataAPI();
|
|
|
|
0,
|
|
|
|
// List<SearchResultEntry> searchResults = analyticsDataAPI.search(tenantId, tableName, query,
|
|
|
|
100,
|
|
|
|
// 0,
|
|
|
|
sortByFields);
|
|
|
|
// 100,
|
|
|
|
List<Event> events = getEventBeans(analyticsDataAPI, tenantId, tableName, new ArrayList<String>(),
|
|
|
|
// sortByFields);
|
|
|
|
searchResults);
|
|
|
|
// List<Event> events = getEventBeans(analyticsDataAPI, tenantId, tableName, new ArrayList<String>(),
|
|
|
|
return Response.ok().entity(events).build();
|
|
|
|
// searchResults);
|
|
|
|
|
|
|
|
// return Response.ok().entity(events).build();
|
|
|
|
} catch (AnalyticsException | UserStoreException e) {
|
|
|
|
//
|
|
|
|
log.error("Failed to perform search on table: " + tableName + " : " + e.getMessage(), e);
|
|
|
|
// } catch (AnalyticsException | UserStoreException e) {
|
|
|
|
throw DeviceMgtUtil.buildBadRequestException(
|
|
|
|
// log.error("Failed to perform search on table: " + tableName + " : " + e.getMessage(), e);
|
|
|
|
Constants.ErrorMessages.STATUS_BAD_REQUEST_MESSAGE_DEFAULT);
|
|
|
|
// throw DeviceMgtUtil.buildBadRequestException(
|
|
|
|
}
|
|
|
|
// Constants.ErrorMessages.STATUS_BAD_REQUEST_MESSAGE_DEFAULT);
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
private List<Event> getEventBeans(AnalyticsDataAPI analyticsDataAPI, int tenantId, String tableName,
|
|
|
|
|
|
|
|
List<String> columns,
|
|
|
|
// private List<Event> getEventBeans(AnalyticsDataAPI analyticsDataAPI, int tenantId, String tableName,
|
|
|
|
List<SearchResultEntry> searchResults) throws AnalyticsException {
|
|
|
|
// List<String> columns,
|
|
|
|
List<String> ids = getIds(searchResults);
|
|
|
|
// List<SearchResultEntry> searchResults) throws AnalyticsException {
|
|
|
|
List<String> requiredColumns = (columns == null || columns.isEmpty()) ? null : columns;
|
|
|
|
// List<String> ids = getIds(searchResults);
|
|
|
|
AnalyticsDataResponse response = analyticsDataAPI.get(tenantId, tableName, 1, requiredColumns, ids);
|
|
|
|
// List<String> requiredColumns = (columns == null || columns.isEmpty()) ? null : columns;
|
|
|
|
List<Record> records = AnalyticsDataAPIUtil.listRecords(analyticsDataAPI, response);
|
|
|
|
// AnalyticsDataResponse response = analyticsDataAPI.get(tenantId, tableName, 1, requiredColumns, ids);
|
|
|
|
Map<String, Event> eventBeanMap = getEventBeanKeyedWithIds(records);
|
|
|
|
// List<Record> records = AnalyticsDataAPIUtil.listRecords(analyticsDataAPI, response);
|
|
|
|
return getSortedEventBeans(eventBeanMap, searchResults);
|
|
|
|
// Map<String, Event> eventBeanMap = getEventBeanKeyedWithIds(records);
|
|
|
|
}
|
|
|
|
// return getSortedEventBeans(eventBeanMap, searchResults);
|
|
|
|
|
|
|
|
// }
|
|
|
|
private List<Event> getSortedEventBeans(Map<String, Event> eventBeanMap,
|
|
|
|
|
|
|
|
List<SearchResultEntry> searchResults) {
|
|
|
|
// private List<Event> getSortedEventBeans(Map<String, Event> eventBeanMap,
|
|
|
|
List<Event> sortedRecords = new ArrayList<>();
|
|
|
|
// List<SearchResultEntry> searchResults) {
|
|
|
|
for (SearchResultEntry entry : searchResults) {
|
|
|
|
// List<Event> sortedRecords = new ArrayList<>();
|
|
|
|
sortedRecords.add(eventBeanMap.get(entry.getId()));
|
|
|
|
// for (SearchResultEntry entry : searchResults) {
|
|
|
|
}
|
|
|
|
// sortedRecords.add(eventBeanMap.get(entry.getId()));
|
|
|
|
return sortedRecords;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// return sortedRecords;
|
|
|
|
|
|
|
|
// }
|
|
|
|
private Map<String, Event> getEventBeanKeyedWithIds(List<Record> records) {
|
|
|
|
|
|
|
|
Map<String, Event> eventBeanMap = new HashMap<>();
|
|
|
|
// private Map<String, Event> getEventBeanKeyedWithIds(List<Record> records) {
|
|
|
|
for (Record record : records) {
|
|
|
|
// Map<String, Event> eventBeanMap = new HashMap<>();
|
|
|
|
Event event = getEventBean(record);
|
|
|
|
// for (Record record : records) {
|
|
|
|
eventBeanMap.put(event.getId(), event);
|
|
|
|
// Event event = getEventBean(record);
|
|
|
|
}
|
|
|
|
// eventBeanMap.put(event.getId(), event);
|
|
|
|
return eventBeanMap;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// return eventBeanMap;
|
|
|
|
|
|
|
|
// }
|
|
|
|
private List<String> getIds(List<SearchResultEntry> searchResults) {
|
|
|
|
|
|
|
|
List<String> ids = new ArrayList<>();
|
|
|
|
// private List<String> getIds(List<SearchResultEntry> searchResults) {
|
|
|
|
if (searchResults != null) {
|
|
|
|
// List<String> ids = new ArrayList<>();
|
|
|
|
for (SearchResultEntry resultEntry : searchResults) {
|
|
|
|
// if (searchResults != null) {
|
|
|
|
ids.add(resultEntry.getId());
|
|
|
|
// for (SearchResultEntry resultEntry : searchResults) {
|
|
|
|
}
|
|
|
|
// ids.add(resultEntry.getId());
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
return ids;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// return ids;
|
|
|
|
|
|
|
|
// }
|
|
|
|
private static Event getEventBean(Record record) {
|
|
|
|
|
|
|
|
Event eventBean = new Event();
|
|
|
|
// private static Event getEventBean(Record record) {
|
|
|
|
eventBean.setId(record.getId());
|
|
|
|
// Event eventBean = new Event();
|
|
|
|
eventBean.setTableName(record.getTableName());
|
|
|
|
// eventBean.setId(record.getId());
|
|
|
|
eventBean.setTimestamp(record.getTimestamp());
|
|
|
|
// eventBean.setTableName(record.getTableName());
|
|
|
|
eventBean.setValues(record.getValues());
|
|
|
|
// eventBean.setTimestamp(record.getTimestamp());
|
|
|
|
return eventBean;
|
|
|
|
// eventBean.setValues(record.getValues());
|
|
|
|
}
|
|
|
|
// return eventBean;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Path("/geo-fence")
|
|
|
|
@Path("/geo-fence")
|
|
|
|
@POST
|
|
|
|
@POST
|
|
|
|