mirror of
https://repository.entgra.net/community/product-iots.git
synced 2025-09-16 23:32:19 +00:00
Merge branch 'master' of https://github.com/wso2/product-iots into das-ext
This commit is contained in:
commit
568bb7d09f
@ -771,14 +771,6 @@
|
||||
</outputDirectory>
|
||||
<fileMode>755</fileMode>
|
||||
</file>
|
||||
<file>
|
||||
<source>
|
||||
../p2-profile-gen/target/wso2carbon-core-${carbon.kernel.version}/repository/deployment/server/webapps/mdm-admin.war
|
||||
</source>
|
||||
<outputDirectory>${pom.artifactId}-${pom.version}/repository/deployment/server/webapps
|
||||
</outputDirectory>
|
||||
<destName>devicemgt_admin.war</destName>
|
||||
</file>
|
||||
<!-- Copying synapse.properties file -->
|
||||
<file>
|
||||
<source>src/repository/conf/synapse.properties</source>
|
||||
|
||||
@ -86,11 +86,11 @@ public class Constants {
|
||||
public static final String EMAIL_ERROR = "user.com";
|
||||
public static final String USER_NAME = "user";
|
||||
public static final String SHORT_USER_NAME = "us";
|
||||
public static final String SHORT_USER_NAME_ERROR_MSG = "Username must be between 3 and 30 characters long.";
|
||||
public static final String SHORT_USER_NAME_ERROR_MSG = "Provided username is invalid.";
|
||||
public static final String FIRST_NAME_ERROR_MSG = "Firstname is a required field. It cannot be empty.";
|
||||
public static final String LAST_NAME_ERROR_MSG = "Lastname is a required field. It cannot be empty.";
|
||||
public static final String NO_EMAIL_ERROR_MSG = "Email is a required field. It cannot be empty.";
|
||||
public static final String WRONG_EMAIL_ERROR_MSG = "Provided email is invalid. Please check.";
|
||||
public static final String WRONG_EMAIL_ERROR_MSG = "Provided email is invalid.";
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -28,17 +28,19 @@
|
||||
<classes>
|
||||
<class name="org.wso2.carbon.iot.integration.web.ui.test.login.LoginFormValidationTest"/>
|
||||
<class name="org.wso2.carbon.iot.integration.web.ui.test.login.LoginTest"/>
|
||||
<class name="org.wso2.carbon.iot.integration.web.ui.test.user.RegistrationFormValidationTest"/>
|
||||
<!--<class name="org.wso2.carbon.iot.integration.web.ui.test.user.RegistrationFormValidationTest"/>-->
|
||||
<class name="org.wso2.carbon.iot.integration.web.ui.test.user.NewUserRegistrationTest"/>
|
||||
<class name="org.wso2.carbon.iot.integration.web.ui.test.group.DeviceGroupTest"/>
|
||||
<class name="org.wso2.carbon.iot.integration.web.ui.test.group.DeviceGroupFailTest"/>
|
||||
<class name="org.wso2.carbon.iot.integration.web.ui.test.user.AddUserFormValidationTest"/>
|
||||
<class name="org.wso2.carbon.iot.integration.web.ui.test.user.AdminFunctionsTest"/>
|
||||
<!--<class name="org.wso2.carbon.iot.integration.web.ui.test.user.AdminFunctionsTest"/>-->
|
||||
<!--
|
||||
<class name="org.wso2.carbon.iot.integration.web.ui.test.samples.SampleInstallationTest"/>
|
||||
<class name="org.wso2.carbon.iot.integration.web.ui.test.samples.SampleInstallationVerification"/>
|
||||
<class name="org.wso2.carbon.iot.integration.web.ui.test.samples.SampleEnrollmentTest"/>
|
||||
<class name="org.wso2.carbon.iot.integration.web.ui.test.samples.SampleEnrolmentVerificationTest"/>
|
||||
<class name="org.wso2.carbon.iot.integration.web.ui.test.samples.SampleFunctionalityTest"/>
|
||||
-->
|
||||
</classes>
|
||||
</test>
|
||||
</suite>
|
||||
|
||||
@ -31,27 +31,14 @@ import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
@API( name="connectedcup", version="1.0.0", context="/connectedcup" , tags = {"connectedcup"})
|
||||
@DeviceType( value = "connectedcup")
|
||||
@API(name = "connectedcup", version = "1.0.0", context = "/connectedcup", tags = {"connectedcup"})
|
||||
@DeviceType(value = "connectedcup")
|
||||
public interface ConnectedCupControllerService {
|
||||
|
||||
@Path("device/coffeelevel")
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Feature(code = "coffeelevel", name = "Coffee Level", type = "monitor",
|
||||
description = "Request Coffee Level from Connected cup")
|
||||
Response readCoffeeLevel(@HeaderParam("owner") String owner, @HeaderParam("deviceId") String deviceId);
|
||||
|
||||
@Path("device/temperature")
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Feature(code = "temperature", name = "Temperature", type = "monitor",
|
||||
description = "Request Temperature reading from Connected cup")
|
||||
Response readTemperature(@HeaderParam("owner") String owner, @HeaderParam("deviceId") String deviceId);
|
||||
|
||||
|
||||
@Path("device/ordercoffee")
|
||||
@POST
|
||||
@Feature(code = "ordercoffee", name = "Order Coffee", type = "control",
|
||||
description = "Order coffee cup")
|
||||
Response orderCoffee(@QueryParam("deviceId") String deviceId, @QueryParam("deviceOwner") String deviceOwner);
|
||||
|
||||
}
|
||||
|
||||
@ -24,11 +24,16 @@ import org.coffeeking.connectedcup.plugin.constants.ConnectedCupConstants;
|
||||
import org.coffeeking.api.transport.ConnectedCupMQTTConnector;
|
||||
import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig;
|
||||
import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException;
|
||||
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager;
|
||||
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorRecord;
|
||||
import org.wso2.carbon.device.mgt.iot.service.IoTServerStartupListener;
|
||||
import org.wso2.carbon.device.mgt.iot.transport.TransportHandlerException;
|
||||
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.HeaderParam;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
public class ConnectedCupControllerServiceImpl implements ConnectedCupControllerService {
|
||||
@ -36,6 +41,17 @@ public class ConnectedCupControllerServiceImpl implements ConnectedCupController
|
||||
private static Log log = LogFactory.getLog(ConnectedCupControllerServiceImpl.class);
|
||||
private static ConnectedCupMQTTConnector connectedCupMQTTConnector;
|
||||
|
||||
@Path("device/ordercoffee")
|
||||
@POST
|
||||
public Response orderCoffee(@QueryParam("deviceId") String deviceId, @QueryParam("deviceOwner") String deviceOwner) {
|
||||
log.info("Coffee ordered....!");
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Sending request to read liquid level value of device [" + deviceId + "] via MQTT");
|
||||
}
|
||||
return Response.ok().entity("Coffee ordered.").build();
|
||||
}
|
||||
|
||||
public ConnectedCupMQTTConnector getConnectedCupMQTTConnector() {
|
||||
return ConnectedCupControllerServiceImpl.connectedCupMQTTConnector;
|
||||
}
|
||||
@ -73,46 +89,4 @@ public class ConnectedCupControllerServiceImpl implements ConnectedCupController
|
||||
return false;
|
||||
}
|
||||
|
||||
public Response readCoffeeLevel(String owner, String deviceId) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Sending request to read liquid level value of device [" + deviceId + "] via MQTT");
|
||||
}
|
||||
|
||||
try {
|
||||
String mqttResource = ConnectedCupConstants.LEVEL_CONTEXT.replace("/", "");
|
||||
connectedCupMQTTConnector.publishDeviceData(owner, deviceId, mqttResource, "");
|
||||
|
||||
SensorRecord sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId,
|
||||
ConnectedCupConstants.SENSOR_LEVEL);
|
||||
return Response.ok().entity(sensorRecord).build();
|
||||
} catch (DeviceControllerException | TransportHandlerException e) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
|
||||
}
|
||||
}
|
||||
|
||||
public Response readTemperature(String owner, String deviceId) {
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Sending request to read connected cup temperature of device " + "[" + deviceId + "] via MQTT");
|
||||
}
|
||||
try {
|
||||
String mqttResource = ConnectedCupConstants.TEMPERATURE_CONTEXT.replace("/", "");
|
||||
connectedCupMQTTConnector.publishDeviceData(owner, deviceId, mqttResource, "");
|
||||
|
||||
SensorRecord sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId,
|
||||
ConnectedCupConstants.SENSOR_TEMPERATURE);
|
||||
return Response.ok().entity(sensorRecord).build();
|
||||
} catch (DeviceControllerException | TransportHandlerException e) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
|
||||
}
|
||||
}
|
||||
|
||||
public Response orderCoffee(String deviceId, String deviceOwner) {
|
||||
log.info("Coffee ordered....!");
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Sending request to read liquid level value of device [" + deviceId + "] via MQTT");
|
||||
}
|
||||
return Response.ok().entity("Coffee ordered.").build();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,6 +32,7 @@ import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
@Path("enrollment")
|
||||
@API( name="connectedcup_mgt", version="1.0.0", context="/connectedcup_mgt", tags = {"connectedcup"})
|
||||
@DeviceType("connectedcup")
|
||||
public interface ConnectedCupManagerService {
|
||||
|
||||
@ -26,16 +26,87 @@ 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 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.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ConnectedCupManagerServiceImpl implements ConnectedCupManagerService{
|
||||
@Path("enrollment")
|
||||
public class ConnectedCupManagerServiceImpl implements ConnectedCupManagerService {
|
||||
|
||||
private static Log log = LogFactory.getLog(ConnectedCupManagerServiceImpl.class);
|
||||
|
||||
@Path("devices/{device_id}")
|
||||
@DELETE
|
||||
public Response removeDevice(@PathParam("device_id") String deviceId) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(ConnectedCupConstants.DEVICE_TYPE);
|
||||
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) {
|
||||
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(ConnectedCupConstants.DEVICE_TYPE);
|
||||
try {
|
||||
Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||
device.setDeviceIdentifier(deviceId);
|
||||
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
||||
device.setName(name);
|
||||
device.setType(ConnectedCupConstants.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());
|
||||
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) {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(ConnectedCupConstants.DEVICE_TYPE);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean register(String deviceId, String name) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
@ -61,58 +132,6 @@ public class ConnectedCupManagerServiceImpl implements ConnectedCupManagerServic
|
||||
}
|
||||
}
|
||||
|
||||
public Response removeDevice(String deviceId) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(ConnectedCupConstants.DEVICE_TYPE);
|
||||
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) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
}
|
||||
|
||||
public Response updateDevice(String deviceId, String name) {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(ConnectedCupConstants.DEVICE_TYPE);
|
||||
try {
|
||||
Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||
device.setDeviceIdentifier(deviceId);
|
||||
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
||||
device.setName(name);
|
||||
device.setType(ConnectedCupConstants.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());
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
}
|
||||
|
||||
public Response getDevice(String deviceId) {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(ConnectedCupConstants.DEVICE_TYPE);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
private static String shortUUID() {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
long l = ByteBuffer.wrap(uuid.toString().getBytes(StandardCharsets.UTF_8)).getLong();
|
||||
|
||||
@ -31,7 +31,6 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
import org.wso2.carbon.device.mgt.iot.config.server.DeviceManagementConfigurationManager;
|
||||
import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig;
|
||||
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager;
|
||||
import org.wso2.carbon.device.mgt.iot.transport.TransportHandlerException;
|
||||
import org.wso2.carbon.device.mgt.iot.transport.mqtt.MQTTTransportHandler;
|
||||
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
|
||||
@ -138,18 +137,6 @@ public class ConnectedCupMQTTConnector extends MQTTTransportHandler {
|
||||
String owner = device.getEnrolmentInfo().getOwner();
|
||||
ctx.setTenantDomain(MultitenantUtils.getTenantDomain(owner), true);
|
||||
ctx.setUsername(owner);
|
||||
switch (messageData[0]) {
|
||||
case "temperature":
|
||||
SensorDataManager.getInstance().setSensorRecord(deviceId, ConnectedCupConstants.SENSOR_TEMPERATURE,
|
||||
String.valueOf(messageData[1]),
|
||||
Calendar.getInstance().getTimeInMillis());
|
||||
break;
|
||||
case "coffeelevel":
|
||||
SensorDataManager.getInstance().setSensorRecord(deviceId, ConnectedCupConstants.SENSOR_LEVEL,
|
||||
String.valueOf(messageData[1]),
|
||||
Calendar.getInstance().getTimeInMillis());
|
||||
break;
|
||||
}
|
||||
if (!ConnectedCupServiceUtils.publishToDAS(deviceId, messageData[0], Float.parseFloat
|
||||
(messageData[1]))) {
|
||||
log.error("MQTT Subscriber: Publishing data to DAS failed.");
|
||||
|
||||
@ -28,7 +28,7 @@ import org.apache.http.impl.nio.client.HttpAsyncClients;
|
||||
import org.coffeeking.connectedcup.plugin.constants.ConnectedCupConstants;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException;
|
||||
import org.wso2.carbon.device.mgt.analytics.data.publisher.service.DeviceAnalyticsService;
|
||||
import org.wso2.carbon.device.mgt.analytics.data.publisher.service.EventsPublisherService;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||
|
||||
import javax.ws.rs.HttpMethod;
|
||||
@ -195,8 +195,8 @@ public class ConnectedCupServiceUtils {
|
||||
|
||||
public static boolean publishToDAS(String deviceId, String sensor, float values) {
|
||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
DeviceAnalyticsService deviceAnalyticsService = (DeviceAnalyticsService) ctx.getOSGiService(
|
||||
DeviceAnalyticsService.class, null);
|
||||
EventsPublisherService deviceAnalyticsService = (EventsPublisherService) ctx.getOSGiService(
|
||||
EventsPublisherService.class, null);
|
||||
String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
|
||||
Object metdaData[] = {owner, ConnectedCupConstants.DEVICE_TYPE, deviceId, System.currentTimeMillis()};
|
||||
Object payloadData[] = {values};
|
||||
|
||||
@ -21,27 +21,31 @@
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
|
||||
version="2.5">
|
||||
<display-name>ConnectedCup-Webapp</display-name>
|
||||
|
||||
<servlet>
|
||||
<description>JAX-WS/JAX-RS Endpoint</description>
|
||||
<display-name>JAX-WS/JAX-RS Servlet</display-name>
|
||||
<servlet-name>CXFServlet</servlet-name>
|
||||
<servlet-class>
|
||||
org.apache.cxf.transport.servlet.CXFServlet
|
||||
</servlet-class>
|
||||
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>CXFServlet</servlet-name>
|
||||
<url-pattern>/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<context-param>
|
||||
<param-name>isAdminService</param-name>
|
||||
<param-value>false</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>doAuthentication</param-name>
|
||||
<param-value>false</param-value>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>isSharedWithAllTenants</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>providerTenantDomain</param-name>
|
||||
<param-value>carbon.super</param-value>
|
||||
</context-param>
|
||||
|
||||
<!--publish to apim-->
|
||||
@ -53,17 +57,5 @@
|
||||
<param-name>managed-api-owner</param-name>
|
||||
<param-value>admin</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-context-template</param-name>
|
||||
<param-value>/connectedcup/{version}</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-application</param-name>
|
||||
<param-value>connectedcup</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-isSecured</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
|
||||
</web-app>
|
||||
|
||||
@ -45,42 +45,6 @@ public interface CurrentSensorControllerService {
|
||||
Response registerDeviceIP(@PathParam("owner") String owner, @PathParam("deviceId") String deviceId,
|
||||
@PathParam("ip") String deviceIP, @PathParam("port") String devicePort);
|
||||
|
||||
/**
|
||||
* @param deviceId
|
||||
* @return
|
||||
*/
|
||||
@Path("device/read-current")
|
||||
@GET
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Feature(code = "read-current", name = "Current", type = "monitor",
|
||||
description = "Request current reading from Arduino agent")
|
||||
Response requestCurrent(@HeaderParam("deviceId") String deviceId);
|
||||
|
||||
/**
|
||||
* @param deviceId
|
||||
* @return
|
||||
*/
|
||||
@Path("device/read-power")
|
||||
@GET
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Feature(code = "read-power", name = "Power", type = "monitor",
|
||||
description = "Request power reading from Arduino agent")
|
||||
Response requestPower(@HeaderParam("deviceId") String deviceId);
|
||||
|
||||
/**
|
||||
* @param deviceId
|
||||
* @return
|
||||
*/
|
||||
@Path("device/read-flowrate")
|
||||
@GET
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Feature(code = "read-flowrate", name = "Flow Rate", type = "monitor",
|
||||
description = "Request flow rate reading from Arduino agent")
|
||||
Response requestFlowRate(@HeaderParam("deviceId") String deviceId);
|
||||
|
||||
/**
|
||||
* @param dataMsg
|
||||
*/
|
||||
|
||||
@ -24,10 +24,17 @@ import org.homeautomation.currentsensor.api.dto.DeviceJSON;
|
||||
import org.homeautomation.currentsensor.api.util.CurrentSensorServiceUtils;
|
||||
import org.homeautomation.currentsensor.plugin.constants.CurrentSensorConstants;
|
||||
import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException;
|
||||
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager;
|
||||
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorRecord;
|
||||
import org.wso2.carbon.device.mgt.iot.service.IoTServerStartupListener;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.HeaderParam;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.util.Calendar;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@ -37,18 +44,10 @@ public class CurrentSensorControllerServiceImpl implements CurrentSensorControll
|
||||
private static Log log = LogFactory.getLog(CurrentSensorControllerServiceImpl.class);
|
||||
private ConcurrentHashMap<String, String> deviceToIpMap = new ConcurrentHashMap<>();
|
||||
|
||||
private boolean waitForServerStartup() {
|
||||
while (!IoTServerStartupListener.isServerReady()) {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Response registerDeviceIP(String owner, String deviceId, String deviceIP, String devicePort) {
|
||||
@Path("device/register/{owner}/{deviceId}/{ip}/{port}")
|
||||
@POST
|
||||
public Response registerDeviceIP(@PathParam("owner") String owner, @PathParam("deviceId") String deviceId,
|
||||
@PathParam("ip") String deviceIP, @PathParam("port") String devicePort) {
|
||||
//TODO:: Need to get IP from the request itself
|
||||
String result;
|
||||
|
||||
@ -64,48 +63,15 @@ public class CurrentSensorControllerServiceImpl implements CurrentSensorControll
|
||||
return Response.ok().entity(result).build();
|
||||
}
|
||||
|
||||
public Response requestCurrent(String deviceId) {
|
||||
SensorRecord sensorRecord = null;
|
||||
try {
|
||||
sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId,
|
||||
CurrentSensorConstants.SENSOR_CURRENT);
|
||||
} catch (DeviceControllerException e) {
|
||||
Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
|
||||
return Response.ok().entity(sensorRecord).build();
|
||||
}
|
||||
|
||||
public Response requestPower(String deviceId) {
|
||||
SensorRecord sensorRecord = null;
|
||||
try {
|
||||
sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId,
|
||||
CurrentSensorConstants.SENSOR_POWER);
|
||||
} catch (DeviceControllerException e) {
|
||||
Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
return Response.ok().entity(sensorRecord).build();
|
||||
}
|
||||
|
||||
public Response requestFlowRate(String deviceId) {
|
||||
SensorRecord sensorRecord = null;
|
||||
try {
|
||||
sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId,
|
||||
CurrentSensorConstants.SENSOR_FLOWRATE);
|
||||
} catch (DeviceControllerException e) {
|
||||
Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
return Response.ok().entity(sensorRecord).build();
|
||||
}
|
||||
|
||||
@Path("device/push-data")
|
||||
@POST
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public Response pushData(final DeviceJSON dataMsg) {
|
||||
|
||||
String owner = dataMsg.owner;
|
||||
String deviceId = dataMsg.deviceId;
|
||||
String deviceIp = dataMsg.reply;
|
||||
float current = dataMsg.current;
|
||||
float flow_rate = dataMsg.flow_rate;
|
||||
|
||||
String registeredIp = deviceToIpMap.get(deviceId);
|
||||
|
||||
if (registeredIp == null) {
|
||||
@ -117,19 +83,6 @@ public class CurrentSensorControllerServiceImpl implements CurrentSensorControll
|
||||
" is already registered under some other IP. Re-registration required");
|
||||
return Response.status(Response.Status.CONFLICT.getStatusCode()).build();
|
||||
}
|
||||
|
||||
SensorDataManager.getInstance().setSensorRecord(deviceId, CurrentSensorConstants.SENSOR_CURRENT,
|
||||
String.valueOf(current),
|
||||
Calendar.getInstance().getTimeInMillis());
|
||||
|
||||
SensorDataManager.getInstance().setSensorRecord(deviceId, CurrentSensorConstants.SENSOR_POWER,
|
||||
String.valueOf(current * 230),
|
||||
Calendar.getInstance().getTimeInMillis());
|
||||
|
||||
SensorDataManager.getInstance().setSensorRecord(deviceId, CurrentSensorConstants.SENSOR_FLOWRATE,
|
||||
String.valueOf(flow_rate),
|
||||
Calendar.getInstance().getTimeInMillis());
|
||||
|
||||
if (!CurrentSensorServiceUtils.publishToDASCurrent(dataMsg.deviceId, current)) {
|
||||
log.warn("An error occured whilst trying to publish pin data of Current Sensor Data with ID [" + deviceId +
|
||||
"] of owner [" + owner + "]");
|
||||
@ -150,4 +103,15 @@ public class CurrentSensorControllerServiceImpl implements CurrentSensorControll
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
private boolean waitForServerStartup() {
|
||||
while (!IoTServerStartupListener.isServerReady()) {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -32,6 +32,7 @@ import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
@Path("enrollment")
|
||||
@API(name = "currentsensor_mgt", version = "1.0.0", context = "/currentsensor_mgt", tags = {"currentsensor"})
|
||||
@DeviceType(value = "currentsensor")
|
||||
public interface CurrentSensorManagerService {
|
||||
|
||||
@ -22,7 +22,6 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.apimgt.application.extension.dto.ApiApplicationKey;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.JWTClient;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.JWTClientManager;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException;
|
||||
import org.homeautomation.currentsensor.api.util.APIUtil;
|
||||
@ -39,18 +38,113 @@ import org.wso2.carbon.device.mgt.iot.util.ZipArchive;
|
||||
import org.wso2.carbon.device.mgt.iot.util.ZipUtil;
|
||||
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.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
@Path("enrollment")
|
||||
class CurrentSensorManagerServiceImpl implements CurrentSensorManagerService {
|
||||
|
||||
private static Log log = LogFactory.getLog(CurrentSensorManagerServiceImpl.class);
|
||||
private static ApiApplicationKey apiApplicationKey;
|
||||
private static final String KEY_TYPE = "PRODUCTION";
|
||||
|
||||
@Path("devices/{device_id}")
|
||||
@DELETE
|
||||
public Response removeDevice(@PathParam("device_id") String deviceId) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(CurrentSensorConstants.DEVICE_TYPE);
|
||||
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) {
|
||||
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(CurrentSensorConstants.DEVICE_TYPE);
|
||||
try {
|
||||
Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||
device.setDeviceIdentifier(deviceId);
|
||||
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
||||
device.setName(name);
|
||||
device.setType(CurrentSensorConstants.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());
|
||||
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) {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(CurrentSensorConstants.DEVICE_TYPE);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@Path("devices/{sketch_type}/download")
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response downloadSketch(@QueryParam("deviceName") String deviceName,
|
||||
@PathParam("sketch_type") String sketchType) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName);
|
||||
Response.ResponseBuilder rb = Response.ok(zipFile.getZipFile());
|
||||
rb.header("Content-Disposition", "attachment; filename=\"" + zipFile.getFileName() + "\"");
|
||||
return rb.build();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
return Response.status(400).entity(ex.getMessage()).build();//bad request
|
||||
} catch (DeviceManagementException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (DeviceControllerException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (JWTClientException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (APIManagerException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (UserStoreException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean register(String deviceId, String name) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
@ -76,79 +170,6 @@ class CurrentSensorManagerServiceImpl implements CurrentSensorManagerService {
|
||||
}
|
||||
}
|
||||
|
||||
public Response removeDevice(String deviceId) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(CurrentSensorConstants.DEVICE_TYPE);
|
||||
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) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
}
|
||||
|
||||
public Response updateDevice(String deviceId, String name) {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(CurrentSensorConstants.DEVICE_TYPE);
|
||||
try {
|
||||
Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||
device.setDeviceIdentifier(deviceId);
|
||||
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
||||
device.setName(name);
|
||||
device.setType(CurrentSensorConstants.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());
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
}
|
||||
|
||||
public Response getDevice(String deviceId) {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(CurrentSensorConstants.DEVICE_TYPE);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
public Response downloadSketch(String deviceName, String sketchType) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName);
|
||||
Response.ResponseBuilder rb = Response.ok(zipFile.getZipFile());
|
||||
rb.header("Content-Disposition", "attachment; filename=\"" + zipFile.getFileName() + "\"");
|
||||
return rb.build();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
return Response.status(400).entity(ex.getMessage()).build();//bad request
|
||||
} catch (DeviceManagementException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (DeviceControllerException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (JWTClientException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (APIManagerException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (UserStoreException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
}
|
||||
}
|
||||
|
||||
private ZipArchive createDownloadFile(String owner, String deviceName)
|
||||
throws DeviceManagementException, JWTClientException, DeviceControllerException, APIManagerException,
|
||||
UserStoreException {
|
||||
@ -166,14 +187,14 @@ class CurrentSensorManagerServiceImpl implements CurrentSensorManagerService {
|
||||
apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(
|
||||
CurrentSensorConstants.DEVICE_TYPE, tags, KEY_TYPE, applicationUsername, true);
|
||||
}
|
||||
JWTClient jwtClient = JWTClientManager.getInstance().getJWTClient();
|
||||
JWTClient jwtClient = APIUtil.getJWTClientManagerService().getJWTClient();
|
||||
String scopes = "device_type_" + CurrentSensorConstants.DEVICE_TYPE + " device_" + deviceId;
|
||||
AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(apiApplicationKey.getConsumerKey(),
|
||||
apiApplicationKey.getConsumerSecret(), owner,
|
||||
scopes);
|
||||
//create token
|
||||
String accessToken = accessTokenInfo.getAccess_token();
|
||||
String refreshToken = accessTokenInfo.getRefresh_token();
|
||||
String accessToken = accessTokenInfo.getAccessToken();
|
||||
String refreshToken = accessTokenInfo.getRefreshToken();
|
||||
//Register the device with CDMF
|
||||
boolean status = register(deviceId, deviceName);
|
||||
if (!status) {
|
||||
|
||||
@ -5,6 +5,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
|
||||
|
||||
/**
|
||||
* This class provides utility functions used by REST-API.
|
||||
@ -52,4 +53,17 @@ public class APIUtil {
|
||||
}
|
||||
return apiManagementProviderService;
|
||||
}
|
||||
|
||||
public static JWTClientManagerService getJWTClientManagerService() {
|
||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
JWTClientManagerService jwtClientManagerService =
|
||||
(JWTClientManagerService) ctx.getOSGiService(JWTClientManagerService.class, null);
|
||||
if (jwtClientManagerService == null) {
|
||||
String msg = "JWT Client manager service has not initialized.";
|
||||
log.error(msg);
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
return jwtClientManagerService;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -23,7 +23,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.homeautomation.currentsensor.plugin.constants.CurrentSensorConstants;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException;
|
||||
import org.wso2.carbon.device.mgt.analytics.data.publisher.service.DeviceAnalyticsService;
|
||||
import org.wso2.carbon.device.mgt.analytics.data.publisher.service.EventsPublisherService;
|
||||
|
||||
public class CurrentSensorServiceUtils {
|
||||
|
||||
@ -34,8 +34,8 @@ public class CurrentSensorServiceUtils {
|
||||
|
||||
public static boolean publishToDASCurrent(String deviceId, float current) {
|
||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
DeviceAnalyticsService deviceAnalyticsService = (DeviceAnalyticsService) ctx.getOSGiService(
|
||||
DeviceAnalyticsService.class, null);
|
||||
EventsPublisherService deviceAnalyticsService = (EventsPublisherService) ctx.getOSGiService(
|
||||
EventsPublisherService.class, null);
|
||||
String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
|
||||
Object metdaData[] = {owner, CurrentSensorConstants.DEVICE_TYPE, deviceId, System.currentTimeMillis()};
|
||||
Object payloadData[] = {current};
|
||||
@ -50,8 +50,8 @@ public class CurrentSensorServiceUtils {
|
||||
|
||||
public static boolean publishToDASPower(String deviceId, float power) {
|
||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
DeviceAnalyticsService deviceAnalyticsService = (DeviceAnalyticsService) ctx.getOSGiService(
|
||||
DeviceAnalyticsService.class, null);
|
||||
EventsPublisherService deviceAnalyticsService = (EventsPublisherService) ctx.getOSGiService(
|
||||
EventsPublisherService.class, null);
|
||||
String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
|
||||
Object metdaData[] = {owner, CurrentSensorConstants.DEVICE_TYPE, deviceId, System.currentTimeMillis()};
|
||||
Object payloadData[] = {power};
|
||||
@ -66,8 +66,8 @@ public class CurrentSensorServiceUtils {
|
||||
|
||||
public static boolean publishToDASFlowRate(String deviceId, float flowRate) {
|
||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
DeviceAnalyticsService deviceAnalyticsService = (DeviceAnalyticsService) ctx.getOSGiService(
|
||||
DeviceAnalyticsService.class, null);
|
||||
EventsPublisherService deviceAnalyticsService = (EventsPublisherService) ctx.getOSGiService(
|
||||
EventsPublisherService.class, null);
|
||||
String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
|
||||
Object metdaData[] = {owner, CurrentSensorConstants.DEVICE_TYPE, deviceId, System.currentTimeMillis()};
|
||||
Object payloadData[] = {flowRate};
|
||||
|
||||
@ -21,19 +21,32 @@
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
|
||||
version="2.5">
|
||||
<display-name>Current-Sensor-Agent-Webapp</display-name>
|
||||
|
||||
<servlet>
|
||||
<description>JAX-WS/JAX-RS IOT Current Sensor Endpoint</description>
|
||||
<display-name>JAX-WS/JAX-RS Servlet</display-name>
|
||||
<servlet-name>CXFServlet</servlet-name>
|
||||
<servlet-class>
|
||||
org.apache.cxf.transport.servlet.CXFServlet
|
||||
</servlet-class>
|
||||
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>CXFServlet</servlet-name>
|
||||
<url-pattern>/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
<context-param>
|
||||
<param-name>isAdminService</param-name>
|
||||
<param-value>false</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>doAuthentication</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>isSharedWithAllTenants</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>providerTenantDomain</param-name>
|
||||
<param-value>carbon.super</param-value>
|
||||
</context-param>
|
||||
|
||||
<!--publish to apim-->
|
||||
<context-param>
|
||||
@ -44,17 +57,5 @@
|
||||
<param-name>managed-api-owner</param-name>
|
||||
<param-value>admin</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-context-template</param-name>
|
||||
<param-value>/currentsensor/{version}</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-application</param-name>
|
||||
<param-value>currentsensor</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-isSecured</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
|
||||
</web-app>
|
||||
|
||||
@ -77,22 +77,6 @@ public interface DoorManagerControllerService {
|
||||
@HeaderParam("protocol") String protocol,
|
||||
@FormParam("state") String state);
|
||||
|
||||
/**
|
||||
* Request current status of door lock safe
|
||||
*
|
||||
* @param owner owner of the device
|
||||
* @param deviceId unique identifier for given device
|
||||
* @param protocol transport protocol which is being using here MQTT
|
||||
*/
|
||||
@GET
|
||||
@Path("device/current-status")
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Feature(code = "current-status", name = "Door Locker Status", type = "monitor",
|
||||
description = "Request current status of door safe")
|
||||
Response requestStatusOfDoorLockSafe(@HeaderParam("owner") String owner, @HeaderParam("deviceId") String deviceId,
|
||||
@HeaderParam("protocol") String protocol);
|
||||
|
||||
/**
|
||||
* @param userInfo user information which are required to test given user is authorized to open requested door
|
||||
* @return if user is authorized open the the door allow to open it
|
||||
|
||||
@ -38,22 +38,27 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||
import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType;
|
||||
import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig;
|
||||
import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException;
|
||||
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager;
|
||||
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorRecord;
|
||||
import org.wso2.carbon.device.mgt.iot.service.IoTServerStartupListener;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.JWTClient;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.JWTClientManager;
|
||||
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 org.wso2.carbon.user.api.UserStoreManager;
|
||||
import javax.ws.rs.core.Context;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.FormParam;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.HeaderParam;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class DoorManagerControllerServiceImpl implements DoorManagerControllerService{
|
||||
public class DoorManagerControllerServiceImpl implements DoorManagerControllerService {
|
||||
|
||||
private static Log log = LogFactory.getLog(DoorManagerControllerServiceImpl.class);
|
||||
private static String CURRENT_STATUS = "doorLockerCurrentStatus";
|
||||
@ -66,7 +71,6 @@ public class DoorManagerControllerServiceImpl implements DoorManagerControllerSe
|
||||
doorManager = new DoorManager();
|
||||
}
|
||||
|
||||
@Context //injected response proxy supporting multiple thread
|
||||
private boolean waitForServerStartup() {
|
||||
while (!IoTServerStartupListener.isServerReady()) {
|
||||
try {
|
||||
@ -102,9 +106,14 @@ public class DoorManagerControllerServiceImpl implements DoorManagerControllerSe
|
||||
connectorThread.start();
|
||||
}
|
||||
|
||||
|
||||
public Response assignUserToLock(String owner, String deviceId, String protocol, String cardNumber, String userName,
|
||||
String emailAddress) {
|
||||
@Path("device/assign-user")
|
||||
@POST
|
||||
public Response assignUserToLock(@HeaderParam("owner") String owner,
|
||||
@HeaderParam("deviceId") String deviceId,
|
||||
@HeaderParam("protocol") String protocol,
|
||||
@FormParam("cardNumber") String cardNumber,
|
||||
@FormParam("userName") String userName,
|
||||
@FormParam("emailAddress") String emailAddress) {
|
||||
|
||||
if (userName != null && cardNumber != null && deviceId != null) {
|
||||
try {
|
||||
@ -119,22 +128,22 @@ public class DoorManagerControllerServiceImpl implements DoorManagerControllerSe
|
||||
apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(
|
||||
DoorManagerConstants.DEVICE_TYPE, tags, KEY_TYPE, applicationUsername, true);
|
||||
}
|
||||
JWTClient jwtClient = JWTClientManager.getInstance().getJWTClient();
|
||||
JWTClient jwtClient = APIUtil.getJWTClientManagerService().getJWTClient();
|
||||
String scopes = "device_type_" + DoorManagerConstants.DEVICE_TYPE + " device_" + deviceId;
|
||||
AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(apiApplicationKey.getConsumerKey(),
|
||||
apiApplicationKey.getConsumerSecret(), owner, scopes);
|
||||
String accessToken = accessTokenInfo.getAccess_token();
|
||||
String accessToken = accessTokenInfo.getAccessToken();
|
||||
if (accessToken == null) {
|
||||
return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
|
||||
}
|
||||
Map<String, String> claims = new HashMap<>();
|
||||
claims.put(DoorManagerConstants.DEVICE_CLAIMS_ACCESS_TOKEN, accessToken);
|
||||
claims.put(DoorManagerConstants.DEVICE_CLAIMS_REFRESH_TOKEN,
|
||||
accessTokenInfo.getRefresh_token());
|
||||
accessTokenInfo.getRefreshToken());
|
||||
claims.put(DoorManagerConstants.DEVICE_CLAIMS_CARD_NUMBER, cardNumber);
|
||||
userStoreManager.setUserClaimValues(userName, claims, null);
|
||||
doorLockSafe.setAccessToken(accessTokenInfo.getAccess_token());
|
||||
doorLockSafe.setRefreshToken(accessTokenInfo.getRefresh_token());
|
||||
doorLockSafe.setAccessToken(accessTokenInfo.getAccessToken());
|
||||
doorLockSafe.setRefreshToken(accessTokenInfo.getRefreshToken());
|
||||
doorLockSafe.setDeviceId(deviceId);
|
||||
doorLockSafe.setOwner(owner);
|
||||
doorLockSafe.setEmailAddress(emailAddress);
|
||||
@ -162,7 +171,12 @@ public class DoorManagerControllerServiceImpl implements DoorManagerControllerSe
|
||||
}
|
||||
}
|
||||
|
||||
public Response changeStatusOfDoorLockSafe(String owner, String deviceId, String protocol, String state) {
|
||||
@Path("device/change-status")
|
||||
@POST
|
||||
public Response changeStatusOfDoorLockSafe(@HeaderParam("owner") String owner,
|
||||
@HeaderParam("deviceId") String deviceId,
|
||||
@HeaderParam("protocol") String protocol,
|
||||
@FormParam("state") String state) {
|
||||
try {
|
||||
int lockerCurrentState;
|
||||
if (state.toUpperCase().equals("LOCK")) {
|
||||
@ -170,8 +184,6 @@ public class DoorManagerControllerServiceImpl implements DoorManagerControllerSe
|
||||
} else {
|
||||
lockerCurrentState = 1;
|
||||
}
|
||||
SensorDataManager.getInstance().setSensorRecord(deviceId, CURRENT_STATUS,
|
||||
String.valueOf(lockerCurrentState), Calendar.getInstance().getTimeInMillis());
|
||||
doorManagerMQTTConnector.sendCommandViaMQTT(owner, deviceId, "DoorManager:", state.toUpperCase());
|
||||
return Response.ok().build();
|
||||
} catch (DeviceManagementException e) {
|
||||
@ -181,16 +193,11 @@ public class DoorManagerControllerServiceImpl implements DoorManagerControllerSe
|
||||
}
|
||||
}
|
||||
|
||||
public Response requestStatusOfDoorLockSafe(String owner, String deviceId, String protocol) {
|
||||
SensorRecord sensorRecord = null;
|
||||
try {
|
||||
sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId, CURRENT_STATUS);
|
||||
return Response.ok(sensorRecord).entity(sensorRecord).build();
|
||||
} catch (DeviceControllerException e) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("device/get-user-info")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@SuppressWarnings("unchecked")
|
||||
public Response get_user_info(final UserInfo userInfo) {
|
||||
if (userInfo.userName != null && userInfo.cardNumber != null && userInfo.deviceId != null) {
|
||||
try {
|
||||
|
||||
@ -32,6 +32,7 @@ import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
@Path("enrollment")
|
||||
@SuppressWarnings("NonJaxWsWebServices")
|
||||
@DeviceType(value = "doormanager")
|
||||
@API(name = "doormanager_mgt", version = "1.0.0", context = "/doormanager_mgt" , tags = {"doormanager"})
|
||||
@ -41,7 +42,6 @@ public interface DoorManagerManagerService {
|
||||
@DELETE
|
||||
Response removeDevice(@PathParam("device_id") String deviceId);
|
||||
|
||||
|
||||
@Path("devices/{device_id}")
|
||||
@PUT
|
||||
Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name);
|
||||
|
||||
@ -37,66 +37,38 @@ import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException;
|
||||
import org.wso2.carbon.device.mgt.iot.util.ZipArchive;
|
||||
import org.wso2.carbon.device.mgt.iot.util.ZipUtil;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.JWTClient;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.JWTClientManager;
|
||||
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.servlet.http.HttpServletResponse;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.Response;
|
||||
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 java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
public class DoorManagerManagerServiceImpl implements DoorManagerManagerService{
|
||||
@Path("enrollment")
|
||||
public class DoorManagerManagerServiceImpl implements DoorManagerManagerService {
|
||||
|
||||
private static Log log = LogFactory.getLog(DoorManagerManagerServiceImpl.class);
|
||||
@Context //injected response proxy supporting multiple thread
|
||||
private HttpServletResponse response;
|
||||
private static ApiApplicationKey apiApplicationKey;
|
||||
private static final String KEY_TYPE = "PRODUCTION";
|
||||
|
||||
/**
|
||||
* Generate UUID
|
||||
*
|
||||
* @return generated UUID
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
private boolean register(String deviceId,String name) {
|
||||
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(DoorManagerConstants.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);
|
||||
device.setName(name);
|
||||
device.setType(DoorManagerConstants.DEVICE_TYPE);
|
||||
enrolmentInfo.setOwner(APIUtil.getAuthenticatedUser());
|
||||
device.setEnrolmentInfo(enrolmentInfo);
|
||||
boolean added = APIUtil.getDeviceManagementService().enrollDevice(device);
|
||||
return added;
|
||||
} catch (DeviceManagementException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public Response removeDevice(String deviceId) {
|
||||
@Path("devices/{device_id}")
|
||||
@DELETE
|
||||
public Response removeDevice(@PathParam("device_id") String deviceId) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
@ -113,7 +85,9 @@ public class DoorManagerManagerServiceImpl implements DoorManagerManagerService{
|
||||
}
|
||||
}
|
||||
|
||||
public Response updateDevice(String deviceId, String name) {
|
||||
@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);
|
||||
@ -134,7 +108,11 @@ public class DoorManagerManagerServiceImpl implements DoorManagerManagerService{
|
||||
}
|
||||
}
|
||||
|
||||
public Response getDevice(String deviceId) {
|
||||
@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);
|
||||
@ -146,7 +124,10 @@ public class DoorManagerManagerServiceImpl implements DoorManagerManagerService{
|
||||
}
|
||||
}
|
||||
|
||||
public Response downloadSketch(String deviceName) {
|
||||
@Path("devices/download")
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response downloadSketch(@QueryParam("deviceName") String deviceName) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName);
|
||||
Response.ResponseBuilder response = Response.ok(FileUtils.readFileToByteArray(zipFile.getZipFile()));
|
||||
@ -170,7 +151,9 @@ public class DoorManagerManagerServiceImpl implements DoorManagerManagerService{
|
||||
}
|
||||
}
|
||||
|
||||
public Response generateSketchLink(String deviceName) {
|
||||
@Path("devices/generate_link")
|
||||
@GET
|
||||
public Response generateSketchLink(@QueryParam("deviceName") String deviceName){
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName);
|
||||
Response.ResponseBuilder rb = Response.ok(zipFile.getDeviceId());
|
||||
@ -207,14 +190,14 @@ public class DoorManagerManagerServiceImpl implements DoorManagerManagerService{
|
||||
apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(
|
||||
DoorManagerConstants.DEVICE_TYPE, tags, KEY_TYPE, applicationUsername, true);
|
||||
}
|
||||
JWTClient jwtClient = JWTClientManager.getInstance().getJWTClient();
|
||||
JWTClient jwtClient = APIUtil.getJWTClientManagerService().getJWTClient();
|
||||
String scopes = "device_type_" + DoorManagerConstants.DEVICE_TYPE + " device_" + deviceId;
|
||||
AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(apiApplicationKey.getConsumerKey(),
|
||||
apiApplicationKey.getConsumerSecret(), owner,
|
||||
scopes);
|
||||
//create token
|
||||
String accessToken = accessTokenInfo.getAccess_token();
|
||||
String refreshToken = accessTokenInfo.getRefresh_token();
|
||||
String accessToken = accessTokenInfo.getAccessToken();
|
||||
String refreshToken = accessTokenInfo.getRefreshToken();
|
||||
//Register the device with CDMF
|
||||
boolean status = register(deviceId, deviceName);
|
||||
if (!status) {
|
||||
@ -229,4 +212,40 @@ public class DoorManagerManagerServiceImpl implements DoorManagerManagerService{
|
||||
return zipFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate UUID
|
||||
*
|
||||
* @return generated UUID
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
private boolean register(String deviceId, String name) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(DoorManagerConstants.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);
|
||||
device.setName(name);
|
||||
device.setType(DoorManagerConstants.DEVICE_TYPE);
|
||||
enrolmentInfo.setOwner(APIUtil.getAuthenticatedUser());
|
||||
device.setEnrolmentInfo(enrolmentInfo);
|
||||
boolean added = APIUtil.getDeviceManagementService().enrollDevice(device);
|
||||
return added;
|
||||
} catch (DeviceManagementException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -28,7 +28,6 @@ import org.homeautomation.doormanager.plugin.constants.DoorManagerConstants;
|
||||
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||
import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig;
|
||||
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager;
|
||||
import org.wso2.carbon.device.mgt.iot.transport.TransportHandlerException;
|
||||
import org.wso2.carbon.device.mgt.iot.transport.mqtt.MQTTTransportHandler;
|
||||
|
||||
@ -117,8 +116,6 @@ public class DoorManagerMQTTConnector extends MQTTTransportHandler {
|
||||
lockerStatus = 1;
|
||||
}
|
||||
try {
|
||||
SensorDataManager.getInstance().setSensorRecord(deviceId, "doorLockerCurrentStatus",
|
||||
String.valueOf(lockerStatus), Calendar.getInstance().getTimeInMillis());
|
||||
if (!DoorManagerServiceUtils.publishToDASLockerStatus(owner, deviceId, lockerStatus)) {
|
||||
log.warn("An error occurred while trying to publish with ID [" + deviceId + "] of owner ["
|
||||
+ owner + "]");
|
||||
|
||||
@ -21,6 +21,7 @@ package org.homeautomation.doormanager.api.util;
|
||||
import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
|
||||
|
||||
/**
|
||||
* This class provides utility functions used by REST-API.
|
||||
@ -65,4 +66,16 @@ public class APIUtil {
|
||||
}
|
||||
return apiManagementProviderService;
|
||||
}
|
||||
|
||||
public static JWTClientManagerService getJWTClientManagerService() {
|
||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
JWTClientManagerService jwtClientManagerService =
|
||||
(JWTClientManagerService) ctx.getOSGiService(JWTClientManagerService.class, null);
|
||||
if (jwtClientManagerService == null) {
|
||||
String msg = "JWT Client manager service has not initialized.";
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
return jwtClientManagerService;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -23,7 +23,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.homeautomation.doormanager.plugin.constants.DoorManagerConstants;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException;
|
||||
import org.wso2.carbon.device.mgt.analytics.data.publisher.service.DeviceAnalyticsService;
|
||||
import org.wso2.carbon.device.mgt.analytics.data.publisher.service.EventsPublisherService;
|
||||
|
||||
public class DoorManagerServiceUtils {
|
||||
|
||||
@ -53,8 +53,8 @@ public class DoorManagerServiceUtils {
|
||||
if (ctx.getTenantDomain(true) == null) {
|
||||
ctx.setTenantDomain("carbon.super", true);
|
||||
}
|
||||
DeviceAnalyticsService deviceAnalyticsService = (DeviceAnalyticsService) ctx.getOSGiService(
|
||||
DeviceAnalyticsService.class, null);
|
||||
EventsPublisherService deviceAnalyticsService = (EventsPublisherService) ctx.getOSGiService(
|
||||
EventsPublisherService.class, null);
|
||||
Object metaData[] = {owner, DoorManagerConstants.DEVICE_TYPE, deviceId, System.currentTimeMillis()};
|
||||
try {
|
||||
deviceAnalyticsService.publishEvent(definition, STREAM_DEFINITION_VERSION, metaData,
|
||||
|
||||
@ -21,13 +21,10 @@
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
|
||||
version="2.5">
|
||||
<display-name>Sample-Webapp-Controller</display-name>
|
||||
|
||||
<servlet>
|
||||
<description>JAX-WS/JAX-RS Endpoint</description>
|
||||
<display-name>JAX-WS/JAX-RS Servlet</display-name>
|
||||
<servlet-name>CXFServlet</servlet-name>
|
||||
<servlet-class>
|
||||
org.apache.cxf.transport.servlet.CXFServlet
|
||||
</servlet-class>
|
||||
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
@ -40,8 +37,17 @@
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>doAuthentication</param-name>
|
||||
<param-value>false</param-value>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>isSharedWithAllTenants</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>providerTenantDomain</param-name>
|
||||
<param-value>carbon.super</param-value>
|
||||
</context-param>
|
||||
|
||||
<!--publish to apim-->
|
||||
<context-param>
|
||||
<param-name>managed-api-enabled</param-name>
|
||||
@ -51,16 +57,5 @@
|
||||
<param-name>managed-api-owner</param-name>
|
||||
<param-value>admin</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-context-template</param-name>
|
||||
<param-value>/doormanager/{version}</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-application</param-name>
|
||||
<param-value>doormanager</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-isSecured</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
|
||||
</web-app>
|
||||
|
||||
Binary file not shown.
@ -51,38 +51,6 @@ public interface FireAlarmControllerService {
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
Response registerDevice(final DeviceJSON agentInfo);
|
||||
|
||||
/**
|
||||
* @param owner device owner
|
||||
* @param deviceId unique identifier for given device type
|
||||
* @param protocol name of supported protocol. here MQTT is used
|
||||
* @return sensor record
|
||||
*/
|
||||
@Path("device/read-temperature")
|
||||
@GET
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Feature(code = "read-temperature", name = "Temperature", type = "monitor",
|
||||
description = "Request temperature reading from device")
|
||||
Response readTemperature(@HeaderParam("owner") String owner,
|
||||
@HeaderParam("deviceId") String deviceId,
|
||||
@HeaderParam("protocol") String protocol);
|
||||
|
||||
/**
|
||||
* @param owner device owner
|
||||
* @param deviceId unique identifier for given device type
|
||||
* @param protocol name of supported protocol. here MQTT is used
|
||||
* @return sensor record
|
||||
*/
|
||||
@Path("device/read-humidity")
|
||||
@GET
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Feature(code = "read-humidity", name = "Humidity", type = "monitor",
|
||||
description = "Request humidity reading from device")
|
||||
Response readHumidity(@HeaderParam("owner") String owner,
|
||||
@HeaderParam("deviceId") String deviceId,
|
||||
@HeaderParam("protocol") String protocol);
|
||||
|
||||
/**
|
||||
* @param owner device owner
|
||||
* @param deviceId unique identifier for given device type
|
||||
|
||||
@ -23,13 +23,19 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.homeautomation.firealarm.api.dto.DeviceJSON;
|
||||
import org.homeautomation.firealarm.api.exception.DeviceTypeException;
|
||||
import org.homeautomation.firealarm.api.transport.MQTTConnector;
|
||||
import org.homeautomation.firealarm.plugin.constants.DeviceTypeConstants;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||
import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig;
|
||||
import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException;
|
||||
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager;
|
||||
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorRecord;
|
||||
import org.wso2.carbon.device.mgt.iot.service.IoTServerStartupListener;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.FormParam;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.HeaderParam;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
public class FireAlarmControllerServiceImpl implements FireAlarmControllerService {
|
||||
@ -37,6 +43,35 @@ public class FireAlarmControllerServiceImpl implements FireAlarmControllerServic
|
||||
private static Log log = LogFactory.getLog(FireAlarmControllerServiceImpl.class);
|
||||
private MQTTConnector mqttConnector;
|
||||
|
||||
@Path("device/register")
|
||||
@POST
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public Response registerDevice(final DeviceJSON agentInfo) {
|
||||
if ((agentInfo.deviceId != null) && (agentInfo.owner != null)) {
|
||||
return Response.status(Response.Status.OK).entity("Device has been registered successfully").build();
|
||||
}
|
||||
return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Message body not " +
|
||||
"well-formed and still invalid").build();
|
||||
}
|
||||
|
||||
@Path("device/change-status")
|
||||
@POST
|
||||
public Response changeBuzzerState(@HeaderParam("owner") String owner,
|
||||
@HeaderParam("deviceId") String deviceId,
|
||||
@HeaderParam("protocol") String protocol,
|
||||
@FormParam("state") String state) {
|
||||
try {
|
||||
mqttConnector.sendCommandViaMQTT(owner, deviceId, "buzzer:", state.toUpperCase());
|
||||
return Response.ok().build();
|
||||
} catch (DeviceManagementException e) {
|
||||
log.error(e);
|
||||
return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
|
||||
} catch (DeviceTypeException e) {
|
||||
log.error(e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean waitForServerStartup() {
|
||||
while (!IoTServerStartupListener.isServerReady()) {
|
||||
try {
|
||||
@ -74,47 +109,4 @@ public class FireAlarmControllerServiceImpl implements FireAlarmControllerServic
|
||||
connectorThread.start();
|
||||
}
|
||||
|
||||
public Response registerDevice(final DeviceJSON agentInfo) {
|
||||
if ((agentInfo.deviceId != null) && (agentInfo.owner != null)) {
|
||||
return Response.status(Response.Status.OK).entity("Device has been registered successfully").build();
|
||||
}
|
||||
return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Message body not " +
|
||||
"well-formed and still invalid").build();
|
||||
}
|
||||
|
||||
public Response readTemperature(String owner, String deviceId, String protocol) {
|
||||
SensorRecord sensorRecord = null;
|
||||
try {
|
||||
sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId,
|
||||
DeviceTypeConstants.SENSOR_TEMPERATURE);
|
||||
} catch (DeviceControllerException e) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
return Response.ok().entity(sensorRecord).build();
|
||||
}
|
||||
|
||||
public Response readHumidity(String owner, String deviceId, String protocol) {
|
||||
SensorRecord sensorRecord = null;
|
||||
try {
|
||||
sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId,
|
||||
DeviceTypeConstants.SENSOR_HUMIDITY);
|
||||
} catch (DeviceControllerException e) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
return Response.ok().entity(sensorRecord).build();
|
||||
}
|
||||
|
||||
public Response changeBuzzerState(String owner, String deviceId, String protocol, String state) {
|
||||
try {
|
||||
mqttConnector.sendCommandViaMQTT(owner, deviceId, "buzzer:", state.toUpperCase());
|
||||
return Response.ok().build();
|
||||
} catch (DeviceManagementException e) {
|
||||
log.error(e);
|
||||
return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
|
||||
} catch (DeviceTypeException e) {
|
||||
log.error(e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,6 +32,7 @@ import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
@Path("enrollment")
|
||||
@SuppressWarnings("NonJaxWsWebServices")
|
||||
@API(name = "firealarm_mgt", version = "1.0.0", context = "/firealarm_mgt" ,tags = {"firealarm"})
|
||||
@DeviceType(value = "firealarm")
|
||||
@ -56,7 +57,6 @@ public interface FireAlarmManagerService {
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
Response downloadSketch(@QueryParam("deviceName") String deviceName);
|
||||
|
||||
|
||||
@Path("devices/generate_link")
|
||||
@GET
|
||||
Response generateSketchLink(@QueryParam("deviceName") String deviceName);
|
||||
|
||||
@ -36,11 +36,19 @@ import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException;
|
||||
import org.wso2.carbon.device.mgt.iot.util.ZipArchive;
|
||||
import org.wso2.carbon.device.mgt.iot.util.ZipUtil;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.JWTClient;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.JWTClientManager;
|
||||
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;
|
||||
@ -48,13 +56,160 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
@Path("enrollment")
|
||||
public class FireAlarmManagerServiceImpl implements FireAlarmManagerService{
|
||||
|
||||
private static Log log = LogFactory.getLog(FireAlarmManagerServiceImpl.class);
|
||||
|
||||
private static ApiApplicationKey apiApplicationKey;
|
||||
private static final String KEY_TYPE = "PRODUCTION";
|
||||
|
||||
@Path("devices/{device_id}")
|
||||
@DELETE
|
||||
public Response removeDevice(@PathParam("device_id") String deviceId) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(DeviceTypeConstants.DEVICE_TYPE);
|
||||
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) {
|
||||
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(DeviceTypeConstants.DEVICE_TYPE);
|
||||
Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||
device.setDeviceIdentifier(deviceId);
|
||||
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
||||
device.setName(name);
|
||||
device.setType(DeviceTypeConstants.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) {
|
||||
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(DeviceTypeConstants.DEVICE_TYPE);
|
||||
Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||
return Response.ok().entity(device).build();
|
||||
} catch (DeviceManagementException e) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
}
|
||||
|
||||
@Path("devices/download")
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response downloadSketch(@QueryParam("deviceName") String deviceName) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName);
|
||||
Response.ResponseBuilder response = Response.ok(FileUtils.readFileToByteArray(zipFile.getZipFile()));
|
||||
response.type("application/zip");
|
||||
response.header("Content-Disposition", "attachment; filename=\"" + zipFile.getFileName() + "\"");
|
||||
return response.build();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
return Response.status(400).entity(ex.getMessage()).build();//bad request
|
||||
} catch (DeviceManagementException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (JWTClientException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (APIManagerException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (DeviceControllerException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (IOException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (UserStoreException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
}
|
||||
}
|
||||
|
||||
@Path("devices/generate_link")
|
||||
@GET
|
||||
public Response generateSketchLink(@QueryParam("deviceName") String deviceName) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName);
|
||||
Response.ResponseBuilder rb = Response.ok(zipFile.getDeviceId());
|
||||
return rb.build();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
return Response.status(400).entity(ex.getMessage()).build();//bad request
|
||||
} catch (DeviceManagementException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (JWTClientException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (APIManagerException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (DeviceControllerException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (UserStoreException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
}
|
||||
}
|
||||
|
||||
private ZipArchive createDownloadFile(String owner, String deviceName)
|
||||
throws DeviceManagementException, JWTClientException, DeviceControllerException, APIManagerException,
|
||||
UserStoreException {
|
||||
if (owner == null) {
|
||||
throw new IllegalArgumentException("Error on createDownloadFile() Owner is null!");
|
||||
}
|
||||
//create new device id
|
||||
String deviceId = shortUUID();
|
||||
String applicationUsername =
|
||||
PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration()
|
||||
.getAdminUserName();
|
||||
if (apiApplicationKey == null) {
|
||||
APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService();
|
||||
String[] tags = {DeviceTypeConstants.DEVICE_TYPE};
|
||||
apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(
|
||||
DeviceTypeConstants.DEVICE_TYPE, tags, KEY_TYPE, applicationUsername, true);
|
||||
}
|
||||
JWTClient jwtClient = APIUtil.getJWTClientManagerService().getJWTClient();
|
||||
String scopes = "device_type_" + DeviceTypeConstants.DEVICE_TYPE + " device_" + deviceId;
|
||||
AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(apiApplicationKey.getConsumerKey(),
|
||||
apiApplicationKey.getConsumerSecret(), owner,
|
||||
scopes);
|
||||
//create token
|
||||
String accessToken = accessTokenInfo.getAccessToken();
|
||||
String refreshToken = accessTokenInfo.getRefreshToken();
|
||||
//Register the device with CDMF
|
||||
boolean 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(),
|
||||
DeviceTypeConstants.DEVICE_TYPE, deviceId,
|
||||
deviceName, accessToken, refreshToken);
|
||||
zipFile.setDeviceId(deviceId);
|
||||
return zipFile;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generate UUID
|
||||
*
|
||||
@ -92,137 +247,4 @@ public class FireAlarmManagerServiceImpl implements FireAlarmManagerService{
|
||||
}
|
||||
}
|
||||
|
||||
public Response removeDevice(String deviceId) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(DeviceTypeConstants.DEVICE_TYPE);
|
||||
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) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
}
|
||||
|
||||
public Response updateDevice(String deviceId, String name) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(DeviceTypeConstants.DEVICE_TYPE);
|
||||
Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||
device.setDeviceIdentifier(deviceId);
|
||||
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
||||
device.setName(name);
|
||||
device.setType(DeviceTypeConstants.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) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
}
|
||||
|
||||
public Response getDevice(String deviceId) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(DeviceTypeConstants.DEVICE_TYPE);
|
||||
Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||
return Response.ok().entity(device).build();
|
||||
} catch (DeviceManagementException e) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
}
|
||||
|
||||
public Response downloadSketch(String deviceName) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName);
|
||||
Response.ResponseBuilder response = Response.ok(FileUtils.readFileToByteArray(zipFile.getZipFile()));
|
||||
response.type("application/zip");
|
||||
response.header("Content-Disposition", "attachment; filename=\"" + zipFile.getFileName() + "\"");
|
||||
return response.build();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
return Response.status(400).entity(ex.getMessage()).build();//bad request
|
||||
} catch (DeviceManagementException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (JWTClientException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (APIManagerException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (DeviceControllerException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (IOException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (UserStoreException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
}
|
||||
}
|
||||
|
||||
public Response generateSketchLink(String deviceName) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName);
|
||||
Response.ResponseBuilder rb = Response.ok(zipFile.getDeviceId());
|
||||
return rb.build();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
return Response.status(400).entity(ex.getMessage()).build();//bad request
|
||||
} catch (DeviceManagementException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (JWTClientException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (APIManagerException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (DeviceControllerException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (UserStoreException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
}
|
||||
}
|
||||
|
||||
private ZipArchive createDownloadFile(String owner, String deviceName)
|
||||
throws DeviceManagementException, JWTClientException, DeviceControllerException, APIManagerException,
|
||||
UserStoreException {
|
||||
if (owner == null) {
|
||||
throw new IllegalArgumentException("Error on createDownloadFile() Owner is null!");
|
||||
}
|
||||
//create new device id
|
||||
String deviceId = shortUUID();
|
||||
String applicationUsername =
|
||||
PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration()
|
||||
.getAdminUserName();
|
||||
if (apiApplicationKey == null) {
|
||||
APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService();
|
||||
String[] tags = {DeviceTypeConstants.DEVICE_TYPE};
|
||||
apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(
|
||||
DeviceTypeConstants.DEVICE_TYPE, tags, KEY_TYPE, applicationUsername, true);
|
||||
}
|
||||
JWTClient jwtClient = JWTClientManager.getInstance().getJWTClient();
|
||||
String scopes = "device_type_" + DeviceTypeConstants.DEVICE_TYPE + " device_" + deviceId;
|
||||
AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(apiApplicationKey.getConsumerKey(),
|
||||
apiApplicationKey.getConsumerSecret(), owner,
|
||||
scopes);
|
||||
//create token
|
||||
String accessToken = accessTokenInfo.getAccess_token();
|
||||
String refreshToken = accessTokenInfo.getRefresh_token();
|
||||
//Register the device with CDMF
|
||||
boolean 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(),
|
||||
DeviceTypeConstants.DEVICE_TYPE, deviceId,
|
||||
deviceName, accessToken, refreshToken);
|
||||
zipFile.setDeviceId(deviceId);
|
||||
return zipFile;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -27,7 +27,6 @@ import org.homeautomation.firealarm.api.util.ServiceUtils;
|
||||
import org.homeautomation.firealarm.plugin.constants.DeviceTypeConstants;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||
import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig;
|
||||
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager;
|
||||
import org.wso2.carbon.device.mgt.iot.transport.TransportHandlerException;
|
||||
import org.wso2.carbon.device.mgt.iot.transport.mqtt.MQTTTransportHandler;
|
||||
|
||||
@ -110,12 +109,6 @@ public class MQTTConnector extends MQTTTransportHandler {
|
||||
log.debug("Received MQTT message for: [OWNER-" + owner + "] & [DEVICE.ID-" + deviceId + "]");
|
||||
}
|
||||
if (messageData.length == 4) {
|
||||
SensorDataManager.getInstance().setSensorRecord(deviceId, DeviceTypeConstants.SENSOR_TEMPERATURE,
|
||||
messageData[1], Calendar.getInstance().getTimeInMillis());
|
||||
SensorDataManager.getInstance().setSensorRecord(deviceId, DeviceTypeConstants.SENSOR_HUMIDITY,
|
||||
messageData[3], Calendar.getInstance().getTimeInMillis());
|
||||
|
||||
|
||||
if (!ServiceUtils.publishTemperatureToDAS(owner, deviceId, messageData[1])) {
|
||||
log.error("MQTT Subscriber: Publishing data to DAS failed.");
|
||||
}
|
||||
|
||||
@ -21,6 +21,7 @@ package org.homeautomation.firealarm.api.util;
|
||||
import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
|
||||
|
||||
/**
|
||||
* This class provides utility functions used by REST-API.
|
||||
@ -64,4 +65,16 @@ public class APIUtil {
|
||||
}
|
||||
return apiManagementProviderService;
|
||||
}
|
||||
|
||||
public static JWTClientManagerService getJWTClientManagerService() {
|
||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
JWTClientManagerService jwtClientManagerService =
|
||||
(JWTClientManagerService) ctx.getOSGiService(JWTClientManagerService.class, null);
|
||||
if (jwtClientManagerService == null) {
|
||||
String msg = "JWT Client manager service has not initialized.";
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
return jwtClientManagerService;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ package org.homeautomation.firealarm.api.util;
|
||||
import org.homeautomation.firealarm.plugin.constants.DeviceTypeConstants;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException;
|
||||
import org.wso2.carbon.device.mgt.analytics.data.publisher.service.DeviceAnalyticsService;
|
||||
import org.wso2.carbon.device.mgt.analytics.data.publisher.service.EventsPublisherService;
|
||||
|
||||
public class ServiceUtils {
|
||||
|
||||
@ -67,8 +67,8 @@ public class ServiceUtils {
|
||||
if (ctx.getTenantDomain(true) == null) {
|
||||
ctx.setTenantDomain("carbon.super", true);
|
||||
}
|
||||
DeviceAnalyticsService deviceAnalyticsService = (DeviceAnalyticsService) ctx.getOSGiService(
|
||||
DeviceAnalyticsService.class, null);
|
||||
EventsPublisherService deviceAnalyticsService = (EventsPublisherService) ctx.getOSGiService(
|
||||
EventsPublisherService.class, null);
|
||||
Object metaData[] = {owner, DeviceTypeConstants.DEVICE_TYPE, deviceId, System.currentTimeMillis()};
|
||||
try {
|
||||
deviceAnalyticsService.publishEvent(definition, SENSOR_STREAM_VERSION, metaData,
|
||||
|
||||
@ -23,13 +23,10 @@
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
|
||||
version="2.5">
|
||||
<display-name>Sample-Webapp-Controller</display-name>
|
||||
|
||||
<servlet>
|
||||
<description>JAX-WS/JAX-RS Endpoint</description>
|
||||
<display-name>JAX-WS/JAX-RS Servlet</display-name>
|
||||
<servlet-name>CXFServlet</servlet-name>
|
||||
<servlet-class>
|
||||
org.apache.cxf.transport.servlet.CXFServlet
|
||||
</servlet-class>
|
||||
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
@ -42,8 +39,17 @@
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>doAuthentication</param-name>
|
||||
<param-value>false</param-value>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>isSharedWithAllTenants</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>providerTenantDomain</param-name>
|
||||
<param-value>carbon.super</param-value>
|
||||
</context-param>
|
||||
|
||||
<!--publish to apim-->
|
||||
<context-param>
|
||||
<param-name>managed-api-enabled</param-name>
|
||||
@ -53,16 +59,5 @@
|
||||
<param-name>managed-api-owner</param-name>
|
||||
<param-value>admin</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-context-template</param-name>
|
||||
<param-value>/firealarm/{version}</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-application</param-name>
|
||||
<param-value>firealarm</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-isSecured</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
|
||||
</web-app>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user