diff --git a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java index 7a51ac6dd7..2a6b8b9e84 100644 --- a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java +++ b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.controller.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java @@ -46,16 +46,7 @@ import org.wso2.carbon.device.mgt.iot.util.ZipUtil; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.HeaderParam; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; +import javax.ws.rs.*; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -139,307 +130,6 @@ public class ArduinoService { return internalControlsQueue; } - /* --------------------------------------------------------------------------------------- - Device management specific APIs - --------------------------------------------------------------------------------------- */ - - /** - * @param deviceId - * @param name - * @param owner - * @return - */ - @Path("manager/device/register") - @PUT - public boolean register(@QueryParam("deviceId") String deviceId, - @QueryParam("name") String name, @QueryParam("owner") String owner) { - - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(deviceId); - deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE); - try { - if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) { - response.setStatus(Response.Status.CONFLICT.getStatusCode()); - 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(ArduinoConstants.DEVICE_TYPE); - enrolmentInfo.setOwner(owner); - device.setEnrolmentInfo(enrolmentInfo); - - boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device); - if (added) { - response.setStatus(Response.Status.OK.getStatusCode()); - } else { - response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); - } - - return added; - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return false; - } finally { - deviceManagement.endTenantFlow(); - } - } - - /** - * @param deviceId - * @param response - */ - @Path("manager/device/remove/{device_id}") - @DELETE - public void removeDevice(@PathParam("device_id") String deviceId, @Context HttpServletResponse response) { - - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(deviceId); - deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE); - try { - boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice( - deviceIdentifier); - if (removed) { - response.setStatus(Response.Status.OK.getStatusCode()); - - } else { - response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); - - } - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - } finally { - deviceManagement.endTenantFlow(); - } - - } - - /** - * @param deviceId - * @param name - * @param response - * @return - */ - @Path("manager/device/update/{device_id}") - @POST - public boolean updateDevice(@PathParam("device_id") String deviceId, - @QueryParam("name") String name, - @Context HttpServletResponse response) { - - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(deviceId); - deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE); - try { - Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); - device.setDeviceIdentifier(deviceId); - - // device.setDeviceTypeId(deviceTypeId); - device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); - - device.setName(name); - device.setType(ArduinoConstants.DEVICE_TYPE); - - boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment(device); - - if (updated) { - response.setStatus(Response.Status.OK.getStatusCode()); - } else { - response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); - - } - return updated; - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return false; - } finally { - deviceManagement.endTenantFlow(); - } - - } - - /** - * @param deviceId - * @return - */ - @Path("manager/device/{device_id}") - @GET - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public Device getDevice(@PathParam("device_id") String deviceId) { - - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(deviceId); - deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE); - - try { - return deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); - - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return null; - } finally { - deviceManagement.endTenantFlow(); - } - - } - - /** - * @param username - * @return - */ - @Path("manager/devices/{username}") - @GET - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public Device[] getArduinoDevices(@PathParam("username") String username) { - - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - - try { - List userDevices = - deviceManagement.getDeviceManagementService().getDevicesOfUser(username); - ArrayList userDevicesforArduino = new ArrayList<>(); - for (Device device : userDevices) { - if (device.getType().equals(ArduinoConstants.DEVICE_TYPE) && - device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.ACTIVE)) { - userDevicesforArduino.add(device); - - } - } - - return userDevicesforArduino.toArray(new Device[]{}); - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return null; - } finally { - deviceManagement.endTenantFlow(); - } - - } - - /** - * @param owner - * @param customDeviceName - * @param sketchType - * @return - */ - @Path("manager/device/{sketch_type}/download") - @GET - @Produces("application/octet-stream") - public Response downloadSketch(@QueryParam("owner") String owner, - @QueryParam("deviceName") String customDeviceName, - @PathParam("sketch_type") String sketchType) { - //TODO:: null check customDeviceName at UI level - try { - ZipArchive zipFile = createDownloadFile(owner, customDeviceName, sketchType); - Response.ResponseBuilder rb = Response.ok(zipFile.getZipFile()); - rb.header("Content-Disposition", - "attachment; filename=\"" + zipFile.getFileName() + "\""); - return rb.build(); - } catch (IllegalArgumentException ex) { - return Response.status(400).entity(ex.getMessage()).build();//bad request - } catch (DeviceManagementException ex) { - return Response.status(500).entity(ex.getMessage()).build(); - } catch (AccessTokenException ex) { - return Response.status(500).entity(ex.getMessage()).build(); - } catch (DeviceControllerException ex) { - return Response.status(500).entity(ex.getMessage()).build(); - } - - } - - /** - * @param owner - * @param customDeviceName - * @param sketchType - * @return - */ - @Path("manager/device/{sketch_type}/generate_link") - @GET - public Response generateSketchLink(@QueryParam("owner") String owner, - @QueryParam("deviceName") String customDeviceName, - @PathParam("sketch_type") String sketchType) { - - try { - ZipArchive zipFile = createDownloadFile(owner, customDeviceName, sketchType); - Response.ResponseBuilder rb = Response.ok(zipFile.getDeviceId()); - return rb.build(); - } catch (IllegalArgumentException ex) { - return Response.status(400).entity(ex.getMessage()).build();//bad request - } catch (DeviceManagementException ex) { - return Response.status(500).entity(ex.getMessage()).build(); - } catch (AccessTokenException ex) { - return Response.status(500).entity(ex.getMessage()).build(); - } catch (DeviceControllerException ex) { - return Response.status(500).entity(ex.getMessage()).build(); - } - - } - - /** - * @param owner - * @param customDeviceName - * @param sketchType - * @return - * @throws DeviceManagementException - * @throws AccessTokenException - * @throws DeviceControllerException - */ - private ZipArchive createDownloadFile(String owner, String customDeviceName, String sketchType) - throws DeviceManagementException, AccessTokenException, DeviceControllerException { - if (owner == null) { - throw new IllegalArgumentException("Error on createDownloadFile() Owner is null!"); - } - - //create new device id - String deviceId = shortUUID(); - - KeyGenerationUtil.createApplicationKeys("arduino"); - - TokenClient accessTokenClient = new TokenClient(ArduinoConstants.DEVICE_TYPE); - AccessTokenInfo accessTokenInfo = accessTokenClient.getAccessToken(owner, deviceId); - - //create token - String accessToken = accessTokenInfo.getAccess_token(); - String refreshToken = accessTokenInfo.getRefresh_token(); - - //Register the device with CDMF - String deviceName = customDeviceName + "_" + deviceId; - boolean status = register(deviceId, deviceName, owner); - - if (!status) { - String msg = "Error occurred while registering the device with " + "id: " + deviceId + " owner:" + owner; - throw new DeviceManagementException(msg); - } - - - ZipUtil ziputil = new ZipUtil(); - ZipArchive zipFile = ziputil.createZipFile(owner, SUPER_TENANT, sketchType, deviceId, deviceName, accessToken, - refreshToken); - zipFile.setDeviceId(deviceId); - return zipFile; - } - - /** - * @return - */ - private static String shortUUID() { - UUID uuid = UUID.randomUUID(); - long l = ByteBuffer.wrap(uuid.toString().getBytes(StandardCharsets.UTF_8)).getLong(); - return Long.toString(l, Character.MAX_RADIX); - } - /* --------------------------------------------------------------------------------------- Device specific APIs - Control APIs + Data-Publishing APIs --------------------------------------------------------------------------------------- */ @@ -496,7 +186,7 @@ public class ArduinoService { public void switchBulb(@HeaderParam("owner") String owner, @HeaderParam("deviceId") String deviceId, @HeaderParam("protocol") String protocol, - @PathParam("state") String state, + @FormParam("state") String state, @Context HttpServletResponse response) { try { diff --git a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java index 7a51ac6dd7..46c8a90a57 100644 --- a/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java +++ b/components/device-mgt-iot-arduino/org.wso2.carbon.device.mgt.iot.arduino.mgt.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/ArduinoService.java @@ -440,269 +440,4 @@ public class ArduinoService { return Long.toString(l, Character.MAX_RADIX); } - /* --------------------------------------------------------------------------------------- - Device specific APIs - Control APIs + Data-Publishing APIs - --------------------------------------------------------------------------------------- */ - - /** - * @param owner - * @param deviceId - * @param deviceIP - * @param devicePort - * @param response - * @param request - * @return - */ - @Path("controller/register/{owner}/{deviceId}/{ip}/{port}") - @POST - public String registerDeviceIP(@PathParam("owner") String owner, - @PathParam("deviceId") String deviceId, - @PathParam("ip") String deviceIP, - @PathParam("port") String devicePort, - @Context HttpServletResponse response, - @Context HttpServletRequest request) { - - //TODO:: Need to get IP from the request itself - String result; - - if (log.isDebugEnabled()) { - log.debug("Got register call from IP: " + deviceIP + " for Device ID: " + deviceId + " of owner: " + owner); - } - - String deviceHttpEndpoint = deviceIP + ":" + devicePort; - deviceToIpMap.put(deviceId, deviceHttpEndpoint); - - result = "Device-IP Registered"; - response.setStatus(Response.Status.OK.getStatusCode()); - - if (log.isDebugEnabled()) { - log.debug(result); - } - - return result; - } - - /** - * @param owner - * @param deviceId - * @param protocol - * @param state - * @param response - */ - @Path("controller/bulb") - @POST - @Feature( code="bulb", name="Control Bulb", type="operation", - description="Control Bulb on Arduino Uno") - public void switchBulb(@HeaderParam("owner") String owner, - @HeaderParam("deviceId") String deviceId, - @HeaderParam("protocol") String protocol, - @PathParam("state") String state, - @Context HttpServletResponse response) { - - try { - DeviceValidator deviceValidator = new DeviceValidator(); - if (!deviceValidator.isExist(owner, SUPER_TENANT, new DeviceIdentifier(deviceId, - ArduinoConstants.DEVICE_TYPE))) { - response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode()); - return; - } - } catch (DeviceManagementException e) { - log.error("DeviceValidation Failed for deviceId: " + deviceId + " of user: " + owner); - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return; - } - - String switchToState = state.toUpperCase(); - - if (!switchToState.equals(ArduinoConstants.STATE_ON) && !switchToState.equals(ArduinoConstants.STATE_OFF)) { - log.error("The requested state change shoud be either - 'ON' or 'OFF'"); - response.setStatus(Response.Status.BAD_REQUEST.getStatusCode()); - return; - } - - String protocolString = protocol.toUpperCase(); - String callUrlPattern = ArduinoConstants.BULB_CONTEXT + switchToState; - - if (log.isDebugEnabled()) { - log.debug("Sending request to switch-bulb of device [" + deviceId + "] via " + - protocolString); - } - - try { - switch (protocolString) { - case HTTP_PROTOCOL: - String deviceHTTPEndpoint = deviceToIpMap.get(deviceId); - if (deviceHTTPEndpoint == null) { - response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode()); - return; - } - ArduinoServiceUtils.sendCommandViaHTTP(deviceHTTPEndpoint, callUrlPattern, true); - break; - case MQTT_PROTOCOL: - String mqttMessage = ArduinoConstants.BULB_CONTEXT.replace("/", ""); - ArduinoServiceUtils.sendCommandViaMQTT(owner, deviceId, mqttMessage, switchToState); - break; - default: - response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); - return; - } - } catch (DeviceManagementException e) { - log.error("Failed to send switch-bulb request to device [" + deviceId + "] via " + protocolString); - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return; - } - response.setStatus(Response.Status.OK.getStatusCode()); - } - - - /** - * @param dataMsg - * @param response - */ - @Path("controller/pushdata") - @POST - @Consumes(MediaType.APPLICATION_JSON) - public void pushData(final DeviceJSON dataMsg, @Context HttpServletResponse response) { - - String owner = dataMsg.owner; - String deviceId = dataMsg.deviceId; - String deviceIp = dataMsg.reply; //TODO:: Get IP from request - float pinData = dataMsg.value; - - try { - DeviceValidator deviceValidator = new DeviceValidator(); - if (!deviceValidator.isExist(owner, SUPER_TENANT, new DeviceIdentifier(deviceId, - ArduinoConstants.DEVICE_TYPE))) { - response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode()); - log.warn("Data Received from unregistered Arduino device [" + deviceId + "] for owner [" + owner + "]"); - return; - } - - String registeredIp = deviceToIpMap.get(deviceId); - - if (registeredIp == null) { - log.warn("Unregistered IP: Arduino Pin Data Received from an un-registered IP " + deviceIp + - " for device ID - " + deviceId); - response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode()); - return; - } else if (!registeredIp.equals(deviceIp)) { - log.warn("Conflicting IP: Received IP is " + deviceIp + ". Device with ID " + deviceId + - " is already registered under some other IP. Re-registration required"); - response.setStatus(Response.Status.CONFLICT.getStatusCode()); - return; - } - - if (log.isDebugEnabled()) { - log.debug("Received Pin Data Value: " + pinData + " degrees C"); - } - SensorDataManager.getInstance().setSensorRecord(deviceId, ArduinoConstants.SENSOR_TEMPERATURE, - String.valueOf(pinData), - Calendar.getInstance().getTimeInMillis()); - - if (!ArduinoServiceUtils.publishToDAS(dataMsg.owner, dataMsg.deviceId, dataMsg.value)) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - log.warn("An error occured whilst trying to publish pin data of Arduino with ID [" + deviceId + - "] of owner [" + owner + "]"); - } - - } catch (DeviceManagementException e) { - String errorMsg = "Validation attempt for deviceId [" + deviceId + "] of owner [" + owner + "] failed.\n"; - log.error(errorMsg + Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase() + "\n" + e.getErrorMessage()); - } - } - - /** - * @param owner - * @param deviceId - * @param response - * @return - */ - @Path("controller/readcontrols") - @GET - public String readControls(@HeaderParam("owner") String owner, - @HeaderParam("deviceId") String deviceId, - @HeaderParam("protocol") String protocol, - @Context HttpServletResponse response) { - String result; - LinkedList deviceControlList = internalControlsQueue.get(deviceId); - - if (deviceControlList == null) { - result = "No controls have been set for device " + deviceId + " of owner " + owner; - response.setStatus(HttpStatus.SC_NO_CONTENT); - } else { - try { - result = deviceControlList.remove(); //returns the head value - response.setStatus(HttpStatus.SC_ACCEPTED); - - } catch (NoSuchElementException ex) { - result = "There are no more controls for device " + deviceId + " of owner " + - owner; - response.setStatus(HttpStatus.SC_NO_CONTENT); - } - } - if (log.isDebugEnabled()) { - log.debug(result); - } - - return result; - } - - - /** - * @param dataMsg - * @param response - */ - @Path("controller/push_temperature") - @POST - @Consumes(MediaType.APPLICATION_JSON) - public void pushTemperatureData(final DeviceJSON dataMsg, - @Context HttpServletResponse response, - @Context HttpServletRequest request) { - String owner = dataMsg.owner; - String deviceId = dataMsg.deviceId; - String deviceIp = dataMsg.reply; //TODO:: Get IP from request - float temperature = dataMsg.value; - - try { - DeviceValidator deviceValidator = new DeviceValidator(); - if (!deviceValidator.isExist(owner, SUPER_TENANT, new DeviceIdentifier(deviceId, - ArduinoConstants.DEVICE_TYPE))) { - response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode()); - log.warn("Temperature data Received from unregistered Arduino device [" + deviceId + "] for owner [" + - owner + "]"); - return; - } - - String registeredIp = deviceToIpMap.get(deviceId); - - if (registeredIp == null) { - log.warn("Unregistered IP: Temperature Data Received from an un-registered IP " + deviceIp + - " for device ID - " + deviceId); - response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode()); - return; - } else if (!registeredIp.equals(deviceIp)) { - log.warn("Conflicting IP: Received IP is " + deviceIp + ". Device with ID " + deviceId + - " is already registered under some other IP. Re-registration required"); - response.setStatus(Response.Status.CONFLICT.getStatusCode()); - return; - } - - if (log.isDebugEnabled()) { - log.debug("Received Pin Data Value: " + temperature + " degrees C"); - } - SensorDataManager.getInstance().setSensorRecord(deviceId, ArduinoConstants.SENSOR_TEMPERATURE, - String.valueOf(temperature), - Calendar.getInstance().getTimeInMillis()); - - if (!ArduinoServiceUtils.publishToDAS(dataMsg.owner, dataMsg.deviceId, dataMsg.value)) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - log.warn("An error occured whilst trying to publish temperature data of Arduino with ID [" + deviceId + - "] of owner [" + owner + "]"); - } - - } catch (DeviceManagementException e) { - String errorMsg = "Validation attempt for deviceId [" + deviceId + "] of owner [" + owner + "] failed.\n"; - log.error(errorMsg + Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase() + "\n" + e.getErrorMessage()); - } - } }