diff --git a/frameworks/native/include/sensor_agent_proxy.h b/frameworks/native/include/sensor_agent_proxy.h index 58e8bd1f5ff5d0d2be2c815839a6694491ccc6d2..707e6e81a9755b6005277a54baaf80f7217b9d10 100644 --- a/frameworks/native/include/sensor_agent_proxy.h +++ b/frameworks/native/include/sensor_agent_proxy.h @@ -55,6 +55,7 @@ private: void ClearSensorInfos() const; std::set GetSubscribeUserCallback(int32_t sensorId); bool IsSubscribeMapEmpty() const; + void PrintAmbientLightReportCount(); static std::recursive_mutex subscribeMutex_; static std::mutex chanelMutex_; OHOS::sptr dataChannel_ = nullptr; @@ -63,6 +64,7 @@ private: int64_t reportInterval_ = -1; std::map> subscribeMap_; std::map> unsubscribeMap_; + std::thread ambientLightCountThread_; static std::mutex createChannelMutex_; }; diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index 2d023b4d0eef11e2ccd2c755ccf232d6c9f80411..2e5f25ce1742db8e337bf1095b5b978f15216fdf 100644 --- a/frameworks/native/src/sensor_agent_proxy.cpp +++ b/frameworks/native/src/sensor_agent_proxy.cpp @@ -30,6 +30,9 @@ SensorInfo *sensorInfos_ = nullptr; std::mutex sensorActiveInfoMutex_; SensorActiveInfo *sensorActiveInfos_ = nullptr; int32_t sensorInfoCount_ = 0; +std::mutex ambientLightCallBackCountMutex_; +int32_t ambientLightCallBackCount_ = 0; +bool stop_ = false; } // namespace #define SEN_CLIENT SensorServiceClient::GetInstance() @@ -38,13 +41,22 @@ std::mutex SensorAgentProxy::chanelMutex_; std::mutex SensorAgentProxy::createChannelMutex_; SensorAgentProxy::SensorAgentProxy() - : dataChannel_(new (std::nothrow) SensorDataChannel()) + : dataChannel_(new (std::nothrow) SensorDataChannel()), ambientLightCountThread_([this]() { + while (!stop_) { + this->PrintAmbientLightReportCount(); + std::this_thread::sleep_for(std::chrono::minutes(1)); + } + }) {} SensorAgentProxy::~SensorAgentProxy() { CALL_LOG_ENTER; ClearSensorInfos(); + if (ambientLightCountThread_.joinable()) { + stop_ = true; + ambientLightCountThread_.join(); + } } std::set SensorAgentProxy::GetSubscribeUserCallback(int32_t sensorId) @@ -82,6 +94,10 @@ void SensorAgentProxy::HandleSensorData(SensorEvent *events, if (eventStream.sensorTypeId == SENSOR_TYPE_ID_HALL_EXT) { PrintSensorData::GetInstance().ControlSensorClientPrint(callback, eventStream); } + if (eventStream.sensorTypeId == SENSOR_TYPE_ID_AMBIENT_LIGHT) { + std::lock_guard ambientLightCallBackCountLock(ambientLightCallBackCountMutex_); + ambientLightCallBackCount_++; + } callback(&eventStream); PrintSensorData::GetInstance().ControlSensorClientPrint(callback, eventStream); } @@ -297,6 +313,13 @@ bool SensorAgentProxy::IsSubscribeMapEmpty() const return subscribeMap_.empty(); } +void SensorAgentProxy::PrintAmbientLightReportCount() +{ + std::lock_guard ambientLightCallBackCountLock(ambientLightCallBackCountMutex_); + SEN_HILOGI("Number of reports per minuts for ambient light, count:%{public}d", ambientLightCallBackCount_); + ambientLightCallBackCount_ = 0; +} + int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser *user) { SEN_HILOGI("In, sensorId:%{public}d", sensorId);