|
|
|
@ -11,7 +11,7 @@
|
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
|
* under the License.
|
|
|
|
|
*/
|
|
|
|
@ -28,6 +28,7 @@ import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException;
|
|
|
|
|
import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementService;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.DeviceManagement;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.DeviceValidator;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.controlqueue.mqtt.MqttConfig;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.controlqueue.xmpp.XmppConfig;
|
|
|
|
@ -74,17 +75,14 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
@DeviceType(value = "virtual_firealarm")
|
|
|
|
|
@SuppressWarnings("Non-Annoted WebService")
|
|
|
|
|
public class VirtualFireAlarmControllerService {
|
|
|
|
|
private static Log log = LogFactory.getLog(VirtualFireAlarmControllerService.class);
|
|
|
|
|
|
|
|
|
|
//TODO; replace this tenant domain
|
|
|
|
|
private static final String SUPER_TENANT = "carbon.super";
|
|
|
|
|
|
|
|
|
|
@Context //injected response proxy supporting multiple thread
|
|
|
|
|
private HttpServletResponse response;
|
|
|
|
|
private static final String XMPP_PROTOCOL = "XMPP";
|
|
|
|
|
private static final String HTTP_PROTOCOL = "HTTP";
|
|
|
|
|
private static final String MQTT_PROTOCOL = "MQTT";
|
|
|
|
|
|
|
|
|
|
private static Log log = LogFactory.getLog(VirtualFireAlarmControllerService.class);
|
|
|
|
|
@Context //injected response proxy supporting multiple thread
|
|
|
|
|
private HttpServletResponse response;
|
|
|
|
|
// consists of utility methods related to encrypting and decrypting messages
|
|
|
|
|
private SecurityManager securityManager;
|
|
|
|
|
// connects to the given MQTT broker and handles MQTT communication
|
|
|
|
@ -94,6 +92,28 @@ public class VirtualFireAlarmControllerService {
|
|
|
|
|
// holds a mapping of the IP addresses to Device-IDs for HTTP communication
|
|
|
|
|
private ConcurrentHashMap<String, String> deviceToIpMap = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
|
|
private boolean waitForServerStartup() {
|
|
|
|
|
while (!DeviceManagement.isServerReady()) {
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
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.
|
|
|
|
|
*
|
|
|
|
@ -107,79 +127,95 @@ public class VirtualFireAlarmControllerService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the `virtualFireAlarmXMPPConnector` variable of this VirtualFirealarm controller service.
|
|
|
|
|
* Fetches the `VirtualFireAlarmXMPPConnector` specific to this VirtualFirealarm controller service.
|
|
|
|
|
*
|
|
|
|
|
* @param virtualFireAlarmXMPPConnector a 'VirtualFireAlarmXMPPConnector' object that handles all XMPP related
|
|
|
|
|
* communications of any connected VirtualFirealarm device-type
|
|
|
|
|
* @return the 'VirtualFireAlarmXMPPConnector' instance bound to the 'virtualFireAlarmXMPPConnector' variable of
|
|
|
|
|
* this service.
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("Unused")
|
|
|
|
|
public void setVirtualFireAlarmXMPPConnector(final VirtualFireAlarmXMPPConnector virtualFireAlarmXMPPConnector) {
|
|
|
|
|
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.");
|
|
|
|
|
}
|
|
|
|
|
public VirtualFireAlarmXMPPConnector getVirtualFireAlarmXMPPConnector() {
|
|
|
|
|
return virtualFireAlarmXMPPConnector;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the `virtualFireAlarmMQTTConnector` variable of this VirtualFirealarm controller service.
|
|
|
|
|
* Sets the `virtualFireAlarmXMPPConnector` variable of this VirtualFirealarm controller service.
|
|
|
|
|
*
|
|
|
|
|
* @param virtualFireAlarmMQTTConnector a 'VirtualFireAlarmMQTTConnector' object that handles all MQTT related
|
|
|
|
|
* @param virtualFireAlarmXMPPConnector a 'VirtualFireAlarmXMPPConnector' object that handles all XMPP related
|
|
|
|
|
* communications of any connected VirtualFirealarm device-type
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("Unused")
|
|
|
|
|
public void setVirtualFireAlarmMQTTConnector(
|
|
|
|
|
final VirtualFireAlarmMQTTConnector virtualFireAlarmMQTTConnector) {
|
|
|
|
|
this.virtualFireAlarmMQTTConnector = virtualFireAlarmMQTTConnector;
|
|
|
|
|
if (MqttConfig.getInstance().isEnabled()) {
|
|
|
|
|
virtualFireAlarmMQTTConnector.connect();
|
|
|
|
|
} else {
|
|
|
|
|
log.warn("MQTT disabled in 'devicemgt-config.xml'. Hence, VirtualFireAlarmMQTTConnector not started.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void setVirtualFireAlarmXMPPConnector(
|
|
|
|
|
final VirtualFireAlarmXMPPConnector virtualFireAlarmXMPPConnector) {
|
|
|
|
|
Runnable connector = new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
if (waitForServerStartup()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
VirtualFireAlarmControllerService.this.virtualFireAlarmXMPPConnector = virtualFireAlarmXMPPConnector;
|
|
|
|
|
|
|
|
|
|
if (XmppConfig.getInstance().isEnabled()) {
|
|
|
|
|
Runnable xmppStarter = new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
virtualFireAlarmXMPPConnector.initConnector();
|
|
|
|
|
virtualFireAlarmXMPPConnector.connect();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
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 `VirtualFireAlarmXMPPConnector` specific to this VirtualFirealarm controller service.
|
|
|
|
|
* Fetches the `VirtualFireAlarmMQTTConnector` specific to this VirtualFirealarm controller service.
|
|
|
|
|
*
|
|
|
|
|
* @return the 'VirtualFireAlarmXMPPConnector' instance bound to the 'virtualFireAlarmXMPPConnector' variable of
|
|
|
|
|
* @return the 'VirtualFireAlarmMQTTConnector' instance bound to the 'virtualFireAlarmMQTTConnector' variable of
|
|
|
|
|
* this service.
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("Unused")
|
|
|
|
|
public VirtualFireAlarmXMPPConnector getVirtualFireAlarmXMPPConnector() {
|
|
|
|
|
return virtualFireAlarmXMPPConnector;
|
|
|
|
|
public VirtualFireAlarmMQTTConnector getVirtualFireAlarmMQTTConnector() {
|
|
|
|
|
return virtualFireAlarmMQTTConnector;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetches the `VirtualFireAlarmMQTTConnector` specific to this VirtualFirealarm controller service.
|
|
|
|
|
* Sets the `virtualFireAlarmMQTTConnector` variable of this VirtualFirealarm controller service.
|
|
|
|
|
*
|
|
|
|
|
* @return the 'VirtualFireAlarmMQTTConnector' instance bound to the 'virtualFireAlarmMQTTConnector' variable of
|
|
|
|
|
* this service.
|
|
|
|
|
* @param virtualFireAlarmMQTTConnector a 'VirtualFireAlarmMQTTConnector' object that handles all MQTT related
|
|
|
|
|
* communications of any connected VirtualFirealarm device-type
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("Unused")
|
|
|
|
|
public VirtualFireAlarmMQTTConnector getVirtualFireAlarmMQTTConnector() {
|
|
|
|
|
return virtualFireAlarmMQTTConnector;
|
|
|
|
|
public void setVirtualFireAlarmMQTTConnector(
|
|
|
|
|
final VirtualFireAlarmMQTTConnector virtualFireAlarmMQTTConnector) {
|
|
|
|
|
Runnable connector = new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
while (!DeviceManagement.isServerReady()) {
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
VirtualFireAlarmControllerService.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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|