diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/dto/AndroidSenseManagerService.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/dto/AndroidSenseManagerService.java
index 86f58a142c..6bdd63c135 100755
--- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/dto/AndroidSenseManagerService.java
+++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.agent/app/src/main/java/org/wso2/carbon/iot/android/sense/util/dto/AndroidSenseManagerService.java
@@ -24,7 +24,7 @@ import javax.ws.rs.QueryParam;
*/
public interface AndroidSenseManagerService {
- @Path("/enrollment/devices/{device_id}")
+ @Path("/device/{device_id}/register")
@POST
AndroidConfiguration register(@PathParam("device_id") String deviceId, @QueryParam("deviceName") String deviceName);
}
diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java
deleted file mode 100644
index 68e48074ac..0000000000
--- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerService.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
- *
- * WSO2 Inc. licenses this file to you under the Apache License,
- * Version 2.0 (the "License"); you may not use this file except
- * in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.wso2.carbon.device.mgt.iot.androidsense.service.impl;
-
-import org.wso2.carbon.apimgt.annotations.api.API;
-import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType;
-import javax.ws.rs.Consumes;
-import javax.ws.rs.DELETE;
-import javax.ws.rs.GET;
-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.core.Response;
-@Path("enrollment")
-@API(name = "android_sense_mgt", version = "1.0.0", context = "/android_sense_mgt", tags = {"android_sense"})
-public interface AndroidSenseManagerService {
-
- @Path("/devices/{device_id}")
- @POST
- Response register(@PathParam("device_id") String deviceId, @QueryParam("deviceName") String deviceName);
-
- @Path("/devices/{device_id}")
- @DELETE
- Response removeDevice(@PathParam("device_id") String deviceId);
-
- @Path("/devices/{device_id}")
- @PUT
- Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name);
-
- @Path("/devices/{device_id}")
- @GET
- @Consumes("application/json")
- @Produces("application/json")
- Response getDevice(@PathParam("device_id") String deviceId);
-
-}
-
diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerServiceImpl.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerServiceImpl.java
deleted file mode 100644
index 5e109aaf7d..0000000000
--- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseManagerServiceImpl.java
+++ /dev/null
@@ -1,181 +0,0 @@
-/*
- * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
- *
- * WSO2 Inc. licenses this file to you under the Apache License,
- * Version 2.0 (the "License"); you may not use this file except
- * in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.wso2.carbon.device.mgt.iot.androidsense.service.impl;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.wso2.carbon.device.mgt.common.Device;
-import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
-import org.wso2.carbon.device.mgt.common.DeviceManagementException;
-import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
-import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
-import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
-import org.wso2.carbon.device.mgt.iot.androidsense.plugin.mqtt.MqttConfig;
-import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util.APIUtil;
-import org.wso2.carbon.device.mgt.iot.androidsense.plugin.constants.AndroidSenseConstants;
-import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util.AndroidConfiguration;
-import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util.Constants;
-import org.wso2.carbon.device.mgt.iot.util.Utils;
-
-import javax.ws.rs.Consumes;
-import javax.ws.rs.DELETE;
-import javax.ws.rs.GET;
-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.core.Response;
-import java.util.Date;
-
-@Path("enrollment")
-public class AndroidSenseManagerServiceImpl implements AndroidSenseManagerService {
-
- private static Log log = LogFactory.getLog(AndroidSenseManagerServiceImpl.class);
-
- @Path("/devices/{device_id}")
- @POST
- public Response register(@PathParam("device_id") String deviceId, @QueryParam("deviceName") String deviceName) {
- DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
- deviceIdentifier.setId(deviceId);
- deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE);
- try {
- if (APIUtil.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
- AndroidConfiguration androidConfiguration = new AndroidConfiguration();
- androidConfiguration.setTenantDomain(APIUtil.getAuthenticatedUserTenantDomain());
- String mqttEndpoint = MqttConfig.getInstance().getBrokerEndpoint();
- if (mqttEndpoint.contains(Constants.LOCALHOST)) {
- mqttEndpoint = mqttEndpoint.replace(Constants.LOCALHOST, Utils.getServerUrl());
- }
- androidConfiguration.setMqttEndpoint(mqttEndpoint);
- return Response.status(Response.Status.ACCEPTED.getStatusCode()).entity(androidConfiguration.toString())
- .build();
- }
- 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);
- device.setName(deviceName);
- device.setType(AndroidSenseConstants.DEVICE_TYPE);
- enrolmentInfo.setOwner(APIUtil.getAuthenticatedUser());
- enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD);
- device.setEnrolmentInfo(enrolmentInfo);
- boolean added = APIUtil.getDeviceManagementService().enrollDevice(device);
- if (added) {
- AndroidConfiguration androidConfiguration = new AndroidConfiguration();
- androidConfiguration.setTenantDomain(APIUtil.getAuthenticatedUserTenantDomain());
- String mqttEndpoint = MqttConfig.getInstance().getBrokerEndpoint();
- if (mqttEndpoint.contains(Constants.LOCALHOST)) {
- mqttEndpoint = mqttEndpoint.replace(Constants.LOCALHOST, Utils.getServerUrl());
- }
- androidConfiguration.setMqttEndpoint(mqttEndpoint);
- return Response.ok(androidConfiguration.toString()).build();
- } else {
- return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).entity(false).build();
- }
- } catch (DeviceManagementException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(false).build();
- }
- }
-
- @Path("/devices/{device_id}")
- @DELETE
- public Response removeDevice(@PathParam("device_id") String deviceId) {
- DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
- deviceIdentifier.setId(deviceId);
- deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE);
- try {
- if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(deviceIdentifier, DeviceGroupConstants.
- Permissions.DEFAULT_ADMIN_PERMISSIONS)) {
- return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
- }
- boolean removed = APIUtil.getDeviceManagementService().disenrollDevice(deviceIdentifier);
- if (removed) {
- return Response.ok().build();
- } else {
- return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).build();
- }
- } catch (DeviceManagementException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
- } catch (DeviceAccessAuthorizationException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
- }
- }
-
- @Path("/devices/{device_id}")
- @PUT
- public Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name) {
- DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
- deviceIdentifier.setId(deviceId);
- deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE);
- try {
- if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(deviceIdentifier, DeviceGroupConstants.
- Permissions.DEFAULT_ADMIN_PERMISSIONS)) {
- return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
- }
- Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
- device.setDeviceIdentifier(deviceId);
- device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
- device.setName(name);
- device.setType(AndroidSenseConstants.DEVICE_TYPE);
- boolean updated = APIUtil.getDeviceManagementService().modifyEnrollment(device);
- if (updated) {
- return Response.ok().build();
- } else {
- return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).build();
- }
- } catch (DeviceManagementException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
- } catch (DeviceAccessAuthorizationException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
- }
- }
-
- @Path("/devices/{device_id}")
- @GET
- @Consumes("application/json")
- @Produces("application/json")
- public Response getDevice(@PathParam("device_id") String deviceId) {
- DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
- deviceIdentifier.setId(deviceId);
- deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE);
- try {
- if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(deviceIdentifier)) {
- return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
- }
- Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
- return Response.ok().entity(device).build();
- } catch (DeviceManagementException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
- } catch (DeviceAccessAuthorizationException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
- }
- }
-
-}
diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseControllerService.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseService.java
similarity index 77%
rename from components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseControllerService.java
rename to components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseService.java
index 2b11cc8631..8ba820d180 100644
--- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseControllerService.java
+++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseService.java
@@ -19,6 +19,7 @@
package org.wso2.carbon.device.mgt.iot.androidsense.service.impl;
import org.wso2.carbon.apimgt.annotations.api.API;
+import org.wso2.carbon.apimgt.annotations.api.Permission;
import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType;
import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.Feature;
import javax.ws.rs.Consumes;
@@ -33,7 +34,7 @@ import javax.ws.rs.core.Response;
@DeviceType(value = "android_sense")
@API(name = "android_sense", version = "1.0.0", context = "/android_sense", tags = {"android_sense"})
-public interface AndroidSenseControllerService {
+public interface AndroidSenseService {
/**
* End point to send the key words to the device
@@ -44,6 +45,7 @@ public interface AndroidSenseControllerService {
@Path("device/{deviceId}/words")
@POST
@Feature(code = "keywords", name = "Add Keywords", description = "Send keywords to the device")
+ @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/operations"})
Response sendKeyWords(@PathParam("deviceId") String deviceId, @QueryParam("keywords") String keywords);
/**
@@ -55,11 +57,13 @@ public interface AndroidSenseControllerService {
@Path("device/{deviceId}/words/threshold")
@POST
@Feature(code = "threshold", name = "Add a Threshold", description = "Set a threshold for word in the device")
+ @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/operations"})
Response sendThreshold(@PathParam("deviceId") String deviceId, @QueryParam("threshold") String threshold);
@Path("device/{deviceId}/words")
@DELETE
@Feature(code = "remove", name = "Remove Keywords", description = "Remove the keywords")
+ @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/operations"})
Response removeKeyWords(@PathParam("deviceId") String deviceId, @QueryParam("words") String words);
/**
@@ -68,9 +72,18 @@ public interface AndroidSenseControllerService {
@Path("stats/{deviceId}/sensors/{sensorName}")
@GET
@Consumes("application/json")
+ @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/stats"})
@Produces("application/json")
Response getAndroidSenseDeviceStats(@PathParam("deviceId") String deviceId, @PathParam("sensorName") String sensor,
@QueryParam("from") long from, @QueryParam("to") long to);
+ /**
+ * Enroll devices.
+ */
+ @Path("device/{device_id}/register")
+ @POST
+ @Permission(scope = "android_sense_user", permissions = {"/permission/admin/device-mgt/user/devices"})
+ Response register(@PathParam("device_id") String deviceId, @QueryParam("deviceName") String deviceName);
+
}
diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseControllerServiceImpl.java b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseServiceImpl.java
similarity index 73%
rename from components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseControllerServiceImpl.java
rename to components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseServiceImpl.java
index b3e4488353..15ec090c19 100644
--- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseControllerServiceImpl.java
+++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/AndroidSenseServiceImpl.java
@@ -23,12 +23,19 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.analytics.dataservice.commons.SORT;
import org.wso2.carbon.analytics.dataservice.commons.SortByField;
import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException;
+import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
+import org.wso2.carbon.device.mgt.common.DeviceManagementException;
+import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
+import org.wso2.carbon.device.mgt.iot.androidsense.plugin.mqtt.MqttConfig;
import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util.APIUtil;
+import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util.AndroidConfiguration;
+import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util.Constants;
import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util.SensorRecord;
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.constants.AndroidSenseConstants;
+import org.wso2.carbon.device.mgt.iot.util.Utils;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
@@ -40,6 +47,7 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
+import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -47,9 +55,9 @@ import java.util.Map;
/**
* The api for
*/
-public class AndroidSenseControllerServiceImpl implements AndroidSenseControllerService {
+public class AndroidSenseServiceImpl implements AndroidSenseService {
- private static Log log = LogFactory.getLog(AndroidSenseControllerServiceImpl.class);
+ private static Log log = LogFactory.getLog(AndroidSenseServiceImpl.class);
@Path("device/{deviceId}/words")
@POST
@@ -198,4 +206,52 @@ public class AndroidSenseControllerServiceImpl implements AndroidSenseController
return sensorEventTableName;
}
+ @Path("device/{device_id}/register")
+ @POST
+ public Response register(@PathParam("device_id") String deviceId, @QueryParam("deviceName") String deviceName) {
+ DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
+ deviceIdentifier.setId(deviceId);
+ deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE);
+ try {
+ if (APIUtil.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
+ AndroidConfiguration androidConfiguration = new AndroidConfiguration();
+ androidConfiguration.setTenantDomain(APIUtil.getAuthenticatedUserTenantDomain());
+ String mqttEndpoint = MqttConfig.getInstance().getBrokerEndpoint();
+ if (mqttEndpoint.contains(Constants.LOCALHOST)) {
+ mqttEndpoint = mqttEndpoint.replace(Constants.LOCALHOST, Utils.getServerUrl());
+ }
+ androidConfiguration.setMqttEndpoint(mqttEndpoint);
+ return Response.status(Response.Status.ACCEPTED.getStatusCode()).entity(androidConfiguration.toString())
+ .build();
+ }
+ 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);
+ device.setName(deviceName);
+ device.setType(AndroidSenseConstants.DEVICE_TYPE);
+ enrolmentInfo.setOwner(APIUtil.getAuthenticatedUser());
+ enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD);
+ device.setEnrolmentInfo(enrolmentInfo);
+ boolean added = APIUtil.getDeviceManagementService().enrollDevice(device);
+ if (added) {
+ AndroidConfiguration androidConfiguration = new AndroidConfiguration();
+ androidConfiguration.setTenantDomain(APIUtil.getAuthenticatedUserTenantDomain());
+ String mqttEndpoint = MqttConfig.getInstance().getBrokerEndpoint();
+ if (mqttEndpoint.contains(Constants.LOCALHOST)) {
+ mqttEndpoint = mqttEndpoint.replace(Constants.LOCALHOST, Utils.getServerUrl());
+ }
+ androidConfiguration.setMqttEndpoint(mqttEndpoint);
+ return Response.ok(androidConfiguration.toString()).build();
+ } else {
+ return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).entity(false).build();
+ }
+ } catch (DeviceManagementException e) {
+ log.error(e.getErrorMessage(), e);
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(false).build();
+ }
+ }
+
}
diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/META-INF/permissions.xml b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/META-INF/permissions.xml
index 26e63ecaf5..d4d5b3b7e6 100644
--- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/META-INF/permissions.xml
+++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/META-INF/permissions.xml
@@ -56,33 +56,12 @@
GET
android_sense_device
-
- Get device
- /device-mgt/user/devices/list
- /enrollment/devices/*
- GET
- android_sense_user
-
Add device
/device-mgt/user/devices
- /enrollment/devices/*
+ /device/*/register
POST
android_sense_user
-
- Remove device
- /device-mgt/user/devices/remove
- /enrollment/devices/*
- DELETE
- android_sense_user
-
-
- Update device
- /device-mgt/user/devices/update
- /enrollment/devices/*
- PUT
- android_sense_user
-
diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/WEB-INF/cxf-servlet.xml
index 2eee6325ff..c82a0fc58c 100644
--- a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/WEB-INF/cxf-servlet.xml
+++ b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/webapp/WEB-INF/cxf-servlet.xml
@@ -24,11 +24,9 @@
-
+
-
diff --git a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.type-view/public/asset/androidsense.apk b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.type-view/public/asset/androidsense.apk
index fb49b9ceeb..37a03c22bb 100644
Binary files a/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.type-view/public/asset/androidsense.apk and b/components/iot-plugins/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.android_sense.type-view/public/asset/androidsense.apk differ
diff --git a/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoControllerServiceImpl.java b/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoControllerServiceImpl.java
deleted file mode 100644
index eecf8aca60..0000000000
--- a/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoControllerServiceImpl.java
+++ /dev/null
@@ -1,150 +0,0 @@
-/*
- * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
- *
- * WSO2 Inc. licenses this file to you under the Apache License,
- * Version 2.0 (the "License"); you may not use this file except
- * in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.wso2.carbon.device.mgt.iot.arduino.service.impl;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.wso2.carbon.analytics.dataservice.commons.SORT;
-import org.wso2.carbon.analytics.dataservice.commons.SortByField;
-import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException;
-import org.wso2.carbon.context.PrivilegedCarbonContext;
-import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
-import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
-import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
-import org.wso2.carbon.device.mgt.iot.arduino.service.impl.dto.SensorRecord;
-import org.wso2.carbon.device.mgt.iot.arduino.service.impl.util.APIUtil;
-import org.wso2.carbon.device.mgt.iot.arduino.plugin.constants.ArduinoConstants;
-import javax.ws.rs.Consumes;
-import javax.ws.rs.GET;
-import javax.ws.rs.POST;
-import javax.ws.rs.Path;
-import javax.ws.rs.PathParam;
-import javax.ws.rs.Produces;
-import javax.ws.rs.QueryParam;
-import javax.ws.rs.core.Response;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.NoSuchElementException;
-public class ArduinoControllerServiceImpl implements ArduinoControllerService {
-
- private static Log log = LogFactory.getLog(ArduinoControllerServiceImpl.class);
- private static Map> internalControlsQueue = new HashMap<>();
-
- @Override
- @Path("device/{deviceId}/bulb")
- @POST
- public Response switchBulb(@PathParam("deviceId") String deviceId, @QueryParam("state") String state) {
- try {
- if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId,
- ArduinoConstants.DEVICE_TYPE), DeviceGroupConstants.Permissions.DEFAULT_OPERATOR_PERMISSIONS)) {
- return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
- }
- LinkedList deviceControlList = internalControlsQueue.get(deviceId);
- String operation = "BULB:" + state.toUpperCase();
- if (deviceControlList == null) {
- deviceControlList = new LinkedList<>();
- deviceControlList.add(operation);
- internalControlsQueue.put(deviceId, deviceControlList);
- } else {
- deviceControlList.add(operation);
- }
- return Response.status(Response.Status.OK.getStatusCode()).build();
- } catch (DeviceAccessAuthorizationException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
- }
- }
-
- @Override
- @Path("device/{deviceId}/controls")
- @GET
- public Response readControls(@PathParam("deviceId") String deviceId) {
- try {
- if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId,
- ArduinoConstants.DEVICE_TYPE), DeviceGroupConstants.Permissions.DEFAULT_OPERATOR_PERMISSIONS)) {
- return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
- }
- String result;
- LinkedList deviceControlList = internalControlsQueue.get(deviceId);
- String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
- if (deviceControlList == null) {
- result = "No controls have been set for device " + deviceId + " of owner " + owner;
- if (log.isDebugEnabled()) {
- log.debug(result);
- }
- return Response.status(Response.Status.CONFLICT.getStatusCode()).entity(result).build();
- } else {
- try {
- result = deviceControlList.remove();
- if (log.isDebugEnabled()) {
- log.debug(result);
- }
- return Response.status(Response.Status.ACCEPTED.getStatusCode()).entity(result).build();
- } catch (NoSuchElementException ex) {
- result = "There are no more controls for device " + deviceId + " of owner " + owner;
- if (log.isDebugEnabled()) {
- log.debug(result);
- }
- return Response.status(Response.Status.NO_CONTENT.getStatusCode()).entity(result).build();
- }
- }
- } catch (DeviceAccessAuthorizationException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
- }
- }
-
- @Override
- @Path("device/stats/{deviceId}")
- @GET
- @Consumes("application/json")
- @Produces("application/json")
- public Response getArduinoTemperatureStats(@PathParam("deviceId") String deviceId, @QueryParam("from") long from,
- @QueryParam("to") long to) {
- try {
- if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId,
- ArduinoConstants.DEVICE_TYPE), DeviceGroupConstants.Permissions.DEFAULT_STATS_MONITOR_PERMISSIONS)) {
- return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
- }
- String fromDate = String.valueOf(from);
- String toDate = String.valueOf(to);
- String query = "deviceId:" + deviceId + " AND deviceType:" +
- ArduinoConstants.DEVICE_TYPE + " AND time : [" + fromDate + " TO " + toDate + "]";
- String sensorTableName = ArduinoConstants.TEMPERATURE_EVENT_TABLE;
- try {
- List sortByFields = new ArrayList<>();
- SortByField sortByField = new SortByField("time", SORT.ASC, false);
- sortByFields.add(sortByField);
- List sensorRecords = APIUtil.getAllEventsForDevice(sensorTableName, query, sortByFields);
- return Response.status(Response.Status.OK.getStatusCode()).entity(sensorRecords).build();
- } catch (AnalyticsException e) {
- String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query;
- log.error(errorMsg);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(errorMsg).build();
- }
- } catch (DeviceAccessAuthorizationException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
- }
- }
-
-}
diff --git a/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoManagerService.java b/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoManagerService.java
deleted file mode 100644
index d161127ac7..0000000000
--- a/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoManagerService.java
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
- *
- * WSO2 Inc. licenses this file to you under the Apache License,
- * Version 2.0 (the "License"); you may not use this file except
- * in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.wso2.carbon.device.mgt.iot.arduino.service.impl;
-
-import org.wso2.carbon.apimgt.annotations.api.API;
-import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType;
-
-import javax.ws.rs.Consumes;
-import javax.ws.rs.DELETE;
-import javax.ws.rs.GET;
-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.core.MediaType;
-import javax.ws.rs.core.Response;
-
-@Path("enrollment")
-@API(name = "arduino_mgt", version = "1.0.0", context = "/arduino_mgt", tags = {"arduino"})
-public interface ArduinoManagerService {
-
- @Path("devices/{device_id}")
- @PUT
- Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name);
-
- @Path("devices/{device_id}")
- @GET
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- Response getDevice(@PathParam("device_id") String deviceId);
-
- @Path("devices/{device_id}")
- @DELETE
- Response removeDevice(@PathParam("device_id") String deviceId);
-
- @Path("devices")
- @GET
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- Response getArduinoDevices();
-
- @Path("devices/download")
- @GET
- @Produces("application/octet-stream")
- Response downloadSketch(@QueryParam("deviceName") String customDeviceName);
-}
diff --git a/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoControllerService.java b/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoService.java
similarity index 73%
rename from components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoControllerService.java
rename to components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoService.java
index d0986ee866..afe0c89b1a 100644
--- a/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoControllerService.java
+++ b/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoService.java
@@ -19,6 +19,7 @@
package org.wso2.carbon.device.mgt.iot.arduino.service.impl;
import org.wso2.carbon.apimgt.annotations.api.API;
+import org.wso2.carbon.apimgt.annotations.api.Permission;
import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType;
import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.Feature;
import javax.ws.rs.Consumes;
@@ -32,15 +33,17 @@ import javax.ws.rs.core.Response;
@API(name = "arduino", version = "1.0.0", context = "/arduino", tags = {"arduino"})
@DeviceType(value = "arduino")
-public interface ArduinoControllerService {
+public interface ArduinoService {
@Path("device/{deviceId}/bulb")
@POST
@Feature(code = "bulb", name = "Control Bulb", description = "Control Bulb on Arduino Uno")
+ @Permission(scope = "arduino_user", permissions = {"/permission/admin/device-mgt/user/operations"})
Response switchBulb(@PathParam("deviceId") String deviceId, @QueryParam("state") String state);
@Path("device/{deviceId}/controls")
@GET
+ @Permission(scope = "arduino_device", permissions = {"/permission/admin/device-mgt/user/operations"})
Response readControls(@PathParam("deviceId") String deviceId);
/**
@@ -50,7 +53,17 @@ public interface ArduinoControllerService {
@GET
@Consumes("application/json")
@Produces("application/json")
+ @Permission(scope = "arduino_user", permissions = {"/permission/admin/device-mgt/user/stats"})
Response getArduinoTemperatureStats(@PathParam("deviceId") String deviceId, @QueryParam("from") long from,
@QueryParam("to") long to);
+ /**
+ * download device agent
+ */
+ @Path("device/download")
+ @GET
+ @Produces("application/octet-stream")
+ @Permission(scope = "arduino_user", permissions = {"/permission/admin/device-mgt/user/devices"})
+ Response downloadSketch(@QueryParam("deviceName") String customDeviceName);
+
}
diff --git a/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoManagerServiceImpl.java b/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoServiceImpl.java
similarity index 57%
rename from components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoManagerServiceImpl.java
rename to components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoServiceImpl.java
index 2b1268e9ea..8bd141809c 100644
--- a/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoManagerServiceImpl.java
+++ b/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/ArduinoServiceImpl.java
@@ -21,6 +21,9 @@ package org.wso2.carbon.device.mgt.iot.arduino.service.impl;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.wso2.carbon.analytics.dataservice.commons.SORT;
+import org.wso2.carbon.analytics.dataservice.commons.SortByField;
+import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException;
import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService;
import org.wso2.carbon.apimgt.application.extension.dto.ApiApplicationKey;
import org.wso2.carbon.apimgt.application.extension.exception.APIManagerException;
@@ -29,6 +32,9 @@ import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
+import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
+import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
+import org.wso2.carbon.device.mgt.iot.arduino.service.impl.dto.SensorRecord;
import org.wso2.carbon.device.mgt.iot.arduino.service.impl.util.APIUtil;
import org.wso2.carbon.device.mgt.iot.arduino.plugin.constants.ArduinoConstants;
import org.wso2.carbon.device.mgt.iot.arduino.service.impl.util.ZipUtil;
@@ -39,115 +45,132 @@ import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientExceptio
import org.wso2.carbon.user.api.UserStoreException;
import javax.ws.rs.Consumes;
-import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
-import javax.ws.rs.PUT;
+import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
-import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
+import java.util.HashMap;
+import java.util.LinkedList;
import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
import java.util.UUID;
-@Path("enrollment")
-public class ArduinoManagerServiceImpl implements ArduinoManagerService {
+public class ArduinoServiceImpl implements ArduinoService {
+ private static Log log = LogFactory.getLog(ArduinoServiceImpl.class);
+ private static Map> internalControlsQueue = new HashMap<>();
private static final String KEY_TYPE = "PRODUCTION";
private static ApiApplicationKey apiApplicationKey;
- private static Log log = LogFactory.getLog(ArduinoManagerServiceImpl.class);
@Override
- @Path("devices/{device_id}")
- @DELETE
- public Response removeDevice(@PathParam("device_id") String deviceId) {
- DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
- deviceIdentifier.setId(deviceId);
- deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE);
+ @Path("device/{deviceId}/bulb")
+ @POST
+ public Response switchBulb(@PathParam("deviceId") String deviceId, @QueryParam("state") String state) {
try {
- boolean removed = APIUtil.getDeviceManagementService().disenrollDevice(deviceIdentifier);
- if (removed) {
- return Response.status(Response.Status.OK.getStatusCode()).entity(true).build();
- } else {
- return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).build();
+ if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId,
+ ArduinoConstants.DEVICE_TYPE), DeviceGroupConstants.Permissions.DEFAULT_OPERATOR_PERMISSIONS)) {
+ return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
}
- } catch (DeviceManagementException e) {
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
- }
- }
-
- @Override
- @Path("devices/{device_id}")
- @PUT
- public Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name) {
- DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
- deviceIdentifier.setId(deviceId);
- deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE);
- try {
- Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
- device.setDeviceIdentifier(deviceId);
- device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
- device.setName(name);
- device.setType(ArduinoConstants.DEVICE_TYPE);
- boolean updated = APIUtil.getDeviceManagementService().modifyEnrollment(device);
- if (updated) {
- return Response.status(Response.Status.OK.getStatusCode()).entity(true).build();
+ LinkedList deviceControlList = internalControlsQueue.get(deviceId);
+ String operation = "BULB:" + state.toUpperCase();
+ if (deviceControlList == null) {
+ deviceControlList = new LinkedList<>();
+ deviceControlList.add(operation);
+ internalControlsQueue.put(deviceId, deviceControlList);
} else {
- return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).build();
+ deviceControlList.add(operation);
}
- } catch (DeviceManagementException e) {
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
+ return Response.status(Response.Status.OK.getStatusCode()).build();
+ } catch (DeviceAccessAuthorizationException e) {
+ log.error(e.getErrorMessage(), e);
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
@Override
- @Path("devices/{device_id}")
+ @Path("device/{deviceId}/controls")
@GET
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- public Response getDevice(@PathParam("device_id") String deviceId) {
- DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
- deviceIdentifier.setId(deviceId);
- deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE);
+ public Response readControls(@PathParam("deviceId") String deviceId) {
try {
- Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
- return Response.status(Response.Status.OK.getStatusCode()).entity(device).build();
- } catch (DeviceManagementException e) {
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
+ if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId,
+ ArduinoConstants.DEVICE_TYPE), DeviceGroupConstants.Permissions.DEFAULT_OPERATOR_PERMISSIONS)) {
+ return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
+ }
+ String result;
+ LinkedList deviceControlList = internalControlsQueue.get(deviceId);
+ String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
+ if (deviceControlList == null) {
+ result = "No controls have been set for device " + deviceId + " of owner " + owner;
+ if (log.isDebugEnabled()) {
+ log.debug(result);
+ }
+ return Response.status(Response.Status.CONFLICT.getStatusCode()).entity(result).build();
+ } else {
+ try {
+ result = deviceControlList.remove();
+ if (log.isDebugEnabled()) {
+ log.debug(result);
+ }
+ return Response.status(Response.Status.ACCEPTED.getStatusCode()).entity(result).build();
+ } catch (NoSuchElementException ex) {
+ result = "There are no more controls for device " + deviceId + " of owner " + owner;
+ if (log.isDebugEnabled()) {
+ log.debug(result);
+ }
+ return Response.status(Response.Status.NO_CONTENT.getStatusCode()).entity(result).build();
+ }
+ }
+ } catch (DeviceAccessAuthorizationException e) {
+ log.error(e.getErrorMessage(), e);
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
@Override
- @Path("devices")
+ @Path("device/stats/{deviceId}")
@GET
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- public Response getArduinoDevices() {
+ @Consumes("application/json")
+ @Produces("application/json")
+ public Response getArduinoTemperatureStats(@PathParam("deviceId") String deviceId, @QueryParam("from") long from,
+ @QueryParam("to") long to) {
try {
- List userDevices = APIUtil.getDeviceManagementService().getDevicesOfUser(
- APIUtil.getAuthenticatedUser());
- 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);
- }
+ if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId,
+ ArduinoConstants.DEVICE_TYPE), DeviceGroupConstants.Permissions.DEFAULT_STATS_MONITOR_PERMISSIONS)) {
+ return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
}
- Device[] devices = userDevicesforArduino.toArray(new Device[]{});
- return Response.status(Response.Status.OK.getStatusCode()).entity(devices).build();
- } catch (DeviceManagementException e) {
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(true).build();
+ String fromDate = String.valueOf(from);
+ String toDate = String.valueOf(to);
+ String query = "deviceId:" + deviceId + " AND deviceType:" +
+ ArduinoConstants.DEVICE_TYPE + " AND time : [" + fromDate + " TO " + toDate + "]";
+ String sensorTableName = ArduinoConstants.TEMPERATURE_EVENT_TABLE;
+ try {
+ List sortByFields = new ArrayList<>();
+ SortByField sortByField = new SortByField("time", SORT.ASC, false);
+ sortByFields.add(sortByField);
+ List sensorRecords = APIUtil.getAllEventsForDevice(sensorTableName, query, sortByFields);
+ return Response.status(Response.Status.OK.getStatusCode()).entity(sensorRecords).build();
+ } catch (AnalyticsException e) {
+ String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query;
+ log.error(errorMsg);
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(errorMsg).build();
+ }
+ } catch (DeviceAccessAuthorizationException e) {
+ log.error(e.getErrorMessage(), e);
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
@Override
- @Path("/devices/download")
+ @Path("/device/download")
@GET
@Produces("application/zip")
public Response downloadSketch(@QueryParam("deviceName") String deviceName) {
@@ -197,7 +220,7 @@ public class ArduinoManagerServiceImpl implements ArduinoManagerService {
ArduinoConstants.DEVICE_TYPE, tags, KEY_TYPE, applicationUsername, true);
}
JWTClient jwtClient = APIUtil.getJWTClientManagerService().getJWTClient();
- String scopes = "device_type_" + ArduinoConstants.DEVICE_TYPE + " device_" + deviceId;
+ String scopes = "arduino_device device_type_" + ArduinoConstants.DEVICE_TYPE + " device_" + deviceId;
AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(apiApplicationKey.getConsumerKey(),
apiApplicationKey.getConsumerSecret(), owner, scopes);
//create token
@@ -210,9 +233,8 @@ public class ArduinoManagerServiceImpl implements ArduinoManagerService {
throw new DeviceManagementException(msg);
}
ZipUtil ziputil = new ZipUtil();
- ZipArchive zipFile = ziputil.createZipFile(owner, APIUtil.getTenantDomainOftheUser(),
- ArduinoConstants.DEVICE_TYPE, deviceId, deviceName, accessToken, refreshToken);
- return zipFile;
+ return ziputil.createZipFile(owner, APIUtil.getTenantDomainOftheUser(),
+ ArduinoConstants.DEVICE_TYPE, deviceId, deviceName, accessToken, refreshToken);
}
private static String shortUUID() {
@@ -245,5 +267,4 @@ public class ArduinoManagerServiceImpl implements ArduinoManagerService {
return false;
}
}
-
}
diff --git a/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/webapp/META-INF/permissions.xml b/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/webapp/META-INF/permissions.xml
index 4966eba2e2..eb8e9f67fd 100644
--- a/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/webapp/META-INF/permissions.xml
+++ b/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/webapp/META-INF/permissions.xml
@@ -50,33 +50,12 @@
GET
arduino_device
-
- Get device
- /device-mgt/user/devices/list
- /enrollment/devices/*
- GET
- arduino_user
-
-
- Remove device
- /device-mgt/user/devices/remove
- /enrollment/devices/*
- DELETE
- arduino_user
-
Download device
/device-mgt/user/devices
- /enrollment/devices/download
+ /device/download
GET
arduino_user
-
- Update device
- /device-mgt/user/devices/update
- /enrollment/devices/*
- PUT
- arduino_user
-
diff --git a/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/webapp/WEB-INF/cxf-servlet.xml
index c902d3024a..a5b895b00b 100644
--- a/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/webapp/WEB-INF/cxf-servlet.xml
+++ b/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/webapp/WEB-INF/cxf-servlet.xml
@@ -25,11 +25,8 @@
-
-
-
+
diff --git a/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.arduino.type-view/private/config.json b/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.arduino.type-view/private/config.json
index 73294d4ecc..e85ea2bc62 100644
--- a/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.arduino.type-view/private/config.json
+++ b/components/iot-plugins/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.arduino.type-view/private/config.json
@@ -2,6 +2,6 @@
"deviceType": {
"label": "Arduino",
"category": "iot",
- "downloadAgentUri": "arduino/enrollment/devices/download"
+ "downloadAgentUri": "arduino/device/download"
}
}
\ No newline at end of file
diff --git a/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiControllerServiceImpl.java b/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiControllerServiceImpl.java
deleted file mode 100644
index d4a8f747cc..0000000000
--- a/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiControllerServiceImpl.java
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
- *
- * WSO2 Inc. licenses this file to you under the Apache License,
- * Version 2.0 (the "License"); you may not use this file except
- * in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.wso2.carbon.analytics.dataservice.commons.SORT;
-import org.wso2.carbon.analytics.dataservice.commons.SortByField;
-import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException;
-import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
-import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
-import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
-import org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl.dto.SensorRecord;
-import org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl.util.APIUtil;
-import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.constants.RaspberrypiConstants;
-
-import javax.ws.rs.Consumes;
-import javax.ws.rs.GET;
-import javax.ws.rs.POST;
-import javax.ws.rs.Path;
-import javax.ws.rs.PathParam;
-import javax.ws.rs.Produces;
-import javax.ws.rs.QueryParam;
-import javax.ws.rs.core.Response;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-public class RaspberryPiControllerServiceImpl implements RaspberryPiControllerService {
-
- private static Log log = LogFactory.getLog(RaspberryPiControllerServiceImpl.class);
-
- @Path("device/{deviceId}/bulb")
- @POST
- public Response switchBulb(@PathParam("deviceId") String deviceId, @QueryParam("state") String state) {
- try {
- if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId,
- RaspberrypiConstants.DEVICE_TYPE), DeviceGroupConstants.Permissions.DEFAULT_OPERATOR_PERMISSIONS)) {
- return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
- }
- String switchToState = state.toUpperCase();
- if (!switchToState.equals(RaspberrypiConstants.STATE_ON) && !switchToState.equals(
- RaspberrypiConstants.STATE_OFF)) {
- log.error("The requested state change shoud be either - 'ON' or 'OFF'");
- return Response.status(Response.Status.BAD_REQUEST.getStatusCode()).build();
- }
- String actualMessage = RaspberrypiConstants.BULB_CONTEXT + ":" + state;
- Map dynamicProperties = new HashMap<>();
- String publishTopic = APIUtil.getTenantDomainOftheUser() + "/"
- + RaspberrypiConstants.DEVICE_TYPE + "/" + deviceId;
- dynamicProperties.put(RaspberrypiConstants.ADAPTER_TOPIC_PROPERTY, publishTopic);
- APIUtil.getOutputEventAdapterService().publish(RaspberrypiConstants.MQTT_ADAPTER_NAME,
- dynamicProperties, actualMessage);
- return Response.ok().build();
- } catch (DeviceAccessAuthorizationException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
- }
- }
-
- @Path("device/stats/{deviceId}")
- @GET
- @Consumes("application/json")
- @Produces("application/json")
- public Response getRaspberryPiTemperatureStats(@PathParam("deviceId") String deviceId,
- @QueryParam("from") long from, @QueryParam("to") long to) {
- String fromDate = String.valueOf(from);
- String toDate = String.valueOf(to);
- String query = "deviceId:" + deviceId + " AND deviceType:" +
- RaspberrypiConstants.DEVICE_TYPE + " AND time : [" + fromDate + " TO " + toDate + "]";
- String sensorTableName = RaspberrypiConstants.TEMPERATURE_EVENT_TABLE;
- try {
- if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId,
- RaspberrypiConstants.DEVICE_TYPE), DeviceGroupConstants.Permissions.DEFAULT_STATS_MONITOR_PERMISSIONS)) {
- return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
- }
- List sortByFields = new ArrayList<>();
- SortByField sortByField = new SortByField("time", SORT.ASC, false);
- sortByFields.add(sortByField);
- List sensorRecords = APIUtil.getAllEventsForDevice(sensorTableName, query, sortByFields);
- return Response.status(Response.Status.OK.getStatusCode()).entity(sensorRecords).build();
- } catch (AnalyticsException e) {
- String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query;
- log.error(errorMsg);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(errorMsg).build();
- } catch (DeviceAccessAuthorizationException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
- }
- }
-
-}
diff --git a/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiManagerService.java b/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiManagerService.java
deleted file mode 100644
index 07c4bc2131..0000000000
--- a/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiManagerService.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
- *
- * WSO2 Inc. licenses this file to you under the Apache License,
- * Version 2.0 (the "License"); you may not use this file except
- * in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl;
-
-import org.wso2.carbon.apimgt.annotations.api.API;
-import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType;
-
-import javax.ws.rs.Consumes;
-import javax.ws.rs.DELETE;
-import javax.ws.rs.GET;
-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.core.MediaType;
-import javax.ws.rs.core.Response;
-
-@Path("enrollment")
-@API(name = "raspberrypi_mgt", version = "1.0.0", context = "/raspberrypi_mgt", tags = {"raspberrypi"})
-@DeviceType(value = "raspberrypi")
-public interface RaspberryPiManagerService {
-
- @Path("devices/{device_id}")
- @DELETE
- Response removeDevice(@PathParam("device_id") String deviceId);
-
- @Path("devices/{device_id}")
- @PUT
- Response updateDevice(@PathParam("device_id") String deviceId,
- @QueryParam("name") String name);
-
- @Path("devices/{device_id}")
- @GET
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- Response getDevice(@PathParam("device_id") String deviceId);
-
- @Path("devices")
- @GET
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- Response getRaspberrypiDevices();
-
- @Path("devicesg/download")
- @GET
- @Produces(MediaType.APPLICATION_JSON)
- Response downloadSketch(@QueryParam("deviceName") String deviceName, @QueryParam("sketch_type") String sketchType);
-
-}
diff --git a/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiControllerService.java b/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiService.java
similarity index 73%
rename from components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiControllerService.java
rename to components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiService.java
index c32353a2b1..299f7be26f 100644
--- a/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiControllerService.java
+++ b/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiService.java
@@ -19,25 +19,27 @@
package org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl;
import org.wso2.carbon.apimgt.annotations.api.API;
+import org.wso2.carbon.apimgt.annotations.api.Permission;
import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType;
import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.Feature;
import javax.ws.rs.Consumes;
-import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@API(name = "raspberrypi", version = "1.0.0", context = "/raspberrypi", tags = {"raspberrypi"})
@DeviceType(value = "raspberrypi")
-public interface RaspberryPiControllerService {
+public interface RaspberryPiService {
@Path("device/{deviceId}/bulb")
@POST
@Feature(code = "bulb", name = "Bulb On / Off", description = "Switch on/off Raspberry Pi agent's bulb. (On / Off)")
+ @Permission(scope = "raspberrypi_user", permissions = {"/permission/admin/device-mgt/user/operations"})
Response switchBulb(@PathParam("deviceId") String deviceId, @QueryParam("state") String state);
/**
@@ -47,7 +49,17 @@ public interface RaspberryPiControllerService {
@GET
@Consumes("application/json")
@Produces("application/json")
+ @Permission(scope = "raspberrypi_user", permissions = {"/permission/admin/device-mgt/user/stats"})
Response getRaspberryPiTemperatureStats(@PathParam("deviceId") String deviceId,
@QueryParam("from") long from, @QueryParam("to") long to);
+ /**
+ * download the agent.
+ */
+ @Path("device/download")
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ @Permission(scope = "raspberrypi_user", permissions = {"/permission/admin/device-mgt/user/devices"})
+ Response downloadSketch(@QueryParam("deviceName") String deviceName, @QueryParam("sketch_type") String sketchType);
+
}
diff --git a/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiManagerServiceImpl.java b/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiServiceImpl.java
similarity index 65%
rename from components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiManagerServiceImpl.java
rename to components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiServiceImpl.java
index 35fa4ad196..49073cb022 100644
--- a/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiManagerServiceImpl.java
+++ b/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/RaspberryPiServiceImpl.java
@@ -21,6 +21,9 @@ package org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.wso2.carbon.analytics.dataservice.commons.SORT;
+import org.wso2.carbon.analytics.dataservice.commons.SortByField;
+import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException;
import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService;
import org.wso2.carbon.apimgt.application.extension.dto.ApiApplicationKey;
import org.wso2.carbon.apimgt.application.extension.exception.APIManagerException;
@@ -29,8 +32,11 @@ import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
-import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.constants.RaspberrypiConstants;
+import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
+import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
+import org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl.dto.SensorRecord;
import org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl.util.APIUtil;
+import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.constants.RaspberrypiConstants;
import org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl.util.ZipUtil;
import org.wso2.carbon.device.mgt.iot.util.ZipArchive;
import org.wso2.carbon.identity.jwt.client.extension.JWTClient;
@@ -39,118 +45,90 @@ import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientExceptio
import org.wso2.carbon.user.api.UserStoreException;
import javax.ws.rs.Consumes;
-import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
-import javax.ws.rs.PUT;
+import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
-import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.UUID;
-@Path("enrollment")
-public class RaspberryPiManagerServiceImpl implements RaspberryPiManagerService {
+public class RaspberryPiServiceImpl implements RaspberryPiService {
- private static Log log = LogFactory.getLog(RaspberryPiManagerServiceImpl.class);
+ private static Log log = LogFactory.getLog(RaspberryPiServiceImpl.class);
private static final String KEY_TYPE = "PRODUCTION";
private static ApiApplicationKey apiApplicationKey;
- @Override
- @Path("devices/{device_id}")
- @DELETE
- public Response removeDevice(@PathParam("device_id") String deviceId) {
+ @Path("device/{deviceId}/bulb")
+ @POST
+ public Response switchBulb(@PathParam("deviceId") String deviceId, @QueryParam("state") String state) {
try {
- DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
- deviceIdentifier.setId(deviceId);
- deviceIdentifier.setType(RaspberrypiConstants.DEVICE_TYPE);
- boolean removed = APIUtil.getDeviceManagementService().disenrollDevice(
- deviceIdentifier);
- if (removed) {
- return Response.ok().build();
- } else {
- return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).build();
+ if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId,
+ RaspberrypiConstants.DEVICE_TYPE), DeviceGroupConstants.Permissions.DEFAULT_OPERATOR_PERMISSIONS)) {
+ return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
}
- } catch (DeviceManagementException e) {
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
- }
- }
-
- @Override
- @Path("devices/{device_id}")
- @PUT
- public Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name) {
- DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
- deviceIdentifier.setId(deviceId);
- deviceIdentifier.setType(RaspberrypiConstants.DEVICE_TYPE);
- try {
- Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
- device.setDeviceIdentifier(deviceId);
- device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
- device.setName(name);
- device.setType(RaspberrypiConstants.DEVICE_TYPE);
- boolean updated = APIUtil.getDeviceManagementService().modifyEnrollment(device);
- if (updated) {
- return Response.ok().build();
- } else {
- return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).build();
+ String switchToState = state.toUpperCase();
+ if (!switchToState.equals(RaspberrypiConstants.STATE_ON) && !switchToState.equals(
+ RaspberrypiConstants.STATE_OFF)) {
+ log.error("The requested state change shoud be either - 'ON' or 'OFF'");
+ return Response.status(Response.Status.BAD_REQUEST.getStatusCode()).build();
}
- } catch (DeviceManagementException e) {
- log.error(e.getErrorMessage());
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
+ String actualMessage = RaspberrypiConstants.BULB_CONTEXT + ":" + state;
+ Map dynamicProperties = new HashMap<>();
+ String publishTopic = APIUtil.getTenantDomainOftheUser() + "/"
+ + RaspberrypiConstants.DEVICE_TYPE + "/" + deviceId;
+ dynamicProperties.put(RaspberrypiConstants.ADAPTER_TOPIC_PROPERTY, publishTopic);
+ APIUtil.getOutputEventAdapterService().publish(RaspberrypiConstants.MQTT_ADAPTER_NAME,
+ dynamicProperties, actualMessage);
+ return Response.ok().build();
+ } catch (DeviceAccessAuthorizationException e) {
+ log.error(e.getErrorMessage(), e);
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
- @Override
- @Path("devices/{device_id}")
+ @Path("device/stats/{deviceId}")
@GET
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- public Response getDevice(@PathParam("device_id") String deviceId) {
- DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
- deviceIdentifier.setId(deviceId);
- deviceIdentifier.setType(RaspberrypiConstants.DEVICE_TYPE);
+ @Consumes("application/json")
+ @Produces("application/json")
+ public Response getRaspberryPiTemperatureStats(@PathParam("deviceId") String deviceId,
+ @QueryParam("from") long from, @QueryParam("to") long to) {
+ String fromDate = String.valueOf(from);
+ String toDate = String.valueOf(to);
+ String query = "deviceId:" + deviceId + " AND deviceType:" +
+ RaspberrypiConstants.DEVICE_TYPE + " AND time : [" + fromDate + " TO " + toDate + "]";
+ String sensorTableName = RaspberrypiConstants.TEMPERATURE_EVENT_TABLE;
try {
- Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
- return Response.ok().entity(device).build();
- } catch (DeviceManagementException ex) {
- log.error("Error occurred while retrieving device with Id " + deviceId + "\n" + ex);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
- }
- }
-
- @Override
- @Path("devices")
- @GET
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- public Response getRaspberrypiDevices() {
- try {
- List userDevices = APIUtil.getDeviceManagementService().getDevicesOfUser(
- APIUtil.getAuthenticatedUser());
- ArrayList usersRaspberrypiDevices = new ArrayList<>();
- for (Device device : userDevices) {
- if (device.getType().equals(RaspberrypiConstants.DEVICE_TYPE) &&
- device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.ACTIVE)) {
- usersRaspberrypiDevices.add(device);
- }
+ if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId,
+ RaspberrypiConstants.DEVICE_TYPE), DeviceGroupConstants.Permissions.DEFAULT_STATS_MONITOR_PERMISSIONS)) {
+ return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
}
- Device[] devices = usersRaspberrypiDevices.toArray(new Device[]{});
- return Response.ok().entity(devices).build();
- } catch (DeviceManagementException e) {
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
+ List sortByFields = new ArrayList<>();
+ SortByField sortByField = new SortByField("time", SORT.ASC, false);
+ sortByFields.add(sortByField);
+ List sensorRecords = APIUtil.getAllEventsForDevice(sensorTableName, query, sortByFields);
+ return Response.status(Response.Status.OK.getStatusCode()).entity(sensorRecords).build();
+ } catch (AnalyticsException e) {
+ String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query;
+ log.error(errorMsg);
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(errorMsg).build();
+ } catch (DeviceAccessAuthorizationException e) {
+ log.error(e.getErrorMessage(), e);
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
@Override
- @Path("devices/download")
+ @Path("device/download")
@GET
@Produces("application/zip")
public Response downloadSketch(@QueryParam("deviceName") String deviceName, @QueryParam("sketchType") String sketchType) {
@@ -234,9 +212,8 @@ public class RaspberryPiManagerServiceImpl implements RaspberryPiManagerService
throw new DeviceManagementException(msg);
}
ZipUtil ziputil = new ZipUtil();
- ZipArchive zipFile = ziputil.createZipFile(owner, APIUtil.getTenantDomainOftheUser(), sketchType,
+ return ziputil.createZipFile(owner, APIUtil.getTenantDomainOftheUser(), sketchType,
deviceId, deviceName, accessToken, refreshToken);
- return zipFile;
}
private static String shortUUID() {
@@ -244,5 +221,4 @@ public class RaspberryPiManagerServiceImpl implements RaspberryPiManagerService
long l = ByteBuffer.wrap(uuid.toString().getBytes(StandardCharsets.UTF_8)).getLong();
return Long.toString(l, Character.MAX_RADIX);
}
-
}
diff --git a/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/webapp/META-INF/permissions.xml b/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/webapp/META-INF/permissions.xml
index 6813569dd2..be74ef64dc 100644
--- a/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/webapp/META-INF/permissions.xml
+++ b/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/webapp/META-INF/permissions.xml
@@ -36,34 +36,13 @@
GET
raspberrypi_user
-
- Remove device
- /device-mgt/user/devices/remove
- /enrollment/devices/*
- DELETE
- raspberrypi_user
-
Download device
/device-mgt/user/devices
- /enrollment/devices/download
+ /device/download
GET
raspberrypi_user
-
- Update device
- /device-mgt/user/devices/update
- /enrollment/devices/*
- POST
- raspberrypi_user
-
-
- Get Devices
- /device-mgt/user/devices/list
- /enrollment/devices
- GET
- raspberrypi_user
-
Control Bulb
/device-mgt/user/operations
diff --git a/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/webapp/WEB-INF/cxf-servlet.xml
index 7272253227..abb72753d1 100644
--- a/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/webapp/WEB-INF/cxf-servlet.xml
+++ b/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/webapp/WEB-INF/cxf-servlet.xml
@@ -27,11 +27,8 @@
-
-
-
+
diff --git a/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.raspberrypi.type-view/private/config.json b/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.raspberrypi.type-view/private/config.json
index 99f710bd89..f19ba03414 100644
--- a/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.raspberrypi.type-view/private/config.json
+++ b/components/iot-plugins/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.raspberrypi.type-view/private/config.json
@@ -2,6 +2,6 @@
"deviceType": {
"label": "Raspberry Pi",
"category": "iot",
- "downloadAgentUri": "raspberrypi/enrollment/devices/download"
+ "downloadAgentUri": "raspberrypi/device/download"
}
}
\ No newline at end of file
diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmManagerService.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmManagerService.java
deleted file mode 100644
index 7b5bdaf097..0000000000
--- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmManagerService.java
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
- *
- * WSO2 Inc. licenses this file to you under the Apache License,
- * Version 2.0 (the "License"); you may not use this file except
- * in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl;
-
-import org.wso2.carbon.apimgt.annotations.api.API;
-import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType;
-
-import javax.ws.rs.Consumes;
-import javax.ws.rs.DELETE;
-import javax.ws.rs.GET;
-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.core.MediaType;
-import javax.ws.rs.core.Response;
-
-@Path("enrollment")
-@API(name = "virtual_firealarm_mgt", version = "1.0.0", context = "/virtual_firealarm_mgt", tags = {"virtual_firealarm"})
-public interface VirtualFireAlarmManagerService {
-
- @Path("/devices/{device_id}")
- @DELETE
- Response removeDevice(@PathParam("device_id") String deviceId);
-
-
- @Path("/devices/{device_id}")
- @PUT
- Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name);
-
- @Path("/devices/{device_id}")
- @GET
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- Response getDevice(@PathParam("device_id") String deviceId);
-
- @Path("/devices")
- @GET
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- Response getFirealarmDevices();
-
- @Path("/devices/download")
- @GET
- @Produces("application/zip")
- Response downloadSketch(@QueryParam("deviceName") String deviceName, @QueryParam("sketchType") String sketchType);
-
-}
\ No newline at end of file
diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmManagerServiceImpl.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmManagerServiceImpl.java
deleted file mode 100644
index 17e1a46110..0000000000
--- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmManagerServiceImpl.java
+++ /dev/null
@@ -1,294 +0,0 @@
-/*
- * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
- *
- * WSO2 Inc. licenses this file to you under the Apache License,
- * Version 2.0 (the "License"); you may not use this file except
- * in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl;
-
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService;
-import org.wso2.carbon.apimgt.application.extension.dto.ApiApplicationKey;
-import org.wso2.carbon.apimgt.application.extension.exception.APIManagerException;
-import org.wso2.carbon.context.PrivilegedCarbonContext;
-import org.wso2.carbon.device.mgt.common.Device;
-import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
-import org.wso2.carbon.device.mgt.common.DeviceManagementException;
-import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
-import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
-import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
-import org.wso2.carbon.device.mgt.iot.util.ZipArchive;
-import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.constants.VirtualFireAlarmConstants;
-import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.exception.VirtualFirealarmDeviceMgtPluginException;
-import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.xmpp.XmppAccount;
-import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.xmpp.XmppConfig;
-import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.xmpp.XmppServerClient;
-import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util.APIUtil;
-import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util.ZipUtil;
-import org.wso2.carbon.identity.jwt.client.extension.JWTClient;
-import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo;
-import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException;
-import org.wso2.carbon.user.api.UserStoreException;
-
-import javax.ws.rs.Consumes;
-import javax.ws.rs.DELETE;
-import javax.ws.rs.GET;
-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.core.MediaType;
-import javax.ws.rs.core.Response;
-import java.io.IOException;
-import java.nio.ByteBuffer;
-import java.nio.charset.StandardCharsets;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.UUID;
-
-@Path("enrollment")
-public class VirtualFireAlarmManagerServiceImpl implements VirtualFireAlarmManagerService {
-
- private static final String KEY_TYPE = "PRODUCTION";
- private static ApiApplicationKey apiApplicationKey;
- private static Log log = LogFactory.getLog(VirtualFireAlarmManagerServiceImpl.class);
-
- @Path("/devices/{device_id}")
- @DELETE
- public Response removeDevice(@PathParam("device_id") String deviceId) {
- try {
- DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
- deviceIdentifier.setId(deviceId);
- deviceIdentifier.setType(VirtualFireAlarmConstants.DEVICE_TYPE);
- if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(deviceIdentifier, DeviceGroupConstants.
- Permissions.DEFAULT_ADMIN_PERMISSIONS)) {
- return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
- }
- boolean removed = APIUtil.getDeviceManagementService().disenrollDevice(
- deviceIdentifier);
- if (removed) {
- return Response.ok().build();
- } else {
- return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).build();
- }
- } catch (DeviceManagementException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
- } catch (DeviceAccessAuthorizationException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
- }
- }
-
- @Path("/devices/{device_id}")
- @PUT
- public Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name) {
- try {
- DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
- deviceIdentifier.setId(deviceId);
- deviceIdentifier.setType(VirtualFireAlarmConstants.DEVICE_TYPE);
- if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(deviceIdentifier, DeviceGroupConstants.
- Permissions.DEFAULT_ADMIN_PERMISSIONS)) {
- return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
- }
- Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
- device.setDeviceIdentifier(deviceId);
- device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
- device.setName(name);
- device.setType(VirtualFireAlarmConstants.DEVICE_TYPE);
- boolean updated = APIUtil.getDeviceManagementService().modifyEnrollment(device);
- if (updated) {
- return Response.ok().build();
- } else {
- return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).build();
- }
- } catch (DeviceManagementException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
- } catch (DeviceAccessAuthorizationException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
- }
- }
-
- @Path("/devices/{device_id}")
- @GET
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- public Response getDevice(@PathParam("device_id") String deviceId) {
- try {
- DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
- deviceIdentifier.setId(deviceId);
- deviceIdentifier.setType(VirtualFireAlarmConstants.DEVICE_TYPE);
- if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(deviceIdentifier)) {
- return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
- }
- Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
- return Response.ok().entity(device).build();
- } catch (DeviceManagementException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
- } catch (DeviceAccessAuthorizationException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
- }
- }
-
- @Path("/devices")
- @GET
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- public Response getFirealarmDevices() {
- try {
- List userDevices =
- APIUtil.getDeviceManagementService().getDevicesOfUser(APIUtil.getAuthenticatedUser());
- ArrayList userDevicesforFirealarm = new ArrayList<>();
- for (Device device : userDevices) {
- if (device.getType().equals(VirtualFireAlarmConstants.DEVICE_TYPE) &&
- device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.ACTIVE)) {
- userDevicesforFirealarm.add(device);
- }
- }
- Device[] devices = userDevicesforFirealarm.toArray(new Device[]{});
- return Response.ok().entity(devices).build();
- } catch (DeviceManagementException e) {
- log.error(e.getErrorMessage(), e);
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
- }
- }
-
- @Path("/devices/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) {
- return Response.status(400).entity(ex.getMessage()).build();//bad request
- } 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();
- } catch (VirtualFirealarmDeviceMgtPluginException ex) {
- log.error(ex.getMessage(), ex);
- return Response.status(500).entity(ex.getMessage()).build();
- }
- }
-
- private boolean register(String deviceId, String name) {
- try {
- DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
- deviceIdentifier.setId(deviceId);
- deviceIdentifier.setType(VirtualFireAlarmConstants.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(VirtualFireAlarmConstants.DEVICE_TYPE);
- enrolmentInfo.setOwner(APIUtil.getAuthenticatedUser());
- device.setEnrolmentInfo(enrolmentInfo);
- return APIUtil.getDeviceManagementService().enrollDevice(device);
- } catch (DeviceManagementException e) {
- log.error(e.getMessage(), e);
- return false;
- }
- }
-
- private ZipArchive createDownloadFile(String owner, String deviceName, String sketchType)
- throws DeviceManagementException, APIManagerException, JWTClientException,
- UserStoreException, VirtualFirealarmDeviceMgtPluginException {
- //create new device id
- String deviceId = shortUUID();
- if (apiApplicationKey == null) {
- String applicationUsername =
- PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration()
- .getAdminUserName();
- APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService();
- String[] tags = {VirtualFireAlarmConstants.DEVICE_TYPE};
- apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(
- VirtualFireAlarmConstants.DEVICE_TYPE, tags, KEY_TYPE, applicationUsername, true);
- }
- JWTClient jwtClient = APIUtil.getJWTClientManagerService().getJWTClient();
- String scopes = "device_type_" + VirtualFireAlarmConstants.DEVICE_TYPE + " device_" + deviceId;
- AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(apiApplicationKey.getConsumerKey(),
- apiApplicationKey.getConsumerSecret(), owner,
- scopes);
- String accessToken = accessTokenInfo.getAccessToken();
- String refreshToken = accessTokenInfo.getRefreshToken();
- //adding registering data
- XmppAccount newXmppAccount = new XmppAccount();
- newXmppAccount.setAccountName(deviceId);
- newXmppAccount.setUsername(deviceId);
- newXmppAccount.setPassword(accessToken);
- newXmppAccount.setEmail(deviceId + "@" + APIUtil.getTenantDomainOftheUser());
- XmppServerClient xmppServerClient = new XmppServerClient();
- boolean status;
- if (XmppConfig.getInstance().isEnabled()) {
- status = xmppServerClient.createAccount(newXmppAccount);
- if (!status) {
- String msg = "XMPP Account was not created for device - " + deviceId + " of owner - " + owner +
- ".XMPP might have been disabled in org.wso2.carbon.device.mgt.iot" +
- ".common.config.server.configs";
- throw new DeviceManagementException(msg);
- }
- }
- status = register(deviceId, deviceName);
- 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, APIUtil.getTenantDomainOftheUser(), sketchType, deviceId,
- deviceName, accessToken, refreshToken);
- return zipFile;
- }
-
- 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);
- }
-
-}
diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmControllerService.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmService.java
similarity index 88%
rename from components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmControllerService.java
rename to components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmService.java
index 0417bcbad8..5e235b872f 100644
--- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmControllerService.java
+++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmService.java
@@ -35,7 +35,7 @@ import javax.ws.rs.core.Response;
*/
@API(name = "virtual_firealarm", version = "1.0.0", context = "/virtual_firealarm", tags = {"virtual_firealarm"})
@DeviceType(value = "virtual_firealarm")
-public interface VirtualFireAlarmControllerService {
+public interface VirtualFireAlarmService {
/**
* This is an API called/used from within the Server(Front-End) or by a device Owner. It sends a control command to
@@ -49,7 +49,7 @@ public interface VirtualFireAlarmControllerService {
*/
@POST
@Path("device/{deviceId}/buzz")
- @Permission(scope = "virtual_firealarm_user", permissions = {"device-mgt/virtual_firealarm/user"})
+ @Permission(scope = "virtual_firealarm_user", permissions = {"/permission/admin/device-mgt/user/operations"})
@Feature(code = "buzz", name = "Buzzer On / Off", description = "Switch on/off Virtual Fire Alarm Buzzer. (On / Off)")
Response switchBuzzer(@PathParam("deviceId") String deviceId, @QueryParam("protocol") String protocol,
@FormParam("state") String state);
@@ -60,10 +60,16 @@ public interface VirtualFireAlarmControllerService {
*/
@Path("device/stats/{deviceId}")
@GET
- @Permission(scope = "virtual_firealarm_user", permissions = {"device-mgt/virtual_firealarm/user"})
+ @Permission(scope = "virtual_firealarm_user", permissions = {"/permission/admin/device-mgt/user/stats"})
@Consumes("application/json")
@Produces("application/json")
Response getVirtualFirealarmStats(@PathParam("deviceId") String deviceId, @QueryParam("from") long from,
@QueryParam("to") long to);
+ @Path("device/download")
+ @GET
+ @Produces("application/zip")
+ @Permission(scope = "virtual_firealarm_user", permissions = {"/permission/admin/device-mgt/user/devices"})
+ Response downloadSketch(@QueryParam("deviceName") String deviceName, @QueryParam("sketchType") String sketchType);
+
}
diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmControllerServiceImpl.java b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java
similarity index 58%
rename from components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmControllerServiceImpl.java
rename to components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java
index e4a8aa55f6..0548ff9d6a 100644
--- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmControllerServiceImpl.java
+++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/VirtualFireAlarmServiceImpl.java
@@ -18,21 +18,38 @@
package org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl;
+import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.analytics.dataservice.commons.SORT;
import org.wso2.carbon.analytics.dataservice.commons.SortByField;
import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException;
+import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService;
+import org.wso2.carbon.apimgt.application.extension.dto.ApiApplicationKey;
+import org.wso2.carbon.apimgt.application.extension.exception.APIManagerException;
+import org.wso2.carbon.context.PrivilegedCarbonContext;
+import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
+import org.wso2.carbon.device.mgt.common.DeviceManagementException;
+import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
+import org.wso2.carbon.device.mgt.iot.util.ZipArchive;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.constants.VirtualFireAlarmConstants;
+import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.exception.VirtualFirealarmDeviceMgtPluginException;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.util.VirtualFirealarmSecurityManager;
+import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.xmpp.XmppAccount;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.xmpp.XmppConfig;
+import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.xmpp.XmppServerClient;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.dto.SensorRecord;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.exception.VirtualFireAlarmException;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util.APIUtil;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util.VirtualFireAlarmServiceUtils;
+import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util.ZipUtil;
+import org.wso2.carbon.identity.jwt.client.extension.JWTClient;
+import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo;
+import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException;
+import org.wso2.carbon.user.api.UserStoreException;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
@@ -44,16 +61,23 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.util.ArrayList;
+import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.UUID;
-public class VirtualFireAlarmControllerServiceImpl implements VirtualFireAlarmControllerService {
+public class VirtualFireAlarmServiceImpl implements VirtualFireAlarmService {
private static final String XMPP_PROTOCOL = "XMPP";
- private static Log log = LogFactory.getLog(VirtualFireAlarmControllerServiceImpl.class);
+ private static final String KEY_TYPE = "PRODUCTION";
+ private static ApiApplicationKey apiApplicationKey;
+ private static Log log = LogFactory.getLog(VirtualFireAlarmServiceImpl.class);
@POST
@Path("device/{deviceId}/buzz")
@@ -192,4 +216,120 @@ public class VirtualFireAlarmControllerServiceImpl implements VirtualFireAlarmCo
}
}
+ @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) {
+ return Response.status(400).entity(ex.getMessage()).build();//bad request
+ } 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();
+ } catch (VirtualFirealarmDeviceMgtPluginException ex) {
+ log.error(ex.getMessage(), ex);
+ return Response.status(500).entity(ex.getMessage()).build();
+ }
+ }
+
+ private boolean register(String deviceId, String name) {
+ try {
+ DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
+ deviceIdentifier.setId(deviceId);
+ deviceIdentifier.setType(VirtualFireAlarmConstants.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(VirtualFireAlarmConstants.DEVICE_TYPE);
+ enrolmentInfo.setOwner(APIUtil.getAuthenticatedUser());
+ device.setEnrolmentInfo(enrolmentInfo);
+ return APIUtil.getDeviceManagementService().enrollDevice(device);
+ } catch (DeviceManagementException e) {
+ log.error(e.getMessage(), e);
+ return false;
+ }
+ }
+
+ private ZipArchive createDownloadFile(String owner, String deviceName, String sketchType)
+ throws DeviceManagementException, APIManagerException, JWTClientException,
+ UserStoreException, VirtualFirealarmDeviceMgtPluginException {
+ //create new device id
+ String deviceId = shortUUID();
+ if (apiApplicationKey == null) {
+ String applicationUsername =
+ PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration()
+ .getAdminUserName();
+ APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService();
+ String[] tags = {VirtualFireAlarmConstants.DEVICE_TYPE};
+ apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(
+ VirtualFireAlarmConstants.DEVICE_TYPE, tags, KEY_TYPE, applicationUsername, true);
+ }
+ JWTClient jwtClient = APIUtil.getJWTClientManagerService().getJWTClient();
+ String scopes = "device_type_" + VirtualFireAlarmConstants.DEVICE_TYPE + " device_" + deviceId;
+ AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(apiApplicationKey.getConsumerKey(),
+ apiApplicationKey.getConsumerSecret(), owner,
+ scopes);
+ String accessToken = accessTokenInfo.getAccessToken();
+ String refreshToken = accessTokenInfo.getRefreshToken();
+ //adding registering data
+ XmppAccount newXmppAccount = new XmppAccount();
+ newXmppAccount.setAccountName(deviceId);
+ newXmppAccount.setUsername(deviceId);
+ newXmppAccount.setPassword(accessToken);
+ newXmppAccount.setEmail(deviceId + "@" + APIUtil.getTenantDomainOftheUser());
+ boolean status;
+ if (XmppConfig.getInstance().isEnabled()) {
+ status = XmppServerClient.createAccount(newXmppAccount);
+ if (!status) {
+ String msg = "XMPP Account was not created for device - " + deviceId + " of owner - " + owner +
+ ".XMPP might have been disabled in org.wso2.carbon.device.mgt.iot" +
+ ".common.config.server.configs";
+ throw new DeviceManagementException(msg);
+ }
+ }
+ status = register(deviceId, deviceName);
+ if (!status) {
+ String msg = "Error occurred while registering the device with " + "id: " + deviceId + " owner:" + owner;
+ throw new DeviceManagementException(msg);
+ }
+ ZipUtil ziputil = new ZipUtil();
+ return ziputil.createZipFile(owner, APIUtil.getTenantDomainOftheUser(), sketchType, deviceId,
+ deviceName, accessToken, refreshToken);
+ }
+
+ 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);
+ }
+
}
diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/META-INF/permissions.xml b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/META-INF/permissions.xml
index 8b8957385b..8c18f748ec 100644
--- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/META-INF/permissions.xml
+++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/META-INF/permissions.xml
@@ -28,48 +28,13 @@
-
- Get device
- /device-mgt/user/devices/list
- /enrollment/devices/*
- GET
- virtual_firealarm_user
-
-
- Remove device
- /device-mgt/user/devices/remove
- /enrollment/devices/*
- DELETE
- virtual_firealarm_user
-
Download device
- /device-mgt/virtual_firealarm/user
- /enrollment/devices/download
+ /device-mgt/user/devices
+ /device/download
GET
virtual_firealarm_user
-
- Update device
- /device-mgt/user/devices/update
- /enrollment/devices/*
- POST
- virtual_firealarm_user
-
-
- Get Devices
- /device-mgt/user/devices/list
- /enrollment/devices
- GET
- virtual_firealarm_user
-
-
- Register Device
- /device-mgt/user/operations
- /device/register/*/*/*
- POST
- virtual_firealarm_device
-
Control Buzz
/device-mgt/user/operations
@@ -77,13 +42,6 @@
POST
virtual_firealarm_user
-
- Push Temperature
- /device-mgt/user/stats
- /device/temperature
- POST
- virtual_firealarm_device
-
Get Stats
/device-mgt/user/stats
diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/WEB-INF/cxf-servlet.xml
index dd9427cea0..37a69c2682 100644
--- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/WEB-INF/cxf-servlet.xml
+++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/webapp/WEB-INF/cxf-servlet.xml
@@ -25,11 +25,8 @@
-
-
-
+
diff --git a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.virtual_firealarm.type-view/private/config.json b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.virtual_firealarm.type-view/private/config.json
index b8435cd39b..e149dddb78 100644
--- a/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.virtual_firealarm.type-view/private/config.json
+++ b/components/iot-plugins/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.type.virtual_firealarm.type-view/private/config.json
@@ -2,6 +2,6 @@
"deviceType": {
"label": "Virtual Firealarm",
"category": "virtual",
- "downloadAgentUri": "virtual_firealarm/enrollment/devices/download"
+ "downloadAgentUri": "virtual_firealarm/device/download"
}
}
\ No newline at end of file