Search in sources :

Example 6 with DeviceIdentifier

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

the class VirtualFireAlarmTestCase method testEnrollment.

@Test(description = "This test case tests the virtual fire alarm enrollment")
public void testEnrollment() throws Exception {
    // Time for deploying the carbon apps
    Thread.sleep(30000);
    RestClient client = new RestClient(backendHTTPSURL, Constants.APPLICATION_ZIP, accessTokenString);
    // Enroll an advanced agent and check whether that enrollment succeeds without issues.
    HttpResponse response = client.get(Constants.VirtualFireAlarmConstants.ENROLLMENT_ENDPOINT + "?deviceName=advanced&sketchType=virtual_firealarm_advanced");
    Assert.assertEquals("Advance fire alarm advance agent enrollment failed", HttpStatus.SC_OK, response.getResponseCode());
    // Enroll an simple agent and check whether that enrollment succeeds without issues.
    response = client.get(Constants.VirtualFireAlarmConstants.ENROLLMENT_ENDPOINT + "?deviceName=simple&sketchType=virtual_firealarm");
    Assert.assertEquals("Advance fire alarm advance agent enrollment failed", HttpStatus.SC_OK, response.getResponseCode());
    response = client.get(Constants.MobileDeviceManagement.GET_DEVICE_COUNT_ENDPOINT + "?type=virtual_firealarm");
    JsonArray jsonArray = new JsonParser().parse(response.getData()).getAsJsonObject().getAsJsonArray("devices");
    Assert.assertEquals("Virtual fire alarm enrollment failed ", 2, jsonArray.size());
    if (userMode != TestUserMode.TENANT_ADMIN) {
        deviceId1 = jsonArray.get(0).getAsJsonObject().getAsJsonPrimitive("deviceIdentifier").getAsString();
        deviceId2 = jsonArray.get(1).getAsJsonObject().getAsJsonPrimitive("deviceIdentifier").getAsString();
    } else {
        tenantDeviceId1 = jsonArray.get(0).getAsJsonObject().getAsJsonPrimitive("deviceIdentifier").getAsString();
        tenantDeviceId2 = jsonArray.get(1).getAsJsonObject().getAsJsonPrimitive("deviceIdentifier").getAsString();
    }
}
Also used : JsonArray(com.google.gson.JsonArray) RestClient(org.wso2.iot.integration.common.RestClient) HttpResponse(org.wso2.carbon.automation.test.utils.http.client.HttpResponse) JsonParser(com.google.gson.JsonParser) Test(org.testng.annotations.Test)

Example 7 with DeviceIdentifier

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

the class DeviceTypeServiceImpl method register.

/**
 * Register device into device management service.
 *
 * @param deviceId unique identifier for given device type instance
 * @param name     name for the device type instance
 * @return check whether device is installed into cdmf
 */
private boolean register(String deviceId, String name) {
    try {
        DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
        deviceIdentifier.setId(deviceId);
        deviceIdentifier.setType(DeviceTypeConstants.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(DeviceTypeConstants.DEVICE_TYPE);
        enrolmentInfo.setOwner(APIUtil.getAuthenticatedUser());
        device.setEnrolmentInfo(enrolmentInfo);
        boolean added = APIUtil.getDeviceManagementService().enrollDevice(device);
        return added;
    } catch (DeviceManagementException e) {
        log.error(e.getMessage(), 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)

Example 8 with DeviceIdentifier

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

the class DeviceTypeServiceImpl method changeStatus.

/**
 * @param deviceId unique identifier for given device type instance
 * @param state    change status of sensor: on/off
 */
@Path("device/{deviceId}/change-status")
@POST
public Response changeStatus(@PathParam("deviceId") String deviceId, @QueryParam("state") String state, @Context HttpServletResponse response) {
    try {
        if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId, DeviceTypeConstants.DEVICE_TYPE))) {
            return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
        }
        String sensorState = state.toUpperCase();
        if (!sensorState.equals(DeviceTypeConstants.STATE_ON) && !sensorState.equals(DeviceTypeConstants.STATE_OFF)) {
            log.error("The requested state change should be either - 'ON' or 'OFF'");
            return Response.status(Response.Status.BAD_REQUEST.getStatusCode()).build();
        }
        Map<String, String> dynamicProperties = new HashMap<>();
        String publishTopic = APIUtil.getAuthenticatedUserTenantDomain() + "/" + DeviceTypeConstants.DEVICE_TYPE + "/" + deviceId + "/command";
        String actualMessage = DeviceTypeConstants.BULB_CONTEXT + ':' + sensorState;
        dynamicProperties.put(DeviceTypeConstants.ADAPTER_TOPIC_PROPERTY, publishTopic);
        Operation commandOp = new CommandOperation();
        commandOp.setCode("change-status");
        commandOp.setType(Operation.Type.COMMAND);
        commandOp.setEnabled(true);
        commandOp.setPayLoad(actualMessage);
        Properties props = new Properties();
        props.setProperty("mqtt.adapter.topic", publishTopic);
        commandOp.setProperties(props);
        List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
        deviceIdentifiers.add(new DeviceIdentifier(deviceId, "sampledevice"));
        APIUtil.getDeviceManagementService().addOperation("sampledevice", commandOp, deviceIdentifiers);
        return Response.ok().build();
    } catch (DeviceAccessAuthorizationException e) {
        log.error(e.getErrorMessage(), e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    } catch (OperationManagementException e) {
        String msg = "Error occurred while executing command operation upon ringing the buzzer";
        log.error(msg, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    } catch (InvalidDeviceException e) {
        String msg = "Error occurred while executing command operation to send keywords";
        log.error(msg, e);
        return Response.status(Response.Status.BAD_REQUEST).build();
    }
}
Also used : DeviceAccessAuthorizationException(org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException) HashMap(java.util.HashMap) CommandOperation(org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation) ArrayList(java.util.ArrayList) Operation(org.wso2.carbon.device.mgt.common.operation.mgt.Operation) CommandOperation(org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation) OperationManagementException(org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException) Properties(java.util.Properties) InvalidDeviceException(org.wso2.carbon.device.mgt.common.InvalidDeviceException) DeviceIdentifier(org.wso2.carbon.device.mgt.common.DeviceIdentifier) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 9 with DeviceIdentifier

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

the class DeviceTypeManager method disenrollDevice.

@Override
public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
    boolean status;
    try {
        if (log.isDebugEnabled()) {
            log.debug("Dis-enrolling sampledevice device : " + deviceId);
        }
        DeviceTypeDAO.beginTransaction();
        status = deviceTypeDAO.getDeviceTypeDAO().deleteDevice(deviceId.getId());
        DeviceTypeDAO.commitTransaction();
    } catch (DeviceMgtPluginException e) {
        try {
            DeviceTypeDAO.rollbackTransaction();
        } catch (DeviceMgtPluginException 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 sampledevice device : " + deviceId.getId();
        log.error(msg, e);
        throw new DeviceManagementException(msg, e);
    }
    return status;
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException)

Example 10 with DeviceIdentifier

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

the class ConnectedCupManager method updateDeviceInfo.

@Override
public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException {
    boolean status;
    try {
        if (log.isDebugEnabled()) {
            log.debug("updating the details of Connected Cup device : " + deviceIdentifier);
        }
        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 info transaction :" + device.toString();
            log.warn(msg, iotDAOEx);
        }
        String msg = "Error while updating the Connected Cup device : " + deviceIdentifier;
        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

DeviceManagementException (org.wso2.carbon.device.mgt.common.DeviceManagementException)6 DeviceIdentifier (org.wso2.carbon.device.mgt.common.DeviceIdentifier)5 Path (javax.ws.rs.Path)4 ArrayList (java.util.ArrayList)3 DeviceAccessAuthorizationException (org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException)3 Date (java.util.Date)2 Consumes (javax.ws.rs.Consumes)2 GET (javax.ws.rs.GET)2 POST (javax.ws.rs.POST)2 Produces (javax.ws.rs.Produces)2 ConnectedCupDeviceMgtPluginException (org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException)2 SortByField (org.wso2.carbon.analytics.dataservice.commons.SortByField)2 AnalyticsException (org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException)2 Device (org.wso2.carbon.device.mgt.common.Device)2 EnrolmentInfo (org.wso2.carbon.device.mgt.common.EnrolmentInfo)2 DeviceMgtPluginException (org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException)2 JsonArray (com.google.gson.JsonArray)1 JsonParser (com.google.gson.JsonParser)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1