Search in sources :

Example 1 with MQTTClientConnection

use of org.structr.messaging.implementation.mqtt.MQTTClientConnection in project structr by structr.

the class MQTTClient method onModification.

static void onModification(MQTTClient thisClient, final SecurityContext securityContext, final ErrorBuffer errorBuffer, final ModificationQueue modificationQueue) throws FrameworkException {
    if (modificationQueue.isPropertyModified(thisClient, StructrApp.key(MQTTClient.class, "protocol")) || modificationQueue.isPropertyModified(thisClient, StructrApp.key(MQTTClient.class, "url")) || modificationQueue.isPropertyModified(thisClient, StructrApp.key(MQTTClient.class, "port"))) {
        MQTTContext.disconnect(thisClient);
    }
    if (modificationQueue.isPropertyModified(thisClient, StructrApp.key(MQTTClient.class, "isEnabled")) || modificationQueue.isPropertyModified(thisClient, StructrApp.key(MQTTClient.class, "protocol")) || modificationQueue.isPropertyModified(thisClient, StructrApp.key(MQTTClient.class, "url")) || modificationQueue.isPropertyModified(thisClient, StructrApp.key(MQTTClient.class, "port"))) {
        MQTTClientConnection connection = MQTTContext.getClientForId(thisClient.getUuid());
        boolean enabled = thisClient.getIsEnabled();
        if (!enabled) {
            if (connection != null && connection.isConnected()) {
                MQTTContext.disconnect(thisClient);
                thisClient.setProperties(securityContext, new PropertyMap(StructrApp.key(MQTTClient.class, "isConnected"), false));
            }
        } else {
            if (connection == null || !connection.isConnected()) {
                MQTTContext.connect(thisClient);
                MQTTContext.subscribeAllTopics(thisClient);
            }
            connection = MQTTContext.getClientForId(thisClient.getUuid());
            if (connection != null) {
                if (connection.isConnected()) {
                    thisClient.setProperties(securityContext, new PropertyMap(StructrApp.key(MQTTClient.class, "isConnected"), true));
                } else {
                    thisClient.setProperties(securityContext, new PropertyMap(StructrApp.key(MQTTClient.class, "isConnected"), false));
                }
            }
        }
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) MQTTClientConnection(org.structr.messaging.implementation.mqtt.MQTTClientConnection)

Aggregations

PropertyMap (org.structr.core.property.PropertyMap)1 MQTTClientConnection (org.structr.messaging.implementation.mqtt.MQTTClientConnection)1