|
|
@ -1,4 +1,3 @@
|
|
|
|
package ${groupId}.${rootArtifactId}.controller.api;
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
|
|
|
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -16,7 +15,9 @@ package ${groupId}.${rootArtifactId}.controller.api;
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
import java.util.Calendar;
|
|
|
|
|
|
|
|
|
|
|
|
package ${groupId}.${rootArtifactId}.controller.api;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
@ -31,15 +32,23 @@ import org.wso2.carbon.apimgt.annotations.api.API;
|
|
|
|
import org.wso2.carbon.apimgt.annotations.device.DeviceType;
|
|
|
|
import org.wso2.carbon.apimgt.annotations.device.DeviceType;
|
|
|
|
import org.wso2.carbon.apimgt.annotations.device.feature.Feature;
|
|
|
|
import org.wso2.carbon.apimgt.annotations.device.feature.Feature;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.DeviceManagement;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.DeviceManagement;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.DeviceValidator;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorDataManager;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorRecord;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.sensormgt.SensorRecord;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException;
|
|
|
|
import org.wso2.carbon.context.CarbonContext;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import javax.ws.rs.*;
|
|
|
|
import javax.ws.rs.Consumes;
|
|
|
|
|
|
|
|
import javax.ws.rs.FormParam;
|
|
|
|
|
|
|
|
import javax.ws.rs.GET;
|
|
|
|
|
|
|
|
import javax.ws.rs.HeaderParam;
|
|
|
|
|
|
|
|
import javax.ws.rs.POST;
|
|
|
|
|
|
|
|
import javax.ws.rs.Path;
|
|
|
|
|
|
|
|
import javax.ws.rs.Produces;
|
|
|
|
import javax.ws.rs.core.Context;
|
|
|
|
import javax.ws.rs.core.Context;
|
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
@ -47,12 +56,14 @@ import javax.ws.rs.core.Response;
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* This is the controller API which is used to control agent side functionality
|
|
|
|
* This is the controller API which is used to control agent side functionality
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
@SuppressWarnings("NonJaxWsWebServices")
|
|
|
|
@API(name = "${deviceType}", version = "1.0.0", context = "/${deviceType}")
|
|
|
|
@API(name = "${deviceType}", version = "1.0.0", context = "/${deviceType}")
|
|
|
|
@DeviceType(value = "${deviceType}")
|
|
|
|
@DeviceType(value = "${deviceType}")
|
|
|
|
public class ControllerService {
|
|
|
|
public class ControllerService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static Log log = LogFactory.getLog(ControllerService.class);
|
|
|
|
private MQTTConnector mqttConnector;
|
|
|
|
private MQTTConnector mqttConnector;
|
|
|
|
private ConcurrentHashMap<String, DeviceJSON> deviceToIpMap = new ConcurrentHashMap<>();
|
|
|
|
private ConcurrentHashMap<String, DeviceJSON> deviceToIpMap = new ConcurrentHashMap<>();
|
|
|
|
private static Log log = LogFactory.getLog(ControllerService.class);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean waitForServerStartup() {
|
|
|
|
private boolean waitForServerStartup() {
|
|
|
|
while (!DeviceManagement.isServerReady()) {
|
|
|
|
while (!DeviceManagement.isServerReady()) {
|
|
|
@ -100,10 +111,9 @@ public class ControllerService {
|
|
|
|
String deviceId = agentInfo.deviceId;
|
|
|
|
String deviceId = agentInfo.deviceId;
|
|
|
|
if ((agentInfo.deviceId != null) && (agentInfo.owner != null)) {
|
|
|
|
if ((agentInfo.deviceId != null) && (agentInfo.owner != null)) {
|
|
|
|
deviceToIpMap.put(deviceId, agentInfo);
|
|
|
|
deviceToIpMap.put(deviceId, agentInfo);
|
|
|
|
return Response.status(Response.Status.OK).entity("Device has been registered successfully").build();
|
|
|
|
return Response.status(Response.Status.OK).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Message body not " +
|
|
|
|
return Response.status(Response.Status.NOT_ACCEPTABLE).build();
|
|
|
|
"well-formed and still invalid").build();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -117,20 +127,22 @@ public class ControllerService {
|
|
|
|
@GET
|
|
|
|
@GET
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
@Feature(code = "read-current-status", name = "Sensor", type = "monitor",
|
|
|
|
@Feature(code = "read-current-status", name = "${nameOfTheSensor}", type = "monitor",
|
|
|
|
description = "Request current status of sensor from device")
|
|
|
|
description = "Request current status of sensor from device")
|
|
|
|
public SensorRecord readCurrentStatus(@HeaderParam("owner") String owner,
|
|
|
|
public SensorRecord readCurrentStatus(@HeaderParam("owner") String owner,
|
|
|
|
@HeaderParam("deviceId") String deviceId,
|
|
|
|
@HeaderParam("deviceId") String deviceId,
|
|
|
|
@HeaderParam("protocol") String protocol,
|
|
|
|
@HeaderParam("protocol") String protocol,
|
|
|
|
@Context HttpServletResponse response) {
|
|
|
|
@Context HttpServletResponse response) {
|
|
|
|
SensorRecord sensorRecord = null;
|
|
|
|
SensorRecord sensorRecord = null;
|
|
|
|
|
|
|
|
if(isPermitted(owner, deviceId, response)){
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId,
|
|
|
|
sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId,
|
|
|
|
DeviceTypeConstants.SENSOR_READING);
|
|
|
|
DeviceTypeConstants.SENSOR_READING);
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.OK.getStatusCode());
|
|
|
|
} catch (DeviceControllerException e) {
|
|
|
|
} catch (DeviceControllerException e) {
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
response.setStatus(Response.Status.OK.getStatusCode());
|
|
|
|
}
|
|
|
|
return sensorRecord;
|
|
|
|
return sensorRecord;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -142,11 +154,17 @@ public class ControllerService {
|
|
|
|
@POST
|
|
|
|
@POST
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
public void pushData(final DeviceJSON agentInfo, @Context HttpServletResponse response) {
|
|
|
|
public void pushData(final DeviceJSON agentInfo, @Context HttpServletResponse response) {
|
|
|
|
|
|
|
|
if(isPermitted(agentInfo.owner, agentInfo.deviceId, response)){
|
|
|
|
if (!ServiceUtils.publishToDASSensorValue(agentInfo.owner, agentInfo.deviceId, agentInfo.sensorValue)) {
|
|
|
|
if (!ServiceUtils.publishToDASSensorValue(agentInfo.owner, agentInfo.deviceId, agentInfo.sensorValue)) {
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
log.warn("An error occurred whilst trying to publish pin data of go Data with ID [" + agentInfo.deviceId +
|
|
|
|
log.warn("An error occurred whilst trying to publish pin data of go Data with ID [" +
|
|
|
|
"] of owner [" + agentInfo.owner + "]");
|
|
|
|
agentInfo.deviceId + "] of owner [" + agentInfo.owner + "]");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.OK.getStatusCode());
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -160,18 +178,48 @@ public class ControllerService {
|
|
|
|
@POST
|
|
|
|
@POST
|
|
|
|
@Feature(code = "change-status", name = "Change status of sensor: on/off", type = "operation",
|
|
|
|
@Feature(code = "change-status", name = "Change status of sensor: on/off", type = "operation",
|
|
|
|
description = "Change status of sensor: on/off")
|
|
|
|
description = "Change status of sensor: on/off")
|
|
|
|
public void changeLockerState(@HeaderParam("owner") String owner, @HeaderParam("deviceId") String deviceId,
|
|
|
|
public void changeStatus(@HeaderParam("owner") String owner, @HeaderParam("deviceId") String deviceId,
|
|
|
|
@HeaderParam("protocol") String protocol, @FormParam("state") String state,
|
|
|
|
@HeaderParam("protocol") String protocol, @FormParam("state") String state,
|
|
|
|
@Context HttpServletResponse response) {
|
|
|
|
@Context HttpServletResponse response) {
|
|
|
|
|
|
|
|
if(isPermitted(owner, deviceId, response)){
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
mqttConnector.sendCommandViaMQTT(owner, deviceId, "Sensor:", state.toUpperCase());
|
|
|
|
mqttConnector.sendCommandViaMQTT(owner, deviceId, "Sensor:", state.toUpperCase());
|
|
|
|
response.setStatus(Response.Status.OK.getStatusCode());
|
|
|
|
response.setStatus(Response.Status.OK.getStatusCode());
|
|
|
|
|
|
|
|
return;
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
log.error(e);
|
|
|
|
log.error(e);
|
|
|
|
response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode());
|
|
|
|
response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode());
|
|
|
|
|
|
|
|
return;
|
|
|
|
} catch (DeviceTypeException e) {
|
|
|
|
} catch (DeviceTypeException e) {
|
|
|
|
log.error(e);
|
|
|
|
log.error(e);
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Check whether user is permitted for given operation
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param owner device owner
|
|
|
|
|
|
|
|
* @param deviceId unique identifier for given device type
|
|
|
|
|
|
|
|
* @param response if this true the user is allowed to related operation
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private boolean isPermitted(String owner, String deviceId, HttpServletResponse response) {
|
|
|
|
|
|
|
|
DeviceValidator deviceValidator = new DeviceValidator();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
|
|
|
|
|
|
|
|
if (!deviceValidator.isExist(owner, tenantDomain, new DeviceIdentifier(
|
|
|
|
|
|
|
|
deviceId, DeviceTypeConstants.DEVICE_TYPE))) {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|