|
|
@ -30,20 +30,26 @@ import org.wso2.carbon.device.mgt.iot.DeviceManagement;
|
|
|
|
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.DeviceIdentifier;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.DeviceValidator;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.ws.rs.*;
|
|
|
|
import javax.ws.rs.*;
|
|
|
|
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;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Calendar;
|
|
|
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
|
|
|
|
|
|
@API(name = "currentsensor", version = "1.0.0", context = "/currentsensor")
|
|
|
|
@API(name = "currentsensor", version = "1.0.0", context = "/currentsensor")
|
|
|
|
@DeviceType(value = "currentsensor")
|
|
|
|
@DeviceType(value = "currentsensor")
|
|
|
|
public class CurrentSensorControllerService {
|
|
|
|
public class CurrentSensorControllerService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static Log log = LogFactory.getLog(CurrentSensorControllerService.class);
|
|
|
|
private static Log log = LogFactory.getLog(CurrentSensorControllerService.class);
|
|
|
|
|
|
|
|
private ConcurrentHashMap<String, String> deviceToIpMap = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
private static final String SUPER_TENANT = "carbon.super";
|
|
|
|
|
|
|
|
|
|
|
|
private boolean waitForServerStartup() {
|
|
|
|
private boolean waitForServerStartup() {
|
|
|
|
while (!DeviceManagement.isServerReady()) {
|
|
|
|
while (!DeviceManagement.isServerReady()) {
|
|
|
@ -56,6 +62,37 @@ public class CurrentSensorControllerService {
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Path("controller/register/{owner}/{deviceId}/{ip}/{port}")
|
|
|
|
|
|
|
|
@POST
|
|
|
|
|
|
|
|
public String registerDeviceIP(@PathParam("owner") String owner,
|
|
|
|
|
|
|
|
@PathParam("deviceId") String deviceId,
|
|
|
|
|
|
|
|
@PathParam("ip") String deviceIP,
|
|
|
|
|
|
|
|
@PathParam("port") String devicePort,
|
|
|
|
|
|
|
|
@Context HttpServletResponse response,
|
|
|
|
|
|
|
|
@Context HttpServletRequest request) {
|
|
|
|
|
|
|
|
System.out.println("Register Call..");
|
|
|
|
|
|
|
|
//TODO:: Need to get IP from the request itself
|
|
|
|
|
|
|
|
String result;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("Got register call from IP: " + deviceIP + " for Device ID: " + deviceId + " of owner: " + owner);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String deviceHttpEndpoint = deviceIP + ":" + devicePort;
|
|
|
|
|
|
|
|
deviceToIpMap.put(deviceId, deviceHttpEndpoint);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result = "Device-IP Registered";
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.OK.getStatusCode());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug(result);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param owner
|
|
|
|
* @param owner
|
|
|
|
* @param deviceId
|
|
|
|
* @param deviceId
|
|
|
@ -67,18 +104,18 @@ public class CurrentSensorControllerService {
|
|
|
|
@GET
|
|
|
|
@GET
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
@Feature( code="read-current", name="Current", type="monitor",
|
|
|
|
@Feature(code = "read-current", name = "Current", type = "monitor",
|
|
|
|
description="Request current reading from Arduino agent")
|
|
|
|
description = "Request current reading from Arduino agent")
|
|
|
|
public SensorRecord requestCurrent(@HeaderParam("owner") String owner,
|
|
|
|
public SensorRecord requestCurrent(@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;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId,
|
|
|
|
sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId,
|
|
|
|
CurrentSensorConstants.SENSOR_CURRENT);
|
|
|
|
CurrentSensorConstants.SENSOR_CURRENT);
|
|
|
|
} catch ( DeviceControllerException e) {
|
|
|
|
} catch (DeviceControllerException e) {
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -97,18 +134,18 @@ public class CurrentSensorControllerService {
|
|
|
|
@GET
|
|
|
|
@GET
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
@Feature( code="read-power", name="Power x100", type="monitor",
|
|
|
|
@Feature(code = "read-power", name = "Power x100", type = "monitor",
|
|
|
|
description="Request power reading from Arduino agent")
|
|
|
|
description = "Request power reading from Arduino agent")
|
|
|
|
public SensorRecord requestPower(@HeaderParam("owner") String owner,
|
|
|
|
public SensorRecord requestPower(@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;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId,
|
|
|
|
sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId,
|
|
|
|
CurrentSensorConstants.SENSOR_POWER);
|
|
|
|
CurrentSensorConstants.SENSOR_POWER);
|
|
|
|
} catch ( DeviceControllerException e) {
|
|
|
|
} catch (DeviceControllerException e) {
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -127,18 +164,18 @@ public class CurrentSensorControllerService {
|
|
|
|
@GET
|
|
|
|
@GET
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
@Feature( code="read-flowrate", name="Flow Rate x100", type="monitor",
|
|
|
|
@Feature(code = "read-flowrate", name = "Flow Rate x100", type = "monitor",
|
|
|
|
description="Request flow rate reading from Arduino agent")
|
|
|
|
description = "Request flow rate reading from Arduino agent")
|
|
|
|
public SensorRecord requestFlowRate(@HeaderParam("owner") String owner,
|
|
|
|
public SensorRecord requestFlowRate(@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;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId,
|
|
|
|
sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId,
|
|
|
|
CurrentSensorConstants.SENSOR_FLOWRATE);
|
|
|
|
CurrentSensorConstants.SENSOR_FLOWRATE);
|
|
|
|
} catch ( DeviceControllerException e) {
|
|
|
|
} catch (DeviceControllerException e) {
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -150,59 +187,74 @@ public class CurrentSensorControllerService {
|
|
|
|
* @param dataMsg
|
|
|
|
* @param dataMsg
|
|
|
|
* @param response
|
|
|
|
* @param response
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@Path("controller/pushcurrent")
|
|
|
|
@Path("controller/push-data")
|
|
|
|
@POST
|
|
|
|
@POST
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
public void pushCurrent(final DeviceJSON dataMsg, @Context HttpServletResponse response) {
|
|
|
|
public void pushData(final DeviceJSON dataMsg, @Context HttpServletResponse response) {
|
|
|
|
|
|
|
|
|
|
|
|
String owner = dataMsg.owner;
|
|
|
|
String owner = dataMsg.owner;
|
|
|
|
String deviceId = dataMsg.deviceId;
|
|
|
|
String deviceId = dataMsg.deviceId;
|
|
|
|
float pinData = dataMsg.value;
|
|
|
|
String deviceIp = dataMsg.reply;
|
|
|
|
|
|
|
|
float current = dataMsg.current;
|
|
|
|
|
|
|
|
float flow_rate = dataMsg.flow_rate;
|
|
|
|
|
|
|
|
|
|
|
|
SensorDataManager.getInstance().setSensorRecord(deviceId, CurrentSensorConstants.SENSOR_CURRENT,
|
|
|
|
try {
|
|
|
|
String.valueOf(pinData),
|
|
|
|
DeviceValidator deviceValidator = new DeviceValidator();
|
|
|
|
Calendar.getInstance().getTimeInMillis());
|
|
|
|
if (!deviceValidator.isExist(owner, SUPER_TENANT, new DeviceIdentifier(deviceId,
|
|
|
|
|
|
|
|
CurrentSensorConstants.DEVICE_TYPE))) {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode());
|
|
|
|
|
|
|
|
log.warn("Temperature data Received from unregistered raspberrypi device [" + deviceId +
|
|
|
|
|
|
|
|
"] for owner [" + owner + "]");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SensorDataManager.getInstance().setSensorRecord(deviceId, CurrentSensorConstants.SENSOR_POWER,
|
|
|
|
String registeredIp = deviceToIpMap.get(deviceId);
|
|
|
|
String.valueOf(pinData * 230 / 100),
|
|
|
|
|
|
|
|
Calendar.getInstance().getTimeInMillis());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!CurrentSensorServiceUtils.publishToDASCurrent(dataMsg.owner, dataMsg.deviceId, pinData)) {
|
|
|
|
if (registeredIp == null) {
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
log.warn("Unregistered IP: Temperature Data Received from an un-registered IP " + deviceIp +
|
|
|
|
log.warn("An error occured whilst trying to publish pin data of Current Sensor Data with ID [" + deviceId +
|
|
|
|
" for device ID - " + deviceId);
|
|
|
|
"] of owner [" + owner + "]");
|
|
|
|
response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
} else if (!registeredIp.equals(deviceIp)) {
|
|
|
|
|
|
|
|
log.warn("Conflicting IP: Received IP is " + deviceIp + ". Device with ID " + deviceId +
|
|
|
|
|
|
|
|
" is already registered under some other IP. Re-registration required");
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.CONFLICT.getStatusCode());
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!CurrentSensorServiceUtils.publishToDASPower(dataMsg.owner, dataMsg.deviceId, pinData * 230 / 100)) {
|
|
|
|
SensorDataManager.getInstance().setSensorRecord(deviceId, CurrentSensorConstants.SENSOR_CURRENT,
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
String.valueOf(current),
|
|
|
|
log.warn("An error occured whilst trying to publish pin data of Power Sensor Data with ID [" + deviceId +
|
|
|
|
Calendar.getInstance().getTimeInMillis());
|
|
|
|
"] of owner [" + owner + "]");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
SensorDataManager.getInstance().setSensorRecord(deviceId, CurrentSensorConstants.SENSOR_POWER,
|
|
|
|
* @param dataMsg
|
|
|
|
String.valueOf(current * 230 / 100),
|
|
|
|
* @param response
|
|
|
|
Calendar.getInstance().getTimeInMillis());
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Path("controller/pushflowrate")
|
|
|
|
|
|
|
|
@POST
|
|
|
|
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
|
|
|
|
public void pushFlowRate(final DeviceJSON dataMsg, @Context HttpServletResponse response) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String owner = dataMsg.owner;
|
|
|
|
SensorDataManager.getInstance().setSensorRecord(deviceId, CurrentSensorConstants.SENSOR_FLOWRATE,
|
|
|
|
String deviceId = dataMsg.deviceId;
|
|
|
|
String.valueOf(flow_rate/100),
|
|
|
|
float pinData = dataMsg.value / 100;
|
|
|
|
Calendar.getInstance().getTimeInMillis());
|
|
|
|
|
|
|
|
|
|
|
|
SensorDataManager.getInstance().setSensorRecord(deviceId, CurrentSensorConstants.SENSOR_FLOWRATE,
|
|
|
|
if (!CurrentSensorServiceUtils.publishToDASCurrent(dataMsg.owner, dataMsg.deviceId, current)) {
|
|
|
|
String.valueOf(pinData),
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
Calendar.getInstance().getTimeInMillis());
|
|
|
|
log.warn("An error occured whilst trying to publish pin data of Current Sensor Data with ID [" + deviceId +
|
|
|
|
|
|
|
|
"] of owner [" + owner + "]");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!CurrentSensorServiceUtils.publishToDASFlowRate(dataMsg.owner, dataMsg.deviceId, pinData)) {
|
|
|
|
if (!CurrentSensorServiceUtils.publishToDASPower(dataMsg.owner, dataMsg.deviceId, current * 230 / 100)) {
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
log.warn("An error occured whilst trying to publish pin data of Current Sensor Data with ID [" + deviceId +
|
|
|
|
log.warn("An error occured whilst trying to publish pin data of Power Sensor Data with ID [" + deviceId +
|
|
|
|
"] of owner [" + owner + "]");
|
|
|
|
"] of owner [" + owner + "]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!CurrentSensorServiceUtils.publishToDASFlowRate(dataMsg.owner, dataMsg.deviceId, flow_rate/100)) {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
|
|
|
log.warn("An error occured whilst trying to publish pin data of Current Sensor Data with ID [" + deviceId +
|
|
|
|
|
|
|
|
"] of owner [" + owner + "]");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
|
|
|
String errorMsg = "Validation attempt for deviceId [" + deviceId + "] of owner [" + owner + "] failed.\n";
|
|
|
|
|
|
|
|
log.error(errorMsg + Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase() + "\n" + e.getErrorMessage());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|