From 5ccab6d822efc2eb6c407ea24961b57d86a8195b Mon Sep 17 00:00:00 2001 From: wuzhihuitmac Date: Wed, 11 Sep 2024 17:33:34 +0800 Subject: [PATCH] Modify the issue about data report Signed-off-by: wuzhihuitmac Change-Id: I3ac94a53f069865c0f0b5f4e5cc3e765713d0d57 --- .../native/include/sensor_agent_proxy.h | 2 +- frameworks/native/src/sensor_agent_proxy.cpp | 27 +++++++++++-------- utils/common/include/print_sensor_data.h | 8 +++--- utils/common/src/print_sensor_data.cpp | 22 +++++++-------- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/frameworks/native/include/sensor_agent_proxy.h b/frameworks/native/include/sensor_agent_proxy.h index f04d6678..b5258f7d 100644 --- a/frameworks/native/include/sensor_agent_proxy.h +++ b/frameworks/native/include/sensor_agent_proxy.h @@ -57,7 +57,7 @@ private: int32_t DestroySensorDataChannel(); int32_t ConvertSensorInfos() const; void ClearSensorInfos() const; - std::set GetSubscribeUser(int32_t sensorId); + std::set GetSubscribeUserCallback(int32_t sensorId); static std::recursive_mutex subscribeMutex_; static std::mutex chanelMutex_; OHOS::sptr dataChannel_ = nullptr; diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index 2e545091..53b1715e 100644 --- a/frameworks/native/src/sensor_agent_proxy.cpp +++ b/frameworks/native/src/sensor_agent_proxy.cpp @@ -49,7 +49,7 @@ SensorAgentProxy::~SensorAgentProxy() ClearSensorInfos(); } -std::set SensorAgentProxy::GetSubscribeUser(int32_t sensorId) +std::set SensorAgentProxy::GetSubscribeUserCallback(int32_t sensorId) { std::lock_guard subscribeLock(subscribeMutex_); auto iter = subscribeMap_.find(sensorId); @@ -57,7 +57,14 @@ std::set SensorAgentProxy::GetSubscribeUser(int32_t sensorId SEN_HILOGE("Sensor is not subscribed"); return {}; } - return {iter->second}; + std::set callback; + for (const auto &it : iter->second) { + auto ret = callback.insert(it->callback); + if (!ret.second) { + SEN_HILOGE("callback insert fail"); + } + } + return callback; } void SensorAgentProxy::HandleSensorData(SensorEvent *events, @@ -71,13 +78,11 @@ void SensorAgentProxy::HandleSensorData(SensorEvent *events, SensorEvent eventStream; for (int32_t i = 0; i < num; ++i) { eventStream = events[i]; - std::set users = GetSubscribeUser(eventStream.sensorTypeId); - for (const auto &user : users) { - CHKPV(user); - RecordSensorCallback fun = user->callback; - CHKPV(fun); - fun(&eventStream); - PrintSensorData::GetInstance().ControlSensorClientPrint(user, eventStream); + auto callbacks = GetSubscribeUserCallback(eventStream.sensorTypeId); + for (const auto &callback : callbacks) { + CHKPV(callback); + callback(&eventStream); + PrintSensorData::GetInstance().ControlSensorClientPrint(callback, eventStream); } } } @@ -255,7 +260,7 @@ int32_t SensorAgentProxy::SubscribeSensor(int32_t sensorId, const SensorUser *us SEN_HILOGD("User has been subscribed"); } if (PrintSensorData::GetInstance().IsContinuousType(sensorId)) { - PrintSensorData::GetInstance().SavePrintUserInfo(user); + PrintSensorData::GetInstance().SavePrintUserInfo(user->callback); } return OHOS::Sensors::SUCCESS; } @@ -291,7 +296,7 @@ int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser * unsubscribeMap_.erase(sensorId); } if (PrintSensorData::GetInstance().IsContinuousType(sensorId)) { - PrintSensorData::GetInstance().RemovePrintUserInfo(user); + PrintSensorData::GetInstance().RemovePrintUserInfo(user->callback); } return OHOS::Sensors::SUCCESS; } diff --git a/utils/common/include/print_sensor_data.h b/utils/common/include/print_sensor_data.h index f6d4e967..86e0bfa8 100644 --- a/utils/common/include/print_sensor_data.h +++ b/utils/common/include/print_sensor_data.h @@ -31,12 +31,12 @@ class PrintSensorData : public Singleton { public: PrintSensorData() = default; virtual ~PrintSensorData() {}; - void ControlSensorClientPrint(const SensorUser *user, const SensorEvent &event); + void ControlSensorClientPrint(const RecordSensorCallback callback, const SensorEvent &event); void ControlSensorHdiPrint(const SensorData &sensorData); void ResetHdiCounter(int32_t sensorId); bool IsContinuousType(int32_t sensorId); - void SavePrintUserInfo(const SensorUser *user); - void RemovePrintUserInfo(const SensorUser *user); + void SavePrintUserInfo(const RecordSensorCallback callback); + void RemovePrintUserInfo(const RecordSensorCallback callback); private: void PrintClientData(const SensorEvent &event); @@ -57,7 +57,7 @@ private: {SENSOR_TYPE_ID_AMBIENT_LIGHT1, info_}, {SENSOR_TYPE_ID_MAGNETIC_FIELD, info_}, }; - std::map clientLoginfo_; + std::map clientLoginfo_; }; } // namespace Sensors } // namespace OHOS diff --git a/utils/common/src/print_sensor_data.cpp b/utils/common/src/print_sensor_data.cpp index 40a443f2..a6ab3df9 100644 --- a/utils/common/src/print_sensor_data.cpp +++ b/utils/common/src/print_sensor_data.cpp @@ -120,7 +120,7 @@ int32_t PrintSensorData::GetDataDimension(int32_t sensorId) } } -void PrintSensorData::ControlSensorClientPrint(const SensorUser *user, const SensorEvent &event) +void PrintSensorData::ControlSensorClientPrint(const RecordSensorCallback callback, const SensorEvent &event) { auto triggerIt = std::find(g_triggerSensorType.begin(), g_triggerSensorType.end(), event.sensorTypeId); if (triggerIt != g_triggerSensorType.end()) { @@ -132,7 +132,7 @@ void PrintSensorData::ControlSensorClientPrint(const SensorUser *user, const Sen return; } std::lock_guard clientLoginfoLock(clientLoginfoMutex_); - auto it = clientLoginfo_.find(user); + auto it = clientLoginfo_.find(callback); if (it == clientLoginfo_.end()) { return; } @@ -175,28 +175,28 @@ bool PrintSensorData::IsContinuousType(int32_t sensorId) sensorId) != g_continuousSensorType.end(); } -void PrintSensorData::SavePrintUserInfo(const SensorUser *user) +void PrintSensorData::SavePrintUserInfo(const RecordSensorCallback callback) { - CHKPV(user); + CHKPV(callback); std::lock_guard clientLoginfoLock(clientLoginfoMutex_); - if (clientLoginfo_.find(user) != clientLoginfo_.end()) { + if (clientLoginfo_.find(callback) != clientLoginfo_.end()) { return; } LogPrintInfo info; - auto status = clientLoginfo_.insert(std::make_pair(user, info)); + auto status = clientLoginfo_.insert(std::make_pair(callback, info)); if (!status.second) { - SEN_HILOGD("User has been subscribed"); + SEN_HILOGD("callback has been saved"); } } -void PrintSensorData::RemovePrintUserInfo(const SensorUser *user) +void PrintSensorData::RemovePrintUserInfo(const RecordSensorCallback callback) { - CHKPV(user); + CHKPV(callback); std::lock_guard clientLoginfoLock(clientLoginfoMutex_); - if (clientLoginfo_.find(user) == clientLoginfo_.end()) { + if (clientLoginfo_.find(callback) == clientLoginfo_.end()) { return; } - clientLoginfo_.erase(user); + clientLoginfo_.erase(callback); } void PrintSensorData::ResetHdiCounter(int32_t sensorId) -- Gitee