Search in sources :

Example 1 with Device

use of org.wso2.carbon.device.mgt.common.Device 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 Device

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

the class APIUtil method getDeviceAccessAuthorizationService.

public static DeviceAccessAuthorizationService getDeviceAccessAuthorizationService() {
    PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    DeviceAccessAuthorizationService deviceAccessAuthorizationService = (DeviceAccessAuthorizationService) ctx.getOSGiService(DeviceAccessAuthorizationService.class, null);
    if (deviceAccessAuthorizationService == null) {
        throw new IllegalStateException("Device Authorization service has not initialized");
    }
    return deviceAccessAuthorizationService;
}
Also used : DeviceAccessAuthorizationService(org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext)

Example 3 with Device

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

the class APIUtil method getDeviceManagementService.

public static DeviceManagementProviderService getDeviceManagementService() {
    PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    DeviceManagementProviderService deviceManagementProviderService = (DeviceManagementProviderService) ctx.getOSGiService(DeviceManagementProviderService.class, null);
    if (deviceManagementProviderService == null) {
        throw new IllegalStateException("Device Management service has not initialized");
    }
    return deviceManagementProviderService;
}
Also used : PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) DeviceManagementProviderService(org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService)

Example 4 with Device

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

the class ConnectedCupDAO method getDevice.

public Device getDevice(String deviceId) throws ConnectedCupDeviceMgtPluginException {
    Connection conn = null;
    PreparedStatement stmt = null;
    Device connectedCupDevice = null;
    ResultSet resultSet = null;
    try {
        conn = ConnectedCupDAOUtil.getConnection();
        String selectDBQuery = "SELECT CONNECTED_CUP_DEVICE_ID, DEVICE_NAME FROM CONNECTED_CUP_DEVICE WHERE CONNECTED_CUP_DEVICE_ID = ?";
        stmt = conn.prepareStatement(selectDBQuery);
        stmt.setString(1, deviceId);
        resultSet = stmt.executeQuery();
        if (resultSet.next()) {
            connectedCupDevice = new Device();
            connectedCupDevice.setName(resultSet.getString(ConnectedCupConstants.DEVICE_PLUGIN_DEVICE_NAME));
            if (log.isDebugEnabled()) {
                log.debug("Connected Cup service " + deviceId + " data has been fetched from" + "Connected Cup database.");
            }
        }
    } catch (SQLException e) {
        String msg = "Error occurred while fetching Connected Cup device : '" + deviceId + "'";
        log.error(msg, e);
        throw new ConnectedCupDeviceMgtPluginException(msg, e);
    } finally {
        ConnectedCupUtils.cleanupResources(stmt, resultSet);
        ConnectedCupDAOUtil.closeConnection();
    }
    return connectedCupDevice;
}
Also used : ConnectedCupDeviceMgtPluginException(org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException) SQLException(java.sql.SQLException) Device(org.wso2.carbon.device.mgt.common.Device) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 5 with Device

use of org.wso2.carbon.device.mgt.common.Device 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)

Aggregations

Test (org.testng.annotations.Test)20 HttpResponse (org.wso2.carbon.automation.test.utils.http.client.HttpResponse)17 DeviceManagementException (org.wso2.carbon.device.mgt.common.DeviceManagementException)13 DeviceMgtPluginException (org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException)11 JsonParser (com.google.gson.JsonParser)10 SQLException (java.sql.SQLException)8 JsonObject (com.google.gson.JsonObject)7 Connection (java.sql.Connection)7 PreparedStatement (java.sql.PreparedStatement)7 HashMap (java.util.HashMap)6 ConnectedCupDeviceMgtPluginException (org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException)6 Device (org.wso2.carbon.device.mgt.common.Device)6 JsonArray (com.google.gson.JsonArray)5 JsonElement (com.google.gson.JsonElement)4 ResultSet (java.sql.ResultSet)4 ArrayList (java.util.ArrayList)4 Path (javax.ws.rs.Path)4 JSONObject (org.json.simple.JSONObject)4 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)4 DeviceIdentifier (org.wso2.carbon.device.mgt.common.DeviceIdentifier)4