现在一起学习一下 thingsboard 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 extensionsconfiglistener)
第二个参数是 consumer,是 lambad 表达式的使用,需要传入一个 string 类型的实现 accept 方法,accept 的实现就是 tenantserviceregistry.updateextensionconfiguration;
进入到 mqttgatewayservice 可以看到类变量 consumer 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);
所以每次云端关于 thingsboard gateway 的配置修改,都会通过 topic v1/devices/me/attributes 将配置下发,然后重新初始化。
————————————————
澳门人威尼斯3966的版权声明:本文为 csdn 博主「一个中文名」的原创文章