feat(gateway_bridge): enhance DaliBridgeRequest parsing with support for unknown keys
Signed-off-by: Tony <tonylu@tony-cloud.com>
This commit is contained in:
@@ -889,26 +889,68 @@ cJSON* GatewayCloudConfigToCjson(const GatewayCloudConfig& config) {
|
|||||||
return root;
|
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 BridgeRequestFromJson(cJSON* root) {
|
||||||
DaliBridgeRequest request;
|
DaliBridgeRequest request;
|
||||||
if (const char* seq = JsonString(root, "seq")) {
|
if (const char* seq = JsonString(root, "seq")) {
|
||||||
request.sequence = 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")) {
|
if (const char* model = JsonString(root, "model")) {
|
||||||
request.modelID = 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")) {
|
if (const char* op = JsonString(root, "op")) {
|
||||||
request.operation = bridgeOperationFromString(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")) {
|
if (const auto addr = JsonInt(root, "addr")) {
|
||||||
request.rawAddress = addr.value();
|
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")) {
|
if (const auto cmd = JsonInt(root, "cmd")) {
|
||||||
request.rawCommand = cmd.value();
|
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")) {
|
if (const auto short_address = JsonInt(root, "shortAddress")) {
|
||||||
request.shortAddress = short_address.value();
|
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")) {
|
if (const cJSON* value = cJSON_GetObjectItemCaseSensitive(root, "value")) {
|
||||||
request.value = FromCjson(value);
|
request.value = FromCjson(value);
|
||||||
}
|
}
|
||||||
@@ -918,6 +960,12 @@ DaliBridgeRequest BridgeRequestFromJson(cJSON* root) {
|
|||||||
request.metadata = *object;
|
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;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user