[core][power_manager] review fixes

This commit is contained in:
Arsentiy Milchakov 2019-01-21 15:59:56 +03:00 committed by mpimenov
parent d5eb25cfb0
commit 1eece0bef3
11 changed files with 47 additions and 37 deletions

View file

@ -87,8 +87,6 @@ set(
notifications/notification_queue_storage.hpp
place_page_info.cpp
place_page_info.hpp
power_management/battery_tracker.cpp
power_management/battery_tracker.hpp
power_management/power_manager.cpp
power_management/power_manager.hpp
power_management/power_management_schemas.cpp

View file

@ -51,7 +51,7 @@ void PowerManager::Load()
m_config = result;
if (m_config.m_scheme == Scheme::Auto)
m_batteryTracker.Subscribe(this);
GetPlatform().GetBatteryTracker().Subscribe(this);
for (size_t i = 0; i < m_config.m_facilities.size(); ++i)
{
@ -86,7 +86,7 @@ void PowerManager::SetFacility(Facility const facility, bool enabled)
auto const isSchemeChanged = m_config.m_scheme != Scheme::None;
if (m_config.m_scheme == Scheme::Auto)
m_batteryTracker.Unsubscribe(this);
GetPlatform().GetBatteryTracker().Unsubscribe(this);
m_config.m_scheme = Scheme::None;
@ -107,9 +107,9 @@ void PowerManager::SetScheme(Scheme const scheme)
m_config.m_scheme = scheme;
if (m_config.m_scheme == Scheme::Auto)
m_batteryTracker.Subscribe(this);
GetPlatform().GetBatteryTracker().Subscribe(this);
else
m_batteryTracker.Unsubscribe(this);
GetPlatform().GetBatteryTracker().Unsubscribe(this);
if (m_config.m_scheme == Scheme::None || m_config.m_scheme == Scheme::Auto)
{
@ -160,8 +160,6 @@ void PowerManager::OnBatteryLevelReceived(uint8_t level)
{
CHECK_LESS_OR_EQUAL(level, 100, ());
GetPlatform().RunDelayedTask(Platform::Thread::Background, std::chrono::minutes(10), [] {});
if (m_config.m_scheme != Scheme::Auto)
return;

View file

@ -1,6 +1,7 @@
#pragma once
#include "map/power_management/battery_tracker.hpp"
#include "platform/battery_tracker.hpp"
#include "map/power_management/power_management_schemas.hpp"
#include "base/visitor.hpp"
@ -12,7 +13,7 @@
namespace power_management
{
// Note: this class is NOT thread-safe.
class PowerManager : public BatteryLevelTracker::Subscriber
class PowerManager : public platform::BatteryLevelTracker::Subscriber
{
public:
class Subscriber
@ -32,6 +33,7 @@ public:
FacilitiesState const & GetFacilities() const;
Scheme const & GetScheme() const;
// BatteryLevelTracker::Subscriber overrides:
void OnBatteryLevelReceived(uint8_t level) override;
void Subscribe(Subscriber * subscriber);
@ -53,6 +55,5 @@ private:
std::vector<Subscriber *> m_subscribers;
Config m_config;
BatteryLevelTracker m_batteryTracker;
};
} // namespace power_management

View file

@ -6,6 +6,8 @@ set(CMAKE_AUTOMOC ON)
set(
SRC
battery_tracker.cpp
battery_tracker.hpp
chunks_download_strategy.cpp
chunks_download_strategy.hpp
constants.hpp

View file

@ -1,4 +1,4 @@
#include "map/power_management/battery_tracker.hpp"
#include "platform/battery_tracker.hpp"
#include "platform/platform.hpp"
@ -6,27 +6,27 @@ namespace
{
auto const kBatteryTrackingInterval = std::chrono::minutes(10);
bool IsLevelExpired(std::chrono::system_clock::time_point lastRequestedTime)
bool IsLevelExpired(std::chrono::system_clock::time_point lastRequestTime)
{
return std::chrono::system_clock::now() - lastRequestedTime > kBatteryTrackingInterval;
return std::chrono::system_clock::now() - lastRequestTime > kBatteryTrackingInterval;
}
} // namespace
namespace power_management
namespace platform
{
void BatteryLevelTracker::Subscribe(Subscriber * subscriber)
{
m_subscribers.push_back(subscriber);
if (IsLevelExpired(m_lastRequestedTime))
if (IsLevelExpired(m_lastRequestTime))
{
// Run periodic requests when first subscriber is added.
// Run periodic requests when the first subscriber is added.
if (m_subscribers.size() == 1)
RequestBatteryLevel();
}
else
{
subscriber->OnBatteryLevelReceived(m_lastReceivedlevel);
subscriber->OnBatteryLevelReceived(m_lastReceivedLevel);
}
}
@ -41,26 +41,20 @@ void BatteryLevelTracker::UnsubscribeAll()
m_subscribers.clear();
}
uint8_t BatteryLevelTracker::GetBatteryLevel()
{
if (IsLevelExpired(m_lastRequestedTime))
{
m_lastReceivedlevel = GetPlatform().GetBatteryLevel();
m_lastRequestedTime = std::chrono::system_clock::now();
}
return m_lastReceivedlevel;
}
void BatteryLevelTracker::RequestBatteryLevel()
{
if (m_subscribers.empty())
return;
auto const level = GetBatteryLevel();
if (IsLevelExpired(m_lastRequestTime))
{
m_lastReceivedLevel = GetPlatform().GetBatteryLevel();
m_lastRequestTime = std::chrono::system_clock::now();
}
for (auto s : m_subscribers)
{
s->OnBatteryLevelReceived(level);
s->OnBatteryLevelReceived(m_lastReceivedLevel);
}
GetPlatform().RunDelayedTask(Platform::Thread::Background, kBatteryTrackingInterval, [this]
@ -71,4 +65,4 @@ void BatteryLevelTracker::RequestBatteryLevel()
});
});
}
} // namespace power_management
} // namespace platform

View file

@ -4,8 +4,9 @@
#include <cstdint>
#include <vector>
namespace power_management
namespace platform
{
// Note: this class is NOT thread-safe.
class BatteryLevelTracker
{
public:
@ -20,11 +21,10 @@ public:
void UnsubscribeAll();
private:
uint8_t GetBatteryLevel();
void RequestBatteryLevel();
std::vector<Subscriber *> m_subscribers;
std::chrono::system_clock::time_point m_lastRequestedTime;
uint8_t m_lastReceivedlevel = 0;
std::chrono::system_clock::time_point m_lastRequestTime;
uint8_t m_lastReceivedLevel = 0;
};
} // power_management
} // platform

View file

@ -329,6 +329,9 @@ unsigned Platform::CpuCores() const
void Platform::ShutdownThreads()
{
ASSERT(m_networkThread && m_fileThread && m_backgroundThread, ());
m_batteryTracker.UnsubscribeAll();
m_networkThread->ShutdownAndJoin();
m_fileThread->ShutdownAndJoin();
m_backgroundThread->ShutdownAndJoin();

View file

@ -1,5 +1,6 @@
#pragma once
#include "platform/battery_tracker.hpp"
#include "platform/country_defines.hpp"
#include "platform/gui_thread.hpp"
#include "platform/http_user_agent.hpp"
@ -131,6 +132,8 @@ protected:
std::unique_ptr<base::WorkerThread> m_fileThread;
std::unique_ptr<base::WorkerThread> m_backgroundThread;
platform::BatteryLevelTracker m_batteryTracker;
public:
Platform();
virtual ~Platform() = default;
@ -291,6 +294,9 @@ public:
static bool IsConnected() { return ConnectionStatus() != EConnectionType::CONNECTION_NONE; }
static ChargingStatus GetChargingStatus();
// Returns current battery level. Possible values are from 0 to 100.
// Returns 100 when actual level is unknown.
static uint8_t GetBatteryLevel();
void SetupMeasurementSystem() const;
@ -347,6 +353,8 @@ public:
// Use this method for testing purposes only.
void SetGuiThread(std::unique_ptr<base::TaskLoop> guiThread);
platform::BatteryLevelTracker & GetBatteryTracker() { return m_batteryTracker; }
private:
void RunThreads();
void ShutdownThreads();

View file

@ -228,6 +228,10 @@ Platform::ChargingStatus Platform::GetChargingStatus()
uint8_t Platform::GetBatteryLevel()
{
auto const level = UIDevice.currentDevice.batteryLevel;
ASSERT_GREATER_OR_EQUAL(level, -1.0, ());
ASSERT_LESS_OR_EQUAL(level, 1.0, ());
if (level == -1.0)
return 100;

View file

@ -262,6 +262,7 @@ Platform::ChargingStatus Platform::GetChargingStatus()
uint8_t Platform::GetBatteryLevel()
{
// This value is always 100 for desktop.
return 100;
}

View file

@ -176,6 +176,7 @@ Platform::ChargingStatus Platform::GetChargingStatus()
uint8_t Platform::GetBatteryLevel()
{
// This value is always 100 for desktop.
return 100;
}