feat(gateway_bridge): enhance DaliBridgeRequest parsing with support for unknown keys

Signed-off-by: Tony <tonylu@tony-cloud.com>
This commit is contained in:
Tony
2026-05-04 09:46:24 +08:00
parent ee1246c942
commit 640e78f688
@@ -889,26 +889,68 @@ cJSON* GatewayCloudConfigToCjson(const GatewayCloudConfig& config) {
return root;
}
bool IsKnownBridgeRequestKey(const char* key) {
if (key == nullptr) return true;
static const char* known[] = {"type", "seq", "sequence", "model",
"modelID", "modelId", "op", "operation",
"addr", "rawAddress", "cmd", "rawCommand",
"shortAddress", "short_address", "value", "meta"};
for (const char* item : known) {
if (std::strcmp(key, item) == 0) return true;
}
return false;
}
DaliBridgeRequest BridgeRequestFromJson(cJSON* root) {
DaliBridgeRequest request;
if (const char* seq = JsonString(root, "seq")) {
request.sequence = seq;
}
if (request.sequence.empty()) {
if (const char* seq = JsonString(root, "sequence")) {
request.sequence = seq;
}
}
if (const char* model = JsonString(root, "model")) {
request.modelID = model;
}
if (request.modelID.empty()) {
if (const char* model = JsonString(root, "modelID")) {
request.modelID = model;
}
}
if (const char* op = JsonString(root, "op")) {
request.operation = bridgeOperationFromString(op);
}
if (!request.operation.has_value()) {
if (const char* op = JsonString(root, "operation")) {
request.operation = bridgeOperationFromString(op);
}
}
if (const auto addr = JsonInt(root, "addr")) {
request.rawAddress = addr.value();
}
if (!request.rawAddress.has_value()) {
if (const auto addr = JsonInt(root, "rawAddress")) {
request.rawAddress = addr.value();
}
}
if (const auto cmd = JsonInt(root, "cmd")) {
request.rawCommand = cmd.value();
}
if (!request.rawCommand.has_value()) {
if (const auto cmd = JsonInt(root, "rawCommand")) {
request.rawCommand = cmd.value();
}
}
if (const auto short_address = JsonInt(root, "shortAddress")) {
request.shortAddress = short_address.value();
}
if (!request.shortAddress.has_value()) {
if (const auto short_address = JsonInt(root, "short_address")) {
request.shortAddress = short_address.value();
}
}
if (const cJSON* value = cJSON_GetObjectItemCaseSensitive(root, "value")) {
request.value = FromCjson(value);
}
@@ -918,6 +960,12 @@ DaliBridgeRequest BridgeRequestFromJson(cJSON* root) {
request.metadata = *object;
}
}
for (const cJSON* child = root != nullptr ? root->child : nullptr; child != nullptr;
child = child->next) {
if (!IsKnownBridgeRequestKey(child->string)) {
request.metadata[child->string] = FromCjson(child);
}
}
return request;
}