• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "thermal_service_subscriber.h"
17 
18 #include <functional>
19 
20 #include "string_operation.h"
21 #include "thermal_service.h"
22 #include "thermal_common.h"
23 #include "constants.h"
24 namespace OHOS {
25 namespace PowerMgr {
26 namespace {
27 const int32_t COUNT_MAX = 6;
28 const int32_t SEC_MIN_NUM = 60;
29 const int32_t THOUSAND_UNIT = 1000;
30 auto g_service = DelayedSpSingleton<ThermalService>::GetInstance();
31 }
ThermalServiceSubscriber()32 ThermalServiceSubscriber::ThermalServiceSubscriber() { }
33 
Init()34 bool ThermalServiceSubscriber::Init()
35 {
36     historyCount_ = g_service->GetBaseinfoObj()->GetHistoryTempCount();
37     return true;
38 }
39 
OnTemperatureChanged(TypeTempMap typeTempMap)40 void ThermalServiceSubscriber::OnTemperatureChanged(TypeTempMap typeTempMap)
41 {
42     std::lock_guard<std::mutex> lock(mutex_);
43     if (typeTempMap.empty()) {
44         THERMAL_HILOGE(COMP_SVC, "failed to get sensor info: %{public}zu", typeTempMap.size());
45         return;
46     }
47 
48     if (!typeTempMap_.empty()) {
49         typeTempMap_.clear();
50     }
51 
52     for (auto it : typeTempMap) {
53         typeTempMap_[it.first] = it.second;
54     }
55 
56     g_service->GetSensorInfo()->SetTypeTempMap(typeTempMap_);
57     g_service->GetSensorInfo()->NotifyObserver();
58 
59     if (count_ == 0) {
60         startTime_ = time(NULL);
61     }
62 
63     if (count_ == COUNT_MAX) {
64         endTime_ = time(NULL);
65         magnification_ ++;
66         count_ = 0;
67     }
68 
69     SetHistoryTypeTempMap(typeTempMap_);
70     count_ ++;
71     return;
72 }
73 
SetHistoryTypeTempMap(TypeTempMap typeTempMap)74 void ThermalServiceSubscriber::SetHistoryTypeTempMap(TypeTempMap typeTempMap)
75 {
76     THERMAL_HILOGD(COMP_SVC, "SetHistoryTypeTempMap: historyCount_=%{public}d", historyCount_);
77 
78     for (auto itMap : typeTempMap) {
79         auto iter = typeHistoryMap_.find(itMap.first);
80         if (iter != typeHistoryMap_.end()) {
81             iter->second.pop_front();
82             iter->second.push_back(itMap.second);
83         } else {
84             std::list<int32_t> historyTempList(historyCount_);
85             historyTempList.push_back(itMap.second);
86             typeHistoryMap_.insert(std::make_pair(itMap.first, historyTempList));
87         }
88     }
89 
90     int32_t sec = 0;
91     if (magnification_ != 0) {
92         sec = static_cast<int32_t>(difftime(endTime_, startTime_)) / magnification_;
93     }
94     THERMAL_HILOGI(COMP_SVC, "SetHistoryTypeTempMap: sec=%{public}d", sec);
95     for (auto history : typeHistoryMap_) {
96         const auto& item = history.second;
97         // The initial value of the sum is 0
98         double sum = std::accumulate(item.begin(), item.end(), 0);
99 
100         double rate = 0;
101         if (sec == SEC_MIN_NUM) {
102             rate = (sum / THOUSAND_UNIT / sec) * SEC_MIN_NUM;
103         }
104 
105         sensorsRateMap_.insert(std::make_pair(history.first, rate));
106     }
107 }
108 } // namespace PowerMgr
109 } // namespace OHOS
110