sending part of IoT states

This commit is contained in:
Xiaoxia
2025-03-08 04:04:40 +08:00
parent b8bf6bea2e
commit 8267f59a6a
5 changed files with 33 additions and 16 deletions

View File

@@ -22,16 +22,32 @@ std::string ThingManager::GetDescriptorsJson() {
return json_str;
}
std::string ThingManager::GetStatesJson() {
std::string json_str = "[";
bool ThingManager::GetStatesJson(std::string& json, bool delta) {
if (!delta) {
last_states_.clear();
}
bool changed = false;
json = "[";
// 枚举thing获取每个thing的state如果发生变化则更新保存到last_states_
// 如果delta为true则只返回变化的部分
for (auto& thing : things_) {
json_str += thing->GetStateJson() + ",";
std::string state = thing->GetStateJson();
if (delta) {
// 如果delta为true则只返回变化的部分
auto it = last_states_.find(thing->name());
if (it != last_states_.end() && it->second == state) {
continue;
}
changed = true;
last_states_[thing->name()] = state;
}
json += state + ",";
}
if (json_str.back() == ',') {
json_str.pop_back();
if (json.back() == ',') {
json.pop_back();
}
json_str += "]";
return json_str;
json += "]";
return changed;
}
void ThingManager::Invoke(const cJSON* command) {