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將配置下發,然後重新初始化。

 

 

 

 

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