|
|
|
@ -28,6 +28,7 @@ import org.wso2.carbon.device.mgt.analytics.data.publisher.AnalyticsDataRecord;
|
|
|
|
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DeviceManagementAnalyticsException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.service.DeviceAnalyticsService;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
|
|
|
|
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.controlqueue.xmpp.XmppConfig;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException;
|
|
|
|
@ -47,6 +48,15 @@ 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;
|
|
|
|
@ -80,127 +90,10 @@ 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<>();
|
|
|
|
|
|
|
|
|
|
private boolean waitForServerStartup() {
|
|
|
|
|
while (!IoTServerStartupListener.isServerReady()) {
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetches the `SecurityManager` specific to this VirtualFirealarm controller service.
|
|
|
|
|
*
|
|
|
|
|
* @return the 'SecurityManager' instance bound to the 'securityManager' variable of this service.
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("Unused")
|
|
|
|
|
public SecurityManager getSecurityManager() {
|
|
|
|
|
return securityManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the `securityManager` variable of this VirtualFirealarm controller service.
|
|
|
|
|
*
|
|
|
|
|
* @param securityManager a 'SecurityManager' object that handles the encryption, decryption, signing and validation
|
|
|
|
|
* of incoming messages from VirtualFirealarm device-types.
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("Unused")
|
|
|
|
|
public void setSecurityManager(SecurityManager securityManager) {
|
|
|
|
|
this.securityManager = securityManager;
|
|
|
|
|
securityManager.initVerificationManager();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetches the `VirtualFireAlarmXMPPConnector` specific to this VirtualFirealarm controller service.
|
|
|
|
|
*
|
|
|
|
|
* @return the 'VirtualFireAlarmXMPPConnector' instance bound to the 'virtualFireAlarmXMPPConnector' variable of
|
|
|
|
|
* this service.
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("Unused")
|
|
|
|
|
public VirtualFireAlarmXMPPConnector getVirtualFireAlarmXMPPConnector() {
|
|
|
|
|
return virtualFireAlarmXMPPConnector;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the `virtualFireAlarmXMPPConnector` variable of this VirtualFirealarm controller service.
|
|
|
|
|
*
|
|
|
|
|
* @param virtualFireAlarmXMPPConnector a 'VirtualFireAlarmXMPPConnector' object that handles all XMPP related
|
|
|
|
|
* communications of any connected VirtualFirealarm device-type
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("Unused")
|
|
|
|
|
public void setVirtualFireAlarmXMPPConnector(
|
|
|
|
|
final VirtualFireAlarmXMPPConnector virtualFireAlarmXMPPConnector) {
|
|
|
|
|
Runnable connector = new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
if (waitForServerStartup()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
VirtualFireAlarmControllerServiceImpl.this.virtualFireAlarmXMPPConnector = virtualFireAlarmXMPPConnector;
|
|
|
|
|
|
|
|
|
|
if (XmppConfig.getInstance().isEnabled()) {
|
|
|
|
|
Runnable xmppStarter = new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
virtualFireAlarmXMPPConnector.initConnector();
|
|
|
|
|
virtualFireAlarmXMPPConnector.connect();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Thread xmppStarterThread = new Thread(xmppStarter);
|
|
|
|
|
xmppStarterThread.setDaemon(true);
|
|
|
|
|
xmppStarterThread.start();
|
|
|
|
|
} else {
|
|
|
|
|
log.warn("XMPP disabled in 'devicemgt-config.xml'. Hence, VirtualFireAlarmXMPPConnector not started.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
Thread connectorThread = new Thread(connector);
|
|
|
|
|
connectorThread.setDaemon(true);
|
|
|
|
|
connectorThread.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetches the `VirtualFireAlarmMQTTConnector` specific to this VirtualFirealarm controller service.
|
|
|
|
|
*
|
|
|
|
|
* @return the 'VirtualFireAlarmMQTTConnector' instance bound to the 'virtualFireAlarmMQTTConnector' variable of
|
|
|
|
|
* this service.
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("Unused")
|
|
|
|
|
public VirtualFireAlarmMQTTConnector getVirtualFireAlarmMQTTConnector() {
|
|
|
|
|
return virtualFireAlarmMQTTConnector;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the `virtualFireAlarmMQTTConnector` variable of this VirtualFirealarm controller service.
|
|
|
|
|
*
|
|
|
|
|
* @param virtualFireAlarmMQTTConnector a 'VirtualFireAlarmMQTTConnector' object that handles all MQTT related
|
|
|
|
|
* communications of any connected VirtualFirealarm device-type
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("Unused")
|
|
|
|
|
public void setVirtualFireAlarmMQTTConnector(
|
|
|
|
|
final VirtualFireAlarmMQTTConnector virtualFireAlarmMQTTConnector) {
|
|
|
|
|
Runnable connector = new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
if (waitForServerStartup()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
VirtualFireAlarmControllerServiceImpl.this.virtualFireAlarmMQTTConnector = virtualFireAlarmMQTTConnector;
|
|
|
|
|
if (MqttConfig.getInstance().isEnabled()) {
|
|
|
|
|
virtualFireAlarmMQTTConnector.connect();
|
|
|
|
|
} else {
|
|
|
|
|
log.warn("MQTT disabled in 'devicemgt-config.xml'. Hence, VirtualFireAlarmMQTTConnector not started.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
Thread connectorThread = new Thread(connector);
|
|
|
|
|
connectorThread.setDaemon(true);
|
|
|
|
|
connectorThread.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Response registerDeviceIP(String deviceId, String deviceIP, String devicePort, HttpServletRequest request) {
|
|
|
|
|
@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);
|
|
|
|
@ -214,7 +107,10 @@ public class VirtualFireAlarmControllerServiceImpl implements VirtualFireAlarmCo
|
|
|
|
|
return Response.ok().entity(result).build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Response switchBuzzer(String deviceId, String protocol, String state) {
|
|
|
|
|
@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)) {
|
|
|
|
@ -252,7 +148,12 @@ public class VirtualFireAlarmControllerServiceImpl implements VirtualFireAlarmCo
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Response requestTemperature(String deviceId, String protocol) {
|
|
|
|
|
@GET
|
|
|
|
|
@Path("device/{deviceId}/temperature")
|
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
|
public Response requestTemperature(@PathParam("deviceId") String deviceId,
|
|
|
|
|
@QueryParam("protocol") String protocol) {
|
|
|
|
|
SensorRecord sensorRecord = null;
|
|
|
|
|
String protocolString = protocol.toUpperCase();
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
@ -289,6 +190,9 @@ public class VirtualFireAlarmControllerServiceImpl implements VirtualFireAlarmCo
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@POST
|
|
|
|
|
@Path("device/temperature")
|
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
|
public Response pushTemperatureData(final DeviceData dataMsg) {
|
|
|
|
|
String deviceId = dataMsg.deviceId;
|
|
|
|
|
String deviceIp = dataMsg.reply;
|
|
|
|
@ -312,7 +216,9 @@ public class VirtualFireAlarmControllerServiceImpl implements VirtualFireAlarmCo
|
|
|
|
|
return Response.ok().build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Response scepRequest(String operation, String message) {
|
|
|
|
|
@GET
|
|
|
|
|
@Path("device/scep")
|
|
|
|
|
public Response scepRequest(@QueryParam("operation") String operation, @QueryParam("message") String message) {
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug("Invoking SCEP operation " + operation);
|
|
|
|
|
}
|
|
|
|
@ -362,7 +268,6 @@ public class VirtualFireAlarmControllerServiceImpl implements VirtualFireAlarmCo
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug("Invoking GetCACaps");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
CertificateManagementService certificateManagementService = VirtualFireAlarmServiceUtils.
|
|
|
|
|
getCertificateManagementService();
|
|
|
|
@ -373,16 +278,15 @@ public class VirtualFireAlarmControllerServiceImpl implements VirtualFireAlarmCo
|
|
|
|
|
} 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) {
|
|
|
|
|
@POST
|
|
|
|
|
@Path("device/scep")
|
|
|
|
|
public Response scepRequestPost(@QueryParam("operation") String operation, InputStream inputStream) {
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug("Invoking SCEP operation " + operation);
|
|
|
|
|
}
|
|
|
|
@ -404,7 +308,14 @@ public class VirtualFireAlarmControllerServiceImpl implements VirtualFireAlarmCo
|
|
|
|
|
return Response.serverError().build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Response getVirtualFirealarmStats(String deviceId, String sensor, String user, long from, long to) {
|
|
|
|
|
@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) {
|
|
|
|
|
try {
|
|
|
|
|
String fromDate = String.valueOf(from);
|
|
|
|
|
String toDate = String.valueOf(to);
|
|
|
|
@ -448,6 +359,126 @@ public class VirtualFireAlarmControllerServiceImpl implements VirtualFireAlarmCo
|
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean waitForServerStartup() {
|
|
|
|
|
while (!IoTServerStartupListener.isServerReady()) {
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetches the `SecurityManager` specific to this VirtualFirealarm controller service.
|
|
|
|
|
*
|
|
|
|
|
* @return the 'SecurityManager' instance bound to the 'securityManager' variable of this service.
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("Unused")
|
|
|
|
|
public SecurityManager getSecurityManager() {
|
|
|
|
|
return securityManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the `securityManager` variable of this VirtualFirealarm controller service.
|
|
|
|
|
*
|
|
|
|
|
* @param securityManager a 'SecurityManager' object that handles the encryption, decryption, signing and validation
|
|
|
|
|
* of incoming messages from VirtualFirealarm device-types.
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("Unused")
|
|
|
|
|
public void setSecurityManager(SecurityManager securityManager) {
|
|
|
|
|
this.securityManager = securityManager;
|
|
|
|
|
securityManager.initVerificationManager();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetches the `VirtualFireAlarmXMPPConnector` specific to this VirtualFirealarm controller service.
|
|
|
|
|
*
|
|
|
|
|
* @return the 'VirtualFireAlarmXMPPConnector' instance bound to the 'virtualFireAlarmXMPPConnector' variable of
|
|
|
|
|
* this service.
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("Unused")
|
|
|
|
|
public VirtualFireAlarmXMPPConnector getVirtualFireAlarmXMPPConnector() {
|
|
|
|
|
return virtualFireAlarmXMPPConnector;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the `virtualFireAlarmXMPPConnector` variable of this VirtualFirealarm controller service.
|
|
|
|
|
*
|
|
|
|
|
* @param virtualFireAlarmXMPPConnector a 'VirtualFireAlarmXMPPConnector' object that handles all XMPP related
|
|
|
|
|
* communications of any connected VirtualFirealarm device-type
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("Unused")
|
|
|
|
|
public void setVirtualFireAlarmXMPPConnector(
|
|
|
|
|
final VirtualFireAlarmXMPPConnector virtualFireAlarmXMPPConnector) {
|
|
|
|
|
Runnable connector = new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
if (waitForServerStartup()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
VirtualFireAlarmControllerServiceImpl.this.virtualFireAlarmXMPPConnector = virtualFireAlarmXMPPConnector;
|
|
|
|
|
|
|
|
|
|
if (XmppConfig.getInstance().isEnabled()) {
|
|
|
|
|
Runnable xmppStarter = new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
virtualFireAlarmXMPPConnector.initConnector();
|
|
|
|
|
virtualFireAlarmXMPPConnector.connect();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Thread xmppStarterThread = new Thread(xmppStarter);
|
|
|
|
|
xmppStarterThread.setDaemon(true);
|
|
|
|
|
xmppStarterThread.start();
|
|
|
|
|
} else {
|
|
|
|
|
log.warn("XMPP disabled in 'devicemgt-config.xml'. Hence, VirtualFireAlarmXMPPConnector not started.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
Thread connectorThread = new Thread(connector);
|
|
|
|
|
connectorThread.setDaemon(true);
|
|
|
|
|
connectorThread.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetches the `VirtualFireAlarmMQTTConnector` specific to this VirtualFirealarm controller service.
|
|
|
|
|
*
|
|
|
|
|
* @return the 'VirtualFireAlarmMQTTConnector' instance bound to the 'virtualFireAlarmMQTTConnector' variable of
|
|
|
|
|
* this service.
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("Unused")
|
|
|
|
|
public VirtualFireAlarmMQTTConnector getVirtualFireAlarmMQTTConnector() {
|
|
|
|
|
return virtualFireAlarmMQTTConnector;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the `virtualFireAlarmMQTTConnector` variable of this VirtualFirealarm controller service.
|
|
|
|
|
*
|
|
|
|
|
* @param virtualFireAlarmMQTTConnector a 'VirtualFireAlarmMQTTConnector' object that handles all MQTT related
|
|
|
|
|
* communications of any connected VirtualFirealarm device-type
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("Unused")
|
|
|
|
|
public void setVirtualFireAlarmMQTTConnector(
|
|
|
|
|
final VirtualFireAlarmMQTTConnector virtualFireAlarmMQTTConnector) {
|
|
|
|
|
Runnable connector = new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
if (waitForServerStartup()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
VirtualFireAlarmControllerServiceImpl.this.virtualFireAlarmMQTTConnector = virtualFireAlarmMQTTConnector;
|
|
|
|
|
if (MqttConfig.getInstance().isEnabled()) {
|
|
|
|
|
virtualFireAlarmMQTTConnector.connect();
|
|
|
|
|
} else {
|
|
|
|
|
log.warn("MQTT disabled in 'devicemgt-config.xml'. Hence, VirtualFireAlarmMQTTConnector not started.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
Thread connectorThread = new Thread(connector);
|
|
|
|
|
connectorThread.setDaemon(true);
|
|
|
|
|
connectorThread.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* get the event table from the sensor name.
|
|
|
|
|
*/
|
|
|
|
|