feat(gateway): add cloud integration for KNX and DALI with configurable transport options

Signed-off-by: Tony <tonylu@tony-cloud.com>
This commit is contained in:
Tony
2026-05-27 15:20:29 +08:00
parent 5622e6ba81
commit 7a820e700c
7 changed files with 242 additions and 10 deletions
@@ -1226,6 +1226,28 @@ std::optional<GatewayBridgeStoredConfig> GatewayBridgeStoredConfigFromJson(std::
GatewayCloudConfig GatewayCloudConfigFromJson(cJSON* root) {
GatewayCloudConfig config;
#if defined(CONFIG_GATEWAY_CLOUD_TOPIC_PREFIX)
config.topicPrefix = CONFIG_GATEWAY_CLOUD_TOPIC_PREFIX;
#endif
#if defined(CONFIG_GATEWAY_CLOUD_CEMI_TRANSPORT_LTE_UART)
config.cemiTransport = "lte_uart";
config.lteUartEnabled = true;
#elif defined(CONFIG_GATEWAY_CLOUD_CEMI_TRANSPORT_MQTT_AND_LTE_UART)
config.cemiTransport = "mqtt_lte_uart";
config.lteUartEnabled = true;
#endif
#if defined(CONFIG_GATEWAY_CLOUD_LTE_UART_PORT)
config.lteUartPort = CONFIG_GATEWAY_CLOUD_LTE_UART_PORT;
#endif
#if defined(CONFIG_GATEWAY_CLOUD_LTE_UART_TX_PIN)
config.lteTxPin = CONFIG_GATEWAY_CLOUD_LTE_UART_TX_PIN;
#endif
#if defined(CONFIG_GATEWAY_CLOUD_LTE_UART_RX_PIN)
config.lteRxPin = CONFIG_GATEWAY_CLOUD_LTE_UART_RX_PIN;
#endif
#if defined(CONFIG_GATEWAY_CLOUD_LTE_UART_BAUDRATE)
config.lteBaudrate = CONFIG_GATEWAY_CLOUD_LTE_UART_BAUDRATE;
#endif
if (const char* value = JsonString(root, "brokerURI")) {
config.brokerURI = value;
}
@@ -1241,6 +1263,28 @@ GatewayCloudConfig GatewayCloudConfigFromJson(cJSON* root) {
if (const char* value = JsonString(root, "topicPrefix")) {
config.topicPrefix = value;
}
if (const char* value = JsonString(root, "cemiTransport")) {
config.cemiTransport = value;
}
config.lteUartEnabled = JsonBool(root, "lteUartEnabled", config.lteUartEnabled);
if (const auto value = JsonInt(root, "lteUartPort")) {
config.lteUartPort = value.value();
}
if (const auto value = JsonInt(root, "lteTxPin")) {
config.lteTxPin = value.value();
}
if (const auto value = JsonInt(root, "lteRxPin")) {
config.lteRxPin = value.value();
}
if (const auto value = JsonInt(root, "lteBaudrate")) {
config.lteBaudrate = value.value();
}
if (const auto value = JsonInt(root, "lteRxBufferSize")) {
config.lteRxBufferSize = value.value();
}
if (const auto value = JsonInt(root, "lteTxBufferSize")) {
config.lteTxBufferSize = value.value();
}
if (const auto qos = JsonInt(root, "qos")) {
config.qos = qos.value();
}
@@ -1257,6 +1301,14 @@ cJSON* GatewayCloudConfigToCjson(const GatewayCloudConfig& config) {
cJSON_AddStringToObject(root, "username", config.username.c_str());
cJSON_AddStringToObject(root, "password", config.password.c_str());
cJSON_AddStringToObject(root, "topicPrefix", config.topicPrefix.c_str());
cJSON_AddStringToObject(root, "cemiTransport", config.cemiTransport.c_str());
cJSON_AddBoolToObject(root, "lteUartEnabled", config.lteUartEnabled);
cJSON_AddNumberToObject(root, "lteUartPort", config.lteUartPort);
cJSON_AddNumberToObject(root, "lteTxPin", config.lteTxPin);
cJSON_AddNumberToObject(root, "lteRxPin", config.lteRxPin);
cJSON_AddNumberToObject(root, "lteBaudrate", config.lteBaudrate);
cJSON_AddNumberToObject(root, "lteRxBufferSize", config.lteRxBufferSize);
cJSON_AddNumberToObject(root, "lteTxBufferSize", config.lteTxBufferSize);
cJSON_AddNumberToObject(root, "qos", config.qos);
return root;
}
@@ -1832,6 +1884,22 @@ struct GatewayBridgeService::ChannelRuntime {
for (const auto& model : bridge_config.models) {
cloud->bridge().upsertModel(model);
}
wireCloudCemiProxyLocked();
}
void wireCloudCemiProxyLocked() {
if (knx_router != nullptr) {
knx_router->setCloudCemiPublisher([this](const uint8_t* data, size_t len) {
if (cloud_started && cloud != nullptr) {
cloud->publishCemiFrame(data, len);
}
});
}
if (cloud != nullptr) {
cloud->setCemiDownlinkHandler([this](const uint8_t* data, size_t len) {
return knx_router != nullptr && knx_router->injectCloudCemiFrame(data, len);
});
}
}
esp_err_t saveBridgeConfig(std::string_view json) {
@@ -2454,8 +2522,12 @@ struct GatewayBridgeService::ChannelRuntime {
cJSON* cloud_remote_json = cJSON_CreateObject();
if (cloud_remote_json != nullptr) {
const auto& cloud_remote = effective_knx->oam_router.cloud_remote;
const auto cloud_stats = knx_router == nullptr
? GatewayKnxTpIpRouter::CloudCemiStats{}
: knx_router->cloudCemiStats();
cJSON_AddBoolToObject(cloud_remote_json, "prepared", true);
cJSON_AddBoolToObject(cloud_remote_json, "enabled", cloud_remote.enabled);
cJSON_AddBoolToObject(cloud_remote_json, "running", cloud_stats.enabled);
cJSON_AddStringToObject(cloud_remote_json, "mode", cloud_remote.mode.c_str());
cJSON_AddBoolToObject(cloud_remote_json, "requireSecureTunnel",
cloud_remote.require_secure_tunnel);
@@ -2467,6 +2539,10 @@ struct GatewayBridgeService::ChannelRuntime {
!cloud_remote.mqtt_topic_prefix.empty());
cJSON_AddBoolToObject(cloud_remote_json, "authTokenRefConfigured",
!cloud_remote.auth_token_ref.empty());
cJSON_AddNumberToObject(cloud_remote_json, "uplinkFrames",
static_cast<double>(cloud_stats.uplink_frames));
cJSON_AddNumberToObject(cloud_remote_json, "downlinkFrames",
static_cast<double>(cloud_stats.downlink_frames));
cJSON_AddItemToObject(knx_json, "cloudKnxRemoteAccess", cloud_remote_json);
}
cJSON* serial_json = cJSON_CreateObject();
@@ -2564,9 +2640,16 @@ struct GatewayBridgeService::ChannelRuntime {
cJSON_AddBoolToObject(cloud_json, "configured", cloud_config_loaded);
cJSON_AddBoolToObject(cloud_json, "started", cloud_started);
cJSON_AddBoolToObject(cloud_json, "connected", cloud != nullptr && cloud->isConnected());
cJSON_AddBoolToObject(cloud_json, "lteUartActive",
cloud != nullptr && cloud->lteUartActive());
if (cloud_config.has_value()) {
cJSON_AddStringToObject(cloud_json, "deviceID", cloud_config->deviceID.c_str());
cJSON_AddStringToObject(cloud_json, "topicPrefix", cloud_config->topicPrefix.c_str());
cJSON_AddStringToObject(cloud_json, "cemiTransport",
cloud_config->cemiTransport.c_str());
cJSON_AddBoolToObject(cloud_json, "lteUartEnabled",
cloud_config->lteUartEnabled);
cJSON_AddNumberToObject(cloud_json, "lteUartPort", cloud_config->lteUartPort);
}
cJSON_AddItemToObject(root, "cloud", cloud_json);
}
@@ -2968,6 +3051,7 @@ struct GatewayBridgeService::ChannelRuntime {
cJSON_AddBoolToObject(root, "configured", cloud_config_loaded);
cJSON_AddBoolToObject(root, "started", cloud_started);
cJSON_AddBoolToObject(root, "connected", cloud != nullptr && cloud->isConnected());
cJSON_AddBoolToObject(root, "lteUartActive", cloud != nullptr && cloud->lteUartActive());
if (cloud_config.has_value()) {
cJSON_AddItemToObject(root, "config", GatewayCloudConfigToCjson(cloud_config.value()));
}