All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.hawkbit.sdk.dmf.DmfTenant Maven / Gradle / Ivy

Go to download

DMF SDK that could be used for development of DMF integrations on JVM based languages

The newest version!
/**
 * Copyright (c) 2024 Contributors to the Eclipse Foundation
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package org.eclipse.hawkbit.sdk.dmf;

import lombok.Getter;
import org.eclipse.hawkbit.sdk.Controller;
import org.eclipse.hawkbit.sdk.Tenant;
import org.eclipse.hawkbit.sdk.dmf.amqp.Amqp;
import org.eclipse.hawkbit.sdk.dmf.amqp.VHost;
import org.springframework.amqp.core.Message;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;

/**
 * An in-memory simulated DMF Tenant to hold the controller twins in
 * memory and be able to retrieve them again.
 */
public class DmfTenant {

    @Getter
    private final Tenant tenant;

    private final Map controllers = new ConcurrentHashMap<>();
    private final Amqp amqp;
    private final VHost vHost;

    public DmfTenant(final Tenant tenant, final Amqp amqp) {
        this(tenant, amqp, true);
    }

    public DmfTenant(final Tenant tenant, final Amqp amqp, final boolean initVHost) {
        this.tenant = tenant;
        this.amqp = amqp;
        this.vHost = amqp.getVhost(tenant.getDmf(), initVHost);
        this.vHost.register(this);
    }

    public void destroy() {
        controllers.values().forEach(DmfController::unregisterThing);
        controllers.clear();
    }

    public DmfController createController(final Controller controller, final UpdateHandler updateHandler) {
        final DmfController dmfController = new DmfController(tenant, controller, updateHandler, vHost);
        controllers.put(controller.getControllerId(), dmfController);
        return dmfController;
    }

    public void removeController(final String controllerId) {
        Optional.ofNullable(controllers.remove(controllerId)).ifPresent(DmfController::unregisterThing);
    }

    public void handleThingDeleted(final String controllerId) {
        Optional.ofNullable(controllers.remove(controllerId)).ifPresent(DmfController::stop);
    }

    public Optional getController(final String controllerId) {
        return Optional.ofNullable(controllers.get(controllerId));
    }

    public void ping(final String correlationId, final BiConsumer listener) {
        vHost.ping(tenant.getTenantId(), correlationId, listener);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy