Search in sources :

Example 1 with DeviceManagementException

use of org.wso2.carbon.device.mgt.common.DeviceManagementException in project product-iots by wso2.

the class ConnectedCupServiceImpl method register.

@Path("device/register")
@POST
public boolean register(@QueryParam("name") String name) {
    try {
        DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
        String deviceId = shortUUID();
        deviceIdentifier.setId(deviceId);
        deviceIdentifier.setType(ConnectedCupConstants.DEVICE_TYPE);
        if (APIUtil.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
            return false;
        }
        Device device = new Device();
        device.setDeviceIdentifier(deviceId);
        EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
        enrolmentInfo.setDateOfEnrolment(new Date().getTime());
        enrolmentInfo.setDateOfLastUpdate(new Date().getTime());
        enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE);
        enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD);
        device.setName(name);
        device.setType(ConnectedCupConstants.DEVICE_TYPE);
        enrolmentInfo.setOwner(APIUtil.getAuthenticatedUser());
        device.setEnrolmentInfo(enrolmentInfo);
        return APIUtil.getDeviceManagementService().enrollDevice(device);
    } catch (DeviceManagementException e) {
        log.error("Failed to enroll device with device name :" + name, e);
        return false;
    }
}
Also used : DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException) Device(org.wso2.carbon.device.mgt.common.Device) EnrolmentInfo(org.wso2.carbon.device.mgt.common.EnrolmentInfo) Date(java.util.Date) DeviceIdentifier(org.wso2.carbon.device.mgt.common.DeviceIdentifier) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 2 with DeviceManagementException

use of org.wso2.carbon.device.mgt.common.DeviceManagementException in project product-iots by wso2.

the class ConnectedCupManager method modifyEnrollment.

@Override
public boolean modifyEnrollment(Device device) throws DeviceManagementException {
    boolean status;
    try {
        if (log.isDebugEnabled()) {
            log.debug("Modifying the Connected Cup device enrollment data");
        }
        ConnectedCupDAOUtil.beginTransaction();
        status = CONNECTED_CUP_DAO_UTIL.getConnectedCupDeviceDAO().updateDevice(device);
        ConnectedCupDAOUtil.commitTransaction();
    } catch (ConnectedCupDeviceMgtPluginException e) {
        try {
            ConnectedCupDAOUtil.rollbackTransaction();
        } catch (ConnectedCupDeviceMgtPluginException iotDAOEx) {
            String msg = "Error occurred while roll back the update device transaction :" + device.toString();
            log.warn(msg, iotDAOEx);
        }
        String msg = "Error while updating the enrollment of the Connected Cup device : " + device.getDeviceIdentifier();
        log.error(msg, e);
        throw new DeviceManagementException(msg, e);
    }
    return status;
}
Also used : ConnectedCupDeviceMgtPluginException(org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException) DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException)

Example 3 with DeviceManagementException

use of org.wso2.carbon.device.mgt.common.DeviceManagementException in project product-iots by wso2.

the class ConnectedCupManager method disenrollDevice.

@Override
public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
    boolean status;
    try {
        if (log.isDebugEnabled()) {
            log.debug("Dis-enrolling Connected Cup device : " + deviceId);
        }
        ConnectedCupDAOUtil.beginTransaction();
        status = CONNECTED_CUP_DAO_UTIL.getConnectedCupDeviceDAO().deleteDevice(deviceId.getId());
        ConnectedCupDAOUtil.commitTransaction();
    } catch (ConnectedCupDeviceMgtPluginException e) {
        try {
            ConnectedCupDAOUtil.rollbackTransaction();
        } catch (ConnectedCupDeviceMgtPluginException iotDAOEx) {
            String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString();
            log.warn(msg, iotDAOEx);
        }
        String msg = "Error while removing the Connected Cup device : " + deviceId.getId();
        log.error(msg, e);
        throw new DeviceManagementException(msg, e);
    }
    return status;
}
Also used : ConnectedCupDeviceMgtPluginException(org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException) DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException)

Example 4 with DeviceManagementException

use of org.wso2.carbon.device.mgt.common.DeviceManagementException in project product-iots by wso2.

the class DeviceTypeServiceImpl method downloadSketch.

/**
 * To download device type agent source code as zip file.
 *
 * @param deviceName name for the device type instance
 * @param sketchType folder name where device type agent was installed into server
 * @return Agent source code as zip file
 */
@Path("/device/download")
@GET
@Produces("application/zip")
public Response downloadSketch(@QueryParam("deviceName") String deviceName, @QueryParam("sketchType") String sketchType) {
    try {
        ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName, sketchType);
        Response.ResponseBuilder response = Response.ok(FileUtils.readFileToByteArray(zipFile.getZipFile()));
        response.status(Response.Status.OK);
        response.type("application/zip");
        response.header("Content-Disposition", "attachment; filename=\"" + zipFile.getFileName() + "\"");
        Response resp = response.build();
        zipFile.getZipFile().delete();
        return resp;
    } catch (IllegalArgumentException ex) {
        // bad request
        return Response.status(400).entity(ex.getMessage()).build();
    } catch (DeviceManagementException ex) {
        log.error(ex.getMessage(), ex);
        return Response.status(500).entity(ex.getMessage()).build();
    } catch (JWTClientException ex) {
        log.error(ex.getMessage(), ex);
        return Response.status(500).entity(ex.getMessage()).build();
    } catch (APIManagerException ex) {
        log.error(ex.getMessage(), ex);
        return Response.status(500).entity(ex.getMessage()).build();
    } catch (IOException ex) {
        log.error(ex.getMessage(), ex);
        return Response.status(500).entity(ex.getMessage()).build();
    } catch (UserStoreException ex) {
        log.error(ex.getMessage(), ex);
        return Response.status(500).entity(ex.getMessage()).build();
    }
}
Also used : Response(javax.ws.rs.core.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException) APIManagerException(org.wso2.carbon.apimgt.application.extension.exception.APIManagerException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) ZipArchive(org.wso2.iot.sampledevice.api.util.ZipArchive) IOException(java.io.IOException) JWTClientException(org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 5 with DeviceManagementException

use of org.wso2.carbon.device.mgt.common.DeviceManagementException in project product-iots by wso2.

the class DeviceTypeManagerService method init.

@Override
public void init() throws DeviceManagementException {
    this.deviceManager = new DeviceTypeManager();
    this.operationMonitoringTaskConfig = new OperationMonitoringTaskConfig();
}
Also used : OperationMonitoringTaskConfig(org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig)

Aggregations

DeviceManagementException (org.wso2.carbon.device.mgt.common.DeviceManagementException)14 ConnectedCupDeviceMgtPluginException (org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException)4 DeviceMgtPluginException (org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException)4 IOException (java.io.IOException)3 Date (java.util.Date)2 Path (javax.ws.rs.Path)2 Device (org.wso2.carbon.device.mgt.common.Device)2 DeviceIdentifier (org.wso2.carbon.device.mgt.common.DeviceIdentifier)2 EnrolmentInfo (org.wso2.carbon.device.mgt.common.EnrolmentInfo)2 ZipArchive (org.wso2.iot.sampledevice.api.util.ZipArchive)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1 Response (javax.ws.rs.core.Response)1 APIManagementProviderService (org.wso2.carbon.apimgt.application.extension.APIManagementProviderService)1