Thingsboard Gateway 根据云端配置初始化

我在博客Thingsboard Gateway简单说了下thingsborad初始化过程和流程图,现在来详细说一下Gateway通过云端配置来初始化的过程;

首先我们注意到TenantServiceRegistry这个类,它的方法updateExtensionConfiguration;如果配置不存在就初始化,存在就update,最终来实现数据的采集。

它负责opc、mqtt、modbus等extensions的初始化,举例DefaultOpcUaService;

public class DefaultOpcUaService extends ExtensionUpdate implements OpcUaService 

通过init来初始化具体执行的Monitor,或者在云端配置更新的时候通过update方法来destroy资源然后重新初始化。

public abstract class ExtensionUpdate implements ExtensionService {

    public void update (TbExtensionConfiguration configurationNode) throws Exception {
        destroy();
        init(configurationNode, true);
    }
}

可以看到唯一调用TenantServiceRegistry.updateExtensionConfiguration的方法在DefaultTenantManagerService的init方法中;

getGatewayServiceBean(TbTenantConfiguration configuration, Consumer<String> extensionsConfigListener)

第二个参数是consumer,是lambad 表达式的使用,需要传入一个String类型的实现accept方法,

accept的实现就是TenantServiceRegistry.updateExtensionConfiguration;

进入到MqttGatewayService可以看到类变量Consumer<String> extensionsConfigListener,找到String类型的c从哪里来

    private void updateConfiguration(String configuration) {
        try {
            if (extensionsConfigListener != null) {
                extensionsConfigListener.accept(configuration);
            }
            onAppliedConfiguration(configuration);
        } catch (Exception e) {
            log.warn("Failed to update extension configurations [[]]", e.getMessage(), e);
        }
    }

而这个String类型的消息configuration实际上是通过onMessage方法的payload得到的,实际上就是mqtt的message

    @Override
    public void onMessage(String topic, ByteBuf payload) {
        String message = payload.toString(StandardCharsets.UTF_8);

        log.trace("Message arrived [{}] {}", topic, message);
        callbackExecutor.submit(() -> {
            try {
                if (topic.equals(GATEWAY_ATTRIBUTES_TOPIC)) {
                    onAttributesUpdate(message);
                } else if (topic.equals(GATEWAY_RESPONSES_ATTRIBUTES_TOPIC)) {
                    onDeviceAttributesResponse(message);
                } else if (topic.equals(GATEWAY_RPC_TOPIC)) {
                    onRpcCommand(message);
                } else if (topic.equals(DEVICE_ATTRIBUTES_TOPIC)) {
                    onGatewayAttributesUpdate(message);
                } else if (topic.equals(DEVICE_GET_ATTRIBUTES_RESPONSE_TOPIC)) {
                    onGatewayAttributesGet(message);
                }
            } catch (Exception e) {
                log.warn("Failed to process arrived message!", message);
            }
        });

    }

实际上如果配置下发,对应的topic是DEVICE_ATTRIBUTES_TOPIC(v1/devices/me/attributes);

 

所以每次云端关于gateway的配置修改,都会通过topic v1/devices/me/attributes将配置下发,然后重新初始化。

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章