Search in sources :

Example 1 with CommandOperation

use of org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation 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)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 DeviceIdentifier (org.wso2.carbon.device.mgt.common.DeviceIdentifier)1 InvalidDeviceException (org.wso2.carbon.device.mgt.common.InvalidDeviceException)1 DeviceAccessAuthorizationException (org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException)1 Operation (org.wso2.carbon.device.mgt.common.operation.mgt.Operation)1 OperationManagementException (org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException)1 CommandOperation (org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation)1