• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef SYSTEM_BATTERY_H
17 #define SYSTEM_BATTERY_H
18 
19 #include "errors.h"
20 #include "napi/native_api.h"
21 #include "napi/native_node_api.h"
22 
23 #include "battery_info.h"
24 
25 namespace OHOS {
26 namespace PowerMgr {
27 class SystemBattery {
28 public:
29     void GetBatteryStats(napi_env env);
30     bool CheckValueType(napi_env env, napi_value value, napi_valuetype checkType);
31     bool CreateCallbackRef(napi_env env, napi_value options);
32 
33     napi_async_work asyncWork = nullptr;
34 private:
35     class Error {
36     public:
37         void SetErrorMsg(int32_t code, const std::string& msg);
IsError()38         inline bool IsError()
39         {
40             return !msg_.empty() && (code_ != ERR_OK);
41         }
GetCode()42         inline int32_t GetCode()
43         {
44             return code_;
45         }
GetMsg()46         inline std::string GetMsg()
47         {
48             return msg_;
49         }
50     private:
51         int32_t code_ { ERR_OK };
52         std::string msg_;
53     };
54 
55     class BatteryInfo {
56     public:
57         bool GetBatteryInfo();
58         double GetLevel();
59         uint32_t IsCharging();
60 
61     private:
62         int32_t capacity_ { INVALID_BATT_INT_VALUE };
63         BatteryChargeState chargingState_;
64     };
65 
66     napi_value GetOptionsFunc(napi_env env, napi_value options, const std::string& name);
67     napi_value CreateResponse(napi_env env);
68     void SuccessCallback(napi_env env);
69     void FailCallback(napi_env env);
70     void CompleteCallback(napi_env env);
71 
72     Error error_;
73     BatteryInfo batteryInfo_;
74     napi_ref successRef_ = nullptr;
75     napi_ref failRef_ = nullptr;
76     napi_ref completeRef_ = nullptr;
77 };
78 } // namespace PowerMgr
79 } // namespace OHOS
80 
81 #endif // SYSTEM_BATTERY_H
82