mirror of
https://repository.entgra.net/community/device-mgt-plugins.git
synced 2025-09-16 23:42:15 +00:00
Merge pull request #229 from NuwanSameera/master
Remove monitor operations from controller APIs.
This commit is contained in:
commit
c609ef1c9b
@ -80,3 +80,4 @@ public interface AndroidSenseControllerService {
|
||||
@QueryParam("from") long from, @QueryParam("to") long to);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -24,18 +24,11 @@ import org.wso2.carbon.analytics.dataservice.commons.SORT;
|
||||
import org.wso2.carbon.analytics.dataservice.commons.SortByField;
|
||||
import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException;
|
||||
import org.wso2.carbon.device.mgt.analytics.data.publisher.service.EventsPublisherService;
|
||||
import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.Feature;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.transport.AndroidSenseMQTTConnector;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util.APIUtil;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util.DeviceData;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util.SensorData;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util.SensorRecord;
|
||||
import org.wso2.carbon.device.mgt.iot.androidsense.plugin.constants.AndroidSenseConstants;
|
||||
import org.wso2.carbon.device.mgt.iot.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.service.IoTServerStartupListener;
|
||||
import org.wso2.carbon.device.mgt.iot.transport.TransportHandlerException;
|
||||
|
||||
|
||||
@ -50,14 +50,6 @@ public interface ArduinoControllerService {
|
||||
Response switchBulb(@PathParam("deviceId") String deviceId, @QueryParam("protocol") String protocol,
|
||||
@FormParam("state") String state);
|
||||
|
||||
@Path("device/{deviceId}/temperature")
|
||||
@GET
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Feature(code = "temperature", name = "Temperature", type = "monitor", description = "Request temperature reading " +
|
||||
"from Arduino agent")
|
||||
Response requestTemperature(@PathParam("deviceId") String deviceId, @QueryParam("protocol") String protocol);
|
||||
|
||||
@Path("device/sensor")
|
||||
@POST
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
|
||||
@ -29,12 +29,19 @@ import org.wso2.carbon.device.mgt.iot.arduino.service.impl.dto.SensorRecord;
|
||||
import org.wso2.carbon.device.mgt.iot.arduino.service.impl.util.APIUtil;
|
||||
import org.wso2.carbon.device.mgt.iot.arduino.service.impl.util.ArduinoServiceUtils;
|
||||
import org.wso2.carbon.device.mgt.iot.arduino.plugin.constants.ArduinoConstants;
|
||||
import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException;
|
||||
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.FormParam;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@ -49,7 +56,10 @@ public class ArduinoControllerServiceImpl implements ArduinoControllerService {
|
||||
private ConcurrentHashMap<String, String> deviceToIpMap = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public Response registerDeviceIP(String deviceId, String deviceIP, String devicePort, HttpServletRequest request) {
|
||||
@Path("device/register/{deviceId}/{ip}/{port}")
|
||||
@POST
|
||||
public Response registerDeviceIP(@PathParam("deviceId") String deviceId, @PathParam("ip") String deviceIP,
|
||||
@PathParam("port") String devicePort, @Context HttpServletRequest request) {
|
||||
String result;
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Got register call from IP: " + deviceIP + " for Device ID: " + deviceId + " of owner: ");
|
||||
@ -64,7 +74,10 @@ public class ArduinoControllerServiceImpl implements ArduinoControllerService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response switchBulb(String deviceId, String protocol, String state) {
|
||||
@Path("device/{deviceId}/bulb")
|
||||
@POST
|
||||
public Response switchBulb(@PathParam("deviceId") String deviceId, @QueryParam("protocol") String protocol,
|
||||
@FormParam("state") String state) {
|
||||
|
||||
LinkedList<String> deviceControlList = internalControlsQueue.get(deviceId);
|
||||
String operation = "BULB:" + state.toUpperCase();
|
||||
@ -80,24 +93,13 @@ public class ArduinoControllerServiceImpl implements ArduinoControllerService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response requestTemperature(String deviceId, String protocol) {
|
||||
try {
|
||||
org.wso2.carbon.device.mgt.iot.sensormgt.SensorRecord sensorRecord =
|
||||
SensorDataManager.getInstance().getSensorRecord(deviceId, ArduinoConstants.SENSOR_TEMPERATURE);
|
||||
return Response.status(Response.Status.OK.getStatusCode()).entity(sensorRecord).build();
|
||||
} catch (DeviceControllerException e) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Path("device/sensor")
|
||||
@POST
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public Response pushData(DeviceData dataMsg) {
|
||||
String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
|
||||
String deviceId = dataMsg.deviceId;
|
||||
float pinData = dataMsg.value;
|
||||
SensorDataManager.getInstance().setSensorRecord(deviceId, ArduinoConstants.SENSOR_TEMPERATURE,
|
||||
String.valueOf(pinData),
|
||||
Calendar.getInstance().getTimeInMillis());
|
||||
if (!ArduinoServiceUtils.publishToDAS(dataMsg.deviceId, dataMsg.value)) {
|
||||
log.warn("An error occured whilst trying to publish pin data of Arduino with ID [" +
|
||||
deviceId + "] of owner [" + owner + "]");
|
||||
@ -107,7 +109,9 @@ public class ArduinoControllerServiceImpl implements ArduinoControllerService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response readControls(String deviceId, String protocol) {
|
||||
@Path("device/{deviceId}/controls")
|
||||
@GET
|
||||
public Response readControls(@PathParam("deviceId") String deviceId, @QueryParam("protocol") String protocol) {
|
||||
String result;
|
||||
LinkedList<String> deviceControlList = internalControlsQueue.get(deviceId);
|
||||
String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
|
||||
@ -135,13 +139,13 @@ public class ArduinoControllerServiceImpl implements ArduinoControllerService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response pushTemperatureData(final DeviceData dataMsg, HttpServletRequest request) {
|
||||
@Path("device/temperature")
|
||||
@POST
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public Response pushTemperatureData(final DeviceData dataMsg, @Context HttpServletRequest request) {
|
||||
String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
|
||||
String deviceId = dataMsg.deviceId;
|
||||
float temperature = dataMsg.value;
|
||||
SensorDataManager.getInstance().setSensorRecord(deviceId, ArduinoConstants.SENSOR_TEMPERATURE,
|
||||
String.valueOf(temperature),
|
||||
Calendar.getInstance().getTimeInMillis());
|
||||
if (!ArduinoServiceUtils.publishToDAS(dataMsg.deviceId, dataMsg.value)) {
|
||||
log.warn("An error occured whilst trying to publish temperature data of Arduino with ID [" + deviceId +
|
||||
"] of owner [" + owner + "]");
|
||||
@ -151,7 +155,12 @@ public class ArduinoControllerServiceImpl implements ArduinoControllerService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response getArduinoTemperatureStats(String deviceId, long from, long to) {
|
||||
@Path("device/stats/{deviceId}/sensors/temperature")
|
||||
@GET
|
||||
@Consumes("application/json")
|
||||
@Produces("application/json")
|
||||
public Response getArduinoTemperatureStats(@PathParam("deviceId") String deviceId, @QueryParam("from") long from,
|
||||
@QueryParam("to") long to) {
|
||||
String fromDate = String.valueOf(from);
|
||||
String toDate = String.valueOf(to);
|
||||
String query = "deviceId:" + deviceId + " AND deviceType:" +
|
||||
|
||||
@ -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 = "arduino_mgt", version = "1.0.0", context = "/arduino_mgt", tags = {"arduino"})
|
||||
@DeviceType(value = "arduino")
|
||||
public interface ArduinoManagerService {
|
||||
|
||||
@ -18,7 +18,6 @@
|
||||
|
||||
package org.wso2.carbon.device.mgt.iot.arduino.service.impl;
|
||||
|
||||
import org.wso2.carbon.apimgt.annotations.api.API;
|
||||
import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService;
|
||||
import org.wso2.carbon.apimgt.application.extension.dto.ApiApplicationKey;
|
||||
import org.wso2.carbon.apimgt.application.extension.exception.APIManagerException;
|
||||
@ -27,7 +26,6 @@ import org.wso2.carbon.device.mgt.common.Device;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||
import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType;
|
||||
import org.wso2.carbon.device.mgt.iot.arduino.service.impl.util.APIUtil;
|
||||
import org.wso2.carbon.device.mgt.iot.arduino.plugin.constants.ArduinoConstants;
|
||||
import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException;
|
||||
@ -37,6 +35,16 @@ import org.wso2.carbon.identity.jwt.client.extension.JWTClient;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException;
|
||||
import org.wso2.carbon.user.api.UserStoreException;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@ -45,13 +53,16 @@ import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Path("enrollment")
|
||||
public class ArduinoManagerServiceImpl implements ArduinoManagerService {
|
||||
|
||||
private static final String KEY_TYPE = "PRODUCTION";
|
||||
private static ApiApplicationKey apiApplicationKey;
|
||||
|
||||
@Override
|
||||
public Response removeDevice(String deviceId) {
|
||||
@Path("devices/{device_id}")
|
||||
@DELETE
|
||||
public Response removeDevice(@PathParam("device_id") String deviceId) {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE);
|
||||
@ -68,7 +79,9 @@ public class ArduinoManagerServiceImpl implements ArduinoManagerService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response updateDevice(String deviceId, String name) {
|
||||
@Path("devices/{device_id}")
|
||||
@PUT
|
||||
public Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name) {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE);
|
||||
@ -90,7 +103,11 @@ public class ArduinoManagerServiceImpl implements ArduinoManagerService {
|
||||
}
|
||||
|
||||
@Override
|
||||
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) {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(ArduinoConstants.DEVICE_TYPE);
|
||||
@ -103,6 +120,10 @@ public class ArduinoManagerServiceImpl implements ArduinoManagerService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Path("devices")
|
||||
@GET
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response getArduinoDevices() {
|
||||
try {
|
||||
List<Device> userDevices = APIUtil.getDeviceManagementService().getDevicesOfUser(
|
||||
@ -122,7 +143,10 @@ public class ArduinoManagerServiceImpl implements ArduinoManagerService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response downloadSketch(String customDeviceName) {
|
||||
@Path("devices/download")
|
||||
@GET
|
||||
@Produces("application/octet-stream")
|
||||
public Response downloadSketch(@QueryParam("deviceName") String customDeviceName) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), customDeviceName);
|
||||
Response.ResponseBuilder rb = Response.ok(zipFile.getZipFile());
|
||||
@ -144,7 +168,9 @@ public class ArduinoManagerServiceImpl implements ArduinoManagerService {
|
||||
}
|
||||
|
||||
@Override
|
||||
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());
|
||||
|
||||
@ -7,56 +7,40 @@
|
||||
<display-name>Arduino</display-name>
|
||||
<description>Arduino</description>
|
||||
|
||||
<servlet>
|
||||
<servlet-name>CXFServlet</servlet-name>
|
||||
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet>
|
||||
<servlet-name>CXFServlet</servlet-name>
|
||||
<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>
|
||||
<param-name>managed-api-enabled</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-owner</param-name>
|
||||
<param-value>admin</param-value>
|
||||
</context-param>
|
||||
|
||||
<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>
|
||||
</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>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<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>/arduino/{version}</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-application</param-name>
|
||||
<param-value>arduino</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-isSecured</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
|
||||
|
||||
|
||||
</web-app>
|
||||
</web-app>
|
||||
@ -21,11 +21,11 @@ package org.wso2.carbon.device.mgt.iot.digitaldisplay.service.impl;
|
||||
import org.wso2.carbon.apimgt.annotations.api.API;
|
||||
import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType;
|
||||
import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.Feature;
|
||||
import javax.websocket.server.PathParam;
|
||||
import javax.ws.rs.FormParam;
|
||||
import javax.ws.rs.HeaderParam;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
@API(name = "digital_display", version = "1.0.0", context = "/digital_display", tags = {"digital_display"})
|
||||
@ -54,7 +54,7 @@ public interface DigitalDisplayControllerService {
|
||||
@POST
|
||||
@Feature(code = "terminate-display", name = "Terminate Display", type = "operation",
|
||||
description = "Terminate all running process in Digital Display")
|
||||
Response terminateDisplay(@PathParam("deviceId") String deviceId, @HeaderParam("sessionId") String sessionId);
|
||||
Response terminateDisplay(@HeaderParam("sessionId") String sessionId, @PathParam("deviceId") String deviceId);
|
||||
|
||||
/**
|
||||
* Reboot running digital display
|
||||
|
||||
@ -20,10 +20,8 @@ package org.wso2.carbon.device.mgt.iot.digitaldisplay.service.impl;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.apimgt.annotations.api.API;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
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.extensions.feature.mgt.annotations.Feature;
|
||||
import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig;
|
||||
import org.wso2.carbon.device.mgt.iot.digitaldisplay.service.impl.exception.DigitalDisplayException;
|
||||
@ -32,13 +30,11 @@ import org.wso2.carbon.device.mgt.iot.digitaldisplay.plugin.constants.DigitalDis
|
||||
import org.wso2.carbon.device.mgt.iot.service.IoTServerStartupListener;
|
||||
import org.wso2.carbon.device.mgt.iot.transport.TransportHandlerException;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.websocket.server.PathParam;
|
||||
import javax.ws.rs.FormParam;
|
||||
import javax.ws.rs.HeaderParam;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
public class DigitalDisplayControllerServiceImpl implements DigitalDisplayControllerService {
|
||||
@ -46,51 +42,9 @@ public class DigitalDisplayControllerServiceImpl implements DigitalDisplayContro
|
||||
private static Log log = LogFactory.getLog(DigitalDisplayControllerServiceImpl.class);
|
||||
private static DigitalDisplayMQTTConnector digitalDisplayMQTTConnector;
|
||||
|
||||
private boolean waitForServerStartup() {
|
||||
while (!IoTServerStartupListener.isServerReady()) {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public DigitalDisplayMQTTConnector getDigitalDisplayMQTTConnector() {
|
||||
return DigitalDisplayControllerServiceImpl.digitalDisplayMQTTConnector;
|
||||
}
|
||||
|
||||
public void setDigitalDisplayMQTTConnector(final
|
||||
DigitalDisplayMQTTConnector digitalDisplayMQTTConnector) {
|
||||
|
||||
Runnable connector = new Runnable() {
|
||||
public void run() {
|
||||
if (waitForServerStartup()) {
|
||||
return;
|
||||
}
|
||||
DigitalDisplayControllerServiceImpl.digitalDisplayMQTTConnector = digitalDisplayMQTTConnector;
|
||||
//The delay is added for the server starts up.
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
if (MqttConfig.getInstance().isEnabled()) {
|
||||
synchronized (digitalDisplayMQTTConnector) {
|
||||
digitalDisplayMQTTConnector.connect();
|
||||
}
|
||||
} else {
|
||||
log.warn("MQTT disabled in 'devicemgt-config.xml'. Hence, not started.");
|
||||
}
|
||||
}
|
||||
};
|
||||
Thread connectorThread = new Thread(connector);
|
||||
connectorThread.setDaemon(true);
|
||||
connectorThread.start();
|
||||
}
|
||||
|
||||
public Response restartBrowser(String deviceId, String sessionId) {
|
||||
@Path("device/{deviceId}/restart-browser")
|
||||
@POST
|
||||
public Response restartBrowser(@PathParam("deviceId") String deviceId, @HeaderParam("sessionId") String sessionId) {
|
||||
try {
|
||||
sendCommandViaMQTT(deviceId, sessionId + "::" + DigitalDisplayConstants.RESTART_BROWSER_CONSTANT + "::", "");
|
||||
return Response.ok().build();
|
||||
@ -101,7 +55,10 @@ public class DigitalDisplayControllerServiceImpl implements DigitalDisplayContro
|
||||
}
|
||||
}
|
||||
|
||||
public Response terminateDisplay(String deviceId, String sessionId) {
|
||||
@Path("device/{deviceId}/terminate-display")
|
||||
@POST
|
||||
public Response terminateDisplay(@HeaderParam("sessionId") String sessionId,
|
||||
@PathParam("deviceId") String deviceId) {
|
||||
try {
|
||||
sendCommandViaMQTT(deviceId, sessionId + "::" + DigitalDisplayConstants.TERMINATE_DISPLAY_CONSTANT + "::", "");
|
||||
return Response.ok().build();
|
||||
@ -110,10 +67,11 @@ public class DigitalDisplayControllerServiceImpl implements DigitalDisplayContro
|
||||
} catch (DigitalDisplayException e) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Response restartDisplay(String deviceId, String sessionId) {
|
||||
@Path("device/{deviceId}/restart-display")
|
||||
@POST
|
||||
public Response restartDisplay(@PathParam("deviceId") String deviceId, @HeaderParam("sessionId") String sessionId) {
|
||||
try {
|
||||
sendCommandViaMQTT(deviceId, sessionId + "::" + DigitalDisplayConstants.RESTART_DISPLAY_CONSTANT + "::", "");
|
||||
return Response.ok().build();
|
||||
@ -124,7 +82,11 @@ public class DigitalDisplayControllerServiceImpl implements DigitalDisplayContro
|
||||
}
|
||||
}
|
||||
|
||||
public Response editSequence(String deviceId, String name, String attribute, String newValue, String sessionId) {
|
||||
@Path("device/{deviceId}/edit-sequence")
|
||||
@POST
|
||||
public Response editSequence(@PathParam("deviceId") String deviceId, @FormParam("name") String name,
|
||||
@FormParam("attribute") String attribute, @FormParam("new-value") String newValue,
|
||||
@HeaderParam("sessionId") String sessionId) {
|
||||
try {
|
||||
String params = name + "|" + attribute + "|" + newValue;
|
||||
sendCommandViaMQTT(deviceId, sessionId + "::" + DigitalDisplayConstants.EDIT_SEQUENCE_CONSTANT + "::", params);
|
||||
@ -136,7 +98,12 @@ public class DigitalDisplayControllerServiceImpl implements DigitalDisplayContro
|
||||
}
|
||||
}
|
||||
|
||||
public Response uploadContent(String deviceId, String remotePath, String screenName, String sessionId) {
|
||||
|
||||
@Path("device/{deviceId}/upload-content")
|
||||
@POST
|
||||
public Response uploadContent(@PathParam("deviceId") String deviceId, @FormParam("remote-path") String remotePath,
|
||||
@FormParam("screen-name") String screenName,
|
||||
@HeaderParam("sessionId") String sessionId) {
|
||||
try {
|
||||
String params = remotePath + "|" + screenName;
|
||||
sendCommandViaMQTT(deviceId, sessionId + "::" + DigitalDisplayConstants.UPLOAD_CONTENT_CONSTANT + "::",
|
||||
@ -149,8 +116,12 @@ public class DigitalDisplayControllerServiceImpl implements DigitalDisplayContro
|
||||
}
|
||||
}
|
||||
|
||||
public Response addNewResource(String deviceId, String type, String time, String path, String name, String position,
|
||||
String sessionId) {
|
||||
@Path("device/{deviceId}/add-resource")
|
||||
@POST
|
||||
public Response addNewResource(@PathParam("deviceId") String deviceId, @FormParam("type") String type,
|
||||
@FormParam("time") String time, @FormParam("path") String path,
|
||||
@FormParam("name") String name, @FormParam("position") String position,
|
||||
@HeaderParam("sessionId") String sessionId) {
|
||||
String params;
|
||||
try {
|
||||
if (position.isEmpty()) {
|
||||
@ -168,7 +139,10 @@ public class DigitalDisplayControllerServiceImpl implements DigitalDisplayContro
|
||||
}
|
||||
}
|
||||
|
||||
public Response removeResource(String deviceId, String name, String sessionId) {
|
||||
@Path("device/{deviceId}/remove-resource")
|
||||
@POST
|
||||
public Response removeResource(@PathParam("deviceId") String deviceId, @FormParam("name") String name,
|
||||
@HeaderParam("sessionId") String sessionId) {
|
||||
try {
|
||||
sendCommandViaMQTT(deviceId, sessionId + "::" + DigitalDisplayConstants.REMOVE_RESOURCE_CONSTANT + "::", name);
|
||||
return Response.ok().build();
|
||||
@ -179,7 +153,9 @@ public class DigitalDisplayControllerServiceImpl implements DigitalDisplayContro
|
||||
}
|
||||
}
|
||||
|
||||
public Response restartServer(String deviceId, String sessionId) {
|
||||
@Path("device/{deviceId}/restart-server")
|
||||
@POST
|
||||
public Response restartServer(@PathParam("deviceId") String deviceId, @HeaderParam("sessionId") String sessionId) {
|
||||
try {
|
||||
sendCommandViaMQTT(deviceId, sessionId + "::" + DigitalDisplayConstants.RESTART_SERVER_CONSTANT + "::", "");
|
||||
return Response.ok().build();
|
||||
@ -190,7 +166,10 @@ public class DigitalDisplayControllerServiceImpl implements DigitalDisplayContro
|
||||
}
|
||||
}
|
||||
|
||||
public Response showScreenshot(String deviceId, String sessionId) {
|
||||
|
||||
@Path("device/{deviceId}/screenshot")
|
||||
@POST
|
||||
public Response showScreenshot(@PathParam("deviceId") String deviceId, @HeaderParam("sessionId") String sessionId) {
|
||||
try {
|
||||
sendCommandViaMQTT(deviceId, sessionId + "::" + DigitalDisplayConstants.SCREENSHOT_CONSTANT + "::", "");
|
||||
return Response.ok().build();
|
||||
@ -201,7 +180,10 @@ public class DigitalDisplayControllerServiceImpl implements DigitalDisplayContro
|
||||
}
|
||||
}
|
||||
|
||||
public Response getDevicestatus(String deviceId, String sessionId) {
|
||||
@Path("device/{deviceId}/get-device-status")
|
||||
@POST
|
||||
public Response getDevicestatus(@PathParam("deviceId") String deviceId,
|
||||
@HeaderParam("sessionId") String sessionId) {
|
||||
try {
|
||||
sendCommandViaMQTT(deviceId, sessionId + "::" + DigitalDisplayConstants.GET_DEVICE_STATUS_CONSTANT + "::", "");
|
||||
return Response.ok().build();
|
||||
@ -212,7 +194,9 @@ public class DigitalDisplayControllerServiceImpl implements DigitalDisplayContro
|
||||
}
|
||||
}
|
||||
|
||||
public Response getResources(String deviceId, String sessionId) {
|
||||
@Path("device/{deviceId}/get-content-list")
|
||||
@POST
|
||||
public Response getResources(@PathParam("deviceId") String deviceId, @HeaderParam("sessionId") String sessionId) {
|
||||
try {
|
||||
sendCommandViaMQTT(deviceId, sessionId + "::" + DigitalDisplayConstants.GET_CONTENTLIST_CONSTANT + "::", "");
|
||||
return Response.ok().build();
|
||||
@ -246,4 +230,41 @@ public class DigitalDisplayControllerServiceImpl implements DigitalDisplayContro
|
||||
}
|
||||
}
|
||||
|
||||
private boolean waitForServerStartup() {
|
||||
while (!IoTServerStartupListener.isServerReady()) {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public DigitalDisplayMQTTConnector getDigitalDisplayMQTTConnector() {
|
||||
return DigitalDisplayControllerServiceImpl.digitalDisplayMQTTConnector;
|
||||
}
|
||||
|
||||
public void setDigitalDisplayMQTTConnector(final
|
||||
DigitalDisplayMQTTConnector digitalDisplayMQTTConnector) {
|
||||
|
||||
Runnable connector = new Runnable() {
|
||||
public void run() {
|
||||
if (waitForServerStartup()) {
|
||||
return;
|
||||
}
|
||||
DigitalDisplayControllerServiceImpl.digitalDisplayMQTTConnector = digitalDisplayMQTTConnector;
|
||||
if (MqttConfig.getInstance().isEnabled()) {
|
||||
digitalDisplayMQTTConnector.connect();
|
||||
} else {
|
||||
log.warn("MQTT disabled in 'devicemgt-config.xml'. " +
|
||||
"Hence, DigitalDisplayMQTTConnector not started.");
|
||||
}
|
||||
}
|
||||
};
|
||||
Thread connectorThread = new Thread(connector);
|
||||
connectorThread.setDaemon(true);
|
||||
connectorThread.start();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -24,7 +24,6 @@ import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
@ -33,6 +32,7 @@ import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
@Path("enrollment")
|
||||
@API(name = "digital_display_mgt", version = "1.0.0", context = "/digital_display_mgt", tags = {"digital_display"})
|
||||
@DeviceType(value = "digital_display")
|
||||
public interface DigitalDisplayManagerService {
|
||||
|
||||
@ -39,6 +39,15 @@ 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;
|
||||
@ -46,12 +55,97 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
@Path("enrollment")
|
||||
public class DigitalDisplayManagerServiceImpl implements DigitalDisplayManagerService {
|
||||
|
||||
private static Log log = LogFactory.getLog(DigitalDisplayManagerServiceImpl.class);
|
||||
private static final String KEY_TYPE = "PRODUCTION";
|
||||
private static ApiApplicationKey apiApplicationKey;
|
||||
|
||||
@Path("devices/{device_id}")
|
||||
@DELETE
|
||||
public Response removeDevice(@PathParam("device_id") String deviceId) {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(DigitalDisplayConstants.DEVICE_TYPE);
|
||||
try {
|
||||
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(DigitalDisplayConstants.DEVICE_TYPE);
|
||||
try {
|
||||
Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||
device.setDeviceIdentifier(deviceId);
|
||||
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
||||
device.setName(name);
|
||||
device.setType(DigitalDisplayConstants.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) {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(DigitalDisplayConstants.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/download")
|
||||
@GET
|
||||
@Produces("application/octet-stream")
|
||||
public Response downloadSketch(@QueryParam("deviceName") String customDeviceName) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), customDeviceName);
|
||||
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 (DeviceControllerException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (APIManagerException 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();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean register(String deviceId, String name) {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
@ -77,81 +171,6 @@ public class DigitalDisplayManagerServiceImpl implements DigitalDisplayManagerSe
|
||||
}
|
||||
}
|
||||
|
||||
public Response removeDevice(String deviceId) {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(DigitalDisplayConstants.DEVICE_TYPE);
|
||||
try {
|
||||
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(DigitalDisplayConstants.DEVICE_TYPE);
|
||||
try {
|
||||
Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||
device.setDeviceIdentifier(deviceId);
|
||||
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
||||
device.setName(name);
|
||||
device.setType(DigitalDisplayConstants.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) {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(DigitalDisplayConstants.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) {
|
||||
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 (DeviceControllerException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (APIManagerException 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();
|
||||
}
|
||||
}
|
||||
|
||||
private ZipArchive createDownloadFile(String owner, String deviceName)
|
||||
throws DeviceManagementException, JWTClientException, DeviceControllerException, APIManagerException,
|
||||
UserStoreException {
|
||||
|
||||
@ -24,7 +24,7 @@
|
||||
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
|
||||
|
||||
|
||||
<jaxrs:server id="DigitalDisplayController" address="/controller">
|
||||
<jaxrs:server id="DigitalDisplay" address="/">
|
||||
<jaxrs:serviceBeans>
|
||||
<bean id="DigitalDisplayManagerControllerService"
|
||||
class="org.wso2.carbon.device.mgt.iot.digitaldisplay.service.impl.DigitalDisplayControllerServiceImpl">
|
||||
|
||||
@ -21,28 +21,16 @@
|
||||
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>Digital-Display-Agent-Webapp</display-name>
|
||||
|
||||
<servlet>
|
||||
<description>JAX-WS/JAX-RS MDM Android 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>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>
|
||||
<context-param>
|
||||
<param-name>isAdminService</param-name>
|
||||
<param-value>false</param-value>
|
||||
@ -51,6 +39,14 @@
|
||||
<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>
|
||||
@ -61,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>/digital_display/{version}</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-application</param-name>
|
||||
<param-value>digital_display</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-isSecured</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
|
||||
</web-app>
|
||||
</web-app>
|
||||
@ -38,7 +38,10 @@ public class DroneControllerServiceImpl implements DroneControllerService {
|
||||
private ConcurrentHashMap<String, String> deviceToIpMap = new ConcurrentHashMap<>();
|
||||
private DroneController droneController = new DroneControllerImpl();
|
||||
|
||||
public Response registerDeviceIP(String deviceId, String deviceIP, String devicePort) {
|
||||
@Path("device/register/{deviceId}/{ip}/{port}")
|
||||
@POST
|
||||
public Response registerDeviceIP(@PathParam("deviceId") String deviceId, @PathParam("ip") String deviceIP,
|
||||
@PathParam("port") String devicePort) {
|
||||
String result;
|
||||
String deviceHttpEndpoint = deviceIP + ":" + devicePort;
|
||||
deviceToIpMap.put(deviceId, deviceHttpEndpoint);
|
||||
@ -49,7 +52,10 @@ public class DroneControllerServiceImpl implements DroneControllerService {
|
||||
return Response.ok(Response.Status.OK.getStatusCode()).build();
|
||||
}
|
||||
|
||||
public Response droneController(String deviceId, String action, String duration, String speed) {
|
||||
@Path("device/{deviceId}/send_command")
|
||||
@POST
|
||||
public Response droneController(@PathParam("deviceId") String deviceId, @FormParam("action") String action,
|
||||
@FormParam("duration") String duration, @FormParam("speed") String speed) {
|
||||
try {
|
||||
DroneAnalyzerServiceUtils.sendControlCommand(droneController, deviceId, action, Double.valueOf(speed),
|
||||
Double.valueOf(duration));
|
||||
|
||||
@ -31,6 +31,7 @@ import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
@Path("enrollment")
|
||||
@API(name = "drone_analyzer_mgt", version = "1.0.0", context = "/drone_analyzer_mgt", tags = {"drone_analyzer"})
|
||||
@DeviceType(value = "drone_analyzer")
|
||||
public interface DroneManagerService {
|
||||
|
||||
@ -39,6 +39,15 @@ import org.wso2.carbon.identity.jwt.client.extension.JWTClient;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo;
|
||||
import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException;
|
||||
import org.wso2.carbon.user.api.UserStoreException;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@ -47,39 +56,16 @@ import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Path("enrollment")
|
||||
public class DroneManagerServiceImpl implements DroneManagerService {
|
||||
|
||||
private static org.apache.commons.logging.Log log = LogFactory.getLog(DroneManagerServiceImpl.class);
|
||||
private static final String KEY_TYPE = "PRODUCTION";
|
||||
private static ApiApplicationKey apiApplicationKey;
|
||||
|
||||
private boolean register(String deviceId, String name) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(DroneConstants.DEVICE_TYPE);
|
||||
if (APIUtil.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
|
||||
return false;
|
||||
}
|
||||
Device device = new Device();
|
||||
device.setDeviceIdentifier(deviceId);
|
||||
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
|
||||
enrolmentInfo.setDateOfEnrolment(new Date().getTime());
|
||||
enrolmentInfo.setDateOfLastUpdate(new Date().getTime());
|
||||
enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE);
|
||||
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD);
|
||||
device.setName(name);
|
||||
device.setType(DroneConstants.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);
|
||||
@ -95,7 +81,10 @@ public class DroneManagerServiceImpl implements DroneManagerService {
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
@ -116,7 +105,11 @@ public class DroneManagerServiceImpl implements DroneManagerService {
|
||||
}
|
||||
}
|
||||
|
||||
public Response getDevice(String deviceId) {
|
||||
@Path("devices/{device_id}")
|
||||
@GET
|
||||
@Consumes("application/json")
|
||||
@Produces("application/json")
|
||||
public Response getDevice(@PathParam("device_id") String deviceId) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
@ -128,6 +121,10 @@ public class DroneManagerServiceImpl implements DroneManagerService {
|
||||
}
|
||||
}
|
||||
|
||||
@Path("devices")
|
||||
@GET
|
||||
@Consumes("application/json")
|
||||
@Produces("application/json")
|
||||
public Response getDroneDevices() {
|
||||
try {
|
||||
List<Device> userDevices = APIUtil.getDeviceManagementService().getDevicesOfUser(APIUtil.getAuthenticatedUser());
|
||||
@ -146,7 +143,11 @@ public class DroneManagerServiceImpl implements DroneManagerService {
|
||||
}
|
||||
}
|
||||
|
||||
public Response downloadSketch(String deviceName, String sketchType) {
|
||||
@Path("devices/{sketch_type}/download")
|
||||
@GET
|
||||
@Produces("application/octet-stream")
|
||||
public Response downloadSketch(@QueryParam("deviceName") String deviceName, @PathParam("sketch_type") String
|
||||
sketchType) {
|
||||
|
||||
//create new device id
|
||||
String deviceId = shortUUID();
|
||||
@ -172,10 +173,12 @@ public class DroneManagerServiceImpl implements DroneManagerService {
|
||||
Response.ResponseBuilder rb = Response.ok(zipFile.getZipFile());
|
||||
rb.header("Content-Disposition", "attachment; filename=\"" + zipFile.getFileName() + "\"");
|
||||
return rb.build();
|
||||
|
||||
}
|
||||
|
||||
public Response generateSketchLink(String deviceName, String sketchType) {
|
||||
@Path("devices/{sketch_type}/generate_link")
|
||||
@GET
|
||||
public Response generateSketchLink(@QueryParam("deviceName") String deviceName,
|
||||
@PathParam("sketch_type") String sketchType) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(deviceName, sketchType);
|
||||
Response.ResponseBuilder rb = Response.ok(zipFile.getDeviceId());
|
||||
@ -195,6 +198,32 @@ public class DroneManagerServiceImpl implements DroneManagerService {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean register(String deviceId, String name) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(DroneConstants.DEVICE_TYPE);
|
||||
if (APIUtil.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
|
||||
return false;
|
||||
}
|
||||
Device device = new Device();
|
||||
device.setDeviceIdentifier(deviceId);
|
||||
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
|
||||
enrolmentInfo.setDateOfEnrolment(new Date().getTime());
|
||||
enrolmentInfo.setDateOfLastUpdate(new Date().getTime());
|
||||
enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE);
|
||||
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD);
|
||||
device.setName(name);
|
||||
device.setType(DroneConstants.DEVICE_TYPE);
|
||||
enrolmentInfo.setOwner(APIUtil.getAuthenticatedUser());
|
||||
device.setEnrolmentInfo(enrolmentInfo);
|
||||
boolean added = APIUtil.getDeviceManagementService().enrollDevice(device);
|
||||
return added;
|
||||
} catch (DeviceManagementException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private ZipArchive createDownloadFile(String deviceName, String sketchType)
|
||||
throws DeviceManagementException, JWTClientException, APIManagerException, DeviceControllerException,
|
||||
UserStoreException {
|
||||
|
||||
@ -6,44 +6,41 @@
|
||||
metadata-complete="true">
|
||||
<display-name>WSO2 IoT Server</display-name>
|
||||
<description>WSO2 IoT Server</description>
|
||||
<servlet>
|
||||
<servlet-name>CXFServlet</servlet-name>
|
||||
<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>
|
||||
</context-param>
|
||||
<servlet>
|
||||
<servlet-name>CXFServlet</servlet-name>
|
||||
<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>
|
||||
<param-name>managed-api-enabled</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<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>/drone_analyzer/{version}</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-application</param-name>
|
||||
<param-value>drone_analyzer</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-isSecured</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
</web-app>
|
||||
<!--publish to apim-->
|
||||
<context-param>
|
||||
<param-name>managed-api-enabled</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-owner</param-name>
|
||||
<param-value>admin</param-value>
|
||||
</context-param>
|
||||
|
||||
</web-app>
|
||||
@ -51,14 +51,6 @@ public interface RaspberryPiControllerService {
|
||||
description = "Switch on/off Raspberry Pi agent's bulb. (On / Off)")
|
||||
Response switchBulb(@PathParam("deviceId") String deviceId, @FormParam("state") String state);
|
||||
|
||||
@Path("device/{deviceId}/readtemperature")
|
||||
@GET
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Feature(code = "readtemperature", name = "Temperature", type = "monitor",
|
||||
description = "Request temperature reading from Raspberry Pi agent")
|
||||
Response requestTemperature(@PathParam("deviceId") String deviceId);
|
||||
|
||||
@Path("device/push_temperature")
|
||||
@POST
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@ -71,7 +63,7 @@ public interface RaspberryPiControllerService {
|
||||
@GET
|
||||
@Consumes("application/json")
|
||||
@Produces("application/json")
|
||||
Response getArduinoTemperatureStats(@PathParam("deviceId") String deviceId, @QueryParam("username") String user,
|
||||
Response getRaspberryPiTemperatureStats(@PathParam("deviceId") String deviceId, @QueryParam("username") String user,
|
||||
@QueryParam("from") long from, @QueryParam("to") long to);
|
||||
|
||||
}
|
||||
|
||||
@ -25,18 +25,25 @@ import org.wso2.carbon.analytics.dataservice.commons.SortByField;
|
||||
import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException;
|
||||
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.raspberrypi.service.impl.dto.DeviceData;
|
||||
import org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl.dto.SensorRecord;
|
||||
import org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl.transport.RaspberryPiMQTTConnector;
|
||||
import org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl.util.APIUtil;
|
||||
import org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl.util.RaspberrypiServiceUtils;
|
||||
import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.constants.RaspberrypiConstants;
|
||||
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager;
|
||||
import org.wso2.carbon.device.mgt.iot.service.IoTServerStartupListener;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.FormParam;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
@ -49,6 +56,100 @@ public class RaspberryPiControllerServiceImpl implements RaspberryPiControllerSe
|
||||
private ConcurrentHashMap<String, String> deviceToIpMap = new ConcurrentHashMap<>();
|
||||
private RaspberryPiMQTTConnector raspberryPiMQTTConnector;
|
||||
|
||||
@Path("device/register/{deviceId}/{ip}/{port}")
|
||||
@POST
|
||||
public Response registerDeviceIP(@PathParam("deviceId") String deviceId, @PathParam("ip") String deviceIP,
|
||||
@PathParam("port") String devicePort, @Context HttpServletRequest request) {
|
||||
String result;
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Got register call from IP: " + deviceIP + " for Device ID: " + deviceId);
|
||||
}
|
||||
String deviceHttpEndpoint = deviceIP + ":" + devicePort;
|
||||
deviceToIpMap.put(deviceId, deviceHttpEndpoint);
|
||||
result = "Device-IP Registered";
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug(result);
|
||||
}
|
||||
return Response.ok().entity(result).build();
|
||||
}
|
||||
|
||||
@Path("device/{deviceId}/bulb")
|
||||
@POST
|
||||
public Response switchBulb(@PathParam("deviceId") String deviceId, @FormParam("state") String state) {
|
||||
String switchToState = state.toUpperCase();
|
||||
if (!switchToState.equals(RaspberrypiConstants.STATE_ON) && !switchToState.equals(
|
||||
RaspberrypiConstants.STATE_OFF)) {
|
||||
log.error("The requested state change shoud be either - 'ON' or 'OFF'");
|
||||
return Response.status(Response.Status.BAD_REQUEST.getStatusCode()).build();
|
||||
}
|
||||
String callUrlPattern = RaspberrypiConstants.BULB_CONTEXT + switchToState;
|
||||
try {
|
||||
String deviceHTTPEndpoint = deviceToIpMap.get(deviceId);
|
||||
if (deviceHTTPEndpoint == null) {
|
||||
return Response.status(Response.Status.PRECONDITION_FAILED.getStatusCode()).build();
|
||||
}
|
||||
RaspberrypiServiceUtils.sendCommandViaHTTP(deviceHTTPEndpoint, callUrlPattern, true);
|
||||
} catch (DeviceManagementException e) {
|
||||
log.error("Failed to send switch-bulb request to device [" + deviceId + "] via ");
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
@Path("device/push_temperature")
|
||||
@POST
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public Response pushTemperatureData(final DeviceData dataMsg, @Context HttpServletRequest request) {
|
||||
String owner = dataMsg.owner;
|
||||
String deviceId = dataMsg.deviceId;
|
||||
String deviceIp = dataMsg.reply;
|
||||
float temperature = dataMsg.value;
|
||||
String registeredIp = deviceToIpMap.get(deviceId);
|
||||
if (registeredIp == null) {
|
||||
log.warn("Unregistered IP: Temperature Data Received from an un-registered IP " + deviceIp +
|
||||
" for device ID - " + deviceId);
|
||||
return Response.status(Response.Status.PRECONDITION_FAILED.getStatusCode()).build();
|
||||
} else if (!registeredIp.equals(deviceIp)) {
|
||||
log.warn("Conflicting IP: Received IP is " + deviceIp + ". Device with ID " + deviceId +
|
||||
" is already registered under some other IP. Re-registration required");
|
||||
return Response.status(Response.Status.CONFLICT.getStatusCode()).build();
|
||||
}
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Received Pin Data Value: " + temperature + " degrees C");
|
||||
}
|
||||
if (!RaspberrypiServiceUtils.publishToDAS(dataMsg.deviceId, dataMsg.value)) {
|
||||
log.warn("An error occured whilst trying to publish temperature data of raspberrypi with ID [" +
|
||||
deviceId + "] of owner [" + owner + "]");
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
@Path("device/stats/{deviceId}/sensors/temperature")
|
||||
@GET
|
||||
@Consumes("application/json")
|
||||
@Produces("application/json")
|
||||
public Response getRaspberryPiTemperatureStats(@PathParam("deviceId") String deviceId,
|
||||
@QueryParam("username") String user,
|
||||
@QueryParam("from") long from, @QueryParam("to") long to) {
|
||||
String fromDate = String.valueOf(from);
|
||||
String toDate = String.valueOf(to);
|
||||
String query = "owner:" + user + " AND deviceId:" + deviceId + " AND deviceType:" +
|
||||
RaspberrypiConstants.DEVICE_TYPE + " AND time : [" + fromDate + " TO " + toDate + "]";
|
||||
String sensorTableName = RaspberrypiConstants.TEMPERATURE_EVENT_TABLE;
|
||||
try {
|
||||
List<SortByField> sortByFields = new ArrayList<>();
|
||||
SortByField sortByField = new SortByField("time", SORT.ASC, false);
|
||||
sortByFields.add(sortByField);
|
||||
List<SensorRecord> sensorRecords = APIUtil.getAllEventsForDevice(sensorTableName, query, sortByFields);
|
||||
return Response.status(Response.Status.OK.getStatusCode()).entity(sensorRecords).build();
|
||||
} catch (AnalyticsException e) {
|
||||
String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query;
|
||||
log.error(errorMsg);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(errorMsg).build();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean waitForServerStartup() {
|
||||
while (!IoTServerStartupListener.isServerReady()) {
|
||||
try {
|
||||
@ -72,18 +173,10 @@ public class RaspberryPiControllerServiceImpl implements RaspberryPiControllerSe
|
||||
return;
|
||||
}
|
||||
RaspberryPiControllerServiceImpl.this.raspberryPiMQTTConnector = raspberryPiMQTTConnector;
|
||||
//The delay is added for the server starts up.
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
if (MqttConfig.getInstance().isEnabled()) {
|
||||
synchronized (raspberryPiMQTTConnector) {
|
||||
raspberryPiMQTTConnector.connect();
|
||||
}
|
||||
raspberryPiMQTTConnector.connect();
|
||||
} else {
|
||||
log.warn("MQTT disabled in 'devicemgt-config.xml'. Hence, not started.");
|
||||
log.warn("MQTT disabled in 'devicemgt-config.xml'. Hence, RaspberryPiMQTTConnector not started.");
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -92,112 +185,4 @@ public class RaspberryPiControllerServiceImpl implements RaspberryPiControllerSe
|
||||
connectorThread.start();
|
||||
}
|
||||
|
||||
public Response registerDeviceIP(String deviceId, String deviceIP, String devicePort, HttpServletRequest request) {
|
||||
String result;
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Got register call from IP: " + deviceIP + " for Device ID: " + deviceId);
|
||||
}
|
||||
String deviceHttpEndpoint = deviceIP + ":" + devicePort;
|
||||
deviceToIpMap.put(deviceId, deviceHttpEndpoint);
|
||||
result = "Device-IP Registered";
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug(result);
|
||||
}
|
||||
return Response.ok().entity(result).build();
|
||||
}
|
||||
|
||||
public Response switchBulb(String deviceId, String state) {
|
||||
String switchToState = state.toUpperCase();
|
||||
if (!switchToState.equals(RaspberrypiConstants.STATE_ON) && !switchToState.equals(
|
||||
RaspberrypiConstants.STATE_OFF)) {
|
||||
log.error("The requested state change shoud be either - 'ON' or 'OFF'");
|
||||
return Response.status(Response.Status.BAD_REQUEST.getStatusCode()).build();
|
||||
}
|
||||
String callUrlPattern = RaspberrypiConstants.BULB_CONTEXT + switchToState;
|
||||
try {
|
||||
String deviceHTTPEndpoint = deviceToIpMap.get(deviceId);
|
||||
if (deviceHTTPEndpoint == null) {
|
||||
return Response.status(Response.Status.PRECONDITION_FAILED.getStatusCode()).build();
|
||||
}
|
||||
RaspberrypiServiceUtils.sendCommandViaHTTP(deviceHTTPEndpoint, callUrlPattern, true);
|
||||
} catch (DeviceManagementException e) {
|
||||
log.error("Failed to send switch-bulb request to device [" + deviceId + "] via ");
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
public Response requestTemperature(@PathParam("deviceId") String deviceId) {
|
||||
org.wso2.carbon.device.mgt.iot.sensormgt.SensorRecord sensorRecord = null;
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Sending request to read raspberrypi-temperature of device [" + deviceId + "] via ");
|
||||
}
|
||||
try {
|
||||
String deviceHTTPEndpoint = deviceToIpMap.get(deviceId);
|
||||
if (deviceHTTPEndpoint == null) {
|
||||
return Response.status(Response.Status.PRECONDITION_FAILED.getStatusCode()).build();
|
||||
}
|
||||
String temperatureValue = RaspberrypiServiceUtils.sendCommandViaHTTP(deviceHTTPEndpoint,
|
||||
RaspberrypiConstants
|
||||
.TEMPERATURE_CONTEXT,
|
||||
false);
|
||||
SensorDataManager.getInstance().setSensorRecord(deviceId, RaspberrypiConstants.SENSOR_TEMPERATURE,
|
||||
temperatureValue, Calendar.getInstance()
|
||||
.getTimeInMillis());
|
||||
sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId,
|
||||
RaspberrypiConstants.SENSOR_TEMPERATURE);
|
||||
} catch (DeviceManagementException | DeviceControllerException e) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
return Response.ok().entity(sensorRecord).build();
|
||||
}
|
||||
|
||||
public Response pushTemperatureData(final DeviceData dataMsg, HttpServletRequest request) {
|
||||
String owner = dataMsg.owner;
|
||||
String deviceId = dataMsg.deviceId;
|
||||
String deviceIp = dataMsg.reply;
|
||||
float temperature = dataMsg.value;
|
||||
String registeredIp = deviceToIpMap.get(deviceId);
|
||||
if (registeredIp == null) {
|
||||
log.warn("Unregistered IP: Temperature Data Received from an un-registered IP " + deviceIp +
|
||||
" for device ID - " + deviceId);
|
||||
return Response.status(Response.Status.PRECONDITION_FAILED.getStatusCode()).build();
|
||||
} else if (!registeredIp.equals(deviceIp)) {
|
||||
log.warn("Conflicting IP: Received IP is " + deviceIp + ". Device with ID " + deviceId +
|
||||
" is already registered under some other IP. Re-registration required");
|
||||
return Response.status(Response.Status.CONFLICT.getStatusCode()).build();
|
||||
}
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Received Pin Data Value: " + temperature + " degrees C");
|
||||
}
|
||||
SensorDataManager.getInstance().setSensorRecord(deviceId, RaspberrypiConstants.SENSOR_TEMPERATURE,
|
||||
String.valueOf(temperature),
|
||||
Calendar.getInstance().getTimeInMillis());
|
||||
if (!RaspberrypiServiceUtils.publishToDAS(dataMsg.deviceId, dataMsg.value)) {
|
||||
log.warn("An error occured whilst trying to publish temperature data of raspberrypi with ID [" +
|
||||
deviceId + "] of owner [" + owner + "]");
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
public Response getArduinoTemperatureStats(String deviceId, String user, long from, long to) {
|
||||
String fromDate = String.valueOf(from);
|
||||
String toDate = String.valueOf(to);
|
||||
String query = "owner:" + user + " AND deviceId:" + deviceId + " AND deviceType:" +
|
||||
RaspberrypiConstants.DEVICE_TYPE + " AND time : [" + fromDate + " TO " + toDate + "]";
|
||||
String sensorTableName = RaspberrypiConstants.TEMPERATURE_EVENT_TABLE;
|
||||
try {
|
||||
List<SortByField> sortByFields = new ArrayList<>();
|
||||
SortByField sortByField = new SortByField("time", SORT.ASC, false);
|
||||
sortByFields.add(sortByField);
|
||||
List<SensorRecord> sensorRecords = APIUtil.getAllEventsForDevice(sensorTableName, query, sortByFields);
|
||||
return Response.status(Response.Status.OK.getStatusCode()).entity(sensorRecords).build();
|
||||
} catch (AnalyticsException e) {
|
||||
String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query;
|
||||
log.error(errorMsg);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(errorMsg).build();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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 = "raspberrypi_mgt", version = "1.0.0", context = "/raspberrypi_mgt", tags = {"raspberrypi"})
|
||||
@DeviceType(value = "raspberrypi")
|
||||
public interface RaspberryPiManagerService {
|
||||
@ -43,7 +44,7 @@ public interface RaspberryPiManagerService {
|
||||
@Path("devices/{device_id}")
|
||||
@PUT
|
||||
Response updateDevice(@PathParam("device_id") String deviceId,
|
||||
@QueryParam("name") String name);
|
||||
@QueryParam("name") String name);
|
||||
|
||||
@Path("devices/{device_id}")
|
||||
@GET
|
||||
@ -57,16 +58,15 @@ public interface RaspberryPiManagerService {
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
Response getRaspberrypiDevices();
|
||||
|
||||
|
||||
@Path("devices/{sketch_type}/download")
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
Response downloadSketch(@QueryParam("deviceName") String deviceName, @PathParam("sketch_type") String
|
||||
sketchType);
|
||||
Response downloadSketch(@QueryParam("deviceName") String deviceName,
|
||||
@PathParam("sketch_type") String sketchType);
|
||||
|
||||
@Path("devices/{sketch_type}/generate_link")
|
||||
@GET
|
||||
Response generateSketchLink(@QueryParam("deviceName") String deviceName,
|
||||
@PathParam("sketch_type") String sketchType);
|
||||
@PathParam("sketch_type") String sketchType);
|
||||
|
||||
}
|
||||
|
||||
@ -42,6 +42,15 @@ 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;
|
||||
@ -51,13 +60,147 @@ import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Path("enrollment")
|
||||
public class RaspberryPiManagerServiceImpl implements RaspberryPiManagerService {
|
||||
|
||||
private static Log log = LogFactory.getLog(RaspberryPiManagerServiceImpl.class);
|
||||
|
||||
private static final String KEY_TYPE = "PRODUCTION";
|
||||
private static ApiApplicationKey apiApplicationKey;
|
||||
|
||||
@Path("devices/{device_id}")
|
||||
@DELETE
|
||||
public Response removeDevice(@PathParam("device_id") String deviceId) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(RaspberrypiConstants.DEVICE_TYPE);
|
||||
boolean removed = APIUtil.getDeviceManagementService().disenrollDevice(
|
||||
deviceIdentifier);
|
||||
if (removed) {
|
||||
return Response.ok().build();
|
||||
} else {
|
||||
return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).build();
|
||||
}
|
||||
} 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(RaspberrypiConstants.DEVICE_TYPE);
|
||||
try {
|
||||
Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||
device.setDeviceIdentifier(deviceId);
|
||||
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
||||
device.setName(name);
|
||||
device.setType(RaspberrypiConstants.DEVICE_TYPE);
|
||||
boolean updated = APIUtil.getDeviceManagementService().modifyEnrollment(device);
|
||||
if (updated) {
|
||||
return Response.ok().build();
|
||||
} else {
|
||||
return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).build();
|
||||
}
|
||||
} 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(RaspberrypiConstants.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")
|
||||
@GET
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response getRaspberrypiDevices() {
|
||||
try {
|
||||
List<Device> userDevices = APIUtil.getDeviceManagementService().getDevicesOfUser(
|
||||
APIUtil.getAuthenticatedUser());
|
||||
ArrayList<Device> usersRaspberrypiDevices = new ArrayList<>();
|
||||
for (Device device : userDevices) {
|
||||
if (device.getType().equals(RaspberrypiConstants.DEVICE_TYPE) &&
|
||||
device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.ACTIVE)) {
|
||||
usersRaspberrypiDevices.add(device);
|
||||
}
|
||||
}
|
||||
Device[] devices = usersRaspberrypiDevices.toArray(new Device[]{});
|
||||
return Response.ok().entity(devices).build();
|
||||
} catch (DeviceManagementException e) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@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, sketchType);
|
||||
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 (DeviceControllerException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (IOException 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();
|
||||
}
|
||||
}
|
||||
|
||||
@Path("devices/{sketch_type}/generate_link")
|
||||
@GET
|
||||
public Response generateSketchLink(@QueryParam("deviceName") String deviceName,
|
||||
@PathParam("sketch_type") String sketchType) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName, sketchType);
|
||||
Response.ResponseBuilder rb = Response.ok(zipFile.getDeviceId());
|
||||
return rb.build();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
return Response.status(400).entity(ex.getMessage()).build();//bad request
|
||||
} catch (DeviceManagementException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (JWTClientException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (DeviceControllerException 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();
|
||||
@ -83,122 +226,6 @@ public class RaspberryPiManagerServiceImpl implements RaspberryPiManagerService
|
||||
}
|
||||
}
|
||||
|
||||
public Response removeDevice(String deviceId) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(RaspberrypiConstants.DEVICE_TYPE);
|
||||
boolean removed = APIUtil.getDeviceManagementService().disenrollDevice(
|
||||
deviceIdentifier);
|
||||
if (removed) {
|
||||
return Response.ok().build();
|
||||
} else {
|
||||
return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).build();
|
||||
}
|
||||
} 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(RaspberrypiConstants.DEVICE_TYPE);
|
||||
try {
|
||||
Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||
device.setDeviceIdentifier(deviceId);
|
||||
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
||||
device.setName(name);
|
||||
device.setType(RaspberrypiConstants.DEVICE_TYPE);
|
||||
boolean updated = APIUtil.getDeviceManagementService().modifyEnrollment(device);
|
||||
if (updated) {
|
||||
return Response.ok().build();
|
||||
} else {
|
||||
return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).build();
|
||||
}
|
||||
} 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(RaspberrypiConstants.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 getRaspberrypiDevices() {
|
||||
try {
|
||||
List<Device> userDevices = APIUtil.getDeviceManagementService().getDevicesOfUser(
|
||||
APIUtil.getAuthenticatedUser());
|
||||
ArrayList<Device> usersRaspberrypiDevices = new ArrayList<>();
|
||||
for (Device device : userDevices) {
|
||||
if (device.getType().equals(RaspberrypiConstants.DEVICE_TYPE) &&
|
||||
device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.ACTIVE)) {
|
||||
usersRaspberrypiDevices.add(device);
|
||||
}
|
||||
}
|
||||
Device[] devices = usersRaspberrypiDevices.toArray(new Device[]{});
|
||||
return Response.ok().entity(devices).build();
|
||||
} catch (DeviceManagementException e) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
}
|
||||
|
||||
public Response downloadSketch(String deviceName, String sketchType) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName, sketchType);
|
||||
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 (DeviceControllerException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (IOException 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();
|
||||
}
|
||||
}
|
||||
|
||||
public Response generateSketchLink(String deviceName, String sketchType) {
|
||||
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName, sketchType);
|
||||
Response.ResponseBuilder rb = Response.ok(zipFile.getDeviceId());
|
||||
return rb.build();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
return Response.status(400).entity(ex.getMessage()).build();//bad request
|
||||
} catch (DeviceManagementException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (JWTClientException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (DeviceControllerException 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, String sketchType)
|
||||
throws DeviceManagementException, JWTClientException, APIManagerException, DeviceControllerException,
|
||||
UserStoreException {
|
||||
|
||||
@ -25,7 +25,6 @@ import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService;
|
||||
import org.wso2.carbon.apimgt.application.extension.dto.ApiApplicationKey;
|
||||
import org.wso2.carbon.apimgt.application.extension.exception.APIManagerException;
|
||||
import org.wso2.carbon.base.MultitenantConstants;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.common.Device;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||
@ -35,7 +34,6 @@ import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig;
|
||||
import org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl.util.APIUtil;
|
||||
import org.wso2.carbon.device.mgt.iot.raspberrypi.service.impl.util.RaspberrypiServiceUtils;
|
||||
import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.constants.RaspberrypiConstants;
|
||||
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.identity.jwt.client.extension.JWTClient;
|
||||
@ -161,8 +159,7 @@ public class RaspberryPiMQTTConnector extends MQTTTransportHandler {
|
||||
|
||||
} else if (receivedMessage.contains("TEMPERATURE")) {
|
||||
String temperatureValue = receivedMessage.split(":")[1];
|
||||
SensorDataManager.getInstance().setSensorRecord(deviceId, RaspberrypiConstants.SENSOR_TEMPERATURE,
|
||||
temperatureValue, Calendar.getInstance().getTimeInMillis());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,48 +7,40 @@
|
||||
<display-name>RaspberryPi</display-name>
|
||||
<description>RaspberryPi</description>
|
||||
|
||||
<servlet>
|
||||
<servlet-name>CXFServlet</servlet-name>
|
||||
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet>
|
||||
<servlet-name>CXFServlet</servlet-name>
|
||||
<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>
|
||||
<param-name>managed-api-enabled</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-owner</param-name>
|
||||
<param-value>admin</param-value>
|
||||
</context-param>
|
||||
|
||||
<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>
|
||||
|
||||
<!--publish to apim-->
|
||||
<context-param>
|
||||
<param-name>managed-api-enabled</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<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>/raspberrypi/{version}</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-application</param-name>
|
||||
<param-value>raspberrypi</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-isSecured</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
|
||||
|
||||
</web-app>
|
||||
</web-app>
|
||||
@ -56,7 +56,7 @@ public interface VirtualFireAlarmControllerService {
|
||||
* @return a custom message indicating whether the DeviceID to IP mapping was successful.
|
||||
*/
|
||||
@POST
|
||||
@Path("register/{deviceId}/{ip}/{port}")
|
||||
@Path("device/register/{deviceId}/{ip}/{port}")
|
||||
Response registerDeviceIP(@PathParam("deviceId") String deviceId, @PathParam("ip") String deviceIP,
|
||||
@PathParam("port") String devicePort, @Context HttpServletRequest request);
|
||||
|
||||
@ -72,31 +72,12 @@ public interface VirtualFireAlarmControllerService {
|
||||
* (Case-Insensitive String)
|
||||
*/
|
||||
@POST
|
||||
@Path("{deviceId}/buzz")
|
||||
@Path("device/{deviceId}/buzz")
|
||||
@Feature(code = "buzz", name = "Buzzer On / Off", type = "operation",
|
||||
description = "Switch on/off Virtual Fire Alarm Buzzer. (On / Off)")
|
||||
Response switchBuzzer(@PathParam("deviceId") String deviceId, @QueryParam("protocol") String protocol,
|
||||
@FormParam("state") String state);
|
||||
|
||||
/**
|
||||
* This is an API called/used from within the Server(Front-End) or by a device Owner. It sends a control command
|
||||
* to the VirtualFirealarm device to 'tell what's its current temperature reading'. The method also takes in the
|
||||
* protocol to be used to connect-to and send the command to the device.
|
||||
*
|
||||
* @param deviceId the ID of the VirtualFirealarm device on which the temperature reading is be read-from.
|
||||
* @param protocol the protocol (HTTP, MQTT, XMPP) to be used to connect-to & send the message to the device.
|
||||
* @return an instance of the 'SensorRecord' object that holds the last updated temperature of the VirtualFirealarm
|
||||
* whose temperature reading was requested.
|
||||
*/
|
||||
@GET
|
||||
@Path("{deviceId}/temperature")
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Feature(code = "temperature", name = "Temperature", type = "monitor",
|
||||
description = "Request Temperature reading from Virtual Fire Alarm")
|
||||
Response requestTemperature(@PathParam("deviceId") String deviceId,
|
||||
@QueryParam("protocol") String protocol);
|
||||
|
||||
/**
|
||||
* This is an API called/used by the VirtualFirealarm device to publish its temperature to the IoT-Server. The
|
||||
* received data from the device is stored in a 'DeviceRecord' under the device's ID in the 'SensorDataManager'
|
||||
@ -105,7 +86,7 @@ public interface VirtualFireAlarmControllerService {
|
||||
* @param dataMsg the temperature data received from the device in JSON format complying to type 'DeviceData'.
|
||||
*/
|
||||
@POST
|
||||
@Path("temperature")
|
||||
@Path("device/temperature")
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
Response pushTemperatureData(final DeviceData dataMsg);
|
||||
|
||||
@ -123,7 +104,7 @@ public interface VirtualFireAlarmControllerService {
|
||||
* @return an HTTP Response object with either the CA-Cert or the CA-Capabilities according to the operation.
|
||||
*/
|
||||
@GET
|
||||
@Path("scep")
|
||||
@Path("device/scep")
|
||||
Response scepRequest(@QueryParam("operation") String operation, @QueryParam("message") String message);
|
||||
|
||||
|
||||
@ -140,13 +121,13 @@ public interface VirtualFireAlarmControllerService {
|
||||
* @return an HTTP Response object with the signed certificate for the device by the CA of the SCEP Server.
|
||||
*/
|
||||
@POST
|
||||
@Path("scep")
|
||||
@Path("device/scep")
|
||||
Response scepRequestPost(@QueryParam("operation") String operation, InputStream inputStream);
|
||||
|
||||
/**
|
||||
* Retrieve Sensor data for the device type
|
||||
*/
|
||||
@Path("stats/{deviceId}/sensors/{sensorName}")
|
||||
@Path("device/stats/{deviceId}/sensors/{sensorName}")
|
||||
@GET
|
||||
@Consumes("application/json")
|
||||
@Produces("application/json")
|
||||
|
||||
@ -29,8 +29,6 @@ import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementService
|
||||
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.controlqueue.xmpp.XmppConfig;
|
||||
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.service.IoTServerStartupListener;
|
||||
import org.wso2.carbon.device.mgt.iot.transport.TransportHandlerException;
|
||||
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.dto.DeviceData;
|
||||
@ -46,11 +44,19 @@ import org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util.scep.SC
|
||||
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.constants.VirtualFireAlarmConstants;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.FormParam;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@ -77,6 +83,208 @@ public class VirtualFireAlarmControllerServiceImpl implements VirtualFireAlarmCo
|
||||
// holds a mapping of the IP addresses to Device-IDs for HTTP communication
|
||||
private ConcurrentHashMap<String, String> deviceToIpMap = new ConcurrentHashMap<>();
|
||||
|
||||
@POST
|
||||
@Path("device/register/{deviceId}/{ip}/{port}")
|
||||
public Response registerDeviceIP(@PathParam("deviceId") String deviceId, @PathParam("ip") String deviceIP,
|
||||
@PathParam("port") String devicePort, @Context HttpServletRequest request) {
|
||||
String result;
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Got register call from IP: " + deviceIP + " for Device ID: " + deviceId);
|
||||
}
|
||||
String deviceHttpEndpoint = deviceIP + ":" + devicePort;
|
||||
deviceToIpMap.put(deviceId, deviceHttpEndpoint);
|
||||
result = "Device-IP Registered";
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug(result);
|
||||
}
|
||||
return Response.ok().entity(result).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("device/{deviceId}/buzz")
|
||||
public Response switchBuzzer(@PathParam("deviceId") String deviceId, @QueryParam("protocol") String protocol,
|
||||
@FormParam("state") String state) {
|
||||
String switchToState = state.toUpperCase();
|
||||
if (!switchToState.equals(VirtualFireAlarmConstants.STATE_ON) && !switchToState.equals(
|
||||
VirtualFireAlarmConstants.STATE_OFF)) {
|
||||
log.error("The requested state change shoud be either - 'ON' or 'OFF'");
|
||||
return Response.status(Response.Status.BAD_REQUEST).build();
|
||||
}
|
||||
String protocolString = protocol.toUpperCase();
|
||||
String callUrlPattern = VirtualFireAlarmConstants.BULB_CONTEXT + switchToState;
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Sending request to switch-bulb of device [" + deviceId + "] via " +
|
||||
protocolString);
|
||||
}
|
||||
try {
|
||||
switch (protocolString) {
|
||||
case HTTP_PROTOCOL:
|
||||
String deviceHTTPEndpoint = deviceToIpMap.get(deviceId);
|
||||
if (deviceHTTPEndpoint == null) {
|
||||
return Response.status(Response.Status.PRECONDITION_FAILED).build();
|
||||
}
|
||||
VirtualFireAlarmServiceUtils.sendCommandViaHTTP(deviceHTTPEndpoint, callUrlPattern, true);
|
||||
break;
|
||||
case XMPP_PROTOCOL:
|
||||
String xmppResource = VirtualFireAlarmConstants.BULB_CONTEXT.replace("/", "");
|
||||
virtualFireAlarmXMPPConnector.publishDeviceData(deviceId, xmppResource, switchToState);
|
||||
break;
|
||||
default:
|
||||
String mqttResource = VirtualFireAlarmConstants.BULB_CONTEXT.replace("/", "");
|
||||
virtualFireAlarmMQTTConnector.publishDeviceData(deviceId, mqttResource, switchToState);
|
||||
break;
|
||||
}
|
||||
return Response.ok().build();
|
||||
} catch (DeviceManagementException | TransportHandlerException e) {
|
||||
log.error("Failed to send switch-bulb request to device [" + deviceId + "] via " + protocolString);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
|
||||
}
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("device/temperature")
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public Response pushTemperatureData(final DeviceData dataMsg) {
|
||||
String deviceId = dataMsg.deviceId;
|
||||
String deviceIp = dataMsg.reply;
|
||||
float temperature = dataMsg.value;
|
||||
String registeredIp = deviceToIpMap.get(deviceId);
|
||||
if (registeredIp == null) {
|
||||
log.warn("Unregistered IP: Temperature Data Received from an un-registered IP " +
|
||||
deviceIp + " for device ID - " + deviceId);
|
||||
return Response.status(Response.Status.PRECONDITION_FAILED).build();
|
||||
} else if (!registeredIp.equals(deviceIp)) {
|
||||
log.warn("Conflicting IP: Received IP is " + deviceIp + ". Device with ID " + deviceId +
|
||||
" is already registered under some other IP. Re-registration required");
|
||||
return Response.status(Response.Status.CONFLICT).build();
|
||||
}
|
||||
if (!VirtualFireAlarmServiceUtils.publishToDAS(dataMsg.deviceId, dataMsg.value)) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
|
||||
}
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("device/scep")
|
||||
public Response scepRequest(@QueryParam("operation") String operation, @QueryParam("message") String message) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Invoking SCEP operation " + operation);
|
||||
}
|
||||
if (SCEPOperation.GET_CA_CERT.getValue().equals(operation)) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Invoking GetCACert");
|
||||
}
|
||||
try {
|
||||
CertificateManagementService certificateManagementService =
|
||||
VirtualFireAlarmServiceUtils.getCertificateManagementService();
|
||||
SCEPResponse scepResponse = certificateManagementService.getCACertSCEP();
|
||||
Response.ResponseBuilder responseBuilder;
|
||||
switch (scepResponse.getResultCriteria()) {
|
||||
case CA_CERT_FAILED:
|
||||
log.error("CA cert failed");
|
||||
responseBuilder = Response.serverError();
|
||||
break;
|
||||
case CA_CERT_RECEIVED:
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("CA certificate received in GetCACert");
|
||||
}
|
||||
responseBuilder = Response.ok(scepResponse.getEncodedResponse(),
|
||||
ContentType.X_X509_CA_CERT);
|
||||
break;
|
||||
case CA_RA_CERT_RECEIVED:
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("CA and RA certificates received in GetCACert");
|
||||
}
|
||||
responseBuilder = Response.ok(scepResponse.getEncodedResponse(),
|
||||
ContentType.X_X509_CA_RA_CERT);
|
||||
break;
|
||||
default:
|
||||
log.error("Invalid SCEP request");
|
||||
responseBuilder = Response.serverError();
|
||||
break;
|
||||
}
|
||||
|
||||
return responseBuilder.build();
|
||||
} catch (VirtualFireAlarmException e) {
|
||||
log.error("Error occurred while enrolling the VirtualFireAlarm device", e);
|
||||
} catch (KeystoreException e) {
|
||||
log.error("Keystore error occurred while enrolling the VirtualFireAlarm device", e);
|
||||
}
|
||||
|
||||
} else if (SCEPOperation.GET_CA_CAPS.getValue().equals(operation)) {
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Invoking GetCACaps");
|
||||
}
|
||||
try {
|
||||
CertificateManagementService certificateManagementService = VirtualFireAlarmServiceUtils.
|
||||
getCertificateManagementService();
|
||||
byte caCaps[] = certificateManagementService.getCACapsSCEP();
|
||||
|
||||
return Response.ok(caCaps, MediaType.TEXT_PLAIN).build();
|
||||
|
||||
} catch (VirtualFireAlarmException e) {
|
||||
log.error("Error occurred while enrolling the device", e);
|
||||
}
|
||||
} else {
|
||||
log.error("Invalid SCEP operation " + operation);
|
||||
}
|
||||
return Response.serverError().build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("device/scep")
|
||||
public Response scepRequestPost(@QueryParam("operation") String operation, InputStream inputStream) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Invoking SCEP operation " + operation);
|
||||
}
|
||||
if (SCEPOperation.PKI_OPERATION.getValue().equals(operation)) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Invoking PKIOperation");
|
||||
}
|
||||
try {
|
||||
CertificateManagementService certificateManagementService = VirtualFireAlarmServiceUtils.
|
||||
getCertificateManagementService();
|
||||
byte pkiMessage[] = certificateManagementService.getPKIMessageSCEP(inputStream);
|
||||
return Response.ok(pkiMessage, ContentType.X_PKI_MESSAGE).build();
|
||||
} catch (VirtualFireAlarmException e) {
|
||||
log.error("Error occurred while enrolling the device", e);
|
||||
} catch (KeystoreException e) {
|
||||
log.error("Keystore error occurred while enrolling the device", e);
|
||||
}
|
||||
}
|
||||
return Response.serverError().build();
|
||||
}
|
||||
|
||||
@Path("device/stats/{deviceId}/sensors/{sensorName}")
|
||||
@GET
|
||||
@Consumes("application/json")
|
||||
@Produces("application/json")
|
||||
public Response getVirtualFirealarmStats(@PathParam("deviceId") String deviceId,
|
||||
@PathParam("sensorName") String sensor,
|
||||
@QueryParam("username") String user, @QueryParam("from") long from,
|
||||
@QueryParam("to") long to) {
|
||||
String fromDate = String.valueOf(from);
|
||||
String toDate = String.valueOf(to);
|
||||
String query = "owner:" + user + " AND deviceId:" + deviceId + " AND deviceType:" +
|
||||
VirtualFireAlarmConstants.DEVICE_TYPE + " AND time : [" + fromDate + " TO " + toDate + "]";
|
||||
String sensorTableName = getSensorEventTableName(sensor);
|
||||
try {
|
||||
if (sensorTableName != null) {
|
||||
List<SortByField> sortByFields = new ArrayList<>();
|
||||
SortByField sortByField = new SortByField("time", SORT.ASC, false);
|
||||
sortByFields.add(sortByField);
|
||||
List<SensorRecord> sensorRecords = APIUtil.getAllEventsForDevice(sensorTableName, query, sortByFields);
|
||||
return Response.status(Response.Status.OK.getStatusCode()).entity(sensorRecords).build();
|
||||
}
|
||||
} catch (AnalyticsException e) {
|
||||
String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query;
|
||||
log.error(errorMsg);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(errorMsg).build();
|
||||
}
|
||||
return Response.status(Response.Status.BAD_REQUEST).build();
|
||||
}
|
||||
|
||||
private boolean waitForServerStartup() {
|
||||
while (!IoTServerStartupListener.isServerReady()) {
|
||||
try {
|
||||
@ -185,16 +393,8 @@ public class VirtualFireAlarmControllerServiceImpl implements VirtualFireAlarmCo
|
||||
return;
|
||||
}
|
||||
VirtualFireAlarmControllerServiceImpl.this.virtualFireAlarmMQTTConnector = virtualFireAlarmMQTTConnector;
|
||||
//The delay is added for the server starts up.
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
if (MqttConfig.getInstance().isEnabled()) {
|
||||
synchronized (virtualFireAlarmMQTTConnector) {
|
||||
virtualFireAlarmMQTTConnector.connect();
|
||||
}
|
||||
virtualFireAlarmMQTTConnector.connect();
|
||||
} else {
|
||||
log.warn("MQTT disabled in 'devicemgt-config.xml'. Hence, VirtualFireAlarmMQTTConnector not started.");
|
||||
}
|
||||
@ -205,232 +405,6 @@ public class VirtualFireAlarmControllerServiceImpl implements VirtualFireAlarmCo
|
||||
connectorThread.start();
|
||||
}
|
||||
|
||||
public Response registerDeviceIP(String deviceId, String deviceIP, String devicePort, HttpServletRequest request) {
|
||||
String result;
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Got register call from IP: " + deviceIP + " for Device ID: " + deviceId);
|
||||
}
|
||||
String deviceHttpEndpoint = deviceIP + ":" + devicePort;
|
||||
deviceToIpMap.put(deviceId, deviceHttpEndpoint);
|
||||
result = "Device-IP Registered";
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug(result);
|
||||
}
|
||||
return Response.ok().entity(result).build();
|
||||
}
|
||||
|
||||
public Response switchBuzzer(String deviceId, String protocol, String state) {
|
||||
String switchToState = state.toUpperCase();
|
||||
if (!switchToState.equals(VirtualFireAlarmConstants.STATE_ON) && !switchToState.equals(
|
||||
VirtualFireAlarmConstants.STATE_OFF)) {
|
||||
log.error("The requested state change shoud be either - 'ON' or 'OFF'");
|
||||
return Response.status(Response.Status.BAD_REQUEST).build();
|
||||
}
|
||||
String protocolString = protocol.toUpperCase();
|
||||
String callUrlPattern = VirtualFireAlarmConstants.BULB_CONTEXT + switchToState;
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Sending request to switch-bulb of device [" + deviceId + "] via " +
|
||||
protocolString);
|
||||
}
|
||||
try {
|
||||
switch (protocolString) {
|
||||
case HTTP_PROTOCOL:
|
||||
String deviceHTTPEndpoint = deviceToIpMap.get(deviceId);
|
||||
if (deviceHTTPEndpoint == null) {
|
||||
return Response.status(Response.Status.PRECONDITION_FAILED).build();
|
||||
}
|
||||
VirtualFireAlarmServiceUtils.sendCommandViaHTTP(deviceHTTPEndpoint, callUrlPattern, true);
|
||||
break;
|
||||
case XMPP_PROTOCOL:
|
||||
String xmppResource = VirtualFireAlarmConstants.BULB_CONTEXT.replace("/", "");
|
||||
virtualFireAlarmXMPPConnector.publishDeviceData(deviceId, xmppResource, switchToState);
|
||||
break;
|
||||
default:
|
||||
String mqttResource = VirtualFireAlarmConstants.BULB_CONTEXT.replace("/", "");
|
||||
virtualFireAlarmMQTTConnector.publishDeviceData(deviceId, mqttResource, switchToState);
|
||||
break;
|
||||
}
|
||||
return Response.ok().build();
|
||||
} catch (DeviceManagementException | TransportHandlerException e) {
|
||||
log.error("Failed to send switch-bulb request to device [" + deviceId + "] via " + protocolString);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
|
||||
}
|
||||
}
|
||||
|
||||
public Response requestTemperature(String deviceId, String protocol) {
|
||||
org.wso2.carbon.device.mgt.iot.sensormgt.SensorRecord sensorRecord = null;
|
||||
String protocolString = protocol.toUpperCase();
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Sending request to read virtual-firealarm-temperature of device " +
|
||||
"[" + deviceId + "] via " + protocolString);
|
||||
}
|
||||
try {
|
||||
switch (protocolString) {
|
||||
case HTTP_PROTOCOL:
|
||||
String deviceHTTPEndpoint = deviceToIpMap.get(deviceId);
|
||||
if (deviceHTTPEndpoint == null) {
|
||||
return Response.status(Response.Status.PRECONDITION_FAILED).build();
|
||||
}
|
||||
String temperatureValue = VirtualFireAlarmServiceUtils.sendCommandViaHTTP(
|
||||
deviceHTTPEndpoint, VirtualFireAlarmConstants.TEMPERATURE_CONTEXT, false);
|
||||
SensorDataManager.getInstance().setSensorRecord(deviceId, VirtualFireAlarmConstants.SENSOR_TEMP,
|
||||
temperatureValue,
|
||||
Calendar.getInstance().getTimeInMillis());
|
||||
break;
|
||||
case XMPP_PROTOCOL:
|
||||
String xmppResource = VirtualFireAlarmConstants.TEMPERATURE_CONTEXT.replace("/", "");
|
||||
virtualFireAlarmMQTTConnector.publishDeviceData(deviceId, xmppResource, "");
|
||||
break;
|
||||
default:
|
||||
String mqttResource = VirtualFireAlarmConstants.TEMPERATURE_CONTEXT.replace("/", "");
|
||||
virtualFireAlarmMQTTConnector.publishDeviceData(deviceId, mqttResource, "");
|
||||
break;
|
||||
}
|
||||
sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId, VirtualFireAlarmConstants
|
||||
.SENSOR_TEMP);
|
||||
return Response.ok().entity(sensorRecord).build();
|
||||
} catch (DeviceManagementException | DeviceControllerException | TransportHandlerException e) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
|
||||
}
|
||||
}
|
||||
|
||||
public Response pushTemperatureData(final DeviceData dataMsg) {
|
||||
String deviceId = dataMsg.deviceId;
|
||||
String deviceIp = dataMsg.reply;
|
||||
float temperature = dataMsg.value;
|
||||
String registeredIp = deviceToIpMap.get(deviceId);
|
||||
if (registeredIp == null) {
|
||||
log.warn("Unregistered IP: Temperature Data Received from an un-registered IP " +
|
||||
deviceIp + " for device ID - " + deviceId);
|
||||
return Response.status(Response.Status.PRECONDITION_FAILED).build();
|
||||
} else if (!registeredIp.equals(deviceIp)) {
|
||||
log.warn("Conflicting IP: Received IP is " + deviceIp + ". Device with ID " + deviceId +
|
||||
" is already registered under some other IP. Re-registration required");
|
||||
return Response.status(Response.Status.CONFLICT).build();
|
||||
}
|
||||
SensorDataManager.getInstance().setSensorRecord(deviceId, VirtualFireAlarmConstants.SENSOR_TEMP,
|
||||
String.valueOf(temperature),
|
||||
Calendar.getInstance().getTimeInMillis());
|
||||
if (!VirtualFireAlarmServiceUtils.publishToDAS(dataMsg.deviceId, dataMsg.value)) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
|
||||
}
|
||||
return Response.ok().build();
|
||||
}
|
||||
|
||||
public Response scepRequest(String operation, String message) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Invoking SCEP operation " + operation);
|
||||
}
|
||||
if (SCEPOperation.GET_CA_CERT.getValue().equals(operation)) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Invoking GetCACert");
|
||||
}
|
||||
try {
|
||||
CertificateManagementService certificateManagementService =
|
||||
VirtualFireAlarmServiceUtils.getCertificateManagementService();
|
||||
SCEPResponse scepResponse = certificateManagementService.getCACertSCEP();
|
||||
Response.ResponseBuilder responseBuilder;
|
||||
switch (scepResponse.getResultCriteria()) {
|
||||
case CA_CERT_FAILED:
|
||||
log.error("CA cert failed");
|
||||
responseBuilder = Response.serverError();
|
||||
break;
|
||||
case CA_CERT_RECEIVED:
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("CA certificate received in GetCACert");
|
||||
}
|
||||
responseBuilder = Response.ok(scepResponse.getEncodedResponse(),
|
||||
ContentType.X_X509_CA_CERT);
|
||||
break;
|
||||
case CA_RA_CERT_RECEIVED:
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("CA and RA certificates received in GetCACert");
|
||||
}
|
||||
responseBuilder = Response.ok(scepResponse.getEncodedResponse(),
|
||||
ContentType.X_X509_CA_RA_CERT);
|
||||
break;
|
||||
default:
|
||||
log.error("Invalid SCEP request");
|
||||
responseBuilder = Response.serverError();
|
||||
break;
|
||||
}
|
||||
|
||||
return responseBuilder.build();
|
||||
} catch (VirtualFireAlarmException e) {
|
||||
log.error("Error occurred while enrolling the VirtualFireAlarm device", e);
|
||||
} catch (KeystoreException e) {
|
||||
log.error("Keystore error occurred while enrolling the VirtualFireAlarm device", e);
|
||||
}
|
||||
|
||||
} else if (SCEPOperation.GET_CA_CAPS.getValue().equals(operation)) {
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Invoking GetCACaps");
|
||||
}
|
||||
|
||||
try {
|
||||
CertificateManagementService certificateManagementService = VirtualFireAlarmServiceUtils.
|
||||
getCertificateManagementService();
|
||||
byte caCaps[] = certificateManagementService.getCACapsSCEP();
|
||||
|
||||
return Response.ok(caCaps, MediaType.TEXT_PLAIN).build();
|
||||
|
||||
} catch (VirtualFireAlarmException e) {
|
||||
log.error("Error occurred while enrolling the device", e);
|
||||
}
|
||||
|
||||
} else {
|
||||
log.error("Invalid SCEP operation " + operation);
|
||||
}
|
||||
|
||||
return Response.serverError().build();
|
||||
|
||||
}
|
||||
|
||||
public Response scepRequestPost(String operation, InputStream inputStream) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Invoking SCEP operation " + operation);
|
||||
}
|
||||
if (SCEPOperation.PKI_OPERATION.getValue().equals(operation)) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Invoking PKIOperation");
|
||||
}
|
||||
try {
|
||||
CertificateManagementService certificateManagementService = VirtualFireAlarmServiceUtils.
|
||||
getCertificateManagementService();
|
||||
byte pkiMessage[] = certificateManagementService.getPKIMessageSCEP(inputStream);
|
||||
return Response.ok(pkiMessage, ContentType.X_PKI_MESSAGE).build();
|
||||
} catch (VirtualFireAlarmException e) {
|
||||
log.error("Error occurred while enrolling the device", e);
|
||||
} catch (KeystoreException e) {
|
||||
log.error("Keystore error occurred while enrolling the device", e);
|
||||
}
|
||||
}
|
||||
return Response.serverError().build();
|
||||
}
|
||||
|
||||
public Response getVirtualFirealarmStats(String deviceId, String sensor, String user, long from, long to) {
|
||||
String fromDate = String.valueOf(from);
|
||||
String toDate = String.valueOf(to);
|
||||
String query = "owner:" + user + " AND deviceId:" + deviceId + " AND deviceType:" +
|
||||
VirtualFireAlarmConstants.DEVICE_TYPE + " AND time : [" + fromDate + " TO " + toDate + "]";
|
||||
String sensorTableName = getSensorEventTableName(sensor);
|
||||
try {
|
||||
if (sensorTableName != null) {
|
||||
List<SortByField> sortByFields = new ArrayList<>();
|
||||
SortByField sortByField = new SortByField("time", SORT.ASC, false);
|
||||
sortByFields.add(sortByField);
|
||||
List<SensorRecord> sensorRecords = APIUtil.getAllEventsForDevice(sensorTableName, query, sortByFields);
|
||||
return Response.status(Response.Status.OK.getStatusCode()).entity(sensorRecords).build();
|
||||
}
|
||||
} catch (AnalyticsException e) {
|
||||
String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query;
|
||||
log.error(errorMsg);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(errorMsg).build();
|
||||
}
|
||||
return Response.status(Response.Status.BAD_REQUEST).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* get the event table from the sensor name.
|
||||
*/
|
||||
|
||||
@ -32,20 +32,21 @@ import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
@Path("enrollment")
|
||||
@API(name = "virtual_firealarm_mgt", version = "1.0.0", context = "/virtual_firealarm_mgt", tags = "virtual_firealarm")
|
||||
@DeviceType(value = "virtual_firealarm")
|
||||
public interface VirtualFireAlarmManagerService {
|
||||
|
||||
@Path("{device_id}")
|
||||
@Path("devices/{device_id}")
|
||||
@DELETE
|
||||
Response removeDevice(@PathParam("device_id") String deviceId);
|
||||
|
||||
|
||||
@Path("{device_id}")
|
||||
@Path("devices/{device_id}")
|
||||
@PUT
|
||||
Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name);
|
||||
|
||||
@Path("{device_id}")
|
||||
@Path("devices/{device_id}")
|
||||
@GET
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@ -57,13 +58,13 @@ public interface VirtualFireAlarmManagerService {
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
Response getFirealarmDevices();
|
||||
|
||||
@Path("download")
|
||||
@Path("devices/download")
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
Response downloadSketch(@QueryParam("deviceName") String deviceName, @QueryParam("sketchType") String sketchType);
|
||||
|
||||
|
||||
@Path("generate_link")
|
||||
@Path("devices/generate_link")
|
||||
@GET
|
||||
Response generateSketchLink(@QueryParam("deviceName") String deviceName,
|
||||
@QueryParam("sketchType") String sketchType);
|
||||
|
||||
@ -40,6 +40,15 @@ 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;
|
||||
@ -49,11 +58,143 @@ import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Path("enrollment")
|
||||
public class VirtualFireAlarmManagerServiceImpl implements VirtualFireAlarmManagerService {
|
||||
|
||||
private static final String KEY_TYPE = "PRODUCTION";
|
||||
private static ApiApplicationKey apiApplicationKey;
|
||||
|
||||
@Path("devices/{device_id}")
|
||||
@DELETE
|
||||
public Response removeDevice(@PathParam("device_id") String deviceId) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(VirtualFireAlarmConstants.DEVICE_TYPE);
|
||||
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(VirtualFireAlarmConstants.DEVICE_TYPE);
|
||||
Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||
device.setDeviceIdentifier(deviceId);
|
||||
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
||||
device.setName(name);
|
||||
device.setType(VirtualFireAlarmConstants.DEVICE_TYPE);
|
||||
boolean updated = APIUtil.getDeviceManagementService().modifyEnrollment(device);
|
||||
if (updated) {
|
||||
return Response.ok().build();
|
||||
} else {
|
||||
return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).build();
|
||||
}
|
||||
} catch (DeviceManagementException e) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
}
|
||||
|
||||
@Path("devices/{device_id}")
|
||||
@GET
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response getDevice(@PathParam("device_id") String deviceId) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(VirtualFireAlarmConstants.DEVICE_TYPE);
|
||||
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")
|
||||
@GET
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response getFirealarmDevices() {
|
||||
try {
|
||||
List<Device> userDevices =
|
||||
APIUtil.getDeviceManagementService().getDevicesOfUser(APIUtil.getAuthenticatedUser());
|
||||
ArrayList<Device> userDevicesforFirealarm = new ArrayList<>();
|
||||
for (Device device : userDevices) {
|
||||
if (device.getType().equals(VirtualFireAlarmConstants.DEVICE_TYPE) &&
|
||||
device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.ACTIVE)) {
|
||||
userDevicesforFirealarm.add(device);
|
||||
}
|
||||
}
|
||||
Device[] devices = userDevicesforFirealarm.toArray(new Device[]{});
|
||||
return Response.ok().entity(devices).build();
|
||||
} catch (DeviceManagementException e) {
|
||||
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,
|
||||
@QueryParam("sketchType") String sketchType) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName, sketchType);
|
||||
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,
|
||||
@QueryParam("sketchType") String sketchType) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName, sketchType);
|
||||
Response.ResponseBuilder rb = Response.ok(zipFile.getDeviceId());
|
||||
return rb.build();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
return Response.status(400).entity(ex.getMessage()).build();//bad request
|
||||
} catch (DeviceManagementException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (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 boolean register(String deviceId, String name) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
@ -80,122 +221,6 @@ public class VirtualFireAlarmManagerServiceImpl implements VirtualFireAlarmManag
|
||||
}
|
||||
}
|
||||
|
||||
public Response removeDevice(String deviceId) {
|
||||
try {
|
||||
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
||||
deviceIdentifier.setId(deviceId);
|
||||
deviceIdentifier.setType(VirtualFireAlarmConstants.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(VirtualFireAlarmConstants.DEVICE_TYPE);
|
||||
Device device = APIUtil.getDeviceManagementService().getDevice(deviceIdentifier);
|
||||
device.setDeviceIdentifier(deviceId);
|
||||
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
||||
device.setName(name);
|
||||
device.setType(VirtualFireAlarmConstants.DEVICE_TYPE);
|
||||
boolean updated = APIUtil.getDeviceManagementService().modifyEnrollment(device);
|
||||
if (updated) {
|
||||
return Response.ok().build();
|
||||
} else {
|
||||
return Response.status(Response.Status.NOT_ACCEPTABLE.getStatusCode()).build();
|
||||
}
|
||||
} catch (DeviceManagementException e) {
|
||||
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(VirtualFireAlarmConstants.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 getFirealarmDevices() {
|
||||
try {
|
||||
List<Device> userDevices =
|
||||
APIUtil.getDeviceManagementService().getDevicesOfUser(APIUtil.getAuthenticatedUser());
|
||||
ArrayList<Device> userDevicesforFirealarm = new ArrayList<>();
|
||||
for (Device device : userDevices) {
|
||||
if (device.getType().equals(VirtualFireAlarmConstants.DEVICE_TYPE) &&
|
||||
device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.ACTIVE)) {
|
||||
userDevicesforFirealarm.add(device);
|
||||
}
|
||||
}
|
||||
Device[] devices = userDevicesforFirealarm.toArray(new Device[]{});
|
||||
return Response.ok().entity(devices).build();
|
||||
} catch (DeviceManagementException e) {
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
|
||||
}
|
||||
}
|
||||
|
||||
public Response downloadSketch(String deviceName, String sketchType) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName, sketchType);
|
||||
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();
|
||||
} finally {
|
||||
PrivilegedCarbonContext.endTenantFlow();
|
||||
}
|
||||
}
|
||||
|
||||
public Response generateSketchLink(String deviceName, String sketchType) {
|
||||
try {
|
||||
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName, sketchType);
|
||||
Response.ResponseBuilder rb = Response.ok(zipFile.getDeviceId());
|
||||
return rb.build();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
return Response.status(400).entity(ex.getMessage()).build();//bad request
|
||||
} catch (DeviceManagementException ex) {
|
||||
return Response.status(500).entity(ex.getMessage()).build();
|
||||
} catch (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();
|
||||
} finally {
|
||||
PrivilegedCarbonContext.endTenantFlow();
|
||||
}
|
||||
}
|
||||
|
||||
private ZipArchive createDownloadFile(String owner, String deviceName, String sketchType)
|
||||
throws DeviceManagementException, APIManagerException, JWTClientException, DeviceControllerException,
|
||||
UserStoreException {
|
||||
|
||||
@ -25,14 +25,12 @@ import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.wso2.carbon.apimgt.application.extension.APIManagementProviderService;
|
||||
import org.wso2.carbon.apimgt.application.extension.dto.ApiApplicationKey;
|
||||
import org.wso2.carbon.apimgt.application.extension.exception.APIManagerException;
|
||||
import org.wso2.carbon.base.MultitenantConstants;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.common.Device;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
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.device.mgt.iot.virtualfirealarm.service.impl.exception.VirtualFireAlarmException;
|
||||
@ -208,9 +206,6 @@ public class VirtualFireAlarmMQTTConnector extends MQTTTransportHandler {
|
||||
|
||||
} else if (actualMessage.contains("TEMPERATURE")) {
|
||||
String temperatureValue = actualMessage.split(":")[1];
|
||||
SensorDataManager.getInstance().setSensorRecord(deviceId, VirtualFireAlarmConstants.SENSOR_TEMP,
|
||||
temperatureValue,
|
||||
Calendar.getInstance().getTimeInMillis());
|
||||
}
|
||||
} catch (VirtualFireAlarmException e) {
|
||||
String errorMsg =
|
||||
|
||||
@ -26,7 +26,6 @@ import org.wso2.carbon.device.mgt.iot.controlqueue.xmpp.XmppAccount;
|
||||
import org.wso2.carbon.device.mgt.iot.controlqueue.xmpp.XmppConfig;
|
||||
import org.wso2.carbon.device.mgt.iot.controlqueue.xmpp.XmppServerClient;
|
||||
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.transport.TransportHandlerException;
|
||||
import org.wso2.carbon.device.mgt.iot.transport.xmpp.XMPPTransportHandler;
|
||||
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.constants.VirtualFireAlarmConstants;
|
||||
@ -182,10 +181,6 @@ public class VirtualFireAlarmXMPPConnector extends XMPPTransportHandler {
|
||||
break;
|
||||
case "CONTROL-REPLY":
|
||||
String tempVal = actualMessage.split(":")[1];
|
||||
SensorDataManager.getInstance().setSensorRecord(deviceId,
|
||||
VirtualFireAlarmConstants.SENSOR_TEMP,
|
||||
tempVal,
|
||||
Calendar.getInstance().getTimeInMillis());
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
@ -23,7 +23,7 @@
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
|
||||
|
||||
<jaxrs:server id="VirtualFireAlarmController" address="/device">
|
||||
<jaxrs:server id="VirtualFireAlarm" address="/">
|
||||
<jaxrs:serviceBeans>
|
||||
<bean id="VirtualFireAlarmControllerService"
|
||||
class="org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.VirtualFireAlarmControllerServiceImpl">
|
||||
@ -31,14 +31,6 @@
|
||||
<property name="virtualFireAlarmMQTTConnector" ref="mqttConnectorBean"/>
|
||||
<property name="virtualFireAlarmXMPPConnector" ref="xmppConnectorBean"/>
|
||||
</bean>
|
||||
</jaxrs:serviceBeans>
|
||||
<jaxrs:providers>
|
||||
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
|
||||
</jaxrs:providers>
|
||||
</jaxrs:server>
|
||||
|
||||
<jaxrs:server id="VirtualFireAlarmManager" address="/devices">
|
||||
<jaxrs:serviceBeans>
|
||||
<bean id="VirtualFireAlarmManagerService"
|
||||
class="org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.VirtualFireAlarmManagerServiceImpl">
|
||||
</bean>
|
||||
|
||||
@ -7,59 +7,40 @@
|
||||
<display-name>WSO2 IoT Server</display-name>
|
||||
<description>WSO2 IoT Server</description>
|
||||
|
||||
<servlet>
|
||||
<servlet-name>CXFServlet</servlet-name>
|
||||
<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>
|
||||
</context-param>
|
||||
|
||||
<!--publish to apim-->
|
||||
<context-param>
|
||||
<param-name>managed-api-enabled</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-owner</param-name>
|
||||
<param-value>admin</param-value>
|
||||
</context-param>
|
||||
<servlet>
|
||||
<servlet-name>CXFServlet</servlet-name>
|
||||
<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>managed-api-context-template</param-name>
|
||||
<param-value>/virtual_firealarm/{version}</param-value>
|
||||
<param-name>isAdminService</param-name>
|
||||
<param-value>false</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-application</param-name>
|
||||
<param-value>virtual_firealarm</param-value>
|
||||
<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>
|
||||
<context-param>
|
||||
<param-name>managed-api-isSecured</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
|
||||
<!-- Below configuration is used to redirect http requests to https -->
|
||||
<!--<security-constraint>-->
|
||||
<!--<web-resource-collection>-->
|
||||
<!--<web-resource-name>IoT</web-resource-name>-->
|
||||
<!--<url-pattern>/*</url-pattern>-->
|
||||
<!--</web-resource-collection>-->
|
||||
<!--<user-data-constraint>-->
|
||||
<!--<transport-guarantee>CONFIDENTIAL</transport-guarantee>-->
|
||||
<!--</user-data-constraint>-->
|
||||
<!--</security-constraint>-->
|
||||
<!--publish to apim-->
|
||||
<context-param>
|
||||
<param-name>managed-api-enabled</param-name>
|
||||
<param-value>true</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>managed-api-owner</param-name>
|
||||
<param-value>admin</param-value>
|
||||
</context-param>
|
||||
|
||||
|
||||
</web-app>
|
||||
</web-app>
|
||||
Loading…
Reference in New Issue
Block a user