• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "battery_stats.h"
17 
18 #include <memory>
19 
20 #include "async_callback_info.h"
21 #include "battery_stats_client.h"
22 #include "battery_stats_info.h"
23 #include "napi_utils.h"
24 #include "stats_common.h"
25 #include "stats_log.h"
26 
27 namespace OHOS {
28 namespace PowerMgr {
BatteryStats(napi_env & env)29 BatteryStats::BatteryStats(napi_env& env) : env_(env) {}
30 
StatsAsyncCallBack(napi_value & value)31 void BatteryStats::StatsAsyncCallBack(napi_value& value)
32 {
33     std::unique_ptr<AsyncCallbackInfo> asyncInfo = std::make_unique<AsyncCallbackInfo>();
34     asyncInfo->CreateCallback(env_, value);
35     napi_value resource = nullptr;
36     NAPI_CALL_RETURN_VOID(env_, napi_create_string_utf8(env_, "StatsAsyncCallBack", NAPI_AUTO_LENGTH, &resource));
37     napi_create_async_work(
38         env_, nullptr, resource,
39         [](napi_env env, void* data) {
40             AsyncCallbackInfo* asCallbackInfo = (AsyncCallbackInfo*)data;
41             STATS_RETURN_IF(asCallbackInfo == nullptr);
42             asCallbackInfo->GetError().Error(asCallbackInfo->GetData().GetBatteryStatsInfo());
43         },
44         [](napi_env env, napi_status status, void* data) {
45             AsyncCallbackInfo* asCallbackInfo = (AsyncCallbackInfo*)data;
46             STATS_RETURN_IF(asCallbackInfo == nullptr);
47             napi_value arrRes = nullptr;
48             asCallbackInfo->GetData().CreateArrayValue(env, arrRes);
49             asCallbackInfo->CallFunction(env, arrRes);
50             asCallbackInfo->Release(env);
51             delete asCallbackInfo;
52         },
53         (void*)asyncInfo.get(), &asyncInfo->GetAsyncWork());
54     NAPI_CALL_RETURN_VOID(env_, napi_queue_async_work(env_, asyncInfo->GetAsyncWork()));
55     asyncInfo.release();
56 }
57 
StatsPromise()58 napi_value BatteryStats::StatsPromise()
59 {
60     std::unique_ptr<AsyncCallbackInfo> asyncInfo = std::make_unique<AsyncCallbackInfo>();
61     napi_value promise;
62     asyncInfo->CreatePromise(env_, promise);
63     STATS_RETURN_IF_WITH_RET(promise == nullptr, nullptr);
64 
65     napi_value resourceName = nullptr;
66     NAPI_CALL_BASE(env_, napi_create_string_utf8(env_, "StatsPromise", NAPI_AUTO_LENGTH, &resourceName), promise);
67     napi_create_async_work(
68         env_, nullptr, resourceName,
69         [](napi_env env, void* data) {
70             AsyncCallbackInfo* asCallbackInfo = (AsyncCallbackInfo*)data;
71             STATS_RETURN_IF(asCallbackInfo == nullptr);
72             asCallbackInfo->GetError().Error(asCallbackInfo->GetData().GetBatteryStatsInfo());
73         },
74         [](napi_env env, napi_status status, void* data) {
75             AsyncCallbackInfo* asCallbackInfo = (AsyncCallbackInfo*)data;
76             STATS_RETURN_IF(asCallbackInfo == nullptr);
77             if (asCallbackInfo->GetError().IsError()) {
78                 napi_reject_deferred(env, asCallbackInfo->GetDeferred(), asCallbackInfo->GetError().GetNapiError(env));
79             } else {
80                 napi_value arrRes = nullptr;
81                 asCallbackInfo->GetData().CreateArrayValue(env, arrRes);
82                 napi_resolve_deferred(env, asCallbackInfo->GetDeferred(), arrRes);
83             }
84             asCallbackInfo->Release(env);
85             delete asCallbackInfo;
86         },
87         (void*)asyncInfo.get(), &asyncInfo->GetAsyncWork());
88     NAPI_CALL_BASE(env_, napi_queue_async_work(env_, asyncInfo->GetAsyncWork()), promise);
89     asyncInfo.release();
90     return promise;
91 }
92 
GetAppStatsMah(napi_callback_info & info,uint32_t maxArgc,uint32_t index)93 napi_value BatteryStats::GetAppStatsMah(napi_callback_info& info, uint32_t maxArgc, uint32_t index)
94 {
95     return GetAppOrPartStats(info, maxArgc, index, [&](int32_t uid, NapiError& error) {
96         double appStatsMah = BatteryStatsClient::GetInstance().GetAppStatsMah(uid);
97         error.Error(BatteryStatsClient::GetInstance().GetLastError());
98         STATS_HILOGD(COMP_FWK, "get app stats mah: %{public}lf for uid: %{public}d", appStatsMah, uid);
99         return appStatsMah;
100     });
101 }
102 
GetAppStatsPercent(napi_callback_info & info,uint32_t maxArgc,uint32_t index)103 napi_value BatteryStats::GetAppStatsPercent(napi_callback_info& info, uint32_t maxArgc, uint32_t index)
104 {
105     return GetAppOrPartStats(info, maxArgc, index, [&](int32_t uid, NapiError& error) {
106         double appStatsPercent = BatteryStatsClient::GetInstance().GetAppStatsPercent(uid);
107         error.Error(BatteryStatsClient::GetInstance().GetLastError());
108         STATS_HILOGD(COMP_FWK, "get app stats percent: %{public}lf for uid: %{public}d", appStatsPercent, uid);
109         return appStatsPercent;
110     });
111 }
112 
GetPartStatsMah(napi_callback_info & info,uint32_t maxArgc,uint32_t index)113 napi_value BatteryStats::GetPartStatsMah(napi_callback_info& info, uint32_t maxArgc, uint32_t index)
114 {
115     return GetAppOrPartStats(info, maxArgc, index, [&](int32_t type, NapiError& error) {
116         BatteryStatsInfo::ConsumptionType naviveType = BatteryStatsInfo::ConsumptionType(type);
117         double partStatsMah = BatteryStatsClient::GetInstance().GetPartStatsMah(naviveType);
118         error.Error(BatteryStatsClient::GetInstance().GetLastError());
119         STATS_HILOGD(COMP_FWK, "get part stats mah: %{public}lf for type: %{public}d", partStatsMah, type);
120         return partStatsMah;
121     });
122 }
123 
GetPartStatsPercent(napi_callback_info & info,uint32_t maxArgc,uint32_t index)124 napi_value BatteryStats::GetPartStatsPercent(napi_callback_info& info, uint32_t maxArgc, uint32_t index)
125 {
126     return GetAppOrPartStats(info, maxArgc, index, [&](int32_t type, NapiError& error) {
127         BatteryStatsInfo::ConsumptionType naviveType = BatteryStatsInfo::ConsumptionType(type);
128         double partStatsPercent = BatteryStatsClient::GetInstance().GetPartStatsPercent(naviveType);
129         error.Error(BatteryStatsClient::GetInstance().GetLastError());
130         STATS_HILOGD(COMP_FWK, "get part stats percent: %{public}lf for type: %{public}d", partStatsPercent, type);
131         return partStatsPercent;
132     });
133 }
134 
GetAppOrPartStats(napi_callback_info & info,uint32_t maxArgc,uint32_t index,std::function<double (int32_t,NapiError &)> getAppOrPart)135 napi_value BatteryStats::GetAppOrPartStats(
136     napi_callback_info& info, uint32_t maxArgc, uint32_t index, std::function<double(int32_t, NapiError&)> getAppOrPart)
137 {
138     size_t argc = maxArgc;
139     napi_value argv[argc];
140     NapiUtils::GetCallbackInfo(env_, info, argc, argv);
141     NapiError error;
142 
143     if (argc != maxArgc || !NapiUtils::CheckValueType(env_, argv[index], napi_number)) {
144         return error.ThrowError(env_, StatsError::ERR_PARAM_INVALID);
145     }
146 
147     int32_t jsValue;
148     napi_get_value_int32(env_, argv[index], &jsValue);
149     double statsData = getAppOrPart(jsValue, error);
150     if (error.IsError()) {
151         return error.ThrowError(env_);
152     }
153 
154     napi_value result = nullptr;
155     napi_create_double(env_, statsData, &result);
156     return result;
157 }
158 } // namespace PowerMgr
159 } // namespace OHOS
160