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