|
|
@ -23,6 +23,9 @@ import org.apache.http.client.methods.HttpGet;
|
|
|
|
import org.apache.http.concurrent.FutureCallback;
|
|
|
|
import org.apache.http.concurrent.FutureCallback;
|
|
|
|
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
|
|
|
|
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
|
|
|
|
import org.apache.http.impl.nio.client.HttpAsyncClients;
|
|
|
|
import org.apache.http.impl.nio.client.HttpAsyncClients;
|
|
|
|
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.analytics.exception.DataPublisherConfigurationException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.analytics.service.DeviceAnalyticsService;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.common.DeviceController;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.common.DeviceController;
|
|
|
@ -65,7 +68,7 @@ public class FireAlarmControllerService {
|
|
|
|
private final String SUPER_TENANT = "carbon.super";
|
|
|
|
private final String SUPER_TENANT = "carbon.super";
|
|
|
|
@Context //injected response proxy supporting multiple thread
|
|
|
|
@Context //injected response proxy supporting multiple thread
|
|
|
|
private HttpServletResponse response;
|
|
|
|
private HttpServletResponse response;
|
|
|
|
|
|
|
|
private static final String TEMPERATURE_STREAM_DEFINITION = "org.wso2.iot.devices.temperature";
|
|
|
|
|
|
|
|
|
|
|
|
private static final String URL_PREFIX = "http://";
|
|
|
|
private static final String URL_PREFIX = "http://";
|
|
|
|
private static final String BULB_CONTEXT = "/BULB/";
|
|
|
|
private static final String BULB_CONTEXT = "/BULB/";
|
|
|
@ -380,143 +383,121 @@ public class FireAlarmControllerService {
|
|
|
|
boolean result;
|
|
|
|
boolean result;
|
|
|
|
String deviceId = dataMsg.deviceId;
|
|
|
|
String deviceId = dataMsg.deviceId;
|
|
|
|
String deviceIp = dataMsg.reply;
|
|
|
|
String deviceIp = dataMsg.reply;
|
|
|
|
String temperature = dataMsg.value;
|
|
|
|
float temperature = dataMsg.value;
|
|
|
|
|
|
|
|
|
|
|
|
String registeredIp = deviceToIpMap.get(deviceId);
|
|
|
|
String registeredIp = deviceToIpMap.get(deviceId);
|
|
|
|
|
|
|
|
|
|
|
|
if (registeredIp == null) {
|
|
|
|
if (registeredIp == null) {
|
|
|
|
log.warn(
|
|
|
|
log.warn("Unregistered IP: Temperature Data Received from an un-registered IP " +
|
|
|
|
"Unregistered IP: Temperature Data Received from an un-registered IP " +
|
|
|
|
deviceIp + " for device ID - " + deviceId);
|
|
|
|
deviceIp +
|
|
|
|
|
|
|
|
" for device ID - " + deviceId);
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode());
|
|
|
|
response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode());
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
} else if (!registeredIp.equals(deviceIp)) {
|
|
|
|
} else if (!registeredIp.equals(deviceIp)) {
|
|
|
|
log.warn("Conflicting IP: Received IP is " + deviceIp + ". Device with ID " +
|
|
|
|
log.warn("Conflicting IP: Received IP is " + deviceIp + ". Device with ID " +
|
|
|
|
deviceId +
|
|
|
|
deviceId + " is already registered under some other IP. Re-registration " + "required");
|
|
|
|
" is already registered under some other IP. Re-registration " +
|
|
|
|
|
|
|
|
"required");
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.CONFLICT.getStatusCode());
|
|
|
|
response.setStatus(Response.Status.CONFLICT.getStatusCode());
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PrivilegedCarbonContext.startTenantFlow();
|
|
|
|
|
|
|
|
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
|
|
|
|
|
|
|
ctx.setTenantDomain(SUPER_TENANT, true);
|
|
|
|
|
|
|
|
DeviceAnalyticsService deviceAnalyticsService = (DeviceAnalyticsService) ctx
|
|
|
|
|
|
|
|
.getOSGiService(DeviceAnalyticsService.class, null);
|
|
|
|
|
|
|
|
Object metdaData[] = {dataMsg.owner, FireAlarmConstants.DEVICE_TYPE, dataMsg.deviceId,
|
|
|
|
|
|
|
|
System.currentTimeMillis()};
|
|
|
|
|
|
|
|
Object payloadData[] = {temperature};
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
DeviceController deviceController = new DeviceController();
|
|
|
|
deviceAnalyticsService.publishEvent(TEMPERATURE_STREAM_DEFINITION, "1.0.0",
|
|
|
|
result = deviceController.pushBamData(dataMsg.owner, FireAlarmConstants
|
|
|
|
metdaData, new Object[0], payloadData);
|
|
|
|
.DEVICE_TYPE,
|
|
|
|
} catch (DataPublisherConfigurationException e) {
|
|
|
|
dataMsg.deviceId,
|
|
|
|
|
|
|
|
System.currentTimeMillis(), "DeviceData",
|
|
|
|
|
|
|
|
temperature,
|
|
|
|
|
|
|
|
DataStreamDefinitions.StreamTypeLabel
|
|
|
|
|
|
|
|
.TEMPERATURE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (UnauthorizedException e) {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
log.error("Data Push Attempt Failed for BAM Publisher: " + e.getMessage());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
} finally {
|
|
|
|
DeviceController deviceController = new DeviceController();
|
|
|
|
PrivilegedCarbonContext.endTenantFlow();
|
|
|
|
result = deviceController.pushCepData(dataMsg.owner, FireAlarmConstants
|
|
|
|
|
|
|
|
.DEVICE_TYPE,
|
|
|
|
|
|
|
|
dataMsg.deviceId,
|
|
|
|
|
|
|
|
System.currentTimeMillis(), "DeviceData",
|
|
|
|
|
|
|
|
temperature,
|
|
|
|
|
|
|
|
DataStreamDefinitions.StreamTypeLabel
|
|
|
|
|
|
|
|
.TEMPERATURE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (UnauthorizedException e) {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
|
|
|
log.error("Data Push Attempt Failed for CEP Publisher: " + e.getMessage());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Service to push all the sensor data collected by the FireAlarm
|
|
|
|
/* Service to push all the sensor data collected by the FireAlarm
|
|
|
|
Called by the FireAlarm device */
|
|
|
|
Called by the FireAlarm device */
|
|
|
|
|
|
|
|
|
|
|
|
@Path("/pushalarmdata")
|
|
|
|
// @Path("/pushalarmdata")
|
|
|
|
@POST
|
|
|
|
// @POST
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
// @Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
public void pushAlarmData(final DeviceJSON dataMsg, @Context HttpServletResponse response) {
|
|
|
|
// public void pushAlarmData(final DeviceJSON dataMsg, @Context HttpServletResponse response) {
|
|
|
|
boolean result;
|
|
|
|
// boolean result;
|
|
|
|
String sensorValues = dataMsg.value;
|
|
|
|
// String sensorValues = dataMsg.value;
|
|
|
|
log.info("Recieved Sensor Data Values: " + sensorValues);
|
|
|
|
// log.info("Recieved Sensor Data Values: " + sensorValues);
|
|
|
|
|
|
|
|
//
|
|
|
|
String sensors[] = sensorValues.split(":");
|
|
|
|
// String sensors[] = sensorValues.split(":");
|
|
|
|
try {
|
|
|
|
// try {
|
|
|
|
if (sensors.length == 3) {
|
|
|
|
// if (sensors.length == 3) {
|
|
|
|
String temperature = sensors[0];
|
|
|
|
// String temperature = sensors[0];
|
|
|
|
String bulb = sensors[1];
|
|
|
|
// String bulb = sensors[1];
|
|
|
|
String sonar = sensors[2];
|
|
|
|
// String sonar = sensors[2];
|
|
|
|
|
|
|
|
|
|
|
|
sensorValues = "Temperature:" + temperature + "C\tBulb Status:" + bulb +
|
|
|
|
// sensorValues = "Temperature:" + temperature + "C\tBulb Status:" + bulb +
|
|
|
|
"\t\tSonar Status:" + sonar;
|
|
|
|
// "\t\tSonar Status:" + sonar;
|
|
|
|
log.info(sensorValues);
|
|
|
|
// log.info(sensorValues);
|
|
|
|
DeviceController deviceController = new DeviceController();
|
|
|
|
// DeviceController deviceController = new DeviceController();
|
|
|
|
result = deviceController.pushBamData(dataMsg.owner, FireAlarmConstants
|
|
|
|
// result = deviceController.pushBamData(dataMsg.owner, FireAlarmConstants
|
|
|
|
.DEVICE_TYPE,
|
|
|
|
// .DEVICE_TYPE,
|
|
|
|
dataMsg.deviceId,
|
|
|
|
// dataMsg.deviceId,
|
|
|
|
System.currentTimeMillis(), "DeviceData",
|
|
|
|
// System.currentTimeMillis(), "DeviceData",
|
|
|
|
temperature, "TEMPERATURE");
|
|
|
|
// temperature, "TEMPERATURE");
|
|
|
|
|
|
|
|
//
|
|
|
|
if (!result) {
|
|
|
|
// if (!result) {
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
// response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
log.error("Error whilst pushing temperature: " + sensorValues);
|
|
|
|
// log.error("Error whilst pushing temperature: " + sensorValues);
|
|
|
|
return;
|
|
|
|
// return;
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
result = deviceController.pushBamData(dataMsg.owner, FireAlarmConstants
|
|
|
|
// result = deviceController.pushBamData(dataMsg.owner, FireAlarmConstants
|
|
|
|
.DEVICE_TYPE,
|
|
|
|
// .DEVICE_TYPE,
|
|
|
|
dataMsg.deviceId,
|
|
|
|
// dataMsg.deviceId,
|
|
|
|
System.currentTimeMillis(), "DeviceData",
|
|
|
|
// System.currentTimeMillis(), "DeviceData",
|
|
|
|
bulb,
|
|
|
|
// bulb,
|
|
|
|
"BULB");
|
|
|
|
// "BULB");
|
|
|
|
|
|
|
|
//
|
|
|
|
if (!result) {
|
|
|
|
// if (!result) {
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
// response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
log.error("Error whilst pushing Bulb data: " + sensorValues);
|
|
|
|
// log.error("Error whilst pushing Bulb data: " + sensorValues);
|
|
|
|
return;
|
|
|
|
// return;
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
result = deviceController.pushBamData(dataMsg.owner, FireAlarmConstants
|
|
|
|
// result = deviceController.pushBamData(dataMsg.owner, FireAlarmConstants
|
|
|
|
.DEVICE_TYPE,
|
|
|
|
// .DEVICE_TYPE,
|
|
|
|
dataMsg.deviceId,
|
|
|
|
// dataMsg.deviceId,
|
|
|
|
System.currentTimeMillis(), "DeviceData",
|
|
|
|
// System.currentTimeMillis(), "DeviceData",
|
|
|
|
sonar,
|
|
|
|
// sonar,
|
|
|
|
"SONAR");
|
|
|
|
// "SONAR");
|
|
|
|
|
|
|
|
//
|
|
|
|
if (!result) {
|
|
|
|
// if (!result) {
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
// response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
log.error("Error whilst pushing Sonar data: " + sensorValues);
|
|
|
|
// log.error("Error whilst pushing Sonar data: " + sensorValues);
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
} else {
|
|
|
|
// } else {
|
|
|
|
DeviceController deviceController = new DeviceController();
|
|
|
|
// DeviceController deviceController = new DeviceController();
|
|
|
|
result = deviceController.pushBamData(dataMsg.owner, FireAlarmConstants
|
|
|
|
// result = deviceController.pushBamData(dataMsg.owner, FireAlarmConstants
|
|
|
|
.DEVICE_TYPE,
|
|
|
|
// .DEVICE_TYPE,
|
|
|
|
dataMsg.deviceId,
|
|
|
|
// dataMsg.deviceId,
|
|
|
|
System.currentTimeMillis(), "DeviceData",
|
|
|
|
// System.currentTimeMillis(), "DeviceData",
|
|
|
|
dataMsg.value, dataMsg.reply);
|
|
|
|
// dataMsg.value, dataMsg.reply);
|
|
|
|
if (!result) {
|
|
|
|
// if (!result) {
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
// response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
log.error("Error whilst pushing sensor data: " + sensorValues);
|
|
|
|
// log.error("Error whilst pushing sensor data: " + sensorValues);
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
} catch (UnauthorizedException e) {
|
|
|
|
// } catch (UnauthorizedException e) {
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
// response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
log.error("Data Push Attempt Failed at Publisher: " + e.getMessage());
|
|
|
|
// log.error("Data Push Attempt Failed at Publisher: " + e.getMessage());
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String sendCommandViaXMPP(String deviceOwner, String deviceId, String resource,
|
|
|
|
private String sendCommandViaXMPP(String deviceOwner, String deviceId, String resource,
|
|
|
|