Search in sources :

Example 1 with SensorRecord

use of org.coffeeking.api.util.SensorRecord in project product-iots by wso2.

the class ConnectedCupServiceImpl method getDeviceStats.

@Path("stats/{deviceId}/sensors/{sensorName}")
@GET
@Consumes("application/json")
@Produces("application/json")
public Response getDeviceStats(@PathParam("deviceId") String deviceId, @PathParam("sensorName") String sensor, @QueryParam("from") long from, @QueryParam("to") long to) {
    String fromDate = String.valueOf(from);
    String toDate = String.valueOf(to);
    String query = " deviceId:" + deviceId + " AND deviceType:" + ConnectedCupConstants.DEVICE_TYPE + " AND time : [" + fromDate + " TO " + toDate + "]";
    String sensorTableName = getSensorEventTableName(sensor);
    try {
        if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId, ConnectedCupConstants.DEVICE_TYPE), DeviceGroupConstants.Permissions.DEFAULT_STATS_MONITOR_PERMISSIONS)) {
            return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
        }
        List<SensorRecord> sensorDatas;
        List<SortByField> sortByFields = new ArrayList<>();
        SortByField sortByField = new SortByField("time", SortType.ASC);
        sortByFields.add(sortByField);
        sensorDatas = APIUtil.getAllEventsForDevice(sensorTableName, query, sortByFields);
        return Response.ok().entity(sensorDatas).build();
    } catch (AnalyticsException e) {
        String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query;
        log.error(errorMsg, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(errorMsg).build();
    } catch (DeviceAccessAuthorizationException e) {
        log.error(e.getErrorMessage(), e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
    }
}
Also used : SortByField(org.wso2.carbon.analytics.dataservice.commons.SortByField) DeviceAccessAuthorizationException(org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException) AnalyticsException(org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException) SensorRecord(org.coffeeking.api.util.SensorRecord) ArrayList(java.util.ArrayList) DeviceIdentifier(org.wso2.carbon.device.mgt.common.DeviceIdentifier) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

ArrayList (java.util.ArrayList)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 SensorRecord (org.coffeeking.api.util.SensorRecord)1 SortByField (org.wso2.carbon.analytics.dataservice.commons.SortByField)1 AnalyticsException (org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException)1 DeviceIdentifier (org.wso2.carbon.device.mgt.common.DeviceIdentifier)1 DeviceAccessAuthorizationException (org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException)1